-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp
1461 lines (1319 loc) · 45.8 KB
/
app
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
#!/usr/bin/env bash
# vim: syntax=sh cc=80 tw=79 ts=4 sw=4 sts=4 et sr
######################################################################
# @Project : app
# @File : app
# @Description : Bash script to bring up the Docker Compose services
# for the project. This script is designed
# to be easily expandable as more services are added
# to the project.
#
# @Author : Alan Szmyt
# @Date : 2024-08-22
# @Version : 1.0
# @References :
# - https://www.gnu.org/software/bash/
# - https://www.gnu.org/software/bash/manual/bash.html
# - https://docs.docker.com/compose/cli-command/
# - https://docs.docker.com/compose/compose-file/
# - https://docs.docker.com/compose/reference/overview/
# - http://redsymbol.net/articles/unofficial-bash-strict-mode/
# - https://www.shellcheck.net/
# - https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html
# - https://tldp.org/LDP/abs/html/
# - https://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html
# - https://man7.org/linux/man-pages/man7/signal.7.html
# - http://redsymbol.net/articles/bash-exit-traps/
# - https://mywiki.wooledge.org/BashGuide/Practices
# - https://bertvv.github.io/cheat-sheets/Bash.html
# - https://google.github.io/styleguide/shellguide.html
# - https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
# - https://docs.docker.com/build/building/variables/
# - https://docs.docker.com/build/building/secrets/
# - https://docs.docker.com/build/building/base-images/
# - https://no-color.org/
# - https://ftp.gnu.org/gnu/aspell/dict/0index.html
#
# @Notes :
# Built-In Variables
# For reference
# $0 name of the script
# $n positional parameters to script/function
# $$ PID of the script
# $! PID of the last command executed (and run in the background)
# $? exit status of the last command (${PIPESTATUS} for pipelined commands)
# $# number of parameters to script/function
# $@ all parameters to script/function (sees arguments as separate word)
# $* all parameters to script/function (sees arguments as single word)
# Note
# $* is rarely the right choice.
# $@ handles empty parameter list and white-space within parameters correctly
######################################################################
IFS=$'\n\t'
daemon_file="/etc/docker/daemon.json"
#######################################
# Log messages depending on the log level.
# This function logs messages based on the project's log level. It will only log messages
# that have a level greater than or equal to the current project log level.
#
# Globals:
# PROJECT_LOG_LEVEL (int): Controls the verbosity of the logs. Default is 3 (INFO).
# Arguments:
# $1: Log level of the message (DEBUG, INFO, WARNING, ERROR).
# $2: Log message to output.
# Outputs:
# Writes log messages to stdout or stderr depending on the log level.
# Returns:
# 0: Always returns 0 (success).
#######################################
function log() {
local LEVELS=("ERROR" "WARNING" "INFO" "DEBUG")
local LOG_LEVEL="${1}"
local MESSAGE="${2}"
# Set default log level if not set (INFO level, which is 3)
local PROJECT_LOG_LEVEL="${PROJECT_LOG_LEVEL:-2}"
# Convert log level to a numeric value
local LOG_LEVEL_NUM=0
for _level in "${!LEVELS[@]}"; do
if [[ "${LEVELS[${_level}]}" == "${LOG_LEVEL}" ]]; then
LOG_LEVEL_NUM="${_level}"
break
fi
done
# Log only if the log level is greater than or equal to the project log level
if [[ "${LOG_LEVEL_NUM}" -le "${PROJECT_LOG_LEVEL}" ]]; then
# Output to stderr for WARNING and ERROR, otherwise stdout
if [[ "${LOG_LEVEL}" == "WARNING" || "${LOG_LEVEL}" == "ERROR" ]]; then
echo "[${LOG_LEVEL}] ${MESSAGE}" >&2
else
echo "[${LOG_LEVEL}] ${MESSAGE}"
fi
fi
}
#######################################
# Log an error message.
# This function logs an error message with the "ERROR" log level.
#
# Globals:
# PROJECT_LOG_LEVEL (int): The current project log level.
# Arguments:
# $*: The message to log.
# Outputs:
# Writes the log message to stderr.
# Returns:
# 0: Always returns 0 (success).
#######################################
function log::error() {
log "ERROR" "$*"
}
#######################################
# Log a warning message.
# This function logs a warning message with the "WARNING" log level.
#
# Globals:
# PROJECT_LOG_LEVEL (int): The current project log level.
# Arguments:
# $*: The message to log.
# Outputs:
# Writes the log message to stderr.
# Returns:
# 0: Always returns 0 (success).
#######################################
function log::warning() {
log "WARNING" "$*"
}
#######################################
# Log an informational message.
# This function logs an info message with the "INFO" log level.
#
# Globals:
# PROJECT_LOG_LEVEL (int): The current project log level.
# Arguments:
# $*: The message to log.
# Outputs:
# Writes the log message to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function log::info() {
log "INFO" "$*"
}
#######################################
# Log a debug message.
# This function logs a debug message with the "DEBUG" log level.
#
# Globals:
# PROJECT_LOG_LEVEL (int): The current project log level.
# Arguments:
# $*: The message to log.
# Outputs:
# Writes the log message to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function log::debug() {
log "DEBUG" "$*"
}
#######################################
# Custom realpath function to resolve absolute path.
# This function mimics the behavior of `realpath` using built-in Bash commands.
#
# Arguments:
# $1: Path to resolve.
# Outputs:
# Writes the absolute path of the file or directory to stdout.
# Returns:
# 0: If successful.
# 1: If the path does not exist.
#######################################
function realpath() {
local _target_file="${1}"
# Check if the file/directory exists
if [[ ! -e "${_target_file}" ]]; then
echo "Error: '${_target_file}' does not exist." >&2
return 1
fi
# Resolve symbolic links if necessary
while [[ -L "${_target_file}" ]]; do
local _link
_link=$(readlink "${_target_file}")
# If the symlink is relative, resolve relative to the directory of the symlink
if [[ "${_link}" != /* ]]; then
_target_file="$(dirname "${_target_file}")/${_link}"
else
_target_file="${_link}"
fi
done
# Get the absolute directory of the target file
local _absolute_directory
_absolute_directory=$(cd "$(dirname "${_target_file}")" && pwd)
# Return the full absolute path
echo "${_absolute_directory}/$(basename "${_target_file}")"
}
#######################################
# Check if a given command is available in the system.
# Globals:
# None
# Arguments:
# _command (string) : The name of the command to check.
# Outputs:
# Returns 0 if the command is available, 1 if not.
#######################################
function app::has_command() {
local _command="${1}"
if command -v "${_command}" >/dev/null 2>&1; then
return 0
else
return 1
fi
}
#######################################
# Check if the current script is being sourced.
# This function checks whether the current script is being sourced or executed.
# If the script is sourced, it will return 0 (true); if executed, it will return 1 (false).
#
# Globals:
# BASH_SOURCE (array): Array of sourced filenames (bash-specific).
# Arguments:
# None
# Outputs:
# None
# Returns:
# 0: If the script is being sourced.
# 1: If the script is being executed directly.
#######################################
function app::is_sourced() {
# If the script is sourced, BASH_SOURCE[0] will be different from $0
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
return 0 # Sourced
else
return 1 # Executed directly
fi
}
#######################################
# Enable errtrace for error trap handler inheritance.
# This function ensures that the error trap handler is inherited by all functions and
# subshells within the script. Without enabling errtrace, error trapping won't work as expected
# in subshells or function calls.
#
# Globals:
# None
# Arguments:
# None
# Outputs:
# None
# Returns:
# 0: Always returns 0 (success).
# References:
# - https://stackoverflow.com/a/28776166/8787985
#######################################
function app::enable_errtrace() {
set -o errtrace
}
#######################################
# Get script path details and assign them to variables.
# Outputs:
# SCRIPT_PATH: Full absolute path to the script.
# SCRIPT_DIR: Directory containing the script.
# SCRIPT_NAME: Script file name with extension.
# SCRIPT_NAME_NO_EXT: Script file name without extension.
# SCRIPT_EXT: Script file extension.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_script_info() {
readonly ORIGINAL_CWD="${PWD}"
readonly SCRIPT_OPTIONS="$*"
# Full path of the script
SCRIPT_PATH=$(realpath "$0")
# Directory containing the script
SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")
# Script file name with extension
SCRIPT_NAME=$(basename "${SCRIPT_PATH}")
# Script file name without extension
SCRIPT_NAME_NO_EXT="${SCRIPT_NAME%.*}"
# Script file extension
SCRIPT_EXT="${SCRIPT_NAME##*.}"
readonly SCRIPT_DIR SCRIPT_NAME SCRIPT_PATH SCRIPT_NAME_NO_EXT SCRIPT_EXT
# Display the information. TODO change to use log::info
printf "SCRIPT_PATH: %s\n" "${SCRIPT_PATH}"
printf "SCRIPT_DIR: %s\n" "${SCRIPT_DIR}"
printf "SCRIPT_NAME_WITH_EXT: %s\n" "${SCRIPT_NAME}"
printf "SCRIPT_NAME_NO_EXT: %s\n" "${SCRIPT_NAME_NO_EXT}"
printf "SCRIPT_EXT: %s\n" "${SCRIPT_EXT}"
}
function app::error_handler() {
local _exit_code=1
# Disable the error trap handler to prevent potential recursion.
trap - ERR
# Consider any further errors non-fatal to ensure we run to completion.
set +o errexit
set +o pipefail
# Validate any provided exit code.
if [[ ${1-} =~ ^[0-9]+$ ]]; then
_exit_code="${1}"
fi
# Output debug data if in Cron mode.
if [[ -n ${cron-} ]]; then
# Restore original file output descriptors
if [[ -n ${script_output-} ]]; then
exec 1>&3 2>&4
fi
# Print basic debugging information
printf '***** Abnormal termination of script *****\n'
printf 'Script Path: %s\n' "${SCRIPT_PATH}"
printf 'Script Parameters: %s\n' "${SCRIPT_OPTIONS}"
printf 'Script Exit Code: %s\n' "${_exit_code}"
# Print the script log if we have it. It's possible we may not if we
# failed before we even called cron_init(). This can happen if bad
# parameters were passed to the script so we bailed out very early.
if [[ -n ${script_output-} ]]; then
# shellcheck disable=SC2312
printf 'Script Output:\n\n%s' "$(cat "${script_output}")"
else
printf 'Script Output: None (failed before log init)\n'
fi
fi
# Exit with failure status.
exit "${_exit_code}"
}
function app::exit_handler() {
cd "${ORIGINAL_CWD}"
# Remove Cron mode script log.
if [[ -n ${cron-} && -f ${script_output-} ]]; then
rm "${script_output}"
fi
# Remove script execution lock
if [[ -d ${script_lock-} ]]; then
rmdir "${script_lock}"
fi
# Restore terminal colours
# printf '%b' "${ta_none}"
}
#######################################
# Print all environment variables in alphabetical order.
# Globals:
# None
# Arguments:
# None
# Outputs:
# Writes sorted environment variables to stdout.
#######################################
function app::print_environment() {
printenv | sort
}
#######################################
# Load environment variables from a .env file, ignoring comments and empty lines.
# Globals:
# None
# Arguments:
# _env_file (string): Path to the .env file.
# Outputs:
# Exports environment variables from the file to the shell.
# Prints an error message if the file is not found.
# Returns:
# 0: If environment variables are successfully loaded.
# 1: If the .env file is not found.
#######################################
function app::load_env_file() {
local _env_file="${1}"
if [[ -f "${_env_file}" ]]; then
# Process each line that is not a comment or empty
while IFS='=' read -r key value; do
# Remove any part after a '#' symbol (inline comments) using parameter expansion
value="${value%%#*}"
# Strip leading/trailing spaces from key and value
key=$(echo "${key}" | xargs)
value=$(echo "${value}" | xargs)
# Ignore empty keys or values
if [[ -n "${key}" && -n "${value}" ]]; then
echo "key: ${key} value: ${value}"
export "${key}"="${value}"
fi
done < <(grep -v '^#' "${_env_file}") # Redirection to avoid subshell
# For debugging, you can print the environment variables
printenv | sort
else
echo ".env file not found: ${_env_file}"
return 1
fi
}
#######################################
# Find the Docker executable and construct the Docker command with optional debug flags.
# Globals:
# PROJECT_DEBUG (optional): If set to 1, Docker debug mode is enabled.
# Arguments:
# None
# Outputs:
# Writes the Docker executable path to stdout.
# Exports the constructed Docker command to be used globally.
# Returns:
# 0: If Docker is found and the command is constructed.
# 1: If Docker is not installed or not in the PATH.
#######################################
function app::find_docker() {
# Find the Docker executable path
local _docker_path
_docker_path=$(command -v docker)
if [[ -z "${_docker_path}" ]]; then
echo "Docker is not installed or not in the PATH."
return 1
fi
printf "Docker found at: %s\n" "${_docker_path}"
# Start constructing the Docker command
local _docker_cmd="${_docker_path}"
# Check if debug is enabled via environment variable
if [[ "${PROJECT_DEBUG:-0}" -eq 1 ]]; then
echo "Enabling Docker debug mode."
docker_command=("${_docker_cmd}" --debug --log-level "debug")
else
docker_command=("${_docker_cmd}")
fi
# Export the docker command so it can be used globally in the script
export docker_command
}
#######################################
# Wrapper function to execute the Docker command with optional arguments.
# Globals:
# docker_command: The Docker command constructed by the find_docker function.
# Arguments:
# ${@}: All arguments passed to this function are forwarded to the Docker command.
# Outputs:
# Executes the Docker command with the provided arguments and outputs the result.
# Returns:
# The return code of the Docker command execution.
#######################################
function app::docker() {
${docker_command} "${@}"
}
#######################################
# Get the operating system running Docker.
# Outputs:
# Prints the operating system running Docker to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_operating_system() {
app::docker info --format "{{.OperatingSystem}}"
}
#######################################
# Get the Containerd version used by Docker.
# Outputs:
# Prints the Containerd version used by Docker to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_containerd_version() {
app::docker info --format "{{.ContainerdCommit.ID}}"
}
#######################################
# Get the Docker root directory where Docker stores container data.
# Outputs:
# Prints the Docker root directory to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_docker_root_dir() {
app::docker info --format "{{.DockerRootDir}}"
}
#######################################
# Get the number of currently running containers.
# Outputs:
# Prints the number of running containers to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_running_containers() {
app::docker info --format "{{.ContainersRunning}}"
}
#######################################
# Get the total number of Docker images on the system.
# Outputs:
# Prints the total number of images to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_total_images() {
app::docker info --format "{{.Images}}"
}
#######################################
# Get the status of the Docker storage driver.
# Outputs:
# Prints the Docker storage driver status to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_driver_status() {
app::docker info --format "{{ .DriverStatus }}"
}
#######################################
# Get the OS of the Docker client.
# Outputs:
# Prints the OS of the Docker client to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_client_os() {
app::docker version --format "{{.Client.Os}}"
}
#######################################
# Get the architecture of the Docker client.
# Outputs:
# Prints the architecture of the Docker client to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_client_arch() {
app::docker version --format "{{.Client.Arch}}"
}
#######################################
# Get the context of the Docker client.
# Outputs:
# Prints the current context of the Docker client to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_client_context() {
app::docker version --format "{{.Client.Context}}"
}
#######################################
# Get the version of the Docker client.
# Outputs:
# Prints the version of the Docker client to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::get_client_version() {
app::docker version --format "{{.Client.Version}}"
}
#######################################
# Gather and print key Docker system information.
# This function gathers various Docker system details by calling the appropriate getter functions.
#
# Outputs:
# Displays Docker system information to stdout.
# Returns:
# 0: Always returns 0 (success).
#######################################
function app::gather_docker_info() {
# shellcheck disable=SC2310
printf "Operating System: %s\n" "$(app::get_operating_system || true)"
# shellcheck disable=SC2310
printf "Containerd Version: %s\n" "$(app::get_containerd_version || true)"
# shellcheck disable=SC2310
printf "Docker Root Directory: %s\n" "$(app::get_docker_root_dir || true)"
# shellcheck disable=SC2310
printf "Running Containers: %s\n" "$(app::get_running_containers || true)"
# shellcheck disable=SC2310
printf "Total Images: %s\n" "$(app::get_total_images || true)"
# shellcheck disable=SC2310
printf "Driver Status: %s\n" "$(app::get_driver_status || true)"
# shellcheck disable=SC2310
printf "Client OS: %s\n" "$(app::get_client_os || true)"
# shellcheck disable=SC2310
printf "Client Arch: %s\n" "$(app::get_client_arch || true)"
# shellcheck disable=SC2310
printf "Client Context: %s\n" "$(app::get_client_context || true)"
# shellcheck disable=SC2310
printf "Client Version: %s\n" "$(app::get_client_version || true)"
}
#######################################
# Check if Docker is running on Docker Desktop.
# This function checks whether Docker is running on Docker Desktop by comparing
# the `operating_system` variable. Returns 0 if Docker Desktop is detected, otherwise returns 1.
#
# Globals:
# operating_system: The operating system running Docker (set by app::gather_docker_info).
# Arguments:
# None
# Outputs:
# None
# Returns:
# 0: If Docker is running on Docker Desktop.
# 1: If Docker is running on a different operating system.
#######################################
function app::is_docker_desktop() {
# Check if running on Docker Desktop or a Linux system with Docker Engine
# shellcheck disable=SC2310
if [[ $(app::get_operating_system || true) == "Docker Desktop" ]]; then
return 0
else
return 1
fi
}
#######################################
# Check if Docker is running in rootless mode.
# This function uses `docker info` to check if Docker is running in rootless mode
# by examining the "rootless" field in the output.
#
# Globals:
# docker_command (function): The Docker command constructed by find_docker.
# Arguments:
# None
# Outputs:
# Writes whether Docker is running in rootless mode or not to stdout.
# Returns:
# 0: If Docker is running in rootless mode.
# 1: If Docker is not running in rootless mode.
#######################################
function app::is_docker_rootless() {
# Use `docker info` to check if Docker is rootless
# shellcheck disable=SC2310
if app::docker info --format "{{.SecurityOptions}}" | grep -q "rootless"; then
echo "Docker is running in rootless mode."
return 0
else
echo "Docker is not running in rootless mode."
return 1
fi
}
# Function to check and modify docker daemon.json and restart the Docker service.
# TODO
function app::configure_containerd_snapshotter() {
local _containerd_snapshotter_setting=".features[\"containerd-snapshotter\"]"
# Check if daemon.json exists, if not create it
if [[ ! -f "${daemon_file}" ]]; then
echo "Docker daemon.json not found. Creating a new one."
echo '{"features": {"containerd-snapshotter": true}}' >"${daemon_file}"
else
# Check if 'containerd-snapshotter' is set to true in the daemon.json
if ! jq --exit-status "${_containerd_snapshotter_setting} == true" "${daemon_file}" >/dev/null 2>&1; then
echo "Setting 'containerd-snapshotter' to true in Docker daemon.json."
# Modify or add the 'containerd-snapshotter' setting to true
jq "${_containerd_snapshotter_setting} |= true" "${daemon_file}" >"/tmp/daemon.json" && mv "/tmp/daemon.json" "${daemon_file}"
else
echo "'containerd-snapshotter' is already enabled."
fi
fi
# Restart the Docker service
echo "Restarting Docker service..."
systemctl restart docker.service
# Wait for Docker to be back up
echo "Waiting for Docker service to restart..."
# shellcheck disable=SC2310
until app::docker info >/dev/null 2>&1; do
sleep 1
done
# Verify that the 'containerd-snapshotter' is in use
# shellcheck disable=SC2310
if app::docker info --format '{{ .DriverStatus }}' | grep -q "driver-type io.containerd.snapshotter.v1"; then
echo "The containerd snapshotter is in use."
else
echo "Failed to verify that the containerd snapshotter is in use."
return 1
fi
}
#######################################
# Find the path to the Docker daemon.json configuration file.
# This function determines the location of the Docker daemon configuration file (daemon.json)
# based on the operating system and Docker setup (e.g., Docker Desktop, rootless mode).
#
# Globals:
# operating_system (optional): The operating system running Docker (set by app::get_operating_system).
# HOME: The home directory of the current user.
# Arguments:
# None
# Outputs:
# Sets the DAEMON_JSON_PATH variable to the path of daemon.json.
# Returns:
# 0: If the path is successfully determined.
# 1: If the path cannot be determined.
#######################################
function app::find_daemon_json_path() {
local os
local daemon_path
# Get the operating system using uname
os=$(uname -s)
# Determine if Docker is running in rootless mode
if app::is_docker_rootless; then
log::info "Docker is running in rootless mode."
if [[ -n "${XDG_CONFIG_HOME}" ]]; then
daemon_path="${XDG_CONFIG_HOME}/docker/daemon.json"
else
daemon_path="${HOME}/.config/docker/daemon.json"
fi
else
# Check if Docker is running on Docker Desktop
if app::is_docker_desktop; then
log::info "Docker is running on Docker Desktop."
case "${os}" in
Darwin)
# macOS Docker Desktop
daemon_path="${HOME}/Library/Group Containers/group.com.docker/settings.json"
;;
MINGW* | CYGWIN* | MSYS* | Windows_NT)
# Windows Docker Desktop
daemon_path="/c/ProgramData/Docker/config/daemon.json"
;;
*)
log::error "Unsupported OS for Docker Desktop: ${os}"
return 1
;;
esac
else
# Standard Docker installation
log::info "Docker is running on a standard installation."
daemon_path="/etc/docker/daemon.json"
fi
fi
# Check if the daemon.json file exists
if [[ -f "${daemon_path}" ]]; then
DAEMON_JSON_PATH="${daemon_path}"
log::info "Found daemon.json at: ${DAEMON_JSON_PATH}"
return 0
else
log::warning "daemon.json not found at expected location: ${daemon_path}"
# Even if the file doesn't exist, return the expected path
DAEMON_JSON_PATH="${daemon_path}"
return 0
fi
}
#######################################
# Check if COMPOSE_PROJECT_NAME is set.
# Globals:
# COMPOSE_PROJECT_NAME
# Arguments:
# None
# Outputs:
# Writes an error message if COMPOSE_PROJECT_NAME is not set.
# Returns:
# 0: If COMPOSE_PROJECT_NAME is set.
# 1: If COMPOSE_PROJECT_NAME is not set.
#######################################
function app::check_compose_project_name() {
if [[ -z "${COMPOSE_PROJECT_NAME}" ]]; then
log::error "COMPOSE_PROJECT_NAME is not set. Please set it and try again."
return 1
fi
}
#######################################
# Get the Docker context name based on COMPOSE_PROJECT_NAME.
# Globals:
# COMPOSE_PROJECT_NAME
# Arguments:
# None
# Outputs:
# Sets the _context_name variable.
# Returns:
# 0: Always returns 0.
#######################################
function app::get_docker_context_name() {
_context_name="${COMPOSE_PROJECT_NAME}-context"
}
#######################################
# Get the Docker host endpoint.
# Globals:
# DOCKER_HOST
# Arguments:
# None
# Outputs:
# Sets the _docker_host variable.
# Returns:
# 0: Always returns 0.
#######################################
function app::get_docker_host() {
_docker_host="${DOCKER_HOST:-unix:///var/run/docker.sock}"
}
#######################################
# Check if the Docker context exists.
# Globals:
# None
# Arguments:
# $_context_name
# Outputs:
# Sets the _context_exists variable to 0 (true) or 1 (false).
# Returns:
# 0: Always returns 0.
#######################################
function app::check_docker_context_exists() {
if app::docker context ls --format "{{.Name}}" | grep -q "^${_context_name}$"; then
_context_exists=0
else
_context_exists=1
fi
}
#######################################
# Get the current Docker endpoint for the context.
# Globals:
# None
# Arguments:
# $_context_name
# Outputs:
# Sets the _current_endpoint variable.
# Returns:
# 0: If successful.
# 1: If failed to get the endpoint.
#######################################
function app::get_current_docker_endpoint() {
_current_endpoint=$(app::docker context inspect "${_context_name}" --format '{{.Endpoints.docker.Host}}')
if [[ -z "${_current_endpoint}" ]]; then
log::error "Failed to get current Docker endpoint for context '${_context_name}'."
return 1
fi
}
#######################################
# Update the Docker context with the new Docker host.
# Globals:
# None
# Arguments:
# $_context_name, $_docker_host
# Outputs:
# Logs the update status.
# Returns:
# 0: If the context is updated successfully.
# 1: If the update fails.
#######################################
function app::update_docker_context_endpoint() {
log::info "Updating context '${_context_name}' to use new Docker host: ${_docker_host}"
if app::docker context update "${_context_name}" --docker "host=${_docker_host}"; then
log::info "Docker context '${_context_name}' updated successfully."
else
log::error "Failed to update Docker context '${_context_name}'."
return 1
fi
}
#######################################
# Create a new Docker context.
# Globals:
# None
# Arguments:
# $_context_name, $_docker_host
# Outputs:
# Logs the creation status.
# Returns:
# 0: If the context is created successfully.
# 1: If the creation fails.
#######################################
function app::create_docker_context() {
log::info "Creating Docker context '${_context_name}' with Docker host: ${_docker_host}"
if app::docker context create --docker "host=${_docker_host}" "${_context_name}"; then
log::info "Docker context '${_context_name}' created successfully."
else
log::error "Failed to create Docker context '${_context_name}'."
return 1
fi
}
#######################################
# Switch to the specified Docker context.
# Globals:
# None
# Arguments:
# $_context_name
# Outputs:
# Logs the switch status.
# Returns:
# 0: If switched successfully.
# 1: If the switch fails.
#######################################
function app::switch_to_docker_context() {
log::info "Switching to Docker context '${_context_name}'..."
if docker context use "${_context_name}"; then
log::info "Successfully switched to Docker context '${_context_name}'."
else
log::error "Failed to switch to Docker context '${_context_name}'."
return 1
fi
}
#######################################
# Main function to configure the Docker context.
# Globals:
# None
# Arguments:
# None
# Outputs:
# Executes the steps to configure Docker context.
# Returns:
# 0: If successful.
# 1: If any step fails.
#######################################
function app::configure_docker_context() {
# shellcheck disable=SC2310
app::check_compose_project_name || return 1
app::get_docker_context_name
app::get_docker_host
app::check_docker_context_exists
if [[ "${_context_exists}" -eq 0 ]]; then
log::info "Docker context '${_context_name}' already exists."
app::get_current_docker_endpoint || return 1
if [[ "${_current_endpoint}" != "${_docker_host}" ]]; then
app::update_docker_context_endpoint || return 1
else
log::info "Docker endpoint is already set to ${_docker_host}. No update needed."
fi
else
app::create_docker_context || return 1
fi
app::switch_to_docker_context || return 1
}
# Function to create/update and switch to a Docker context based on the project name
function app::configure_sdocker_context() {
# Ensure COMPOSE_PROJECT_NAME is set
if [[ -z "${COMPOSE_PROJECT_NAME}" ]]; then
echo "COMPOSE_PROJECT_NAME is not set. Please set it and try again."
return 1
fi
# Define the context name and Docker endpoint
local _context_name="${COMPOSE_PROJECT_NAME}-context"
local _docker_host="${DOCKER_HOST:-unix:///var/run/docker.sock}"
# Check if the context already exists
# shellcheck disable=SC2310
if app::docker context ls --format "{{.Name}}" | grep -q "^${_context_name}$"; then
echo "Docker context '${_context_name}' already exists."