57 lines
1.6 KiB
Bash
57 lines
1.6 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Translate a path visible inside a containerized CI job into the source path
|
|
# understood by the Docker daemon. On a regular host this returns the input
|
|
# unchanged.
|
|
docker_daemon_path() {
|
|
local path=$1
|
|
local container_reference=${ECHOION_DOCKER_CONTAINER_ID:-}
|
|
local mount_data=
|
|
local mount_source mount_destination
|
|
local best_source='' best_destination=''
|
|
|
|
if [[ -z $container_reference ]]; then
|
|
container_reference=${JOB_CONTAINER_NAME:-${HOSTNAME:-}}
|
|
fi
|
|
|
|
if [[ -n ${ECHOION_DOCKER_CONTAINER_ID:-} ||
|
|
-f /.dockerenv || -f /run/.containerenv ]]; then
|
|
if [[ -n $container_reference ]]; then
|
|
mount_data=$(
|
|
docker inspect \
|
|
--format '{{range .Mounts}}{{printf "%s\t%s\n" .Source .Destination}}{{end}}' \
|
|
"$container_reference" 2>/dev/null || true
|
|
)
|
|
fi
|
|
fi
|
|
|
|
while IFS=$'\t' read -r mount_source mount_destination; do
|
|
[[ -n $mount_source && -n $mount_destination ]] || continue
|
|
case $path in
|
|
"$mount_destination"|"$mount_destination"/*)
|
|
if ((${#mount_destination} > ${#best_destination})); then
|
|
best_source=$mount_source
|
|
best_destination=$mount_destination
|
|
fi
|
|
;;
|
|
*)
|
|
if [[ $mount_destination == / && $path == /* ]]; then
|
|
best_source=$mount_source
|
|
best_destination=$mount_destination
|
|
fi
|
|
;;
|
|
esac
|
|
done <<<"$mount_data"
|
|
|
|
if [[ -z $best_destination ]]; then
|
|
printf '%s\n' "$path"
|
|
elif [[ $path == "$best_destination" ]]; then
|
|
printf '%s\n' "$best_source"
|
|
elif [[ $best_destination == / ]]; then
|
|
printf '%s/%s\n' "${best_source%/}" "${path#/}"
|
|
else
|
|
printf '%s/%s\n' \
|
|
"${best_source%/}" "${path#"$best_destination"/}"
|
|
fi
|
|
}
|