forked from intel/cloud-native-ai-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_image_manager.sh
executable file
·214 lines (183 loc) · 5.95 KB
/
docker_image_manager.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
#!/bin/bash
set -e
current_directory=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")
top_directory=$(dirname "${current_directory}")
container_directory="${top_directory}/container"
action="all"
registry=""
container="all"
tag="latest"
docker_build_clean_param=""
all_containers=()
function scan_all_containers {
mapfile -t dirs < <(cd "${container_directory}" && ls -d ./*/)
for dir in "${dirs[@]}"
do
dir=${dir#./}
all_containers+=("${dir::-1}")
done
}
function usage {
cat << EOM
usage: $(basename "$0") [OPTION]...
-a <build|download|publish|save|all> all is default, which not include save. Please execute save explicity if need.
-r <registry prefix> the prefix string for registry
-c <container name> same as directory name
-g <tag> container image tag
-f Clean build
EOM
exit 1
}
function process_args {
while getopts ":a:r:c:g:hf" option; do
case "${option}" in
a) action=${OPTARG};;
r) registry=${OPTARG};;
c) container=${OPTARG};;
g) tag=${OPTARG};;
h) usage;;
f) docker_build_clean_param="--no-cache --rm";;
*) echo "Invalid option: -${OPTARG}" >&2
usage
;;
esac
done
if [[ ! "$action" =~ ^(build|download|publish|save|all)$ ]]; then
echo "invalid type: $action"
usage
fi
if [[ "$container" != "all" ]]; then
if [[ ! "${all_containers[*]}" =~ ${container} ]]; then
echo "invalid container name: $container"
usage
fi
fi
if [[ -z "$registry" ]]; then
if [[ -z "$EIP_REGISTRY" ]]; then
echo "Error: Please specify your docker registry via -r <registry prefix> or set environment variable EIP_REGISTRY."
exit 1
else
registry=$EIP_REGISTRY
fi
fi
}
function build_a_image {
local img_container=$1
echo "Build container image => ${registry}/${img_container}:${tag}"
if [ -f "${container_directory}/${img_container}/pre-build.sh" ]; then
echo "Execute pre build script at ${container_directory}/${img_container}/pre-build.sh"
"${container_directory}/${img_container}/pre-build.sh" || { echo 'Fail to execute pre-build.sh'; exit 1; }
fi
docker_build_args=(
"--build-arg" "http_proxy"
"--build-arg" "https_proxy"
"--build-arg" "no_proxy"
"--build-arg" "pip_mirror"
"-f" "${container_directory}/${img_container}/Dockerfile"
"${top_directory}"
"--tag" "${registry}/${img_container}:${tag}"
)
if [[ -n "${docker_build_clean_param}" ]]; then
read -ar split_params <<< "${docker_build_clean_param}"
docker_build_args+=("${split_params[@]}")
fi
docker build "${docker_build_args[@]}" || \
{ echo "Fail to build docker ${registry}/${img_container}:${tag}"; exit 1; }
echo "Complete build image => ${registry}/${img_container}:${tag}"
if [ -f "${container_directory}/${img_container}/post-build.sh" ]; then
echo "Execute post build script at ${container_directory}/${img_container}/post-build.sh"
"${container_directory}/${img_container}/post-build.sh" || { echo "Fail to execute post-build.sh"; exit 1; }
fi
echo -e "\n\n"
}
function build_images {
if [[ "$container" == "all" ]]; then
for img_container in "${all_containers[@]}"
do
build_a_image "$img_container"
done
else
build_a_image "$container"
fi
}
function publish_a_image {
local img_container=$1
echo "Publish container image: ${registry}/${img_container}:${tag} ..."
docker push "${registry}/${img_container}:${tag}" || \
{ echo "Fail to push docker ${registry}/${img_container}:${tag}"; exit 1; }
echo -e "Complete publish container image ${registry}/${img_container}:${tag} ...\n"
}
function publish_images {
if [[ "$container" == "all" ]]; then
for img_container in "${all_containers[@]}"
do
publish_a_image "$img_container"
done
else
publish_a_image "$container"
fi
}
function download_a_image {
local img_container=$1
echo "Download container image: ${registry}/${img_container}:${tag} ..."
crictl pull "${registry}/${img_container}:${tag}" || \
{ echo "Fail to download images ${registry}/${img_container}:${tag}"; exit 1; }
echo -e "Complete download container image ${registry}/${img_container}:${tag} ...\n"
}
function download_images {
if [[ "$container" == "all" ]]; then
for img_container in "${all_containers[@]}"
do
download_a_image "$img_container"
done
else
download_a_image "$container"
fi
}
function save_a_image {
local img_container=$1
echo "Save container image ${registry}/${img_container}:${tag} => ${top_directory}/images/ ... "
mkdir -p "${top_directory}/images/"
docker save -o "${top_directory}/images/${img_container}-${tag}.tar" "${registry}/${img_container}:${tag}"
docker save "${registry}/${img_container}:${tag}" | gzip > "${top_directory}/images/${img_container}-${tag}.tgz"
}
function save_images {
if [[ "$container" == "all" ]]; then
for img_container in "${all_containers[@]}"
do
save_a_image "$img_container"
done
else
save_a_image "$container"
fi
}
function check_docker {
if ! command -v docker &> /dev/null
then
echo "Docker could not be found. Please install Docker."
exit
fi
}
check_docker
scan_all_containers
process_args "$@"
echo ""
echo "-------------------------"
echo "action: ${action}"
echo "container: ${container}"
echo "tag: ${tag}"
echo "registry: ${registry}"
echo "-------------------------"
echo ""
if [[ "$action" =~ ^(build|all)$ ]]; then
build_images
fi
if [[ "$action" =~ ^(publish|all)$ ]]; then
publish_images
fi
if [[ "$action" =~ ^(save)$ ]]; then
save_images
fi
if [[ "$action" =~ ^(download)$ ]]; then
download_images
fi