Skip to content

Commit

Permalink
Auto-create bind mount directories inside the deployment dir (#440)
Browse files Browse the repository at this point in the history
* Auto-create bind mount directories inside the deployment dir

* Fix deploy test
  • Loading branch information
dboreham authored Jun 28, 2023
1 parent 831a8cd commit c76195c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/data/compose/docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
environment:
CERC_SCRIPT_DEBUG: ${CERC_SCRIPT_DEBUG}
volumes:
- test-data:/var
- test-data:/data
ports:
- "80"

Expand Down
2 changes: 1 addition & 1 deletion app/data/container-build/cerc-test-container/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 21 additions & 5 deletions app/deployment_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c76195c

Please sign in to comment.