-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathcp-into-docker-run
executable file
·250 lines (212 loc) · 6.78 KB
/
cp-into-docker-run
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env bash
# @Function
# Copy the command into docker container and run the command in container.
#
# Example:
# cp-into-docker-run -c container_foo command_copied_into_container command_arg1
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-3.x/docs/shell.md#-cp-into-docker-run
# @author Jerry Lee (oldratlee at gmail dot com)
set -eEuo pipefail
readonly PROG=${0##*/}
readonly PROG_VERSION='3.x-dev'
################################################################################
# util functions
################################################################################
redPrint() {
# if stdout is a terminal, turn on color output.
# '-t' check: is a terminal?
# check isatty in bash https://stackoverflow.com/questions/10022323
if [ -t 1 ]; then
printf '\e[1;31m%s\e[0m\n' "$*"
else
printf '%s\n' "$*"
fi
}
die() {
local prompt_help=false exit_staus=2
while (($# > 0)); do
case "$1" in
-h)
prompt_help=true
shift
;;
-s)
exit_staus=$2
shift 2
;;
*)
break
;;
esac
done
(($# > 0)) && redPrint "$PROG: $*"
$prompt_help && echo "Try '$PROG --help' for more information."
exit "$exit_staus"
} >&2
isAbsolutePath() {
[[ "$1" =~ ^/ ]]
}
# `realpath` command exists on Linux and macOS, return resolved physical path
# - realpath command on macOS do NOT support option `-e`;
# combined `[ -e $file ]` to check file existence first.
# - How can I get the behavior of GNU's readlink -f on a Mac?
# https://stackoverflow.com/questions/1055671
realpath() {
[ -e "$1" ] && command realpath -- "$1"
}
usage() {
cat <<EOF
Usage: $PROG [OPTION]... command [command-args]...
Copy the command into docker container
and run the command in container.
Example:
$PROG -c container_foo command_copied_into_container command_arg1
docker options:
-c, --container destination docker container
-u, --docker-user docker username or UID to run command
optional, docker default is (maybe) root user
-w, --workdir absolute working directory inside the container
optional, docker default is (maybe) root dir
-t, --tmpdir tmp dir in docker to copy command
optional, default is /tmp
-p, --cp-path destination path in docker of the command(including file name)
if specified, command will be kept when run finished
optional, default is under tmp dir and deleted when run finished
run options:
-v, --verbose show operation step infos
miscellaneous:
-h, --help display this help and exit
-V, --version display version information and exit
EOF
exit
}
progVersion() {
printf '%s\n' "$PROG $PROG_VERSION"
exit
}
################################################################################
# parse options
################################################################################
container_name=
docker_user=
docker_workdir=
docker_tmpdir=/tmp
docker_command_cp_path=
verbose=false
args=()
while (($# > 0)); do
case "$1" in
-c | --container)
container_name=$2
shift 2
;;
-u | --docker-user)
docker_user=$2
shift 2
;;
-w | --workdir)
docker_workdir=$2
shift 2
;;
-t | --tmpdir)
docker_tmpdir=$2
shift 2
;;
-p | --cp-path)
docker_command_cp_path=$2
shift 2
;;
-v | --verbose)
verbose=true
shift
;;
-h | --help)
usage
;;
-V | --version)
progVersion
;;
--)
shift
args=(${args[@]:+"${args[@]}"} "$@")
break
;;
-*)
die -h "unrecognized option '$1'"
;;
*)
# if not option, treat all follow arguments as command
args=(${args[@]:+"${args[@]}"} "$@")
break
;;
esac
done
readonly container_name docker_user docker_workdir docker_tmpdir docker_command_cp_path verbose args
[ -n "$container_name" ] ||
die -h "requires destination docker container name, specified by option -c/--container!"
if [ -n "$docker_workdir" ]; then
isAbsolutePath "$docker_workdir" ||
die "docker workdir(-w/--workdir) must be absolute path: $docker_workdir"
elif [ -n "$docker_command_cp_path" ]; then
isAbsolutePath "$docker_command_cp_path" ||
die "when no docker workdir(-w/--workdir) is specified, the command path in docker to copy(-p/--cp-path) must be absolute path: $docker_command_cp_path"
fi
################################################################################
# biz logic
################################################################################
########################################
# check docker command existence
########################################
type -P docker &>/dev/null || die 'docker command not found!'
########################################
# prepare vars for docker operation
########################################
readonly specified_run_command=${args[0]}
run_command=$specified_run_command
if [ ! -f "$specified_run_command" ]; then
type -P "$specified_run_command" &>/dev/null ||
die "specified command not exists and not found in PATH: $specified_run_command"
run_command=$(type -P "$specified_run_command")
fi
run_command=$(realpath "$run_command")
readonly run_command run_command_base_name=${run_command##*/}
run_timestamp=$(date "+%Y%m%d_%H%M%S")
readonly run_timestamp
readonly uuid="${PROG}_${run_timestamp}_${$}_${RANDOM}"
if [ -n "$docker_command_cp_path" ]; then
if isAbsolutePath "$docker_command_cp_path"; then
readonly run_command_in_docker=$docker_command_cp_path
else
readonly run_command_in_docker="${docker_workdir:+"$docker_workdir/"}$docker_command_cp_path"
fi
run_command_dir_in_docker=$(dirname -- "$run_command_in_docker")
readonly run_command_dir_in_docker
else
readonly work_tmp_dir_in_docker=$docker_tmpdir/$uuid
readonly run_command_in_docker="$work_tmp_dir_in_docker/$run_command_base_name"
readonly run_command_dir_in_docker=$work_tmp_dir_in_docker
fi
cleanupWhenExit() {
[ -n "${work_tmp_dir_in_docker:-}" ] || return 0
# remove tmp dir in docker by root user
docker exec "$container_name" rm -rf -- "$work_tmp_dir_in_docker" &>/dev/null
}
trap cleanupWhenExit EXIT
########################################
# docker operations
########################################
logAndRun() {
$verbose && printf '%s\n' "[$PROG] $*" >&2
"$@"
}
logAndRun docker exec ${docker_user:+"--user=$docker_user"} "$container_name" \
mkdir -p -- "$run_command_dir_in_docker"
logAndRun docker cp "$run_command" "$container_name:$run_command_in_docker"
logAndRun docker exec ${docker_user:+"--user=$docker_user"} "$container_name" \
chmod +x "$run_command_in_docker"
logAndRun docker exec -i -t \
${docker_user:+"--user=$docker_user"} \
${docker_workdir:+"--workdir=$docker_workdir"} \
"$container_name" \
"$run_command_in_docker" "${args[@]:1:${#args[@]}}"