-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker-wrapper
executable file
·113 lines (98 loc) · 3.5 KB
/
docker-wrapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env bash
#
# Wrap running `nava-platform` via Docker. Pass arguments as you would to
#`nava-platform` itself.
#
# Since the tool heavily involves manipulating files on the host system, some of
# which are in locations that may need created first, it can be fiddly to
# manually run via Docker.
#
# So this script tries to automate the annoying parts for common situations. To
# not explode in complexity, it does make some assumptions (or reserves the
# right to):
#
# - File path arguments will start with either `.` or `/`
#
set -euo pipefail
processed_args=()
docker_flags=()
detected_host_paths=()
looks_like_path() {
[[ "$1" =~ ^[.]*/ ]]
}
uname_out="$(uname -s)"
case "${uname_out}" in
Linux*) host_os=Linux;;
Darwin*) host_os=Mac;;
*) host_os="UNKNOWN:${uname_out}"
esac
# not strictly required with magic in `docker-entry` script and mostly for when
# running on Linux, but explicitly run as host user to help avoid any file
# permission issues with mounted locations from the host file system
docker_flags+=(--user "$(id -u):$(id -g)")
# connect the host git config if present, for better chance committing with work
# inside the container (theoretically someone may not have their name/email
# configured even if the config file exists)
if [[ -e "$HOME/.gitconfig" ]]; then
docker_flags+=("-v=$HOME/.gitconfig:/.gitconfig")
fi
# figure out what location to mount for logs
HOST_LOG_DIR=${HOST_LOG_DIR:-}
if [[ -z "${HOST_LOG_DIR}" ]]; then
if [[ "${host_os}" == "Linux" ]]; then
HOST_LOG_DIR="${XDG_STATE_HOME:-${HOME}/.local/state}/nava-platform-cli/log"
elif [[ "${host_os}" == "Mac" ]]; then
HOST_LOG_DIR="${HOME}/Library/Logs/nava-platform-cli"
else
echo "Your host OS '${host_os}' is not supported automatically. Please set HOST_LOG_DIR env var explicitly and try again."
exit 1
fi
fi
if [[ ! -d "${HOST_LOG_DIR}" ]]; then
mkdir -p "${HOST_LOG_DIR}"
fi
docker_flags+=("-v=${HOST_LOG_DIR}:/.local/state/nava-platform-cli/log:z")
# process the script arguments
for ((i=1;i<=$#;i++))
do
eval "arg=\${$i}"
option=""
# shellcheck disable=SC2154
if [[ "${arg}" =~ ^- && "${arg}" == *"="* ]]; then
# part before the =
option="${arg%%=*}"
# part after the =
value="${arg#*=}"
processed_args+=("${option}")
else
value="${arg}"
fi
if looks_like_path "${value}"; then
abs_value=$(realpath "${value}")
detected_host_paths+=("${abs_value}")
docker_flags+=("-v=${abs_value}:${abs_value}:z")
processed_args+=("${abs_value}")
else
processed_args+=("${value}")
fi
done
# if host paths don't exist yet, particularly directories, create them before
# Docker does (with incorrect permissions) when it goes to mount them as a
# volume
#
# TODO: this is basically only to support initializing a project with a
# template. We could isolate that functionality in an `init` command that we
# have simpler special handling for or just not support that mode via docker?
for dpath in "${detected_host_paths[@]}"; do
if [[ ! -e "${dpath}" ]]; then
# basically, does the path look like a directory?
# - ends in a slash
# - or doesn't contain a `.` followed by a few characters, limiting the
# "file extension" to 8 charactes as the e2e tests create tmp directories
# with a dot followed by 9 characters
if [[ "${dpath}" =~ /$ || ! "${dpath}" =~ \..{1,8}$ ]]; then
mkdir -p "${dpath}"
fi
fi
done
docker run --interactive --rm "${docker_flags[@]}" nava-platform-cli "${processed_args[@]}"