-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_util_docker
More file actions
executable file
·140 lines (121 loc) · 5.92 KB
/
ft_util_docker
File metadata and controls
executable file
·140 lines (121 loc) · 5.92 KB
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
#!/usr/bin/env bash
# Simple function to run and log docker commands
function run_docker_cmd() {
local cmd="$1"
$S_LOG -s debug -d "$S_NAME" -d "docker_cmd" "$cmd"
$cmd
local exit_code=$?
$S_LOG -s ${exit_code} -d "$S_NAME" "[${cmd}] returned EXIT_CODE=${exit_code}"
return $exit_code
}
export -f run_docker_cmd
# Generate arguments for docker compose command
# docker-compose_arg "/path/docker-compose.yml" ["/path/docker-compose.env"] ["profile1,profile2"]
function docker-compose_arg() {
for i in $(echo ${1} | tr "," "\n"); do arg_str+=" --file ${i}"; done
[ -n "${2}" ] && for i in $(echo ${2} | tr "," "\n"); do arg_str+=" --env-file ${i}"; done
[ -n "${3}" ] && for i in $(echo ${3} | tr "," "\n"); do arg_str+=" --profile ${i}"; done
echo "${arg_str}"
}
export -f docker-compose_arg
# docker-compose_configcheck "/path/docker-compose.yml" ["/path/docker-compose.env"] ["profile1,profile2"]
function docker-compose_configcheck() {
local docker_cmd="docker compose $(docker-compose_arg ${1} ${2} ${3}) config --quiet"
$S_LOG -s debug -d "$S_NAME" -d "docker_cmd" "$docker_cmd"
$docker_cmd 2>&1 | $S_LOG -s warn -d "$S_NAME|$1" -i
local exit_code=${PIPESTATUS[0]}
$S_LOG -s ${exit_code} -d "$S_NAME" "[${docker_cmd}] returned EXIT_CODE=${exit_code}"
[ $exit_code -ne 0 ] && exit $exit_code
}
export -f docker-compose_configcheck
# Remove old images which repo are used in docker compose config
# docker-compose_prune_img "/path/docker-compose.yml" ["/path/docker-compose.env"] ["profile1,profile2"]
# Modified script from https://stackoverflow.com/a/37133531
function docker-compose_prune_img() {
local imgs_conf=($(docker compose $(docker-compose_arg ${1} ${2} ${3}) config --images)) # Get images list from docker compose conf
local imgs_used=($(docker ps --all --format '{{.Image}}' | uniq)) # Get all the images currently in use
local imgs_all=($(docker images --format '{{.Repository}}:{{.Tag}}:{{.ID}}')) # Get all the images currently available
local imgs_to_remove=() # Array to collect images to be removed
# Remove the unused images that are not part of imgs_conf and not currently in use
for img in "${imgs_all[@]}"; do
local img_repo=$(echo "$img" | awk -F: '{print $1}')
local img_name=$(echo "$img_repo" | awk -F/ '{print $(NF)}')
local img_tag=$(echo "$img" | awk -F: '{print $2}')
local img_id_trunc=$(echo "$img" | awk -F: '{print $3}')
# Check if the image is in use
local img_in_use=false
for used in "${imgs_used[@]}"; do
if [[ "${img_repo}:${img_tag}" == "$used" ]] || [[ "${img_id_trunc}" == "$used" ]]; then
img_in_use=true
break
fi
done
# Check if the image repository is part of the config images (ignoring tag)
local img_in_conf=false
for conf in "${imgs_conf[@]}"; do
local conf_img_name=$(echo "$conf" | awk -F: '{print $1}' | awk -F/ '{print $(NF)}')
if [[ "$img_name" == "$conf_img_name" ]]; then
img_in_conf=true
break
fi
done
# Remove the image if repo is in conf and specific image not in use
[[ "$img_in_conf" == true && "$img_in_use" == false ]] && imgs_to_remove+=("$img_id_trunc")
done
# Remove all collected images in one command
if [ ${#imgs_to_remove[@]} -gt 0 ]; then
run_docker_cmd "docker image rm ${imgs_to_remove[*]}" || exit
else
$S_LOG -d "$S_NAME" "No unused images to remove."
fi
}
export -f docker-compose_prune_img
# Pull new images if exist
# docker-compose_pull "/path/docker-compose.yml" ["/path/docker-compose.env"] ["profile1,profile2"]
function docker-compose_pull() {
run_docker_cmd "docker compose $(docker-compose_arg ${1} ${2} ${3}) pull --quiet"
}
export -f docker-compose_pull
# If images or configuration changed, container(s) will be recreated
# docker-compose_up "/path/docker-compose.yml" ["/path/docker-compose.env"] ["profile1,profile2"]
function docker-compose_up() {
run_docker_cmd "docker compose $(docker-compose_arg ${1} ${2} ${3}) up --detach --always-recreate-deps --quiet-pull" || exit
}
export -f docker-compose_up
# docker-compose_down "/path/docker-compose.yml" ["/path/docker-compose.env"] ["profile1,profile2"]
function docker-compose_down() {
run_docker_cmd "docker compose $(docker-compose_arg ${1} ${2} ${3}) down"
}
export -f docker-compose_down
# docker_container_wait_healthy container_name
function docker_container_wait_healthy() {
if [ "$(docker inspect -f {{.State.Health.Status}} $1)" != "healthy" ]; then
$S_LOG -d "$S_NAME" "Waiting for container $1 is started and healthy"
while [ "$(docker inspect -f {{.State.Health.Status}} $1)" != "healthy" ]; do
printf "."
sleep 5
done
echo
fi
}
export -f docker_container_wait_healthy
# verify_docker_local_volumes "my_project" "/srv/project/docker-data"
verify_docker_local_volumes() {
local project_name=$1
local project_path=$2
# run the docker volume inspect command with filters and format
local inspect_output=$(docker volume ls -q --filter name=${project_name}_* | xargs docker volume inspect --format '{{json .Options}} {{.Name}}')
# check if output is empty or null
if [ -z "$inspect_output" ]; then
$S_LOG -s err -d "$S_NAME" "Error: No volumes found for project ${project_name}."
return 1
fi
# check if any options are null and throw error if found
local null_option=$(echo "$inspect_output" | grep "null" | awk '{print $2}')
if [ -n "$null_option" ]; then
$S_LOG -s err -d "$S_NAME" "Options null for volume $null_option"
$S_LOG -s err -d "$S_NAME" "This means that the local volume have NOT been binded to $project_path"
return 1
fi
}
export -f verify_docker_local_volumes