You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

187 lines
4.9 KiB

#!/bin/bash
## Bash wrap script to launch the ``compose`` docker with right options.
##
##
## Launcher
## - should need minimum requirement to run
## - no shell libs
##
read-0() {
local eof= IFS=''
while [ "$1" ]; do
read -r -d '' -- "$1" || eof=1
shift
done
[ -z "$eof" ]
}
get_running_compose_containers() {
## XXXvlab: docker bug: there will be a final newline anyway
docker ps --filter label="compose.service" --format='{{.ID}}'
}
get_volumes_for_container() {
local container="$1"
docker inspect \
--format '{{range $mount := .Mounts}}{{$mount.Source}}{{"\x00"}}{{$mount.Destination}}{{"\x00"}}{{end}}' \
"$container"
}
is_volume_used() {
local volume="$1"
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
}
clean_unused_sessions() {
for f in /var/lib/compose/sessions/*; do
[ -e "$f" ] || continue
is_volume_used "$f" && continue
rm -f "$f"
done
}
mk_docker_run_options() {
docker_run_opts=("-v" "/var/run/docker.sock:/var/run/docker.sock")
## CACHE/DATA DIRS
docker_run_opts+=("-v" "/var/lib/compose:/var/lib/compose")
docker_run_opts+=("-v" "/var/cache/compose:/var/cache/compose")
docker_run_opts+=("-v" "/etc/timezone:/etc/timezone:ro")
## current dir
if parent=$(while true; do
[ -e "./compose.yml" ] && {
echo "$PWD"
exit 0
}
[ "$PWD" == "/" ] && exit 1
cd ..
done
); then
docker_path=/var/lib/compose/root/$(basename "$parent")
docker_run_opts+=("-v" "$parent:$docker_path:ro" \
"-w" "$docker_path")
fi
##
## Load config files
##
if [ -z "$DISABLE_SYSTEM_CONFIG_FILE" ]; then
if [ -r /etc/default/charm ]; then
docker_run_opts+=("-v" "/etc/default/charm:/etc/default/charm:ro")
. /etc/default/charm
fi
if [ -r "/etc/default/compose" ]; then
docker_run_opts+=("-v" "/etc/default/$exname:/etc/default/$exname:ro")
. /etc/default/compose
fi
## XXXvlab: should provide YML config opportunities in possible parent dirs ?
## userdir ? and global /etc/compose.yml ?
for cfgfile in /etc/compose.conf /etc/compose.local.conf \
/etc/default/compose /etc/compose/local.conf; do
[ -e "$cfgfile" ] || continue
docker_run_opts+=("-v" "$cfgfile:$cfgfile:ro")
. "$cfgfile"
done
else
docker_run_opts+=("-e" "DISABLE_SYSTEM_CONFIG_FILE=$DISABLE_SYSTEM_CONFIG_FILE")
fi
##
## Checking vars
##
## CHARM_STORE
CHARM_STORE=${CHARM_STORE:-/srv/charm-store}
[ -L "$CHARM_STORE" ] && {
CHARM_STORE=$(readlink "$CHARM_STORE") || exit 1
}
docker_run_opts+=("-v" "$CHARM_STORE:/srv/charm-store:ro")
## COMPOSE_YML_FILE
if [ "${COMPOSE_YML_FILE+x}" ]; then
if [ -e "${COMPOSE_YML_FILE}" ]; then
docker_run_opts+=("-v" "$COMPOSE_YML_FILE:/tmp/compose.yml:ro")
docker_run_opts+=("-e" "COMPOSE_YML_FILE=/tmp/compose.yml")
fi
fi
## DATASTORE
if [ "${DATASTORE+x}" ]; then
if [ -d "${DATASTORE}" ]; then
docker_run_opts+=("-v" "$DATASTORE:/srv/datastore/data:rw")
docker_run_opts+=("-e" "DATASTORE=/srv/datastore/data")
fi
fi
## CONFIGSTORE
if [ "${CONFIGSTORE+x}" ]; then
if [ -d "${CONFIGSTORE}" ]; then
docker_run_opts+=("-v" "$CONFIGSTORE:/srv/datastore/config:rw")
docker_run_opts+=("-e" "CONFIGSTORE=/srv/datastore/config")
fi
fi
COMPOSE_LAUNCHER_BIN=$(readlink -f "${BASH_SOURCE[0]}")
filename=$(mktemp -p /tmp/ -t launch_opts-XXXXXXXXXXXXXXXX)
{
printf "%s\0" "${docker_run_opts[@]}"
} > "$filename"
sha=$(sha256sum "$filename")
sha=${sha:0:64}
dest="/var/lib/compose/sessions/$sha"
{
printf "%s\0" "-v" "$dest:$dest"
printf "%s\0" "-e" "COMPOSE_LAUNCHER_OPTS=$dest"
printf "%s\0" "-e" "COMPOSE_LAUNCHER_BIN=$COMPOSE_LAUNCHER_BIN"
} >> "$filename"
mkdir -p /var/lib/compose/sessions
mv "$filename" "$dest"
echo "$dest"
}
run() {
docker_run_opts=()
if [ -z "$COMPOSE_LAUNCHER_OPTS" ]; then
clean_unused_sessions
COMPOSE_LAUNCHER_OPTS="$(mk_docker_run_options)"
fi
while read-0 opt; do
docker_run_opts+=("$opt")
done < <(cat "$COMPOSE_LAUNCHER_OPTS")
COMPOSE_DOCKER_IMAGE=${COMPOSE_DOCKER_IMAGE:-docker.0k.io/compose}
exec docker run -ti "${docker_run_opts[@]}" "${COMPOSE_DOCKER_IMAGE}" "$@"
}
run "$@"