From e37864afc3432c2894591c17eae484627a4999cc Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Wed, 28 Nov 2018 15:59:17 +0100 Subject: [PATCH] new: dev: provide ``get_host_path`` to find host path of a local directory. This is useful when preparing to launch a docker container from a ``compose`` in docker command. --- bin/compose-core | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/bin/compose-core b/bin/compose-core index b0e9d31..21232c4 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -2444,6 +2444,42 @@ get_master_services() { export -f get_master_services +get_current_docker_container_id() { + local line + line=$(cat "/proc/self/cpuset") || return 1 + [[ "$line" == *docker* ]] || return 1 + echo "${line##*/}" +} +export -f get_current_docker_container_id + + +## if we are in a docker compose, we might want to know what is the +## real host path of some local paths. +get_host_path() { + local path="$1" + path=$(realpath "$path") || return 1 + container_id=$(get_current_docker_container_id) || { + print "%s" "$path" + return 0 + } + biggest_dst= + current_src= + while read-0 src dst; do + [[ "$path" == "$dst"* ]] || continue + if [[ "${#biggest_dst}" < "${#dst}" ]]; then + biggest_dst="$dst" + current_src="$src" + fi + done < <(get_volumes_for_container "$container_id") + if [ "$current_src" ]; then + printf "%s" "$current_src" + else + return 1 + fi +} +export -f get_host_path + + _setup_state_dir() { export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX) #debug "Creating temporary state directory in '$state_tmpdir'."