@ -2161,6 +2161,98 @@ get_default_project_name() {
export -f get_default_project_name
export -f get_default_project_name
get_running_compose_containers() {
## XXXvlab: docker bug: there will be a final newline anyway
docker ps --filter label="compose.service" --format='{{.ID}}'
}
export -f get_running_compose_containers
get_volumes_for_container() {
local container="$1"
docker inspect \
--format '{{range $mount := .Mounts}}{{$mount.Source}}{{"\x00"}}{{$mount.Destination}}{{"\x00"}}{{end}}' \
"$container"
}
export -f get_volumes_for_container
is_volume_used() {
local volume="$1" container_id src dst
while read container_id; do
while read-0 src dst; do
[[ "$src" == "$volume"/* ]] && return 0
done < <(get_volumes_for_container "$container_id")
done < <(get_running_compose_containers)
return 1
}
export -f is_volume_used
clean_unused_docker_compose() {
for f in /var/lib/compose/docker-compose/*; do
[ -e "$f" ] || continue
is_volume_used "$f" && continue
debug "Cleaning unused docker-compose ${f##*/}"
rm -rf "$f" || return 1
done
}
export -f clean_unused_docker_compose
file_get_hash() {
local file="$1" sha
sha=$(sha256sum "$file")
sha=${sha:0:64}
echo "$sha"
}
export -f file_get_hash
docker_compose_store() {
local file="$1" sha
sha=$(file_get_hash "$file") || return 1
project=$(get_default_project_name) || return 1
dst="/var/lib/compose/docker-compose/$sha/$project"
mkdir -p "$dst" || return 1
cat <<EOF > "$dst/.env" || return 1
DOCKER_COMPOSE_PATH=$dst
EOF
cp "$file" "$dst/docker-compose.yml" || return 1
mkdir -p "$dst/bin" || return 1
cat <<EOF > "$dst/bin/dc" || return 1
#!/bin/bash
$(declare -f read-0)
docker_run_opts=()
while read-0 opt; do
docker_run_opts+=("\$opt")
done < <(cat "$COMPOSE_LAUNCHER_OPTS")
docker_run_opts+=(
"-w" "$dst"
"--entrypoint" "/usr/local/bin/docker-compose"
)
[ -t 1 ] && {
docker_run_opts+=("-ti")
}
exec docker run "\${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}" "\$@"
EOF
chmod +x "$dst/bin/dc" || return 1
printf "%s" "$sha"
}
launch_docker_compose() {
launch_docker_compose() {
local charm docker_compose_tmpdir docker_compose_dir
local charm docker_compose_tmpdir docker_compose_dir
docker_compose_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
docker_compose_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
@ -2192,9 +2284,21 @@ launch_docker_compose() {
# done
# done
mkdir "$docker_compose_dir/.data"
mkdir "$docker_compose_dir/.data"
if [ -z "$COMPOSE_DISABLE_DOCKER_COMPOSE_STORE" ]; then
sha=$(docker_compose_store "$docker_compose_dir/docker-compose.yml") || return 1
fi
{
{
{
{
if [ -z "$COMPOSE_DISABLE_DOCKER_COMPOSE_STORE" ]; then
cd "/var/lib/compose/docker-compose/$sha/$project"
else
cd "$docker_compose_dir"
cd "$docker_compose_dir"
fi
if [ -f ".env" ]; then
debug "${WHITE}.env$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL:"
debug "$(cat ".env" | prefix " $GRAY|$NORMAL ")"
fi
debug "${WHITE}docker-compose.yml$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL:"
debug "${WHITE}docker-compose.yml$NORMAL for $DARKYELLOW$SERVICE_PACK$NORMAL:"
debug "$(cat "docker-compose.yml" | prefix " $GRAY|$NORMAL ")"
debug "$(cat "docker-compose.yml" | prefix " $GRAY|$NORMAL ")"
debug "${WHITE}Launching$NORMAL: docker-compose $@"
debug "${WHITE}Launching$NORMAL: docker-compose $@"
@ -3086,3 +3190,5 @@ esac || exit 1
if [ "$post_hook" -a "${#services_args[@]}" != 0 ]; then
if [ "$post_hook" -a "${#services_args[@]}" != 0 ]; then
run_service_hook post_deploy "${services_args[@]}" || exit 1
run_service_hook post_deploy "${services_args[@]}" || exit 1
fi
fi
clean_unused_docker_compose || return 1