-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsource_commands
executable file
·284 lines (241 loc) · 6.99 KB
/
source_commands
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/sh
image_exists() {
# See if a Docker image named $1 exists locally.
docker inspect --type="image" "$1" >&/dev/null
}
enumerate_devices() {
# Add a bunch of (optional) devices
# (Don't add them unconditionally: if they don't exist, it
# would prevent the container from starting)
# FIXME: zsh annoyingly complains when no matches are found for those
DEVICES=
for DEV in /dev/kvm /dev/dri/* /dev/snd/*; do
if [ -b "$DEV" -o -c "$DEV" ]; then
DEVICES="$DEVICES --device $DEV:$DEV"
fi
done
echo "$DEVICES"
}
enumerate_volumes() {
# And a few (optional) files
# (Like in enumerate_devices, they don't always exist everywhere)
unset VOLUMES
RW_VOLS="
$HOME/.ssh
/usr/local/man
$HOME/.config
/tmp/.X11-unix
/run/user
`pwd`
"
RO_VOLS="
/etc/passwd
/etc/group
/etc/localtime
"
VOLUMES=
for VOL in $RW_VOLS ; do
if [ -e "$VOL" ]; then
VOLUMES="$VOLUMES --volume $VOL:$VOL"
fi
done
for VOL in $RO_VOLS ; do
if [ -e "$VOL" ]; then
VOLUMES="$VOLUMES --volume $VOL:$VOL:ro"
fi
done
# User arguments
# If any arguments correspond to files present on the host, bind-mount
# the parent directory
for ARG in "$@"; do
verbose_echo " -> $ARG"
# If arg starts with -, ignore it.
if [[ "$ARG" == -* ]]; then
verbose_echo "Flag detected, ignoring: $ARG"
continue
fi
# If ARG is a link, resolve it.
if [ -L "$ARG" ]; then
ARG=$(readlink --canonicalize --no-newline $ARG)
fi
# If ARG is a file, mount its parent directory.
# FIXME: what about important directories like /, /usr...?
if [ -e "$ARG" ]; then
DIRNAME="$(dirname "$ARG")"
if [ -d "$DIRNAME" ]; then
verbose_echo "Directory detected: $DIRNAME"
ABSPATH=$(cd $DIRNAME ; echo $PWD)
verbose_echo "Resolved to: $ABSPATH"
# If the volume is already in our list, skip it
case "$VOLUMES" in
*" $ABSPATH:$ABSPATH"*)
verbose_echo "Filtering out duplicate volume $ABSPATH"
;;
*)
VOLUMES="$VOLUMES --volume $ABSPATH:$ABSPATH"
;;
esac
else
verbose_echo "$DIRNAME is not a directory; skipping it"
fi
fi
done
echo "$VOLUMES"
}
enumerate_envvars() {
echo $(env | sort | cut -d= -f1 | awk '{print "--env", $1}')
}
enumerate_ports() {
if ! [[ $CLINK_RUN_FLAGS == *"--net"* ]]; then
PORTS="--publish-all"
fi
echo "$PORTS"
}
print_run_cmd() {
RUNFLAGS="
DASHT
DEBUG_MODE
DEVICES
ENTRYPOINT
ENVVARS
INTERACTIVE
PORTS
USER_ID
VOLUMES
WORKDIR
EXTRA_RUN_FLAGS
"
for FLAG in $RUNFLAGS; do
echo -n "$FLAG: "
echo $(echo "${!FLAG}" | sed 's/--/\n--/g')
done
}
docker_run_image() {
# Run Docker image.
# Arguments: <imagename> [optional args passed at the end of docker run...]
if ! docker version > /dev/null; then
echo "Docker isn't running!"
exit 1
fi
# fixme
docker rm ${1}-run &> /dev/null
IMAGE="$1"
shift
DEVICES=$(enumerate_devices)
VOLUMES=$(enumerate_volumes "$@")
ENVVARS=$(enumerate_envvars)
WORKDIR="--workdir $(pwd)"
USER_ID="--user $(id -u)"
INTERACTIVE="--interactive"
PORTS=$(enumerate_ports)
# DEBUG_MODE="--debug"
# Check if we are on a tty to decide whether to allocate one
DASHT=
tty -s && DASHT=-t
ENTRYPOINT=
#ENTRYPOINT="--entrypoint bash"
CONTAINER_NAME=$(basename "$image")
EXTRA_RUN_FLAGS="$CLINK_RUN_FLAGS"
if [ -n "$EXTRA_RUN_FLAGS" ]; then
extra="with extra flags: $EXTRA_RUN_FLAGS"
fi
>&2 echo "[[ Running $IMAGE in a container $extra ]]"
verbose_echo "$(print_run_cmd)"
docker \
$DEBUG_MODE \
run \
--rm "$DASHT" \
--name "${CONTAINER_NAME}-run" \
$DEVICES \
$ENTRYPOINT \
$ENVVARS \
$INTERACTIVE \
$PORTS \
$USER_ID \
$VOLUMES \
$WORKDIR \
$EXTRA_RUN_FLAGS \
$IMAGE "$@"
}
which() {
# fixme: We may be overriding user-defined aliases for which here.
# If user has defined their own aliases for which, we may or may not take precedence
real_which=":"
for which_path in /bin/which /usr/bin/which; do
if [ -x "$which_path" ]; then
real_which="$which_path"
fi
done
$real_which "$1" 2>/dev/null \
|| type -a "$1" 2>/dev/null \
|| declare -f "$1" 2>/dev/null \
|| echo "No command found named $1."
image=$(pick_image $1)
if image_exists "$image"; then
verbose_echo "$image is a clink!"
# Print to stderr to avoid causing unexpected behaviors
>&2 docker images "$image"
else
verbose_echo "$image is not a clink."
fi
}
enumerate_prefixes() {
# List all possible namespaces to look for images in.
# If CLINK_REGISTRIES envvar is present, use it.
# Otherwise, if DOCKER_HUB_USER is present, use it instead.
# Otherwise, don't check any private registry namespaces.
if [ -z $CLINK_REGISTRIES ]; then
ret="$DOCKER_HUB_USER"
fi
# User can export semicolon-separated CLINK_REGISTRIES envvar
for item in $(echo ${CLINK_REGISTRIES} | tr ';' ' '); do
# Only add it if it's not already present
if ! [[ $ret == *"$item"* ]]; then
ret="$ret $item"
fi
done
verbose_echo "Private registry namespaces: $ret"
echo $ret | tr ' ' '\n'
}
pick_image() {
cmd="$1"
if image_exists "$cmd"; then
verbose_echo "basename image $cmd exists"
image="$cmd"
else
verbose_echo "basename image $cmd doesn't exist"
prefixes=$(enumerate_prefixes)
for prefix in $prefixes; do
verbose_echo "Checking image $prefix/$cmd exists"
check_image="$prefix/$cmd"
if image_exists "$check_image"; then
image=$check_image
verbose_echo "Found image $image"
break
else
verbose_echo "$check_image doesn't exist"
unset image
fi
done
fi
echo "$image"
}
command_not_found() {
cmd="$1"
verbose_echo "$cmd not found in path; attempting clinkery..."
image=$(pick_image $cmd)
shift
if [ -z "$image" ]; then
echo "$0: $cmd: command/image not found"
docker images | grep -- "$cmd"
return
else
docker_run_image "$image" "$@"
fi
}
command_not_found_handle () {
command_not_found "$@"
}
command_not_found_handler() {
command_not_found "$@"
}