This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-grabber.sh
More file actions
executable file
·483 lines (422 loc) · 19.1 KB
/
version-grabber.sh
File metadata and controls
executable file
·483 lines (422 loc) · 19.1 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
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
# shellcheck disable=SC2148
# -------------------------------------------------------------------------------- #
# Description #
# -------------------------------------------------------------------------------- #
# This script will attempt to get the version information for specific packages. #
# The aim is to make it work in multiples OSs, Shells and Package managers. #
# #
# It will display ready to use config that can be added directly into a Dockerfile #
# and meets the current hadolint specifications. #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
# Global Variables #
# -------------------------------------------------------------------------------- #
# Global variables that we use throughout the script. #
# -------------------------------------------------------------------------------- #
CRLF='\\\n'
TAB1='\t'
TAB2='\t\t'
# -------------------------------------------------------------------------------- #
# Get apk versions #
# -------------------------------------------------------------------------------- #
# Get version information for apk based operating systems. #
# -------------------------------------------------------------------------------- #
function get_apk_versions()
{
local packages="${1:-}"
local virtual_packages="${2:-}"
local output=''
local IFS=' '
output="${output}${TAB1}apk update && ${CRLF}"
if [[ -n "${packages}" ]]; then
output="${output}${TAB1}apk add --no-cache ${CRLF}"
for package in $packages; do
version=$(apk policy "${package}" 2>/dev/null | sed -n 2p | sed 's/:$//g' | sed 's/^[[:space:]]*//' || true)
if [[ -n "${version}" ]]; then
output="${output}${TAB2}$package=$version ${CRLF}"
fi
done
output="${output}${TAB2}&& ${CRLF}"
fi
if [[ -n "${virtual_packages}" ]]; then
output="${output}${TAB1}apk add --no-cache --virtual ${CRLF}"
for package in $virtual_packages; do
output="${output}${TAB2}$package ${CRLF}"
done
output="${output}${TAB2}&& ${CRLF}"
fi
echo -e "${output}"
}
# -------------------------------------------------------------------------------- #
# Get apt versions #
# -------------------------------------------------------------------------------- #
# Get version information for apt based operating systems. #
# -------------------------------------------------------------------------------- #
function get_apt_versions()
{
local packages="${1:-}"
local output=''
local IFS=' '
output="${output}${TAB1}apt-get update && ${CRLF}"
packages=$(clean_string "${packages}")
if [[ -n "${packages}" ]]; then
output="${output}${TAB1}apt-get -y --no-install-recommends install ${CRLF}"
for package in $packages; do
version=$(apt-cache policy "${package}" 2>/dev/null | grep 'Candidate:' | awk -F ' ' '{print $2}' || true)
if [[ -n "${version}" ]]; then
output="${output}${TAB2}$package=$version ${CRLF}"
fi
done
output="${output}${TAB2}&& ${CRLF}"
fi
echo -e "${output}"
}
# -------------------------------------------------------------------------------- #
# Get microdnf versions #
# -------------------------------------------------------------------------------- #
# Get version information for apt based operating systems. #
# -------------------------------------------------------------------------------- #
function get_microdnf_versions()
{
local packages="${1:-}"
local group_list="${2:-}"
local output=''
local IFS=' '
local yum_version=''
yum_version=$(microdnf repoquery yum | tail -n 1)
output="${output}${TAB1}microdnf -y update && ${CRLF}"
output="${output}${TAB1}microdnf install -y ${yum_version} && ${CRLF}"
output="${output}${TAB1}yum makecache && ${CRLF}"
if [[ -n "${packages}" ]]; then
output="${output}${TAB1}yum install -y ${CRLF}"
for package in $packages; do
version=$(yum info "${package}" 2>/dev/null | grep '^Version' | head -n 1 | awk -F ' : ' '{print $2}' || true)
if [[ -n "${version}" ]]; then
output="${output}${TAB2}$package-$version ${CRLF}"
fi
done
output="${output}${TAB2}&& ${CRLF}"
fi
if [[ -n "${group_list}" ]]; then
#
# We have to do some clever sliting to honour ""
#
declare -a "groups=($( echo "$group_list" | sed 's/[][`~!@#$%^&*():;<>.,?/\|{}=+-]/\\&/g' ))"
output="${output}${TAB1}yum groupinstall -y ${CRLF}"
# shellcheck disable=SC2154
for group in "${groups[@]}"; do
output="${output}${TAB2}\"$group\" ${CRLF}"
done
output="${output}${TAB2}&& ${CRLF}"
fi
echo -e "${output}"
}
# -------------------------------------------------------------------------------- #
# Get pacman versions #
# -------------------------------------------------------------------------------- #
# Get version information for pacman based operating systems. #
# -------------------------------------------------------------------------------- #
function get_pacman_versions()
{
local packages="${1:-}"
local output=''
local IFS=' '
output="${output}${TAB1}pacman -Syu --noconfirm && ${CRLF}"
if [[ -n "${packages}" ]]; then
output="${output}${TAB1}pacman -S --noconfirm ${CRLF}"
for package in $packages; do
version=$(pacman -Si "${package}" 2>/dev/null | grep 'Version' | awk -F ' ' '{print $3}' || true)
if [[ -n "${version}" ]]; then
output="${output}${TAB2}$package=$version ${CRLF}"
fi
done
output="${output}${TAB2}&& ${CRLF}"
fi
echo -e "${output}"
}
# -------------------------------------------------------------------------------- #
# Get tdnf versions #
# -------------------------------------------------------------------------------- #
# Get version information for tdnf based operating systems. #
# -------------------------------------------------------------------------------- #
function get_tdnf_versions()
{
local packages="${1:-}"
local output=''
local IFS=' '
output="${output}${TAB1}tdnf makecache && ${CRLF}"
if [[ -n "${packages}" ]]; then
output="${output}${TAB1}tdnf install -y ${CRLF}"
for package in $packages; do
version=$(tdnf info "${package}" 2>/dev/null | grep '^Version' | head -n 1 | awk -F ' : ' '{print $2}' || true)
if [[ -n "${version}" ]]; then
output="${output}${TAB2}$package-$version ${CRLF}"
fi
done
output="${output}${TAB2}&& ${CRLF}"
fi
echo -e "${output}"
}
# -------------------------------------------------------------------------------- #
# Get yum versions #
# -------------------------------------------------------------------------------- #
# Get version information for yum based operating systems. #
# -------------------------------------------------------------------------------- #
function get_yum_versions()
{
local packages="${1:-}"
local group_list="${2:-}"
local output=''
local IFS=' '
output="${output}${TAB1}yum makecache && ${CRLF}"
if [[ -n "${packages}" ]]; then
output="${output}${TAB1}yum install -y ${CRLF}"
for package in ${packages}; do
version=$(yum info "${package}" 2>/dev/null | grep '^Version' | head -n 1 | awk -F ' : ' '{print $2}' || true)
if [[ -n "${version}" ]]; then
output="${output}${TAB2}$package-$version ${CRLF}"
fi
done
output="${output}${TAB2}&& ${CRLF}"
fi
if [[ -n "${group_list}" ]]; then
#
# We have to do some clever sliting to honour ""
#
declare -a "groups=($( echo "$group_list" | sed 's/[][`~!@#$%^&*():;<>.,?/\|{}=+-]/\\&/g' ))"
output="${output}${TAB1}yum groupinstall -y ${CRLF}"
# shellcheck disable=SC2154
for group in "${groups[@]}"; do
output="${output}${TAB2}\"$group\" ${CRLF}"
done
output="${output}${TAB2}&& ${CRLF}"
fi
echo -e "${output}"
}
# -------------------------------------------------------------------------------- #
# Clean String #
# -------------------------------------------------------------------------------- #
# Clean the presented string and remove unwanted characters. [Order IS important!] #
# #
# To ensure that this works in multiple shells we have to avoid using bashisms. #
# #
# clean="${clean##*( )}" # Remove leading spaces #
# clean="${clean%%*( )}" # Remove trailing spaces #
# clean="${clean##+([[:space:]])}" # globbing to remove repeated whitespace #
# -------------------------------------------------------------------------------- #
function clean_string()
{
clean="${1:-}"
# Remove leading and trailing spaces
clean="$(echo -e "${clean}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# Remove leading and trailing "
clean="$(echo "${clean}" | sed -e 's/^"//' -e 's/"$//')"
# Remove leading and trailing spaces
clean="$(echo -e "${clean}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# Remove repeated whitespace
# shellcheck disable=SC2001
clean="$(echo "${clean}" | sed -e 's/ \{2,\}/ /g')"
echo "${clean}"
}
# -------------------------------------------------------------------------------- #
# Identify provider #
# -------------------------------------------------------------------------------- #
# Identify which package provider the OS is using. #
# -------------------------------------------------------------------------------- #
function detect_distribution
{
local os_name
if type "lsb_release" > /dev/null 2>&1; then
os_name=$(lsb_release -a | grep 'Distributor ID' | awk -F: '{ print $2 }')
else
if [[ -f /etc/redhat-release ]]; then
os_name=$(sed s/\ release.*// < /etc/redhat-release)
fi
if [[ -f /etc/os-release ]]; then
os_name=$(grep '^ID' /etc/os-release | grep -v '^ID_LIKE' | awk -F= '{ print $2 }')
fi
fi
if [[ -z "${os_name}" ]]; then
os_name="unknown"
fi
os_name=$(clean_string "${os_name}")
echo "${os_name}"
}
# -------------------------------------------------------------------------------- #
# Identify provider #
# -------------------------------------------------------------------------------- #
# Identify which package provider the OS is using. #
# -------------------------------------------------------------------------------- #
function discover_by_operating_system
{
local os_name
os_name=$(detect_distribution)
case "${os_name}" in
# Alma Linux (almalinux)
almalinux)
get_yum_versions "${ALMA_PACKAGES:-}" "${ALMA_GROUPS:-}"
;;
# Alpine Linux (alpine)
alpine)
get_apk_versions "${ALPINE_PACKAGES:-}" "${ALPINE_VIRTUAL_PACKAGES:-}"
;;
# Amazon Linux (amazonlinux)
amzn)
get_yum_versions "${AMAZON_PACKAGES:-}" "${AMAZON_GROUPS:-}"
;;
# Arch Linux (archlinux)
arch)
get_pacman_versions "${ARCH_PACKAGES:-}"
;;
# Centos (centos)
centos)
get_yum_versions "${CENTOS_PACKAGES:-}" "${CENTOS_GROUPS:-}"
;;
# Debian (debian)
debian)
get_apt_versions "${DEBIAN_PACKAGES:-}"
;;
# Oracle Linux (oraclelinux)
ol)
get_yum_versions "${ORACLE_PACKAGES:-}" "${ORACLE_GROUPS:-}"
;;
# Photon (photon)
photon)
get_tdnf_versions "${PHOTON_PACKAGES:-}"
;;
# Rocky Linux (rocky)
rocky)
get_yum_versions "${ROCKY_PACKAGES:-}" "${ROCKY_GROUPS:-}"
;;
# Scientific Linux (sl)
scientific)
get_yum_versions "${SCIENTIFIC_PACKAGES:-}" "${SCIENTIFIC_GROUPS:-}"
;;
# Ubuntu
ubuntu)
get_apt_versions "${UBUNTU_PACKAGES:-}"
;;
*)
echo "Unknown OS (${os_name})"
esac
}
# -------------------------------------------------------------------------------- #
# Identify provider #
# -------------------------------------------------------------------------------- #
# Identify which package provider the OS is using. #
# -------------------------------------------------------------------------------- #
#
# Should we change the order to the most useful ones come first ?? eg tdnf comes with yum as well
#
function discover_by_package_manager
{
if command -v apk > /dev/null; then
get_apk_versions "${APK_PACKAGES:-}" "${APK_VIRTUAL_PACKAGES:-}"
elif command -v apt > /dev/null; then
get_apt_versions "${APT_PACKAGES:-}"
elif command -v microdnf > /dev/null; then
get_microdnf_versions "${YUM_PACKAGES:-}" "${YUM_GROUPS:-}"
elif command -v pacman > /dev/null; then
get_pacman_versions "${PACMAN_PACKAGES:-}"
elif command -v tdnf > /dev/null; then
get_tdnf_versions "${TDNF_PACKAGES:-}"
elif command -v yum > /dev/null; then
get_yum_versions "${YUM_PACKAGES:-}" "${YUM_GROUPS:-}"
else
echo "Unsupport OS type"
fi
}
# -------------------------------------------------------------------------------- #
# Force Install of Awk #
# -------------------------------------------------------------------------------- #
# Some of the OS' e.g. Photon do not have aws installed and we need it. #
# -------------------------------------------------------------------------------- #
function force_update_and_install_of_prereqs
{
if command -v apk > /dev/null; then
{
apk update &&
apk add gawk
} > /dev/null 2>&1 || true
elif command -v apt > /dev/null; then
{
apt-get update &&
apt-get install -y gawk
} > /dev/null 2>&1 || true
elif command -v microdnf > /dev/null; then
{
microdnf -y update &&
microdnf install -y yum &&
yum makecache
} > /dev/null 2>&1 || true
elif command -v pacman > /dev/null; then
{
pacman -Syu &&
pacman -S --noconfirm awk
} > /dev/null 2>&1 || true
elif command -v tdnf > /dev/null; then
{
tdnf makecache &&
tdnf install -y gawk
} > /dev/null 2>&1 || true
elif command -v yum > /dev/null; then
{
yum makecache &&
yum install -y awk gawk
} > /dev/null 2>&1 || true
else
echo "Unsupport OS"
exit
fi
}
# -------------------------------------------------------------------------------- #
# Set strict mode #
# -------------------------------------------------------------------------------- #
# errexit = Any expression that exits with a non-zero exit code terminates #
# execution of the script, and the exit code of the expression becomes the exit #
# code of the script. #
# #
# pipefail = This setting prevents errors in a pipeline from being masked. If any #
# command in a pipeline fails, that return code will be used as the return code of #
# the whole pipeline. By default, the pipeline's return code is that of the last #
# command - even if it succeeds. #
# #
# noclobber = Prevents files from being overwritten when redirected (>|). #
# #
# nounset = Any reference to any variable that hasn't previously defined, with the #
# exceptions of $* and $@ is an error, and causes the program to immediately exit. #
# -------------------------------------------------------------------------------- #
function set_strict_mode()
{
set -o errexit -o noclobber -o nounset -o pipefail
IFS=$'\n\t'
}
# -------------------------------------------------------------------------------- #
# Main() #
# -------------------------------------------------------------------------------- #
# The main function where all of the heavy lifting and script config is done. #
# -------------------------------------------------------------------------------- #
function main
{
set_strict_mode
force_update_and_install_of_prereqs
#
# Ensuring case-insensitive matching works in any shell - avoid bashisms like ^^
#
if [[ -n "${DISCOVER_BY:-}" ]] && echo "${DISCOVER_BY}" | grep -qi '^OS$'; then
discover_by_operating_system
else
discover_by_package_manager
fi
}
# -------------------------------------------------------------------------------- #
# Main() #
# -------------------------------------------------------------------------------- #
# This is the actual 'script' and the functions/sub routines are called in order. #
# -------------------------------------------------------------------------------- #
main
# -------------------------------------------------------------------------------- #
# End of Script #
# -------------------------------------------------------------------------------- #
# This is the end - nothing more to see here. #
# -------------------------------------------------------------------------------- #