-
Notifications
You must be signed in to change notification settings - Fork 30
/
dcs
executable file
·645 lines (572 loc) · 18.1 KB
/
dcs
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
#!/bin/bash
### docker container control box V. 2.8
### Code from https://github.com/ralfyang/docker_cli_dashboard. Powered by Github.
### Made by ralf [email protected]
### Rewritten by tescom [email protected] 2018.08.08
## Make sure build DIR
BUildDirConf="${HOME}/build_dir.conf"
if [ ! -f ${BUildDirConf} ];then
touch ${BUildDirConf}
fi
extra_input=$1
BuildDir=$(cat ${BUildDirConf})
exefile=$(command -v dcs)
## Please modify under variable for the private repository if you have.
#Docker_repo=$(cat /data/z/etc/init.d/docker | grep "^connection" | awk -F '=' '{print $2}' | sed -e 's/"//g' )
#Docker_repo="[Docker repository IP]:[port]"
Docker_repo=""
Shell_cmd="/bin/bash"
HOST=$(hostname)
DATE=$(date +%Y%m%d)
## find a command
if [[ $(command -v podman) == "" ]];then
Docker_comm=$(command -v docker)
else
Docker_comm=$(command -v podman)
fi
## OS Check
Docker_version=$($Docker_comm -v | awk -F',' '{print $1}')
## Check the sudo
if [[ $(whoami) = "root" ]];then
SUDO=""
else
SUDO="sudo"
fi
Comm="${SUDO} ${Docker_comm}"
#ChkOS=$(cat /etc/*-release 2> /dev/null | head -1)
if [[ $(uname) == Linux ]];then
osv="linux"
elif [[ $(uname) == Darwin ]];then
Comm="${Docker_comm}"
osv="osx"
fi
## Temporary file create for work memory
Tmp_imgfile="${HOME}/docker_imgtmp.txt"
Tmp_psfile="${HOME}/docker_pstmp.txt"
Tmp_psAfile="${HOME}/docker_psAtmp.txt"
Tmp_tag="${HOME}/docker_tag.txt"
exit_abnormal() {
stty echo
rm -f ${Tmp_imgfile} ${Tmp_psfile} ${Tmp_psAfile} ${Tmp_tag} 2> /dev/null
exit 1
}
### Break sign catch for line break fix
trap exit_abnormal SIGINT SIGTERM
BAR="========================================================================================================================================================="
{
echo ${BAR}
echo " [:: Docker Images ::]"
echo ${BAR}
${Comm} images |awk '{printf "%.2d | "$0"\n",NR-1}' | sed -e 's/^00/ /g'
} >| ${Tmp_imgfile}
{
echo ""
echo ""
echo ${BAR}
echo " [:: Active Processors ::]"
echo ${BAR}
${Comm} ps |awk '{printf "%.2d | "$0"\n",NR-1}' |sed -e 's/^00/ /g'
} >| ${Tmp_psAfile}
{
echo ""
echo ""
echo ${BAR}
echo " [:: All Processors ::]"
echo ${BAR}
${Comm} ps -a |awk '{printf "%.2d | "$0"\n",NR-1}' |sed -e 's/^00/ /g'
} >| ${Tmp_psfile}
if [[ $osv = "osx" ]];then
for tmpfiles in ${Tmp_imgfile} ${Tmp_imgfile} ${Tmp_psAfile}
do
sed -i "" 's/^0 / /g' ${tmpfiles}
done
else
for tmpfiles in ${Tmp_imgfile} ${Tmp_imgfile} ${Tmp_psAfile}
do
sed -i 's/^0 / /g' ${tmpfiles}
done
fi
Print_screen(){
## Print screen
clear -x
cat ${Tmp_imgfile} ${Tmp_psAfile} ${Tmp_psfile}
echo ""
echo ""
echo "======================================"
echo " Please Insert a command as below :) "
echo "= Processor ========================== = Image ============================== = Commit & Push ====================="
echo " Run the container ---------- [ run ]| Pull Image ------------- [ pull ] | Commit --------------------- [commit]"
echo " Start ---------- [ start]| Remove Image ------------- [ rmi ] | Login to registry ---------- [ login]"
echo " Stop ---------- [ stop ]| Build Image ------------- [ build] | Tagging & Push ------------- [ push ]"
echo " Send command ---------- [ exec ]| Purge volume ------------- [ clean] | Prune image/volume/network - [ prune]"
echo " Into the running docker ---- [ it ]| Save All images to tarball - [ save ] | Container Stats ------------ [ stat ]"
echo " Remove PS ---------- [ rm ]| Load the image from DIR ---- [ load ] | * Star here! - https://bit.ly/git_dcs"
echo " Show log ---------- [ log ]| --------------------------------------| upgrade check & upgrade upgrade"
echo " Purge Processor ---------- [ purge]| Cli ---------- [ Type free as below ] | Exit Ctrl+c"
echo " All Processor stop!! ------- [ aps ]| ex) stop 910930 19809 102039 09103 | ${Docker_version}"
echo "======================================| ===================================== | ====================================="
printf "CMD>> "
}
if [[ ${extra_input} = "" ]];then
Print_screen
read -r CommandX
else
CommandX=${extra_input}
fi
## Make a number & array
ImageNum=$(tail -1 ${Tmp_imgfile} | awk '{print $1}')
PsNum=$(tail -1 ${Tmp_psfile} | awk '{print $1}')
PsANum=$(tail -1 ${Tmp_psAfile} | awk '{print $1}')
ImageIDArray=($(cat ${Tmp_imgfile} | awk '{print $5}' | tail -n +4))
ProcessorIDArray=($(cat ${Tmp_psfile} | awk '{print $3}' | tail -n +5))
ProcessorAIDArray=($(cat ${Tmp_psAfile} | awk '{print $3}' | tail -n +5))
## Function for command as below
# 2018.08.08 change container start options
# -it /bin/sh --> -id
# 2018.08.21 add volume mount option
run_container(){
echo " == Do you need to make port forwarding ? [host-port:container-port or null] "
read -r portcheck
if [[ ${portcheck} = "" ]];then
port_expose=""
else
port_parser=$(echo "${portcheck}" | sed -e 's/ [0-9]*:[0-9]/ -p&/g')
port_expose="-p ${port_parser}"
fi
while : ; do
echo " == Do you need to make volume mount ? [host-path:container-path or null] "
read -r volumemount
if [[ ${volumemount} = "" ]];then
volume_mount_option=""
break
else
chk_dir=$(echo ${volumemount} | awk -F: '{print $1}')
if [ -d ${chk_dir} ];then
echo ""
echo "Directory Check... Done"
echo ""
volume_mount_option="-v ${volumemount}"
break
else
echo ""
echo "${chk_dir} does not exist."
echo "Please check directory path!!"
echo ""
fi
fi
done
${Comm} run ${port_expose} ${volume_mount_option} -id --name $2 ${ImageIDArray[$1]}
}
# 2018-08.09 change sa to start & delete attach
start_container(){
${Comm} start $1
# ${Comm} attach $1
}
comm_container(){
${Comm} exec ${ProcessorAIDArray[$1]} $2
echo " - Please insert a any key if you have done - "
read -r
}
build_image(){
if [[ ! -d ${BuildDir} ]];then
echo "${BAR}"
echo " Please insert a Directory for image build under the Dockerfile [ex) /data/git/dockerfiles_dir]"
read -r ReadDir
#sudo sed -i "s#^BuildDir=\"\"#BuildDir=\"${ReadDir}\"#g" ${exefile}
echo "${ReadDir}" > ${BUildDirConf}
cd ${ReadDir} || return
else
cd ${BuildDir} || return
fi
if [[ ! -f ./docker_build_auto.sh ]];then
echo "${BAR}"
echo "Do you need to install docker_build_auto.sh here ? [ y/n ]"
read -r Answ
if [[ ${Answ} = "y" ]] || [[ ${Answ} = "Y" ]];then
wget ralfyang.github.io/docker_build_auto/docker_build_auto.sh
sudo chmod 755 docker_build_auto.sh
else
echo "${BAR}"
echo "You need to install the docker_build_auto.sh script for this work"
echo "${BAR}"
exit 0
fi
fi
./docker_build_auto.sh
}
rm_container(){
${Comm} rm ${ProcessorIDArray[$1]}
}
it_container(){
env_chk=$(${Comm} exec -it ${ProcessorAIDArray[$1]} $Shell_cmd -c "echo \"OK\"" > /tmp/env_chk)
if [[ $(grep "OK" /tmp/env_chk) != "" ]];then
Shell_cmd="/bin/bash"
else
Shell_cmd="/bin/sh"
fi
rm -f /tmp/env_chk
${Comm} exec -it ${ProcessorAIDArray[$1]} $Shell_cmd -c "echo \"alias ll='ls -lai'\" >> ~/.bashrc && $Shell_cmd"
}
pull_image(){
${Comm} pull $1
}
login_repository(){
Registry=$1
if [[ ${Registry} = "" ]];then
echo "Do you want to login to Private registry ? [y/N]"
read -r que_registry
if [[ ${que_registry} = "y" ]] || [[ ${que_registry} = "Y" ]] ;then
echo " Please insert a Private registry url:"
read -r Registry
${Comm} login ${Registry}
else
${Comm} login
fi
else
${Comm} login ${Registry}
fi
}
remove_image(){
${Comm} rmi -f ${ImageIDArray[$1]}
}
ci_image(){
${Comm} commit ${ProcessorAIDArray[$1]} $2
echo "${Comm} commit ${ProcessorAIDArray[$1]} $2"
}
save_all_images(){
echo "Please make sure the directory for backup [default= {User_Home}]:"
read -r Save_Dir
if [[ ${Save_Dir} = "" ]];then
Save_Dir="${HOME}"
else
echo " Docker image will be save in \"${Save_Dir}\" "
fi
docker_images=$(${Comm} images | grep -v "^REPOSITORY" | awk '{print $1":"$2}')
docker_image_count=$(${Comm} images | grep -v "^REPOSITORY" | wc -l)
mkdir -p ${Save_Dir}/tmp${DATE}/
echo " === Exporting the image to files === "
count=1
for i in ${docker_images}
do
echo "[ ${count} / ${docker_image_count} ] - ${i}"
tar_name=$(echo "${i}" |sed -e 's/:/_/g' -e 's#/#_#g' )
${Comm} save ${i} > ${Save_Dir}/tmp${DATE}/${tar_name}.tgz
count=$(( count+1 ))
done
echo " === Compressing image files to Tarball file (in ${Save_Dir}/docker_images_output_${HOST}_${DATE}.tgz) === "
cd ${Save_Dir}/tmp${DATE} || return
tar zcvf ${Save_Dir}/docker_images_output_${HOST}_${DATE}.tgz ./*.tgz 2> /dev/null
cd ${Save_Dir} || return
rm -Rf ${Save_Dir}/tmp${DATE} 2> /dev/null
echo " === Done === "
echo " === You can use this file ${Save_Dir}/docker_images_output_${HOST}_${DATE}.tgz for Backup or magration === "
sleep 1
}
load_all_images(){
echo " Please insert a Directory name as the file existed "
read -r tarball_images_dir
if [[ ${tarball_images_dir} = "" ]];then
echo " Please make sure the Directory name !!"
exit
fi
cd ${tarball_images_dir} || return
## Exception for the save file from dcs
tar zxfv docker_images_output*.tgz 2> /dev/null
docker_images_tgz=$(ls ${tarball_images_dir} |grep ".tgz" |grep -v "^docker_images_output*.tgz")
docker_images_count=$(ls ${tarball_images_dir} |grep ".tgz" |grep -v "^docker_images_output*.tgz" |wc -l)
echo " === Decompressing the file and apply the image to the Docker... === "
count=1
for i in ${docker_images_tgz}
do
echo "[ ${count} / ${docker_images_count} ] - ${i}"
${Comm} load < ${tarball_images_dir}/${i}
count=$(( count+1 ))
done
echo " === Done === "
sleep 1
}
Log_container(){
clear -x
${Comm} logs ${ProcessorAIDArray[$1]}
echo ""
echo " - Please insert an any key if you all done -"
read -r
}
Stop_container(){
${Comm} stop ${ProcessorAIDArray[$1]}
echo "${Comm} stop ${ProcessorAIDArray[$1]}"
}
# 2018.08.08 Add function
Container_stat(){
if [[ $1 == "all" ]];then
${Comm} stats --$1
else
${Comm} stats ${ProcessorAIDArray[$1]}
fi
}
Purge_image(){
${Comm} rm -v $(${Comm} ps --filter status=exited -q 2>/dev/null) 2>/dev/null
${Comm} rm -v $(${Comm} ps --filter status=created -q 2>/dev/null) 2>/dev/null
}
All_stop_process(){
${Comm} ps |awk '{print $1}' | grep -v 'CONTAINER' | xargs ${Comm} stop
echo "--------------------- ${Comm} ps |awk '{print $1}' | grep -v 'CONTAINER' | xargs ${Comm} stop----------------------"
}
Purge_Old_image(){
#${Comm} rmi $(docker images --filter "dangling=true" -q --no-trunc) 2> /dev/null
${Comm} container prune --force 2>/dev/null
echo " --- container clean: Done ---"
echo ""
${Comm} image prune -a --force 2>/dev/null
echo " --- Image clean: Done ---"
echo ""
${Comm} network prune --force 2>/dev/null
echo " --- Network clean: Done ---"
echo ""
${Comm} volume prune --force 2>/dev/null
echo " --- Volume clean: Done ---"
sleep 3
}
Clean_volume(){
${Comm} volume ls -qf dangling=true | xargs ${Comm} volume rm
}
if [[ $(echo "${CommandX}" | grep '\-y') != "" ]];then
answ="y"
#CommandX=$(echo "${CommandX}" | sed -e 's/ -y//g')
CommandX=$("${CommandX// -y/ }")
fi
## Case selection for instructions
if [[ $(echo "${CommandX}" | awk '{print $2}' ) = "" ]];then
case "${CommandX}" in
build)
build_image
;;
run)
echo " == Please insert a image number [01 - ${ImageNum} ]"
read -r ImageNum
echo " == Please insert a new name for Run :"
read -r DockerName
if [[ ${ImageNum} != "" ]];then
run_container ${ImageNum} ${DockerName}
else
echo " == No Image ID == "
fi
;;
# 2018.08.08 change name (sa --> start)
start)
echo " == Please insert docker name for start :"
read -r St_docker
if [[ ${St_docker} != "" ]];then
start_container ${St_docker}
else
echo " == No Processor ID == "
fi
;;
exec)
echo " == Please insert a Active Processor number [01 - ${PsANum} ]"
read -r ActiveProcessorNum
echo " == Please insert a command for send to container ?"
read -r CommandToContainer
if [[ ${ActiveProcessorNum} != "" ]];then
comm_container ${ActiveProcessorNum} ${CommandToContainer}
else
echo " == No Processor No. == "
fi
;;
it)
echo " == Please insert a Processor number for into that [01 - ${PsANum} ]"
read -r ProcessorNum
if [[ ${ProcessorNum} != "" ]];then
it_container ${ProcessorNum}
else
echo " == No Processor No. == "
fi
;;
rm)
echo " == Please insert a Processor number for Remove [01 - ${PsNum} ]"
read -r ProcessorNum
echo " == [ ${ProcessorIDArray[${ProcessorNum}]} ]: Are you sure ? [y / n] "
read -r RmAnswer
if [[ ${ProcessorNum} != "" ]];then
if [[ ${RmAnswer} = "y" ]];then
rm_container ${ProcessorNum}
fi
else
echo " == No Processor No. == "
fi
;;
pull)
echo " == Please insert an image name for pulls:"
read -r ImagePullname
if [[ ${ImagePullname} != "" ]];then
pull_image ${ImagePullname}
else
echo " == No image name == "
fi
;;
aps)
if [[ ${answ} != "y" ]];then
echo " == All active processor will be stop. Are sure that ? [ y/n ]:"
read -r AllprocessQ
if [[ ${AllprocessQ} = "y" ]];then
All_stop_process
fi
else
All_stop_process
fi
;;
purge)
if [[ ${answ} != "y" ]];then
echo " == No use processor will be remove. Are sure that ? [ y/n ]:"
read -r ImagePurge
if [[ ${ImagePurge} = "y" ]];then
Purge_image
fi
else
Purge_image
fi
;;
prune)
if [[ ${answ} != "y" ]];then
# echo " == No use image will be remove. Are sure that ? [ y/n ]:"
# read -r ImageOldPurge
# if [[ ${ImageOldPurge} = "y" ]];then
Purge_Old_image
# fi
else
Purge_Old_image
fi
;;
clean)
if [[ ${answ} != "y" ]];then
echo " == No use volume will be remove. Are sure that ? [ y/n ]:"
read -r VolumeClean
if [[ ${VolumeClean} = "y" ]];then
Clean_volume
fi
else
Clean_volume
fi
;;
save)
if [[ ${answ} != "y" ]];then
echo " == All images will be backup to file. OK ? [ y/n ]:"
read -r SaveImg
if [[ ${SaveImg} = "y" ]];then
save_all_images
fi
else
save_all_images
fi
;;
load)
load_all_images
;;
login)
login_repository $1
;;
rmi)
echo " == Please insert a number for remove the image [01 - ${ImageNum}] "
read -r RmImageNum
echo " == [ ${ImageIDArray[$RmImageNum]} ]: Are you sure ? [y / n]"
read -r RmIAnswer
if [[ ${RmImageNum} != "" ]];then
if [[ ${RmIAnswer} = "y" ]];then
remove_image ${RmImageNum}
fi
else
echo " == No image No. == "
fi
;;
commit)
echo " == Please insert a number of Active processor [01 - ${PsANum}] "
read -r CiNum
echo " == Please make sure the name for use:"
read -r CiAnswer
if [[ ${CiNum} != "" ]];then
ci_image ${CiNum} ${CiAnswer}
else
echo " == No image No. == "
fi
;;
push)
if [[ ${Docker_repo} != "" ]];then
${Comm} images | grep -v "${Docker_repo}" | awk '{print NR-1,$0}' | sed -e 's/^0/ /g' > ${Tmp_tag}
else
${Comm} images | awk '{print NR-1,$0}' | sed -e 's/^0/ /g' > ${Tmp_tag}
fi
cat ${Tmp_tag}
TagNum=$(cat ${Tmp_tag} | tail -1 | awk '{print $1}')
echo " == Please choose a number for tag and push [01 - ${TagNum}] "
read -r TagPush
echo -e "\033[32;3;7m * Please login first to the Docker registry \033[0m ex) docker login"
if [[ ${TagPush} != "" ]];then
echo "Taget image= $(grep \"^${TagPush} \" ${Tmp_tag} | awk '{print $2":"$3}')"
TagetPush=$(grep "^${TagPush} " ${Tmp_tag} | awk '{print $2":"$3}')
if [[ ${Docker_repo} = "" ]];then
echo "== Do you need to make a tag ? [y / n]"
read -r Tagcomm
if [[ ${Tagcomm} = "y" ]];then
echo "Please insert a tag name:"
read -r Tagname
${Comm} tag ${TagetPush} ${Tagname}
${Comm} push ${Tagname}
else
${Comm} push ${TagetPush}
fi
else
${Comm} tag ${TagetPush} ${Docker_repo}/${TagetPush}
${Comm} push ${Docker_repo}/${TagetPush}
fi
fi
;;
log)
echo " == Please insert a number of Active processor [01 - ${PsANum}] "
read -r LogNum
if [[ ${LogNum} != "" ]];then
Log_container ${LogNum}
else
echo " == No image No. == "
fi
;;
stop)
echo " == Please insert a number of Active processor [01 - ${PsANum}] "
read -r StopNum
if [[ ${StopNum} != "" ]];then
Stop_container ${StopNum}
else
echo " == No image No. == "
fi
;;
upgrade)
Current_ver=$(cat ${exefile} |grep 'V. [0-9].[0-9]' | sed -e 's/^### [[:alnum:]* ]* V. //g')
Latest_ver=$(curl -sLH "Cache-Control: no-cache" bit.ly/ralf_dcs |grep 'V. [0-9].[0-9]' | sed -e 's/^### [[:alnum:]* ]* V. //g')
if [[ ${Current_ver} < ${Latest_ver} ]];then
curl -sLH "Cache-Control: no-cache" bit.ly/ralf_dcs -o /tmp/curl
sudo cp /tmp/curl ${exefile}
sudo rm -f /tmp/curl
chmod 755 ${exefile}
echo " === dcs has been updated!! - Version ${Current_ver} -> ${Latest_ver} ==="
exit 0
else
echo " === Now you have latest version of dcs - Version ${Current_ver} ==="
sleep 3
fi
;;
# 2018.08.08 add stat command
stat)
echo " == Please insert a number of Active processor [01 - ${PsANum} or enter(all)] "
read -r StatNum
if [[ ${StatNum} != "" ]];then
Container_stat ${StatNum}
else
Container_stat all
fi
;;
esac
else
${Comm} ${CommandX}
fi
if [[ ${extra_input} = "" ]];then
${exefile}
fi