-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshed-hub
executable file
·533 lines (456 loc) · 15.5 KB
/
shed-hub
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/bin/bash
#
# shed-hub -- A wrapper for Docker global commands
#
# Joan Ordinas <[email protected]>
declare -r VERSION='1.1' # Shed version
declare -r SELF=${0##*/}
declare -ar CONFIG=( '/usr/local/etc/shed.conf' "$HOME/.shed" '.shed' )
# Prefix to all image names
# [REGISTRYHOST/][USERNAME/]NAME[#ALT][:TAG]
# ^^^^^^^^^^^^^^^^^^^^^^^^^^
declare TAGS_PREFIX='shed'
export LESS=\
'--chop-long-lines '\
'--clear-screen '\
'--hilite-search '\
'--HILITE-UNREAD '\
'--ignore-case '\
'--LONG-PROMPT '\
'--RAW-CONTROL-CHARS '\
'--squeeze-blank-lines '\
'--status-column '\
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;37m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[01;44;33m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[01;32m' # begin underline
########################################################################
# Utililies
########################################################################
function fatal { # abort with message
echo 1>&2 "fatal: $*"
exit 1
}
function pager { # colorized less
local color=$'\e''[92m'
[[ $TERM == 'screen-256color' ]] && color=$'\e''[32m'
[[ -z $PAGER ]] && PAGER=$(type -p less)
sed -e s/^/$color/ | $PAGER
}
########################################################################
# Load Shedfile and configuration files
########################################################################
if [[ -e ./Shedfile ]]; then
declare -a PROJECT BUILDS IMAGES CONTAINERS
source ./Shedfile || fatal "Syntax error loading ./Shedfile"
fi
# can override TAGS_PREFIX
for f in ${CONFIG[@]}; do
[[ -e $f ]] || continue
source $f || fatal "Syntax error loading $f"
done
########################################################################
# Main and help
########################################################################
function main {
(( $# > 0 )) || help_main
local opt COMMAND
while getopts :Vh-: opt; do
case $opt in
V) version ;;
h) help_main ;;
-) case $OPTARG in
help) help_main ;;
*) usage option "--$OPTARG" ;;
esac
;;
?) usage option "-$OPTARG" ;;
esac
done
shift $((OPTIND-1)); OPTIND=1
case $1 in
b|bu|bui|buil|build) COMMAND='build' ;;
e|ev|eve|even|event|events) COMMAND='events' ;;
i|in|inf|info) COMMAND='info' ;;
ins|insp|inspe|inspec|inspect) COMMAND='inspect' ;;
lo|log|logi|login) COMMAND='login' ;;
logo|logou|logout) COMMAND='logout' ;;
p|pu|pul|pull) COMMAND='pull' ;;
pus|push) COMMAND='push' ;;
q|qu|que|quer|query) COMMAND='query' ;;
s|se|sea|sear|searc|search) COMMAND='search' ;;
v|ve|ver|vers|versi|versio|version) COMMAND='version' ;;
*) usage command "$1" ;;
esac
shift
hub_$COMMAND "$@"
}
function version {
echo "$SELF $VERSION"
exit 0
}
function usage {
cat 1>&2 <<EOF
Unknown $1: $2
Usage: $SELF [-h | --help | -V]
$SELF COMMAND [ARGS]
EOF
exit 1
}
function help_main {
cat <<EOF | pager
Usage: $SELF [-h | --help | -V]
$SELF COMMAND [ARGS]
Options:
-h, --help Help information
-V Show version information
Commands:
build [OPTIONS]
Build a project from the Shedfile source
events [OPTIONS]
Get real time events from the server
info [OPTIONS]
Display system-wide information
inspect [OPTIONS] CONTAINER | IMAGE [CONTAINER | IMAGE...]
Return low-level information on a container or image
login [OPTIONS] [SERVER]
Register or log in to a Docker registry server
logout [OPTIONS] [SERVER]
Log out from a Docker registry server
pull [OPTIONS] NAME[:TAG | @DIGEST]
Pull an image or a repository from a Docker registry server
Pull an image or a repository from a registry
push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
query [OPTIONS] CONTAINER | IMAGE [QUERY]
Return YAML formated information on a container or image
search [OPTIONS] TERM
Search the Docker Hub for images
version [OPTIONS]
Show the Docker version information
Run '$SELF COMMAND --help' for more information on a command.
EOF
exit 1
}
function help_command {
local alt cmd=$1 arg=$2
if [[ $arg == '-h' || $arg == '--help' ]]; then
docker $cmd --help 2>&1 \
| sed -e '1d' -e "s/docker\s\+$cmd/$SELF $cmd/g" \
| pager
exit 1
fi
}
########################################################################
# Commands
########################################################################
# build [OPTIONS]
function hub_build {
local opt
local -i remove=0 quiet=0
while getopts :hrq-: opt; do
case $opt in
r) remove=1 ;;
q) quiet=1 ;;
h) help_build ;;
-) case $OPTARG in
remove) remove=1 ;;
remove=true) remove=1 ;;
remove=false) remove=0 ;;
quiet) quiet=1 ;;
quiet=true) quiet=1 ;;
quiet=false) quiet=0 ;;
help) help_build ;;
*) help_build ;;
esac
;;
?) help_build ;;
esac
done
#shift $((OPTIND-1)); OPTIND=1
[[ -e ./Shedfile ]] || fatal 'No Shedfile found for project in .'
# pull images
local image
for image in ${IMAGES[@]}
do
docker pull $image
done
# build images
local prefix=$TAGS_PREFIX path tag='latest'
for path in ${BUILDS[@]}
do
# Parse image tag: [REGISTRYHOST/][USERNAME/]NAME[#ALT][:TAG]
# path will be split several times
local ifs=$IFS
# if has tag with / ([REGISTRYHOST/][USERNAME/]NAME[#ALT][:TAG])
if [[ $path == */* ]]; then
IFS='/'
set -- $path # split on '/'
IFS=$ifs # restore
if (( $# == 3 )); then
prefix="$1/$2"
path=$3
elif (( $# == 2 )); then
prefix=$1
path=$2
else
fatal 'Too many / in image tag'
fi
fi
# if has tag with : (NAME[:TAG])
if [[ $path == *:* ]]; then
IFS=':'
set -- $path # split on ':'
IFS=$ifs # restore
(( $# == 2 )) || fatal 'Too many : in image tag'
path=$1
tag=$2
fi
# alternative name for dockerfile?
local FILE=$path/Dockerfile # default
# if tag has NAME with a # (NAME#DOCKERFILE)
local alternate
if [[ $path == *'#'* ]]; then
IFS='#'
set -- $path # split on '#'
IFS=$ifs # restore
(( $# == 2 )) || fatal 'Too many # in image tag'
alternate=1
path=$1
alternate=$2
FILE=$path/$alternate
fi
# now $path must be a directory name
[[ -d $path ]] || fatal "Directory $path does not exists"
[[ -f $FILE ]] || fatal "Dockerfile $FILE does not exists"
# set final image name, prefixed if necessary
local -l name
if [[ -n $alternate ]]; then
name=${alternate%%.*} # without extension
elif [[ $path == . ]]; then
name=${PWD##*/}
else
name=$path
fi
[[ -n $PROJECT && $PROJECT != $name ]] && name="${PROJECT}_$name"
# define more arguments to build
local -a BUILD_ARG
local CGROUP_PARENT
local CPUSET_CPUS
local CPUSET_MEMS
local CPU_PERIOD
local CPU_QUOTA
local CPU_SHARES
local DISABLE_CONTENT_TRUST
local FORCE_RM
local ISOLATION
local MEMORY
local MEMORY_SWAP
local NO_CACHE
local PULL
local QUIET
local RM
local SHM_SIZE
local -a TAG
local -a ULIMIT
(( quiet )) && QUIET="--quiet=true" # can be overrided
local shedfile="${FILE}.shed"
# source definitions if available
if [[ -f $shedfile ]]; then
source $shedfile || fatal "Syntax error loading $shedfile"
# unset empty parameters
[[ -z "${CGROUP_PARENT-unset}" ]] && unset CGROUP_PARENT
[[ -z "${CPUSET_CPUS-unset}" ]] && unset CPUSET_CPUS
[[ -z "${CPUSET_MEMS-unset}" ]] && unset CPUSET_MEMS
[[ -z "${CPU_PERIOD-unset}" ]] && unset CPU_PERIOD
[[ -z "${CPU_QUOTA-unset}" ]] && unset CPU_QUOTA
[[ -z "${CPU_SHARES-unset}" ]] && unset CPU_SHARES
[[ -z "${DISABLE_CONTENT_TRUST-unset}" ]] && unset DISABLE_CONTENT_TRUST
[[ -z "${FORCE_RM-unset}" ]] && unset FORCE_RM
[[ -z "${ISOLATION-unset}" ]] && unset ISOLATION
[[ -z "${MEMORY-unset}" ]] && unset MEMORY
[[ -z "${MEMORY_SWAP-unset}" ]] && unset MEMORY_SWAP
[[ -z "${NO_CACHE-unset}" ]] && unset NO_CACHE
[[ -z "${PULL-unset}" ]] && unset PULL
[[ -z "${QUIET-unset}" ]] && unset QUIET
[[ -z "${RM-unset}" ]] && unset RM
[[ -z "${SHM_SIZE-unset}" ]] && unset SHM_SIZE
# format parameters
[[ -n $CGROUP_PARENT ]] && CGROUP_PARENT="--cgroup-parent=$CGROUP_PARENT"
[[ -n $CPUSET_CPUS ]] && CPUSET_CPUS="--cpuset-cpus=$CPUSET_CPUS"
[[ -n $CPUSET_MEMS ]] && CPUSET_MEMS="--cpuset-mems=$CPUSET_MEMS"
[[ -n $CPU_PERIOD ]] && CPU_PERIOD="--cpu-period=$CPU_PERIOD"
[[ -n $CPU_QUOTA ]] && CPU_QUOTA="--cpu-quota=$CPU_QUOTA"
[[ -n $CPU_SHARES ]] && CPU_SHARES="--cpu-shares=$CPU_SHARES"
[[ -n $DISABLE_CONTENT_TRUST ]] && DISABLE_CONTENT_TRUST="--disable-content-trust=$DISABLE_CONTENT_TRUST"
[[ -n $FORCE_RM ]] && FORCE_RM="--force-rm=$FORCE_RM"
[[ -n $ISOLATION ]] && ISOLATION="--isolation=$ISOLATION"
[[ -n $MEMORY ]] && MEMORY="--memory=$MEMORY"
[[ -n $MEMORY_SWAP ]] && MEMORY_SWAP="--memory-swap=$MEMORY_SWAP"
[[ -n $NO_CACHE ]] && NO_CACHE="--no-cache=$NO_CACHE"
[[ -n $PULL ]] && PULL="--pull=$PULL"
[[ -n $RM ]] && RM="--rm=$RM"
[[ -n $SHM_SIZE ]] && SHM_SIZE="--shm-size=$SHM_SIZE"
[[ -n $QUIET ]] && QUIET="--quiet=$QUIET"
local -i i
for i in ${!BUILD_ARG[@]}; do BUILD_ARG[i]="--build-arg=${BUILD_ARG[i]}"; done
for i in ${!TAG[@]}; do TAG[i]="--tag=${TAG[i]}"; done
for i in ${!ULIMIT[@]}; do ULIMIT[i]="--ulimit=${ULIMIT[i]}"; done
fi
# build image
docker build \
--file "$FILE" \
--tag "${prefix}/${name}:${tag}" \
"${BUILD_ARG[@]}" \
"${CGROUP_PARENT[@]}" \
"${CPUSET_CPUS[@]}" \
"${CPUSET_MEMS[@]}" \
"${CPU_PERIOD[@]}" \
"${CPU_QUOTA[@]}" \
"${CPU_SHARES[@]}" \
"${DISABLE_CONTENT_TRUST[@]}" \
"${FORCE_RM[@]}" \
"${ISOLATION[@]}" \
"${MEMORY[@]}" \
"${MEMORY_SWAP[@]}" \
"${NO_CACHE[@]}" \
"${PULL[@]}" \
"${QUIET[@]}" \
"${RM[@]}" \
"${SHM_SIZE[@]}" \
"${TAG[@]}" \
"${ULIMIT[@]}" \
"$path"
# tag latest
if [[ $tag != 'latest' ]]; then
if docker history ${prefix}/${name}:latest &>/dev/null; then
# latest exists
docker rmi --force ${prefix}/${name}:latest &>/dev/null
fi
docker tag ${prefix}/${name}:${tag} ${prefix}/${name}:latest
fi
done
# build containers
local container
for container in ${CONTAINERS[@]}
do
# set final container name, prefixed if necessary
local -l name=$container
[[ -e ${container}.shed ]] || fatal "Container Shedfile ${container}.shed not found"
[[ -n $PROJECT && $PROJECT != $name ]] && name="${PROJECT}_$name"
# remove container if requested
if (( remove )); then
docker stop $name &>/dev/null
docker rm $name &>/dev/null
fi
# build container
$(dirname $0)/shed-container build --name=$name ${container}.shed
done
}
function help_build {
cat <<EOF | pager
Usage: $SELF build [OPTIONS]
Build a project from the Shedfile source
-r, --remove=false Remove containers, stopping if necessary
-q, --quiet=false Suppress the verbose output generated by the containers
EOF
exit 1
}
# events [OPTIONS]
function hub_events {
help_command events "$@"
docker events "$@"
}
# info [OPTIONS]
function hub_info {
help_command info "$@"
(( $# == 0 )) || help_command info -h
docker info | pager
}
# inspect [OPTIONS] CONTAINER | IMAGE [CONTAINER | IMAGE...]
function hub_inspect {
help_command inspect "$@"
(( $# > 0 )) || help_command inspect -h
docker inspect "$@"
}
# login [OPTIONS] [SERVER]
function hub_login {
help_command login "$@"
docker login "$@"
}
# logout [OPTIONS] [SERVER]
function hub_logout {
help_command logout "$@"
(( $# < 2 )) || help_command logout -h
docker logout "$1"
}
# pull [OPTIONS] NAME[:TAG | @DIGEST]
function hub_pull {
help_command pull "$@"
(( $# > 0 )) || help_command pull -h
docker pull "$@"
}
# push [OPTIONS] NAME[:TAG]
function hub_push {
help_command push "$@"
(( $# == 1 )) || help_command push -h
docker push "$1"
}
# query [OPTIONS] CONTAINER | IMAGE
function hub_query {
local opt
while getopts :h-: opt; do
case $opt in
h) help_query ;;
-) case $OPTARG in
help) help_query ;;
*) help_query ;;
esac
;;
?) help_query ;;
esac
done
#shift $((OPTIND-1)); OPTIND=1
(( $# > 0 )) || help_query
local j2y='import sys, yaml, json; yaml.dump(json.load(sys.stdin), sys.stdout, indent=4, width=200, default_flow_style=False)'
local fmt='{{json .}}' pager=pager
if (( $# == 2 )); then
fmt="{{json .$2}}"
pager=cat
fi
docker inspect --format "$fmt" $1 2>&1 1>/dev/null || exit $?
docker inspect --format "$fmt" $1 \
| python -c "$j2y" \
| sed -e 's/\!\!python\/unicode //g' \
-e "s/''/__Q__/g;s/'//g;s/__Q__/''/g" \
-e '/^\.\.\.$/d' \
| $pager
}
function help_query {
cat <<EOF | pager
Usage: query [OPTIONS] CONTAINER | IMAGE [QUERY]
Return YAML formated information on a container or image
EOF
exit 1
}
# search [OPTIONS] TERM
function hub_search {
help_command search "$@"
(( $# > 0 )) || help_command search -h
docker search "$@" 2>&1 | pager
}
# version [OPTIONS]
function hub_version {
help_command version "$@"
(( $# == 0 )) || help_command version -h
docker version | pager
}
########################################################################
# Call main
########################################################################
main "$@"
exit $?
# vim:syntax=sh:ai:sw=4:ts=4:et