From 44ac8bf62c6041e78adb8d7f4f239aa4ba2f6603 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sun, 15 Dec 2019 23:48:54 +0100 Subject: [PATCH] fix: prevent permission errors when compose is writing cache files Separate ``compose`` and ``compose-core`` cache location as the first needs possibly user permission while the latter will require root permission. Signed-off-by: Valentin Lab --- bin/compose | 100 +++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/bin/compose b/bin/compose index 253e246..382e391 100755 --- a/bin/compose +++ b/bin/compose @@ -398,7 +398,7 @@ get_compose_file_opt() { _get_compose_file_opt() { local hash_bin="$1" override="$2" \ - cache_file="$COMPOSE_CACHE/$FUNCNAME.cache.$(p0 "$@" | md5_compat)" + cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$(p0 "$@" | md5_compat)" if [ -e "$cache_file" ]; then cat "$cache_file" && touch "$cache_file" || return 1 @@ -455,7 +455,7 @@ replace_compose_file_opt() { _replace_compose_file_opt() { local hash_bin="$1" override="$2" \ - cache_file="$COMPOSE_CACHE/$FUNCNAME.cache.$(p0 "$@" | md5_compat)" + cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$(p0 "$@" | md5_compat)" if [ -e "$cache_file" ]; then cat "$cache_file" && touch "$cache_file" || return 1 @@ -499,7 +499,7 @@ _replace_compose_file_opt() { get_compose_opts_list() { local hash_bin="$1" override="$2" \ - cache_file="$COMPOSE_CACHE/$FUNCNAME.cache.$1" + cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$1" if [ -e "$cache_file" ]; then cat "$cache_file" && @@ -540,7 +540,7 @@ single_opts_filter() { get_compose_multi_opts_list() { local hash_bin="$1" override="$2" \ - cache_file="$COMPOSE_CACHE/$FUNCNAME.cache.$1" opts_list + cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$1" opts_list if [ -e "$cache_file" ]; then cat "$cache_file" && touch "$cache_file" || return 1 @@ -554,7 +554,7 @@ get_compose_multi_opts_list() { get_compose_single_opts_list() { local hash_bin="$1" override="$2" \ - cache_file="$COMPOSE_CACHE/$FUNCNAME.cache.$1" opts_list + cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$1" opts_list if [ -e "$cache_file" ]; then cat "$cache_file" && touch "$cache_file" || return 1 @@ -566,7 +566,7 @@ get_compose_single_opts_list() { get_volume_opt() { - local cache_file="$COMPOSE_CACHE/$FUNCNAME.cache.$(p0 "$@" | md5_compat)" + local cache_file="$COMPOSE_LAUNCHER_CACHE/$FUNCNAME.cache.$(p0 "$@" | md5_compat)" if [ -e "$cache_file" ]; then cat "$cache_file" && touch "$cache_file" || return 1 @@ -648,6 +648,46 @@ pretty_print() { } +set_os() { + OS="$(get_os)" + case "$OS" in + linux) + COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$HOME/.compose"} + COMPOSE_VAR=${COMPOSE_VAR:-/var/lib/compose} + COMPOSE_CACHE=${COMPOSE_CACHE:-/var/cache/compose} + DATASTORE=${DATASTORE:-/srv/datastore/data} + CONFIGSTORE=${CONFIGSTORE:-/srv/datastore/config} + if [ "$UID" == 0 ]; then + SESSION_DIR=${SESSION_DIR:-"$COMPOSE_VAR"/sessions} + CHARM_STORE=${CHARM_STORE:-/srv/charm-store} + TZ_PATH=${TZ_PATH:-"$COMPOSE_VAR"/timezones} + COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_CACHE"} + else + SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions} + CHARM_STORE=${CHARM_STORE:-"$HOME"/.charm-store} + TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones} + COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache} + fi + ;; + mac) + COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$HOME/.compose"} + COMPOSE_VAR=${COMPOSE_VAR:-"$COMPOSE_LOCAL_ROOT"/lib} + COMPOSE_CACHE=${COMPOSE_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache} + SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions} + DATASTORE=${DATASTORE:-"$COMPOSE_LOCAL_ROOT"/data} + CONFIGSTORE=${CONFIGSTORE:-"$COMPOSE_LOCAL_ROOT"/config} + CHARM_STORE=${CHARM_STORE:-"$HOME"/.charm-store} + TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones} + COMPOSE_LAUNCHER_CACHE=${COMPOSE_LAUNCHER_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache} + ;; + *) + echo "System '$os' not supported yet." >&2 + exit 1 + ;; + esac +} + + mk_docker_run_options() { ## Order matters, files get to override vars @@ -667,8 +707,9 @@ mk_docker_run_options() { ~/.compose.conf ) - docker_run_opts=("-v" "/var/run/docker.sock:/var/run/docker.sock") + set_os || return 1 + docker_run_opts=("-v" "/var/run/docker.sock:/var/run/docker.sock") ## ## Load config files @@ -687,40 +728,7 @@ mk_docker_run_options() { fi - COMPOSE_LOCAL_ROOT=${COMPOSE_LOCAL_ROOT:-"$HOME/.compose"} - - case "$(get_os)" in - linux) - COMPOSE_VAR=${COMPOSE_VAR:-/var/lib/compose} - COMPOSE_CACHE=${COMPOSE_CACHE:-/var/cache/compose} - DATASTORE=${DATASTORE:-/srv/datastore/data} - CONFIGSTORE=${CONFIGSTORE:-/srv/datastore/config} - if [ "$UID" == 0 ]; then - SESSION_DIR=${SESSION_DIR:-"$COMPOSE_VAR"/sessions} - CHARM_STORE=${CHARM_STORE:-/srv/charm-store} - TZ_PATH=${TZ_PATH:-"$COMPOSE_VAR"/timezones} - else - SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions} - CHARM_STORE=${CHARM_STORE:-"$HOME"/.charm-store} - TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones} - fi - ;; - mac) - COMPOSE_VAR=${COMPOSE_VAR:-"$COMPOSE_LOCAL_ROOT"/lib} - COMPOSE_CACHE=${COMPOSE_CACHE:-"$COMPOSE_LOCAL_ROOT"/cache} - SESSION_DIR=${SESSION_DIR:-"$COMPOSE_LOCAL_ROOT"/sessions} - DATASTORE=${DATASTORE:-"$COMPOSE_LOCAL_ROOT"/data} - CONFIGSTORE=${CONFIGSTORE:-"$COMPOSE_LOCAL_ROOT"/config} - CHARM_STORE=${CHARM_STORE:-"$HOME"/.charm-store} - TZ_PATH=${TZ_PATH:-"$COMPOSE_LOCAL_ROOT"/timezones} - ;; - *) - echo "System '$os' not supported yet." >&2 - exit 1 - ;; - esac - - mkdir -p "$COMPOSE_CACHE" + mkdir -p "$COMPOSE_LAUNCHER_CACHE" ## get TZ value and prepare TZ_PATH TZ=$(get_tz) || exit 1 @@ -880,6 +888,7 @@ mk_docker_run_options() { echo " CONFIGSTORE: $CONFIGSTORE" echo " COMPOSE_VAR: $COMPOSE_VAR" echo " COMPOSE_CACHE: $COMPOSE_CACHE" + echo " COMPOSE_LAUNCHER_CACHE: $COMPOSE_LAUNCHER_CACHE" echo " SESSION_DIR: $SESSION_DIR" echo " TZ_PATH: $TZ_PATH" fi >&2 @@ -904,15 +913,10 @@ run() { else env= fi - if [[ "$vol" == "true" && "$opt" == *":/var/cache/compose" ]]; then - COMPOSE_CACHE=${opt%%:*} - elif [ "$opt" == "-v" ]; then - vol=true - else - vol= - fi done < <(cat "$COMPOSE_LAUNCHER_OPTS") + set_os + array_read-0 cmd_args < <(replace_compose_file_opt "$COMPOSE_DOCKER_IMAGE" "$@") set -- "${cmd_args[@]}"