-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdocker_mainmenu.sh
executable file
·555 lines (452 loc) · 20.8 KB
/
docker_mainmenu.sh
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
#!/bin/bash
#--------------------------------------------------------------------
#REMARK:
# The command 'source' requires '#!/bin/bash' to be defined.
#--------------------------------------------------------------------
docker__get_source_fullpath__sub() {
#Define constants
local PHASE_CHECK_CACHE=1
local PHASE_FIND_PATH=10
local PHASE_EXIT=100
#Define variables
local phase=""
local current_dir=""
local parent_dir=""
local search_dir=""
local tmp_dir=""
local development_tools_foldername=""
local lTPP3_ROOTFS_foldername=""
local global_filename=""
local parentDir_of_LTPP3_ROOTFS_dir=""
local mainmenu_path_cache_filename=""
local mainmenu_path_cache_fpath=""
local find_dir_result_arr=()
local find_dir_result_arritem=""
local path_of_development_tools_found=""
local parentpath_of_development_tools=""
local isfound=""
local retry_ctr=0
#Set variables
phase="${PHASE_CHECK_CACHE}"
current_dir=$(dirname $(readlink -f $0))
parent_dir="$(dirname "${current_dir}")"
tmp_dir=/tmp
development_tools_foldername="development_tools"
global_filename="docker_global.sh"
lTPP3_ROOTFS_foldername="LTPP3_ROOTFS"
mainmenu_path_cache_filename="docker__mainmenu_path.cache"
mainmenu_path_cache_fpath="${tmp_dir}/${mainmenu_path_cache_filename}"
result=false
#Start loop
while true
do
case "${phase}" in
"${PHASE_CHECK_CACHE}")
if [[ -f "${mainmenu_path_cache_fpath}" ]]; then
#Get the directory stored in cache-file
docker__LTPP3_ROOTFS_development_tools__dir=$(awk 'NR==1' "${mainmenu_path_cache_fpath}")
#Move one directory up
parentpath_of_development_tools=$(dirname "${docker__LTPP3_ROOTFS_development_tools__dir}")
#Check if 'development_tools' is in the 'LTPP3_ROOTFS' folder
isfound=$(docker__checkif_paths_are_related "${current_dir}" \
"${parentpath_of_development_tools}" "${lTPP3_ROOTFS_foldername}")
if [[ ${isfound} == false ]]; then
phase="${PHASE_FIND_PATH}"
else
result=true
phase="${PHASE_EXIT}"
fi
else
phase="${PHASE_FIND_PATH}"
fi
;;
"${PHASE_FIND_PATH}")
#Print
echo -e "---:\e[30;38;5;215mSTART\e[0;0m: find path of folder \e[30;38;5;246m'${development_tools_foldername}\e[0;0m"
#Initialize variables
docker__LTPP3_ROOTFS_development_tools__dir=""
search_dir="${current_dir}" #start with search in the current dir
#Start loop
while true
do
#Get all the directories containing the foldername 'LTPP3_ROOTFS'...
#... and read to array 'find_result_arr'
readarray -t find_dir_result_arr < <(find "${search_dir}" -type d -iname "${lTPP3_ROOTFS_foldername}" 2> /dev/null)
#Iterate thru each array-item
for find_dir_result_arritem in "${find_dir_result_arr[@]}"
do
echo -e "---:\e[30;38;5;215mCHECKING\e[0;0m: ${find_dir_result_arritem}"
#Find path
isfound=$(docker__checkif_paths_are_related "${current_dir}" \
"${find_dir_result_arritem}" "${lTPP3_ROOTFS_foldername}")
if [[ ${isfound} == true ]]; then
#Update variable 'path_of_development_tools_found'
path_of_development_tools_found="${find_dir_result_arritem}/${development_tools_foldername}"
#Check if 'directory' exist
if [[ -d "${path_of_development_tools_found}" ]]; then #directory exists
#Update variable
#Remark:
# 'docker__LTPP3_ROOTFS_development_tools__dir' is a global variable.
# This variable will be passed 'globally' to script 'docker_global.sh'.
docker__LTPP3_ROOTFS_development_tools__dir="${path_of_development_tools_found}"
break
fi
fi
done
#Check if 'docker__LTPP3_ROOTFS_development_tools__dir' contains any data
if [[ -z "${docker__LTPP3_ROOTFS_development_tools__dir}" ]]; then #contains no data
case "${retry_ctr}" in
0)
search_dir="${parent_dir}" #next search in the 'parent' directory
;;
1)
search_dir="/" #finally search in the 'main' directory (the search may take longer)
;;
*)
echo -e "\r"
echo -e "***\e[1;31mERROR\e[0;0m: folder \e[30;38;5;246m${development_tools_foldername}\e[0;0m: \e[30;38;5;131mNot Found\e[0;0m"
echo -e "\r"
#Update variable
result=false
#set phase
phase="${PHASE_EXIT}"
break
;;
esac
((retry_ctr++))
else #contains data
#Print
echo -e "---:\e[30;38;5;215mCOMPLETED\e[0;0m: find path of folder \e[30;38;5;246m'${development_tools_foldername}\e[0;0m"
#Write to file
echo "${docker__LTPP3_ROOTFS_development_tools__dir}" | tee "${mainmenu_path_cache_fpath}" >/dev/null
#Print
echo -e "---:\e[30;38;5;215mSTATUS\e[0;0m: write path to temporary cache-file: \e[1;33mDONE\e[0;0m"
#Update variable
result=true
#set phase
phase="${PHASE_EXIT}"
break
fi
done
;;
"${PHASE_EXIT}")
break
;;
esac
done
#Exit if 'result = false'
if [[ ${result} == false ]]; then
exit 99
fi
#Retrieve directories
#Remark:
# 'docker__LTPP3_ROOTFS__dir' is a global variable.
# This variable will be passed 'globally' to script 'docker_global.sh'.
docker__LTPP3_ROOTFS__dir=${docker__LTPP3_ROOTFS_development_tools__dir%/*} #move one directory up: LTPP3_ROOTFS/
parentDir_of_LTPP3_ROOTFS_dir=${docker__LTPP3_ROOTFS__dir%/*} #move two directories up. This directory is the one-level higher than LTPP3_ROOTFS/
#Get full-path
#Remark:
# 'docker__global__fpath' is a global variable.
# This variable will be passed 'globally' to script 'docker_global.sh'.
docker__global__fpath=${docker__LTPP3_ROOTFS_development_tools__dir}/${global_filename}
}
docker__checkif_paths_are_related() {
#Input args
local scriptdir__input=${1}
local finddir__input=${2}
local pattern__input=${3}
#Define constants
local PHASE_PATTERN_CHECK1=1
local PHASE_PATTERN_CHECK2=10
local PHASE_PATH_COMPARISON=20
local PHASE_EXIT=100
#Define variables
local phase="${PHASE_PATTERN_CHECK1}"
local isfound1=""
local isfound2=""
local isfound3=""
local ret=false
while true
do
case "${phase}" in
"${PHASE_PATTERN_CHECK1}")
#Check if 'pattern__input' is found in 'scriptdir__input'
isfound1=$(echo "${scriptdir__input}" | \
grep -o "${pattern__input}.*" | \
cut -d"/" -f1 | grep -w "^${pattern__input}$")
if [[ -z "${isfound1}" ]]; then
ret=false
phase="${PHASE_EXIT}"
else
phase="${PHASE_PATTERN_CHECK2}"
fi
;;
"${PHASE_PATTERN_CHECK2}")
#Check if 'pattern__input' is found in 'finddir__input'
isfound2=$(echo "${finddir__input}" | \
grep -o "${pattern__input}.*" | \
cut -d"/" -f1 | grep -w "^${pattern__input}$")
if [[ -z "${isfound2}" ]]; then
ret=false
phase="${PHASE_EXIT}"
else
phase="${PHASE_PATH_COMPARISON}"
fi
;;
"${PHASE_PATH_COMPARISON}")
#Check if 'development_tools' is under the folder 'LTPP3_ROOTFS'
isfound3=$(echo "${scriptdir__input}" | \
grep -w "${finddir__input}.*")
if [[ -z "${isfound3}" ]]; then
ret=false
else
ret=true
fi
phase="${PHASE_EXIT}"
;;
"${PHASE_EXIT}")
break
;;
esac
done
#Output
echo "${ret}"
return 0
}
docker__load_global_fpath_paths__sub() {
source ${docker__global__fpath}
}
docker__load_constants__sub() {
DOCKER__MENUTITLE="${DOCKER__FG_LIGHTBLUE}DOCKER: ${DOCKER__FG_DARKBLUE}MAIN-MENU${DOCKER__NOCOLOR}"
DOCKER__REGEX_OUTSIDE_CONTAINER="[1-3890rcseidgq]"
DOCKER__REGEX_INSIDE_CONTAINER="[90bq]"
}
docker__checkIf_user_is_root__sub()
{
#Define local variable
currUser=$(whoami)
#Exec command
if [[ ${currUser} != "root" ]]; then
moveDown_and_cleanLines__func "${DOCKER__NUMOFLINES_1}"
echo -e "***${DOCKER__FG_LIGHTRED}ERROR${DOCKER__NOCOLOR}: current user is not ${DOCKER__FG_LIGHTGREY}root${DOCKER__NOCOLOR}"
moveDown_and_cleanLines__func "${DOCKER__NUMOFLINES_1}"
exit 99
fi
}
docker__init_variables__sub() {
docker__tibboHeader_prepend_numOfLines=${DOCKER__NUMOFLINES_2}
docker__regEx="${DOCKER__REGEX_OUTSIDE_CONTAINER}"
docker__myChoice=""
docker__git_current_branchName=${DOCKER__EMPTYSTRING}
docker__git_current_abbrevCommitHash=${DOCKER__EMPTYSTRING}
docker__git_current_unpushed_abbrevCommitHash=${DOCKER__EMPTYSTRING}
docker__git_current_push_status=${DOCKER__EMPTYSTRING}
docker__git_current_tag=${DOCKER__EMPTYSTRING}
docker__isRunning_inside_container=false
}
docker__enable_objects__sub() {
cursor_show__func
enable_expansion__func
enable_keyboard_input__func
enable_ctrl_c__func
enable_stty_intr__func
}
docker__checkif_isrunning_in_container__sub() {
#Check if running inside a container
docker__isRunning_inside_container=$(checkIf_isRunning_inside_container__func)
}
docker__mainmenu__sub() {
#Initialization
docker__tibboHeader_prepend_numOfLines=${DOCKER__NUMOFLINES_2}
#Check if running inside a container
docker__checkif_isrunning_in_container__sub
#If running inside containerm then change the 'docker_regEx' value
#Remark:
# By doing this, we will restrict access to certain options which
# are not relevant for when running inside a container.
if [[ ${docker__isRunning_inside_container} == true ]]; then
docker__regEx=${DOCKER__REGEX_INSIDE_CONTAINER}
fi
while true
do
#Get Git-information
#Output:
# docker_git_current_info_msg
docker__get_git_info__sub
#Print Tibbo-title
load_tibbo_title__func "${docker__tibboHeader_prepend_numOfLines}"
#Set 'docker__tibboHeader_prepend_numOfLines'
docker__tibboHeader_prepend_numOfLines=${DOCKER__NUMOFLINES_1}
#Print horizontal line
duplicate_char__func "${DOCKER__DASH}" "${DOCKER__TABLEWIDTH}"
#Print menut-title
show_leadingAndTrailingStrings_separatedBySpaces__func "${DOCKER__MENUTITLE}" "${docker_git_current_info_msg}" "${DOCKER__TABLEWIDTH}"
#Print horizontal line
duplicate_char__func "${DOCKER__DASH}" "${DOCKER__TABLEWIDTH}"
#Print menu-options
#Only print this part if NOT running in a container
if [[ ${docker__isRunning_inside_container} == false ]]; then
echo -e "${DOCKER__FOURSPACES}1. ${DOCKER__MENU} create ${DOCKER__FG_BORDEAUX}image${DOCKER__NOCOLOR}(s) using docker-file/list"
echo -e "${DOCKER__FOURSPACES}2. ${DOCKER__MENU} create${DOCKER__FG_LIGHTGREY}/${DOCKER__NOCOLOR}remove${DOCKER__FG_LIGHTGREY}/${DOCKER__NOCOLOR}rename ${DOCKER__FG_BORDEAUX}image${DOCKER__NOCOLOR}"
echo -e "${DOCKER__FOURSPACES}3. ${DOCKER__MENU} run${DOCKER__FG_LIGHTGREY}/${DOCKER__NOCOLOR}remove${DOCKER__FG_LIGHTGREY}/${DOCKER__NOCOLOR}build ${DOCKER__FG_BRIGHTPRUPLE}container${DOCKER__NOCOLOR}"
fi
#Only print this part if NOT running in a container
if [[ ${docker__isRunning_inside_container} == false ]]; then
echo -e "${DOCKER__FOURSPACES}8. Copy file from${DOCKER__FG_LIGHTGREY}/${DOCKER__NOCOLOR}to ${DOCKER__FG_BRIGHTPRUPLE}container${DOCKER__NOCOLOR}"
fi
echo -e "${DOCKER__FOURSPACES}9. Chroot from inside${DOCKER__FG_LIGHTGREY}/${DOCKER__NOCOLOR}outside ${DOCKER__FG_BRIGHTPRUPLE}container${DOCKER__NOCOLOR}"
echo -e "${DOCKER__FOURSPACES}0. Enter Command Prompt"
#Only print this part if NOT running in a container
duplicate_char__func "${DOCKER__DASH}" "${DOCKER__TABLEWIDTH}"
if [[ ${docker__isRunning_inside_container} == false ]]; then
echo -e "${DOCKER__FOURSPACES}r. ${DOCKER__FG_PURPLE}Repository${DOCKER__NOCOLOR}-list"
echo -e "${DOCKER__FOURSPACES}c. ${DOCKER__FG_BRIGHTPRUPLE}Container${DOCKER__NOCOLOR}-list"
duplicate_char__func "${DOCKER__DASH}" "${DOCKER__TABLEWIDTH}"
echo -e "${DOCKER__FOURSPACES}s. ${DOCKER__FG_YELLOW}SSH${DOCKER__NOCOLOR} to ${DOCKER__FG_BRIGHTPRUPLE}container${DOCKER__NOCOLOR}"
duplicate_char__func "${DOCKER__DASH}" "${DOCKER__TABLEWIDTH}"
echo -e "${DOCKER__FOURSPACES}i. Load from ${DOCKER__FG_BORDEAUX}image${DOCKER__NOCOLOR} file"
echo -e "${DOCKER__FOURSPACES}e. Save to ${DOCKER__FG_BORDEAUX}image${DOCKER__NOCOLOR} file"
duplicate_char__func "${DOCKER__DASH}" "${DOCKER__TABLEWIDTH}"
echo -e "${DOCKER__FOURSPACES}d. ${DOCKER__MENU} Dockerhub"
echo -e "${DOCKER__FOURSPACES}g. ${DOCKER__MENU} Github"
else
echo -e "${DOCKER__FOURSPACES}b. ${DOCKER__MENU} overlay & ISPBOOOT.BIN"
fi
duplicate_char__func "${DOCKER__DASH}" "${DOCKER__TABLEWIDTH}"
echo -e "${DOCKER__FOURSPACES}q. ${DOCKER__QUIT_CTRL_C}"
duplicate_char__func "${DOCKER__DASH}" "${DOCKER__TABLEWIDTH}"
# moveDown_and_cleanLines__func "${DOCKER__NUMOFLINES_1}"
while true
do
#Select an option
read -N1 -r -p "${DOCKER__PLEASE_CHOOSE_AN_OPTION}" docker__myChoice
moveDown_and_cleanLines__func "${DOCKER__NUMOFLINES_1}"
#Only continue if a valid option is selected
if [[ ! -z ${docker__myChoice} ]]; then
if [[ ${docker__myChoice} =~ ${docker__regEx} ]]; then
# moveDown_and_cleanLines__func "${DOCKER__NUMOFLINES_1}"
break
else
if [[ ${docker__myChoice} == ${DOCKER__ENTER} ]]; then
moveUp_and_cleanLines__func "${DOCKER__NUMOFLINES_2}"
else
moveUp_and_cleanLines__func "${DOCKER__NUMOFLINES_1}"
fi
fi
else
moveUp_and_cleanLines__func "${DOCKER__NUMOFLINES_1}"
fi
done
#Goto the selected option
case ${docker__myChoice} in
1)
${docker__create_images_from_dockerfile_dockerlist_menu__fpath}
;;
2)
${docker__image_create_remove_rename_menu__fpath}
;;
3)
#Depending on whether we are currently running inside a container
# or not execute the correct script-file.
# 1. if NOT inside a container -> run 'docker__container_run_remove_build_menu__fpath'
# 2. if INSIDE a container -> run 'docker__fs_partition_menu__fpath'
if [[ ${docker__isRunning_inside_container} == false ]]; then #NOT INSIDE container
${docker__container_run_remove_build_menu__fpath}
else #INSIDE container
${docker__fs_partition_menu__fpath}
fi
;;
8)
${docker__cp_fromto_container__fpath}
;;
9)
${docker__run_chroot__fpath}
;;
0)
${docker__enter_cmdline_mode__fpath} "${DOCKER__EMPTYSTRING}" "${DOCKER__EMPTYSTRING}" "${DOCKER__EMPTYSTRING}"
;;
c)
docker__show_containerList_handler__sub
;;
r)
docker__show_repositoryList_handler__sub
;;
s)
${docker__ssh_to_host__fpath}
;;
i)
${docker__load__fpath}
;;
e)
${docker__save__fpath}
;;
d)
${docker__dockerhub_menu__fpath}
;;
g)
${docker__git_menu__fpath}
;;
b)
#Note: this script will only be triggered if 'docker__isRunning_inside_container = true'
if [[ ${docker__isRunning_inside_container} == true ]]; then
${docker__fs_partition_menu__fpath}
fi
;;
q)
moveDown_and_cleanLines__func "${DOCKER__NUMOFLINES_1}"
exit
;;
esac
done
}
docker__show_repositoryList_handler__sub() {
#Show repo-list
show_repoList_or_containerList_w_menuTitle_w_confirmation__func "${DOCKER__MENUTITLE_REPOSITORYLIST}" \
"${DOCKER__ERRMSG_NO_IMAGES_FOUND}" \
"${docker__images_cmd}" \
"${DOCKER__NUMOFLINES_0}" \
"${DOCKER__TIMEOUT_10}" \
"${DOCKER__NUMOFLINES_0}" \
"${DOCKER__NUMOFLINES_0}" \
"${DOCKER__NUMOFLINES_2}"
}
docker__show_containerList_handler__sub() {
#Show container-list
show_repoList_or_containerList_w_menuTitle_w_confirmation__func "${DOCKER__MENUTITLE_CONTAINERLIST}" \
"${DOCKER__ERRMSG_NO_CONTAINERS_FOUND}" \
"${docker__ps_a_cmd}" \
"${DOCKER__NUMOFLINES_0}" \
"${DOCKER__TIMEOUT_10}" \
"${DOCKER__NUMOFLINES_0}" \
"${DOCKER__NUMOFLINES_0}" \
"${DOCKER__NUMOFLINES_2}"
}
docker__get_git_info__sub() {
#Get information
docker__git_current_branchName=`git__get_current_branchName__func`
docker__git_current_abbrevCommitHash=`git__log_for_pushed_and_unpushed_commits__func "${DOCKER__EMPTYSTRING}" \
"${GIT__LAST_COMMIT}" \
"${GIT__PLACEHOLDER_ABBREV_COMMIT_HASH}"`
docker__git_push_status=`git__checkIf_branch_isPushed__func "${docker__git_current_branchName}"`
docker__git_current_tag=`git__get_tag_for_specified_branchName__func "${docker__git_current_branchName}" "${DOCKER__FALSE}"`
if [[ -z "${docker__git_current_tag}" ]]; then
docker__git_current_tag="${GIT__NOT_TAGGED}"
fi
#Generate message to be shown
docker_git_current_info_msg="${DOCKER__FG_LIGHTBLUE}${docker__git_current_branchName}${DOCKER__NOCOLOR}:"
docker_git_current_info_msg+="${DOCKER__FG_DARKBLUE}${docker__git_current_abbrevCommitHash}${DOCKER__NOCOLOR}"
docker_git_current_info_msg+="(${DOCKER__FG_DARKBLUE}${docker__git_push_status}${DOCKER__NOCOLOR}):"
docker_git_current_info_msg+="${DOCKER__FG_LIGHTBLUE}${docker__git_current_tag}${DOCKER__NOCOLOR}"
}
#---MAIN SUBROUTINE
main__sub() {
docker__get_source_fullpath__sub
docker__load_global_fpath_paths__sub
docker__load_constants__sub
docker__checkIf_user_is_root__sub
docker__init_variables__sub
docker__enable_objects__sub
docker__mainmenu__sub
}
#Execute main subroutine
main__sub