From c76195c491b40835cac0c3fecb9f18c02b93e733 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Tue, 27 Jun 2023 21:18:04 -0600 Subject: [PATCH] Auto-create bind mount directories inside the deployment dir (#440) * Auto-create bind mount directories inside the deployment dir * Fix deploy test --- app/data/compose/docker-compose-test.yml | 2 +- .../cerc-test-container/run.sh | 2 +- app/deployment_create.py | 26 +++++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/data/compose/docker-compose-test.yml b/app/data/compose/docker-compose-test.yml index d20c3cfc..19660f89 100644 --- a/app/data/compose/docker-compose-test.yml +++ b/app/data/compose/docker-compose-test.yml @@ -5,7 +5,7 @@ services: environment: CERC_SCRIPT_DEBUG: ${CERC_SCRIPT_DEBUG} volumes: - - test-data:/var + - test-data:/data ports: - "80" diff --git a/app/data/container-build/cerc-test-container/run.sh b/app/data/container-build/cerc-test-container/run.sh index 5aa33467..205c231a 100755 --- a/app/data/container-build/cerc-test-container/run.sh +++ b/app/data/container-build/cerc-test-container/run.sh @@ -4,7 +4,7 @@ if [ -n "$CERC_SCRIPT_DEBUG" ]; then set -x fi # Test if the container's filesystem is old (run previously) or new -EXISTSFILENAME=/var/exists +EXISTSFILENAME=/data/exists echo "Test container starting" if [[ -f "$EXISTSFILENAME" ]]; then diff --git a/app/deployment_create.py b/app/deployment_create.py index d1416389..5a9ddbcd 100644 --- a/app/deployment_create.py +++ b/app/deployment_create.py @@ -60,8 +60,22 @@ def _get_named_volumes(stack): return named_volumes +# If we're mounting a volume from a relatie path, then we +# assume the directory doesn't exist yet and create it +# so the deployment will start +# Also warn if the path is absolute and doesn't exist +def _create_bind_dir_if_relative(volume, path_string, compose_dir): + path = Path(path_string) + if not path.is_absolute(): + absolute_path = Path(compose_dir).parent.joinpath(path) + absolute_path.mkdir(parents=True, exist_ok=True) + else: + if not path.exists(): + print(f"WARNING: mount path for volume {volume} does not exist: {path_string}") + + # See: https://stackoverflow.com/questions/45699189/editing-docker-compose-yml-with-pyyaml -def _fixup_pod_file(pod, spec): +def _fixup_pod_file(pod, spec, compose_dir): # Fix up volumes if "volumes" in spec: spec_volumes = spec["volumes"] @@ -70,10 +84,12 @@ def _fixup_pod_file(pod, spec): for volume in pod_volumes.keys(): if volume in spec_volumes: volume_spec = spec_volumes[volume] - new_volume_spec = {"name": volume, + volume_spec_fixedup = volume_spec if Path(volume_spec).is_absolute() else f".{volume_spec}" + _create_bind_dir_if_relative(volume, volume_spec, compose_dir) + new_volume_spec = {"driver": "local", "driver_opts": { "type": "none", - "device": volume_spec, + "device": volume_spec_fixedup, "o": "bind" } } @@ -94,7 +110,7 @@ def init(ctx, output): if named_volumes: volume_descriptors = {} for named_volume in named_volumes: - volume_descriptors[named_volume] = f"./data/{named_volume}" + volume_descriptors[named_volume] = f"../data/{named_volume}" spec_file_content["volumes"] = volume_descriptors with open(output, "w") as output_file: yaml.dump(spec_file_content, output_file) @@ -130,7 +146,7 @@ def create(ctx, spec_file, deployment_dir): for pod in pods: pod_file_path = os.path.join(_get_compose_file_dir(), f"docker-compose-{pod}.yml") parsed_pod_file = yaml.load(open(pod_file_path, "r")) - _fixup_pod_file(parsed_pod_file, parsed_spec) + _fixup_pod_file(parsed_pod_file, parsed_spec, destination_compose_dir) with open(os.path.join(destination_compose_dir, os.path.basename(pod_file_path)), "w") as output_file: yaml.dump(parsed_pod_file, output_file) # Copy the config files for the pod, if any