-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
executable file
·327 lines (284 loc) · 8.52 KB
/
script.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
#!/bin/bash
script_version="0.1"
script_dir="$(dirname "$(realpath "$0")")"
# Run all commands by default
script_command=fetch
# COLORS
ncolors=$(command -v tput >/dev/null && tput colors) # supports color
if [[ -n $ncolors && -z $NO_COLOR ]]; then
TERMCOLS=$(tput cols)
CLEAR="$(tput sgr0)"
# 4 bit colors
if test $ncolors -ge 8; then
# Normal
BLACK="$(tput setaf 0)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
GREY="$(tput setaf 7)"
fi
# >4 bit colors
if test $ncolors -gt 8; then
# High intensity
BLACK_I="$(tput setaf 8)"
RED_I="$(tput setaf 9)"
GREEN_I="$(tput setaf 10)"
YELLOW_I="$(tput setaf 11)"
BLUE_I="$(tput setaf 12)"
MAGENTA_I="$(tput setaf 13)"
CYAN_I="$(tput setaf 14)"
WHITE="$(tput setaf 15)"
else
BLACK_I=$BLACK
RED_I=$RED
GREEN_I=$GREEN
YELLOW_I=$YELLOW
BLUE_I=$BLUE
MAGENTA_I=$MAGENTA
CYAN_I=$CYAN
WHITE=$GREY
fi
# Styles
BOLD="$(tput bold)"
fi
function error_msg() {
# Error message
# $1 - Message string argument
# $2 (optional) - exit code
echo -e "${RED}${BOLD}[X] ${1}${CLEAR}"
[[ -n $2 ]] && exit $2
}
function warning_msg() {
echo -e "${YELLOW}${BOLD}[!] ${*}${CLEAR}"
}
function success_msg() {
echo -e "${GREEN}${BOLD}[✓] ${*}${CLEAR}"
}
function info_msg() {
# Info message if $verbose is set
[ -n "$verbose" ] && echo -e "${CYAN}[i] ${*}${CLEAR}"
}
# Display Help
help() {
echo -e "${CYAN}${BOLD}Docker GOST multi script v${script_version}${CLEAR}"
echo
echo -e "${YELLOW}ABOUT${CLEAR}"
echo -e "Script for managing Docker GOST CI/Build process"
echo
echo -e "${YELLOW}SYNTAX${CLEAR}"
echo -e "./script.sh [build|fetch|all] [-h|v|n] [-p] [ARG]"
echo
echo -e "${YELLOW}EXAMPLE${CLEAR}"
echo -e "Build all docker images without pusshing with verbose output"
echo -e "./script.sh build --no-push -v"
echo
echo -e "${YELLOW}COMMANDS${CLEAR}"
echo -e "build Build docker images."
echo -e "fetch Fetch latest versions."
echo -e "fetch-openssl Fetch latest openssl version and print to console."
echo -e "fetch-nginx Fetch latest nginx version and print to console."
echo
echo -e "${YELLOW}OPTIONS${CLEAR}"
echo -e "-h --help Print this Help."
echo -e "-v --verbose Verbose output"
echo -e "-b --no-build Don't trigger build if new version is available"
echo -e "-n --no-push Don't push images, only build"
echo
echo -e "${GREEN}${TERMCOLS} colors ${CLEAR}"
}
# ARG parser
if [ $# -eq 0 ]; then
# If no arguments, display help
help
else
while [ $# -gt 0 ]; do
case $1 in
build)
script_command=build
shift # shift argument
;;
fetch)
script_command=fetch
shift # shift argument
;;
fetch-openssl)
script_command=fetch-openssl
shift # shift argument
;;
fetch-nginx)
script_command=fetch-nginx
shift # shift argument
;;
--help | -h)
help
shift # shift argument
exit 0
;;
--verbose | -v)
verbose=true
shift # shift argument
;;
--no-build | -b)
no_build=true
shift # shift argument
;;
--no-push | -n)
no_push=true
shift # shift argument
;;
-*)
error_msg "Unknown option $1" 22
exit 1
;;
*)
error_msg "Unknown argument $1"
echo 'If you want to pass an argument with spaces'
echo 'pass the argument like this: "my argument"'
exit 1
;;
esac
done
fi
function build_images {
# Check for required commands
command -v jq >/dev/null || error_msg "Please install jq JSON parser package"
# Set versions
OPENSSL_VERSION=$(jq -r '.versions.openssl' data.json)
NGINX_VERSION=$(jq -r '.versions.nginx' data.json)
info_msg "Copying data.json"
cp -v "${script_dir}/data.json" "${script_dir}/.data.json"
info_msg "Replacing variables in .data.json"
sed -i "s/%%openssl%%/${OPENSSL_VERSION}/g" "${script_dir}/.data.json"
sed -i "s/%%nginx%%/${NGINX_VERSION}/g" "${script_dir}/.data.json"
info_msg ".data.json diff:\n $(diff --color data.json .data.json)"
# Set docker repository
docker_repo=$(jq -r '.docker.repository' data.json)
# Load images from data JSON to Bash list
local images=($(jq -r '.images[]|.name' .data.json))
# Loop through all images in list
for image in "${images[@]}"; do
info_msg "Building $image for $docker_repo"
# Load tags for image from data JSON to Bash list
local tags=($(jq -r ".images[] | select(.name == \"${image}\") | .tags | join (\" \")" .data.json))
local dockerfile="$(jq -r ".images[] | select(.name == \"${image}\") | .dockerfile" .data.json)"
local full_img_name="${docker_repo}:${image}"
# Build image
info_msg "Building ${full_img_name}"
docker build --progress plain \
--build-arg="OPENSSL_VERSION=openssl-${OPENSSL_VERSION}" \
--build-arg="NGINX_VERSION=${NGINX_VERSION}" \
-f "${dockerfile}" \
--tag "${full_img_name}" \
. &&
success_msg "Image ${full_img_name} build sucess" ||
failed_builds="${failed_builds} ${full_img_name}"
# Loop through all tags in list
for tag in "${tags[@]}"; do
info_msg "Retaging ${full_img_name} to ${docker_repo}:${tag}"
# Retag image with all alternative tags
docker tag "${full_img_name}" "${docker_repo}:${tag}" &&
success_msg "Sucessfully retagged ${full_img_name} to ${docker_repo}:${tag}" ||
error_msg "Failed to retag ${full_img_name} to ${docker_repo}:${tag}" 1
# Check if --no-push tag was passed
if [ -z "$no_push" ]; then
# Push retagged image
docker push "${docker_repo}:${tag}" &&
success_msg "Sucessfully pushed ${docker_repo}:${tag}" ||
error_msg "Failed to push ${docker_repo}:${tag}" 1
else
warning_msg "Not pushing image since --no-push flag was passed"
fi
done
success_msg "Succesfully built and pushed $image for $docker_repo"
done
success_msg "Succesfully finished built procedure"
}
function fetch_version_openssl {
# Get latest OpenSSL version and print result
curl -s "https://api.github.com/repos/openssl/openssl/tags" |
# Get all tag names
jq -r '.[]|.name' |
# Get "openssl-3.x.x" pattern
grep -P -m 1 'openssl-3\.[0-9]+\.[0-9]+$' |
# Get version number after "openssl-"
cut -d "-" -f2
}
function fetch_version_nginx {
# Get latest Nginx version and print result
curl -s "https://api.github.com/repos/nginx/nginx/tags" |
# Get all tag names
jq -r '.[]|.name' |
# Get first "release-x.x.x" match
grep -P -m 1 'release-[0-9]+\.[0-9]+\.[0-9]+$' |
# Get version number after "release-"
cut -d "-" -f2
}
function fetch_versions {
# Check for required commands
command -v sponge >/dev/null || error_msg "Please install sponge utility from moreutils package"
command -v jq >/dev/null || error_msg "Please install jq JSON parser package"
command -v curl >/dev/null || error_msg "Please install curl"
info_msg "Fetching versions"
# Get latest openssl version
local openssl_version=$(fetch_version_openssl)
# Get latest nginx version
local nginx_version=$(fetch_version_nginx)
info_msg "Fetched latest versions:"
info_msg "OpenSSL $openssl_version"
info_msg "Nginx $nginx_version"
[[ "$nginx_version" != $(jq -r '.versions.nginx' data.json) ]] &&
warning_msg "New Nginx version" &&
version_trigger=1 ||
success_msg "Nginx version unchaged"
[[ "$openssl_version" != $(jq -r '.versions.openssl' data.json) ]] &&
warning_msg "New OpenSSL version" &&
version_trigger=1 ||
success_msg "OpenSSL version unchaged"
[[ -n "$version_trigger" ]] && warning_msg "New Version detected"
# Save version data to json
jq ".versions.openssl=\"$openssl_version\"" data.json | sponge data.json
jq ".versions.nginx=\"$nginx_version\"" data.json | sponge data.json
success_msg "All versions fetched and saved to data.json file"
# If no_build is undefined and version_trigger is not empty
if [ -z "$no_build" ] && [ -n "$version_trigger" ]; then
warning_msg "Build triggered"
build_images
elif [ -n "$version_trigger" ]; then
warning_msg "Version change detected but build not triggered"
else
success_msg "Build not triggered"
fi
}
# Run Command parser
case $script_command in
build)
build_images
shift # shift argument
;;
fetch)
fetch_versions
shift # shift argument
;;
fetch-openssl)
fetch_version_openssl
shift # shift argument
;;
fetch-nginx)
fetch_version_nginx
shift # shift argument
;;
*)
error_msg "Unknown command '$script_command'"
error_msg "Please see list of commands that you can run in help"
exit 1
;;
esac
# Check for failed variables and output bad exit code
if [ -n "$failed_builds" ]; then
error_msg "Some builds were not sucessfull:\n${YELLOW}${failed_builds}"
exit 1
fi
exit 0