-
Notifications
You must be signed in to change notification settings - Fork 1
/
recker
executable file
·3255 lines (2966 loc) · 88.8 KB
/
recker
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
:<<'@@'
@NAME recker
@DESCRIPTION Docker tiny helper
@AUTHOR HiLoTECH
@URL http://www.hilotech.jp/
@DATE 2016/05/12
@@
set -e
set -u
DEBUG="${DEBUG-}"
VERBOSE="${VERBOSE-}"
[[ -n "${VERBOSE-}" ]] \
&& set -v
readonly _VERSION='0.9.20160311'
readonly _RECKER_ABS=$( readlink -f $0 )
readonly _RECKER="${_RECKER_ABS##*/}"
_DOCKER_VERSION=$( \
_ar=( \
$( \
docker --version \
| cut -d ' ' -f 3 \
| sed 's|^\([0-9\.]\{1,\}\).*$|\1|' \
| cut -d ',' -f 1 \
| cut --output-delimiter=' ' -d '.' -f 1- \
) \
); \
echo $(( _ar[0]*10000+_ar[1]*100+_ar[2]*1 )) \
)
mapfile _USAGE <<'_EOF_'
NAME
Recker - Docker command helper
SYNOPSIS
recker [subcommand] [options] other_arguments
DESCRIPTION
Recker gives you short-hands for Docker's complex commands. At the same
time it becomes a character-based-GUI interface for manipulating Docker
images and containers. Main features are:
* Character-based-GUI selecting function for containers/images
* Command line auto-complete-definition for Bash
* Scaffold function for building images
* HTTP/SSH port auto-mapper for running containers
PREREQUISITES
* Docker
Version>=1.9.0
* Percol
Python-based CUI selection helper. https://github.com/mooz/percol
* Bash
Version>=4
SUBCOMMANDS
images | images-a
Show docker local images. Docker's too long output lines are trimmed
suitable for your terminal width. 'images-a' works same as 'images'
but '-a' option (show all images) will be applied.
ps | ps-a
Show docker containers.
rmi | rmi-a
Remove docker images.
stop | stop-a
Stop containers.
kill | kill-a
Kill containers.
stop-rm | stop-rm-a
Stop containers and remove them.
kill-rm | kill-rm-a
Kill containers and remove them.
get-container-ip
Show container's IP address.
get-container-ports
Show container's mapped ports.
get-container-port-http | get-container-port-ssh | get-container-port-smtp
Same as get-container-ports for http, ssh and smtp.
get-containers-names
Show the list of running container's names.
top
Show running processes of a container.
run
Select an image/context directory and run the container.
scaffold
Make a context directory's scaffold for building Docker image.
build
Select a context directory made by 'scaffold' and build the image.
clean
Clean up context directory. Removes containers, volumes, etc.
select-containers | select-containers-a
Select containers and display its' names.
select-images | select-images-a
Select images and display its' names.
bashcompletion
Print defining script of bash completion for Recker.
For temporarily use do like this:
$ source <(recker bashcompletion)
cut
Cut off the output of Docker which spews too long lines. Suitable
for your terminal width.
Example:
$ docker foo bar | recker cut
usage | help | man
Show this message.
version
Print this Recker's version.
SUPPORTED OS
* Linux
I tested only CentOS 6, 7...
and on Raspbian a little.
_EOF_
# Use /etc/recker/recker.conf or ~.recker/recker.conf to override
# Shell script variable-definition format such as:
# DOCKER_ENTERING_COMMAND='/bin/zsh'
declare -A _CONF=(
[DIR_CONF_GLOBAL]='/etc/recker'
[DIR_CONF_USERS]='.recker'
[FILE_CONFIG]='recker.conf'
[DIR_DOCKER_BASE_GLOBAL]='/var/docker'
[DIR_DOCKER_BASE_USERS]='Docker'
[DIR_DOCKER_DOCKERFILES]='Dockerfiles'
[DIR_DOCKER_VOLUMES]='volumes'
[DOCKER_BUILD_OPT]='--force-rm --rm=true --no-cache=true '
[DOCKER_BUILD_BASE_IMG]='centos:centos6'
[DOCKER_MAINTAINER_NAME]='recker'
[DOCKER_ENTRY_EXECUTABLE]='/bin/bash'
[DOCKER_ENTERING_COMMAND]='/bin/bash'
[DOCKER_SERVICE_ENTRYPOINT]='/etc/container_init.sh'
[DOCKER_RUN_SWITCH_BG]='-dt'
[DOCKER_RUN_SWITCH_FG]='-it --rm=true'
[DEVICE_NET]='docker0'
[AUTOMAP_PORT_HTTP_FROM]=8000
[AUTOMAP_PORT_SSH_FROM]=2200
[AUTOMAP_PORT_SMTP_FROM]=2500
[EXEC_DOCKER]='docker'
[EXEC_PERCOL]='percol'
[EXEC_IP]='ip'
[EXEC_POSTMAP]='postmap'
[COMMAND_PRE_RUN]=''
[COMMAND_POST_RUN]=''
# [POSTFIX_TRANSPORT]='/etc/postfix/transport-recker'
)
declare -r -a _SUB_COMMANDS=(
'build' 'run' 'run-fg' 'run-bg' 'clean'
'scaffold' 'enter'
'implode' 'explode'
'images' 'images-a' 'ps' 'ps-a' 'top'
'rmi' 'rmi-a'
'stop' 'stop-a' 'kill' 'kill-a'
'stop-rm' 'stop-rm-a' 'rm' 'kill-rm' 'kill-rm-a'
'stop-all-containers' 'kill-all-containers' 'rm-all-containers'
'destroy-all-containers'
'select-images' 'select-images-a'
'select-containers' 'select-containers-a'
'get-container-ip'
'get-containers-fqdn-and-port-smtp'
'get-container-ports' 'get-container-port-http'
'get-container-port-ssh'
'get-container-port-smtp'
'get-containers-names' 'get-containers-names-and-port-http'
'usage' 'help' 'man' 'version' 'cut'
'exec-all'
'bashcompletion'
'install-percol'
'_comp'
'_print_container_lib_sh'
)
declare -r -A _QUERIES=(
[_DOCKER_INSPECT_PORTS]='{{range $key, $value := .HostConfig.PortBindings }}{{if $value }}{{ $key }}:{{ range $value }}{{ println .HostPort }}{{end}}{{end}}{{end}}'
[_DOCKER_INSPECT_IP]='{{ .NetworkSettings.IPAddress }}'
)
declare -r -A _SEDEXP=(
[GET_DOCKER_CNAME]='s|^[^[:blank:]]\{1,\}[[:blank:]].*[[:blank:]]\([^[:blank:]]\{1,\}\)[[:blank:]]\{1,\}$|\1|'
[GET_DOCKER_CID_NAME]='s|^\([^[:blank:]]\{1,\}\)[[:blank:]].*[[:blank:]]\([^[:blank:]]\{1,\}\)[[:blank:]]\{1,\}$|\1 \2|'
)
declare -r -A _CONST=(
[CLEANUP_UNITS_ALL]='CONTAINER IMAGE VOLUME USER DB_USER DATABASE FILE DIR'
[CLEANUP_UNITS_PREBUILD]='CONTAINER IMAGE VOLUME USER DB_USER DATABASE FILE'
)
function _caution() {
echo -e "\e[31m$@\e[m" >&2
}
function _mention() {
echo -e "\e[34m$@\e[m" >&2
}
function _warn() {
echo -e "\e[31m$@\e[m" >&2
}
function _die() {
echo -e "\e[31m$@\e[m" >&2
exit 1
}
function _sureYesOrNo() {
_caution "$@ [y/n]"
local i
while read i
do
i="${i-}"
i="${i,,}"
if [[ "${i-}" =~ ^[yn]$ ]]; then
echo "${i-}"
break
fi
done
}
function _noExecP() {
if which "${1-}" >& /dev/null; then
return 1
else
return 0
fi
}
function _noExecThenDie() {
_noExecP "$1" && \
_die "[Error] $1 not found in your PATH"
return 0
}
function _has() {
local _list="${1-}"
local _elem="${2-}"
[[ -z "${_list-}" || -z "${_elem-}" ]] \
&& return 1
echo "${_list-}" | tr ' ' '\n' | grep -q '^'${_elem-}'$' \
|| return 1
}
# Usage: local -A __opts=`_getOptsArray --hoge=fuga --age=uge`
# to get the value: ${__opts["hoge"]}
function _getOptsArray() {
local __name=''
local __value=''
local __declaration=''
local -A __opts
for OPT in "$@"
do
__name=${OPT%%=*}
__name=${__name#--}
[[ -z "$__name" ]] && continue
__value=${OPT#*=}
__name=${__name//-/_}
__declaration="$__declaration[\"$__name\"]=\"$__value\" "
done
__declaration="( $__declaration)"
echo $__declaration
}
function _getEuid() {
echo $(id -u)
}
function _makeSureDir() {
[[ -d "$1" ]] && return 0
mkdir -p "$1"
return "$?"
}
function _initGlobal() {
[[ $(_getEuid) -ne 0 ]] && return 1
local _confDir=${_CONF[DIR_CONF_GLOBAL]}
_makeSureDir $_confDir
[[ ! -r "$_confDir/${_CONF[FILE_CONFIG]}" ]] && \
touch "$_confDir/${_CONF[FILE_CONFIG]}"
local _baseDir=${_CONF[DIR_DOCKER_BASE_GLOBAL]}
_makeSureDir $_baseDir
_makeSureDir "$_baseDir/${_CONF[DIR_DOCKER_VOLUMES]}"
_makeSureDir "$_baseDir/${_CONF[DIR_DOCKER_DOCKERFILES]}"
}
function _initUserland() {
[[ $(_getEuid) -eq 0 ]] && return 1
local _confDir="$HOME/${_CONF[DIR_CONF_USERS]}"
_makeSureDir $_confDir
[[ ! -r "$_confDir/${_CONF[FILE_CONFIG]}" ]] && \
touch "$_confDir/${_CONF[FILE_CONFIG]}"
_makeSureDir "${_CONF[DIR_DOCKER_BASE_THISUSER]}"
_makeSureDir "${_CONF[DIR_DOCKER_DOCKERFILES_THISUSER]}"
_makeSureDir "${_CONF[DIR_DOCKER_VOLUMES_THISUSER]}"
}
function _setExecs() {
_PERCOL=${_CONF[EXEC_PERCOL]}
_DOCKER=${_CONF[EXEC_DOCKER]}
_POSTMAP=${_CONF[EXEC_POSTMAP]}
}
function _isPriorDockerVersion() {
local _dockerVersion=$( \
$_DOCKER --version | cut -d ' ' -f 3 \
)
_dockerVersion=${_dockerVersion/,/}
local _semantics=( ${_dockerVersion//./ } )
[[ "${1-}" -ge 0 ]] && [[ "${1-}" -ge "${_semantics[0]-}" ]] || \
return 1
[[ "${2-}" -ge 0 ]] && [[ "${2-}" -ge "${_semantics[1]-}" ]] || \
return 1
[[ "${3-}" -ge 0 ]] && [[ "${3-}" -ge "${_semantics[2]-}" ]] || \
return 1
}
function _getOSName() {
local _result=''
uname >& /dev/null || return 1
_result=$( uname )
echo "${_result-}"
}
function _getDistName() {
# TODO: consult /etc/os-release and/or lsb_release too.
local _name
local i
for i in debian fedora redhat gentoo arch SuSE
do
if [[ \
-e "/etc/${i-}-release" \
|| -e "/etc/${i-}_release" \
|| -e "/etc/${i-}_version" \
]]; then
_name="${i-}"
break
fi
done
[[ ! "${_name-}" ]] && return 1
[[ "${_name-}" = 'redhat' ]] \
&& grep -q 'CentOS' /etc/redhat-release >& /dev/null \
&& _name='centos'
[[ "${_name-}" = 'debian' ]] \
&& lsb_release -si >& /dev/null \
&& [[ "$(lsb_release -si)" = "Ubuntu" ]] \
&& _name='ubuntu'
echo "${_name,,}"
}
function _checkEnvironment() {
[[ ! "${BASH_VERSION-}" ]] && \
_die '[Error] This program needs BASH'
local bashMajorVersion=${BASH_VERSION%%.*}
[[ $bashMajorVersion -lt 4 ]] && \
_die '[Error] This program needs BASH 4.x over'
local i
for i in docker; # $_PERCOL
do
_noExecThenDie "$i"
done
}
function _init() {
_setExecs
_checkEnvironment
_initGlobal || _initUserland || :
}
function _showUsage(){
printf %s "${_USAGE[@]}"
}
function _getHostIPv4() {
echo $(ip route | grep "${_CONF[DEVICE_NET]}" | awk '{print $9}')
}
###
### read config files
###
_confFiles=( \
"${_CONF[DIR_CONF_GLOBAL]}/${_CONF[FILE_CONFIG]}" \
)
[[ $(_getEuid) -eq 0 ]] \
|| _confFiles+=( "$HOME/${_CONF[DIR_CONF_USERS]}/${_CONF[FILE_CONFIG]}" )
for i in "${_confFiles[@]}"
do
_confFile="${i-}"
[[ -r "${_confFile-}" ]] && \
source <( \
grep '^[A-Z0-9_]\{1,\}=.*$' "${_confFile-}" \
| sed -e 's|^\([A-Z0-9_]\{1,\}\)\(=.*\)$|_CONF[\1]\2|' \
)
done
if [[ $(_getEuid) -ne 0 ]]; then
# DIR_DOCKER_BASE_THISUSER
_CONF[DIR_DOCKER_BASE_THISUSER]="${_CONF[DIR_DOCKER_BASE_USERS]}"
[[ ! "${_CONF[DIR_DOCKER_BASE_THISUSER]}" =~ ^/ ]] \
&& _CONF[DIR_DOCKER_BASE_THISUSER]="$HOME/${_CONF[DIR_DOCKER_BASE_USERS]}"
# DIR_DOCKER_DOCKERFILES_THISUSER
_CONF[DIR_DOCKER_DOCKERFILES_THISUSER]="${_CONF[DIR_DOCKER_DOCKERFILES]}"
[[ ! "${_CONF[DIR_DOCKER_DOCKERFILES_THISUSER]}" =~ ^/ ]] \
&& _CONF[DIR_DOCKER_DOCKERFILES_THISUSER]="${_CONF[DIR_DOCKER_BASE_THISUSER]}/${_CONF[DIR_DOCKER_DOCKERFILES_THISUSER]}"
# DIR_DOCKER_VOLUMES_THISUSER
_CONF[DIR_DOCKER_VOLUMES_THISUSER]="${_CONF[DIR_DOCKER_VOLUMES]}"
[[ ! "${_CONF[DIR_DOCKER_VOLUMES_THISUSER]}" =~ ^/ ]] \
&& _CONF[DIR_DOCKER_VOLUMES_THISUSER]="${_CONF[DIR_DOCKER_BASE_THISUSER]}/${_CONF[DIR_DOCKER_VOLUMES_THISUSER]}"
fi
###
### initial check
###
_init
##
## subcommands
##
function _recker-cut() {
local curentWidth=$(tput cols)
local suitableWidth=$((curentWidth-1))
cat - | cut -b -$suitableWidth
}
function _recker-images() {
local opt=''
if [[ "${1-}" = '-a' ]]; then
opt='-at'
shift
fi
${_DOCKER-} images $opt 2> /dev/null | _recker-cut
}
function _recker-images-a() {
_recker-images -a
}
function _recker-ps() {
local opt=''
if [[ "${1-}" = '-a' ]]; then
opt='-a'
shift
fi
${_DOCKER-} ps $opt $@ | _recker-cut
}
function _recker-ps-a() {
_recker-ps -a "$@"
}
function _recker-version() {
echo "Recker version $_VERSION"
}
function _recker-usage(){
_showUsage
}
function _recker-help(){
_recker-usage
}
function _recker-man(){
_recker-usage
}
###
### subcommand for scaffolding
###
### template: Dockerfile
mapfile _T_DOCKERFILE <<'_EOF_'
FROM %%BASE_IMAGE%%
ARG RUN_AS_SERVICE
ARG VOLUMES
ARG PORTS
ARG LINKS
ARG EXEC
ARG SWAPPY
ARG OOMK_CANCEL
# Upload setup scripts
ADD _upload /tmp/_upload
ADD container_build.sh /var/recker/usr/container_build.sh
ADD container_services.sh /var/recker/usr/container_services.sh
RUN \
mkdir -p \
/var/recker/lib \
/var/recker/usr \
/var/recker/tar \
/var/recker/log \
&& \
chmod +x /var/recker/usr/container_build.sh && \
ln -s /var/recker/usr/container_build.sh /tmp/container_build.sh && \
chmod +x /var/recker/usr/container_services.sh && \
ln -s /var/recker/usr/container_services.sh /etc/container_services.sh && \
mv /tmp/_upload/container_lib.sh /var/recker/lib/container_lib.sh && \
chmod +x /var/recker/lib/container_lib.sh && \
mv /tmp/_upload/envs_onbuild.sh /var/recker/lib/envs_onbuild.sh
# I need bash ;(
RUN \
[ ! -x /bin/bash ] \
&& if grep -q Alpine /etc/issue; then \
apk add --update bash tar openrc && rm -rf /var/cache/apk/* ; \
fi
# Preparation checker
RUN \
/bin/echo -e '#!/bin/bash\n\
[[ -f /var/recker/log/container_runonce.sh.done ]] \
|| exit 1\n\
' > /usr/local/bin/is_preparation_done \
&& chmod +x /usr/local/bin/is_preparation_done
# Set entry point shell script
RUN \
/bin/echo -e '#!/bin/bash\n\
: > /var/recker/log/container.log\n\
exec >>/var/recker/log/container.log\n\
exec 2>&1\n\
[[ -f /var/recker/lib/container_lib.sh ]] \\\n\
&& source /var/recker/lib/container_lib.sh \n\
[[ -f /var/recker/lib/envs_onbuild.sh ]] \\\n\
&& source /var/recker/lib/envs_onbuild.sh \n\
[[ \\\n\
-z "${VOLUMES-}" \\\n\
|| "${DONT_PRESERVE_SHARED_VOLUME-}" -eq 1 \\\n\
]] \\\n\
|| _cUntarSharedVolumes\n\
[[ \\\n\
! -f /var/recker/log/container_runonce.sh.done \\\n\
&& -f /var/recker/usr/container_runonce.sh \\\n\
]] \\\n\
&& source /var/recker/usr/container_runonce.sh \\\n\
&& touch /var/recker/log/container_runonce.sh.done \n\
[[ -f /var/recker/usr/container_services.sh ]] \\\n\
&& source /var/recker/usr/container_services.sh \n\
while true; do sleep 10; done\n\
' \
> /var/recker/lib/core_init.sh\
&& chmod +x /var/recker/lib/core_init.sh \
&& if grep -q Alpine /etc/issue; then \
mkdir -p /etc/local.d || : ; \
echo -e '#!/bin/bash\n\
set -eu\n\
/sbin/start-stop-daemon \
--start \
--pidfile /var/run/core_init.pid \
--make-pidfile \
--background \
--exec /var/recker/lib/core_init.sh \
|| exit 1 \n\
' \
> /etc/local.d/container.start \
&& chmod +x /etc/local.d/container.start ;\
else \
echo -e '#!/bin/bash\n\
set -eu\n\
source /var/recker/lib/core_init.sh\n\
' \
> /etc/container_init.sh \
&& chmod +x /etc/container_init.sh ; \
fi
# Build and cleaning up
RUN \
chmod +x /tmp/container_build.sh \
&& touch /var/recker/log/build.log \
&& bash -c '( \
source /var/recker/lib/envs_onbuild.sh && \
source /var/recker/lib/container_lib.sh && \
source /tmp/container_build.sh && \
_cPreserveSharedVolumes \
) ' 2>&1 \
| tee -a /var/recker/log/build.log
RUN \
/bin/rm /tmp/container_build.sh \
&& /bin/rm -r /tmp/_upload
_EOF_
### template: container_build.sh
mapfile _T_CONTAINER_BUILD_SH <<'_EOF_'
#!/bin/bash
# __WRITE_HERE_HOW_TO_BUILD_CONTAINER__
# _cSetScriptFor1stRuntime sets STDIN to container's first
# runtime script.
# example)
#_cSetScriptFor1stRuntime <<'______SCRIPT_FOR_1ST_RUNTIME'
# :
#______SCRIPT_FOR_1ST_RUNTIME
# __WRITE_HERE_1ST_RUNTIME_SCRIPT__
# _cSetScriptContainerServices sets STDIN as a script for
# bootstrap container's services.
# example)
#_cSetScriptContainerServices <<'______SCRIPT_FOR_SERVICES'
# :
#______SCRIPT_FOR_SERVICES
# __WRITE_HERE_SERVICE_SCRIPT__
_EOF_
### template: config.sh
mapfile _T_CONFIG_SH <<'_EOF_'
RUN_AS_SERVICE=
EXEC=
AUTOMAP_PORT_HTTP=
AUTOMAP_PORT_SSH=
AUTOMAP_PORT_SMTP=
VOLUMES=
PORTS=
LINKS=
BASE_IMAGE=%%BASE_IMAGE%%
COMMAND_POST_RUN=
PRIVILEGED=
PERMIT_CLEANUP="CONTAINER IMAGE VOLUME USER DB_USER DATABASE FILE DIR"
BACKUP_BEFORE_CLEANUP="VOLUME DATABASE"
#SET VARIABLENAME=VARIABLEVALUE
_EOF_
### template: pre_build.sh
mapfile _T_PRE_BUILD_SH <<'_EOF_'
#!/bin/bash
# __WRITE_HERE_PRE_BUILD_PROCESS__
_EOF_
### template: post_build.sh
mapfile _T_POST_BUILD_SH <<'_EOF_'
#!/bin/bash
# __WRITE_HERE_POST_BUILD_PROCESS__
_EOF_
### template: container_services.sh
mapfile _T_CONTAINER_SERVICES_SH <<'_EOF_'
#!/bin/bash
# `container_services.sh` is now obsolete.
# Please use `_cSetScriptContainerServices` in `container_build.sh`
# But this file will be treated properly for the sake of compatibility.
_EOF_
function _recker-scaffold(){
local _longOpts
while [[ \
-n "${1-}" \
&& "${1-}" =~ ^--[a-zA-Z0-9_][a-zA-Z0-9_=\"-][a-zA-Z0-9_=\"-]* \
]]
do
_longOpts="${_longOpts-} ${1-}"
shift
done
local -A __opts=$( _getOptsArray ${_longOpts-} )
local _optImage
if [[ "${__opts[image]-}" || "${__opts[img]-}" ]]; then
_optImage="${__opts[image]-}""${__opts[img]-}"
fi
local _name=''
local i
if [[ "${1-}" ]]; then
_name="${1-}"
shift
else
_warn "specify name for scaffolding new Dockerfile"
return 1
fi
local _optRecommendScriptFor
if [[ ${__opts[recommend_script_for]-} ]]; then
_optRecommendScriptFor="${__opts[recommend_script_for]-}"
fi
local _context="${_CONF[DIR_DOCKER_BASE_GLOBAL]-}/${_CONF[DIR_DOCKER_DOCKERFILES]-}/${_name-}"
[[ $(_getEuid) -ne 0 ]] \
&& _context="${_CONF[DIR_DOCKER_DOCKERFILES_THISUSER]-}/${_name-}"
if [[ -d "${_context-}" ]]; then
_warn "that context already exists: ${_context-}"
return 1
fi
local _imgName="${_name-}"
local _maintainerName="${USER-}"
[[ -n "${_CONF[DOCKER_MAINTAINER_NAME]-}" ]] \
&& _maintainerName="${_CONF[DOCKER_MAINTAINER_NAME]}"
local -A _ICONF
local _imgConfigFile="${_context-}/config.sh"
if [[ -r "${_imgConfigFile-}" ]]; then
source <( \
grep '^[A-Z0-9_]\{1,\}=.*$' "${_imgConfigFile-}" \
| grep -v '^SET[[:blank:]]' \
| sed -e 's|^\([A-Z0-9_]\{1,\}\)\(=.*\)$|_ICONF[\1]\2|' \
)
fi
for i in _upload
do
_makeSureDir "${_context-}/$i"
done
local _fn=''
local _tn=''
local _t=''
local _key=''
local -A _VARS=(
[BASE_IMAGE]="${_CONF[DOCKER_BUILD_BASE_IMG]-}"
)
[[ -n "${_ICONF[BASE_IMG]-}" ]] \
&& _VARS[BASE_IMAGE]="${_ICONF[BASE_IMG]-}"
[[ ! -z "${_optImage-}" ]] \
&& _VARS[BASE_IMAGE]="${_optImage-}"
for i in \
Dockerfile container_build.sh config.sh \
pre_build.sh post_build.sh \
;
do
_fn=$i
_tn="${i//./_}"
_tn="_T_${_tn^^}"
_t=$( eval "printf %s \"\${${_tn-}[@]}\"" )
for _key in "${!_VARS[@]}"
do
_t="${_t//%%${_key-}%%/${_VARS[${_key-}]-}}"
done
echo "${_t-}" > "${_context-}/${_fn-}"
[[ "${_fn-}" = 'Dockerfile' || "${_fn-}" = 'config.sh' ]] \
|| chmod +x "${_context-}/${_fn-}"
done
if [[ ${__opts[with_sysinit]-} ]]; then
__opts[conf_run_as_service]=1
__opts[conf_privileged]=1
__opts[conf_exec]=/sbin/init
fi
_fn="${_context-}/config.sh"
local _value
for i in ${!__opts[@]};
do
if [[ \
"${i-}" =~ ^[A-Za-z0-9_-][A-Za-z0-9_-]*$ \
&& "${i-}" =~ ^conf \
]]; then
:
else
continue
fi
_value=${__opts[${i-}]-}
i=${i#conf-}
i=${i#conf_}
i=${i^^}
if grep -q "^${i-}" ${_fn}; then
sed -i \
-e "s|^\(${i-}\)=.\{0,\}\$|\1=${_value-}|" \
${_fn-}
else
echo "${i-}=${_value-}" >> ${_fn-}
fi
done
if [[ "${_optRecommendScriptFor-}" ]]; then
_fn="${_context-}/container_build.sh"
local _dist=${_optRecommendScriptFor-}
local _t
function __setRecommend () {
local _dist="${1}"
local _type="${2}"
[[ "${_type-}" ]] && _type="_${_type-}"
local _tag="${3}"
local _targetFile="${4}"
_tn="_B_RECOMMEND_SCRIPT${_type-}_${_dist^^}"
_t=$( eval "printf %s \"\${${_tn-}[@]}\"" )
if [[ "${_t-}" ]]; then
local _counterPart=$(\
echo "${_t-}" \
| sed -e 's|\\|\\\\|g' -e 's|\&|\\&|g'\
| sed -r ':loop; N; $!b loop; s/[\r\n]/\\\\n/g'
)
sed -i \
-e "s?^\(\#[[:blank:]]\{1,\}${_tag-}\)\$?\1\n${_counterPart-}?" \
-e 's?\\n?\n?g' \
${_targetFile-}
fi
}
__setRecommend \
"${_dist-}" \
"" \
"__WRITE_HERE_HOW_TO_BUILD_CONTAINER__" \
"${_fn-}"
__setRecommend \
"${_dist-}" \
"1STRUNTIME" \
"__WRITE_HERE_1ST_RUNTIME_SCRIPT__" \
"${_fn-}"
__setRecommend \
"${_dist-}" \
"SERVICES" \
"__WRITE_HERE_SERVICE_SCRIPT__" \
"${_fn-}"
fi
_mention "scaffolding completed in context: ${_context-}"
}
###
### subcommands depends on percol
###
###
### Built-in files for building images
###
### File-content: container_lib.sh
### for _print_container_lib_sh
mapfile _B_CONTAINER_LIB_SH <<'_EOF_BUILT_IN_FILE'
#!/bin/bash
# Check distribution
DIST='unknown'
ISSUES=$( cat /etc/issue ) || DIST='unknown'
[[ ${ISSUES-} =~ 'CentOS' ]] && DIST='centos'
[[ ${ISSUES-} =~ 'GNU/Linux' ]] && DIST='debian'
[[ ${ISSUES-} =~ 'Alpine' ]] && DIST='alpine'
function _cIsDist () {
[[ ${DIST-} =~ "${1-}" ]] && return
return 1
}
ARCH='unknown'
MACHINE=$( uname -m ) || ARCH='unknown'
[[ ${MACHINE-} =~ 'arm' ]] && ARCH='arm'
[[ ${MACHINE-} =~ 'x86' ]] && ARCH='x86'
[[ ${MACHINE-} =~ 'x86_64' ]] && ARCH='x86_64'
function _cIsArch () {
[[ ${ARCH-} =~ "${1-}" ]] && return
return 1
}
# File path
_C_BASHRC=''
case "${DIST-}" in
centos )
_C_BASHRC='/etc/bashrc'
;;
debian )
_C_BASHRC='/etc/bash.bashrc'
;;
esac
# Services
function _cRunServices() {
for i in "$@"
do
[[ -x systemctl ]] && \
systemctl start $i || \
service $i start
done
}
# Strip strange Docker args/envs
function _cStripEnv() {
[[ "${1-}" ]] || return
local _e="${1#\"}"
_e="${_e%\"}"
echo ${_e//__/ }
}
# Tar shared volumes
function _cTarSharedVolumes() {
[[ "$@" ]] || return 1
mkdir -p /var/recker/tar
tar \
cvzf \
/var/recker/tar/shared_dirs.tgz \
--directory / \
--preserve-permissions \
$@
# --acls \
# --xattrs \
}
# Untar shared volumes
function _cUntarSharedVolumes() {
[[ -r /var/recker/tar/shared_dirs.tgz ]] || return 1
tar \
xvzf \
/var/recker/tar/shared_dirs.tgz \
--directory / \
--preserve-permissions \
--overwrite \
--overwrite-dir
# --acls \
# --xattrs
}
#
function _cPreserveSharedVolumes () {
[[ -z "${VOLUMES-}" || "${DONT_PRESERVE_SHARED_VOLUME-}" -eq 1 ]] \
&& return
_cTarSharedVolumes ${VOLUMES-}
}
#
function _cSetScriptFor1stRuntime () {
cat <<'_EOF_' > /var/recker/usr/container_runonce.sh
#!/bin/bash
_EOF_
cat - >> /var/recker/usr/container_runonce.sh
chmod +x /var/recker/usr/container_runonce.sh
}
#
function _cSetScriptContainerServices () {
cat <<'_EOF_' > /var/recker/usr/container_services.sh
#!/bin/bash
_EOF_
cat - >> /var/recker/usr/container_services.sh
chmod +x /var/recker/usr/container_services.sh
}
_EOF_BUILT_IN_FILE
mapfile _B_RECOMMEND_SCRIPT_CENTOS <<'_EOF_BUILT_IN_FILE'
_cIsDist centos \
&& {
echo 'ZONE="Asia/Tokyo"' > /etc/sysconfig/clock
/bin/rm /etc/localtime
ln -fs /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
[[ "${_C_BASHRC}" ]] && \
echo '[[ "${PS1-}" ]] && PS1=":D $PS1"' >> ${_C_BASHRC}
}
_EOF_BUILT_IN_FILE
mapfile _B_RECOMMEND_SCRIPT_1STRUNTIME_CENTOS <<'_EOF_BUILT_IN_FILE'
#
_EOF_BUILT_IN_FILE
mapfile _B_RECOMMEND_SCRIPT_SERVICES_CENTOS <<'_EOF_BUILT_IN_FILE'
#
_EOF_BUILT_IN_FILE
# Debian
mapfile _B_RECOMMEND_SCRIPT_DEBIAN <<'_EOF_BUILT_IN_FILE'
_cIsDist debian \
&& {
[[ "${_C_BASHRC}" ]] && \
echo '[[ "${PS1-}" ]] && PS1=":D $PS1"' >> ${_C_BASHRC}
}
_EOF_BUILT_IN_FILE
mapfile _B_RECOMMEND_SCRIPT_1STRUNTIME_DEBIAN <<'_EOF_BUILT_IN_FILE'
#
_EOF_BUILT_IN_FILE
mapfile _B_RECOMMEND_SCRIPT_SERVICES_DEBIAN <<'_EOF_BUILT_IN_FILE'
#
_EOF_BUILT_IN_FILE
# Alpine Linux
mapfile _B_RECOMMEND_SCRIPT_ALPINE <<'_EOF_BUILT_IN_FILE'
_cIsDist alpine \
&& {
sed -i \
-e 's/#rc_sys=""/rc_sys="lxc"/g' \
-e 's/^#\(rc_logger="YES"\)$/\1/' \
/etc/rc.conf
echo 'rc_provide="loopback net"' >> /etc/rc.conf
sed -i 's|^tty|#tty|' /etc/inittab
echo 'id:3:initdefault:' >> /etc/inittab
echo 'Asia/Tokyo' > /etc/timezone
cat <<'_EOF' > /etc/network/interfaces
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet dhcp
_EOF
apk update
for i in \
urandom procfs devfs sysctl dmesg loopback \