-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-versions.sh
executable file
·496 lines (421 loc) · 20.3 KB
/
get-versions.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
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
#!/usr/bin/env bash
# -------------------------------------------------------------------------------- #
# Description #
# -------------------------------------------------------------------------------- #
# When building docker containers it is considered best (or at least good) #
# practice to pin the packages you install to specific versions. Identifying all #
# these versions can be a long, slow and often boring process. #
# #
# This is a tool to assist in generating a list of packages and their associated #
# versions for use within a Dockerfile. #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
# Required commands #
# -------------------------------------------------------------------------------- #
# These commands MUST exist in order for the script to correctly run. #
# -------------------------------------------------------------------------------- #
PREREQ_COMMANDS=( "docker" )
# -------------------------------------------------------------------------------- #
# Flags #
# -------------------------------------------------------------------------------- #
# A set of global flags that we use for configuration. #
# -------------------------------------------------------------------------------- #
NO_HEADERS=false # Shouold we hide the header / footer?
USE_COLOURS=true # Should we use colours in our output ?
FORCE_TERMINAL=true # Force terminal type if requied
TERMINAL_TYPE=xterm # What terminal should we force?
DEFAULT_SCREEN_WIDTH=128 # Default width to use
DYNAMIC_SCREEN_WIDTH=false # Should we find the width dynamically?
# -------------------------------------------------------------------------------- #
# The wrapper function #
# -------------------------------------------------------------------------------- #
# This is where you code goes and is effectively your main() function. #
# -------------------------------------------------------------------------------- #
function wrapper()
{
draw_header
PACKAGES=$(docker run --rm -v "${GRABBER_SCRIPT}":/version-grabber --env-file="${CONFIG_FILE}" "${OSNAME}":"${TAGNAME}" "${SHELLNAME}" /version-grabber)
echo "${PACKAGES}"
draw_line
}
# -------------------------------------------------------------------------------- #
# Usage (-h parameter) #
# -------------------------------------------------------------------------------- #
# This function is used to show the user 'how' to use the script. #
# -------------------------------------------------------------------------------- #
function usage()
{
[[ -n "${*}" ]] && error " Error: ${*}"
cat <<EOF
Usage: $0 [ -hd ] [ -p ] [ -c value ] [ -g value ] [ -o value ] [ -s value ] [ -t value ]
-h | --help : Print this screen
-d | --debug : Enable debugging (set -x)
-p | --package : Package list only (No headers or other information)
-c | --config : config file name (including path)
-g | --grabber : version grabber script (including path) [Default: ~/bin/version-grabber.sh]
-o | --os : which operating system to use (docker container)
-s | --shell : which shell to use inside the container [Default: bash]
-t | --tag : which tag to use [Default: latest]
EOF
clean_exit 1;
}
# -------------------------------------------------------------------------------- #
# Test Getopt #
# -------------------------------------------------------------------------------- #
# Test to ensure we have the GNU getopt available. #
# -------------------------------------------------------------------------------- #
function test_getopt
{
if getopt --test > /dev/null && true; then
error "'getopt --test' failed in this environment - Please ensure you are using the gnu getopt."
if [[ "$(uname -s)" == "Darwin" ]]; then
error "You are using MAcOS - please ensure you have installed gnu-getopt and updated your path."
fi
exit 1
fi
}
# -------------------------------------------------------------------------------- #
# Process Arguments #
# -------------------------------------------------------------------------------- #
# This function will process the input from the command line and work out what it #
# is that the user wants to see. #
# #
# This is the main processing function where all the processing logic is handled. #
# -------------------------------------------------------------------------------- #
function process_arguments()
{
local options
local longopts
local error_msg
if [[ $# -eq 0 ]]; then
usage
fi
test_getopt
options=hdpc:g:o:s:t:
longopts=help,debug,package,config:,grabber:,os:,shell:,tag:
if ! PARSED=$(getopt --options=$options --longoptions=$longopts --name "$0" -- "$@" 2>&1) && true; then
error_msg=$(echo -e "${PARSED}" | head -n 1 | awk -F ':' '{print $2}')
usage "${error_msg}"
fi
eval set -- "${PARSED}"
while true; do
case "${1}" in
-h|--help)
usage
;;
-d|--debug)
set -x
shift
;;
-p|--package)
NO_HEADERS=true
shift
;;
-c|--config)
CONFIG_FILE=$(realpath "${2}")
if [[ ! -r "${CONFIG_FILE}" ]]; then
error "Cannot read config file: ${CONFIG_FILE}"
fi
shift 2
;;
-g|--grabber)
GRABBER_SCRIPT=$(realpath "${2}")
if [[ ! -r "${GRABBER_SCRIPT}" ]]; then
error "Cannot read grabber script: ${GRABBER_SCRIPT}"
fi
shift 2
;;
-o|--os)
OSNAME=${2}
shift 2
;;
-s|--shell)
SHELLNAME=${2}
shift 2
;;
-t|--tag)
TAGNAME=${2}
shift 2
;;
--)
shift
break
;;
esac
done
[[ -z "${CONFIG_FILE}" ]] && usage
[[ -z "${OSNAME}" ]] && usage
if [[ -z "${GRABBER_SCRIPT}" ]]; then
GRABBER_SCRIPT="$(realpath ~/bin/version-grabber.sh)"
fi
SHELLNAME="${SHELLNAME:-bash}"
TAGNAME="${TAGNAME:-latest}"
wrapper
clean_exit
}
# -------------------------------------------------------------------------------- #
# STOP HERE! #
# -------------------------------------------------------------------------------- #
# The functions below are part of the template and should not require any changes #
# in order to make use of this template. If you are going to edit code beyound #
# this point please ensure you fully understand the impact of those changes! #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
# Utiltity Functions #
# -------------------------------------------------------------------------------- #
# The following functions are all utility functions used within the script but are #
# not specific to the display of the colours and only serve to handle things like, #
# signal handling, user interface and command line option processing. #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
# Init Colours #
# -------------------------------------------------------------------------------- #
# This function will check to see if we are able to support colours and how many #
# we are able to support. #
# #
# The script will give and error and exit if there is no colour support or there #
# are less than 8 supported colours. #
# #
# Variables intentionally not defined 'local' as we want them to be global. #
# -------------------------------------------------------------------------------- #
function init_colours()
{
local ncolors
fgRed=''
fgGreen=''
fgYellow=''
fgCyan=''
bold=''
reset=''
screen_width=${DEFAULT_SCREEN_WIDTH}
if [[ "${USE_COLOURS}" = false ]]; then
return
fi
if ! test -t 1; then
if [[ "${FORCE_TERMINAL}" = true ]]; then
export TERM=${TERMINAL_TYPE}
else
return
fi
fi
if ! tput longname > /dev/null 2>&1; then
return
fi
ncolors=$(tput colors)
if ! test -n "${ncolors}" || test "${ncolors}" -le 7; then
return
fi
fgRed=$(tput setaf 1)
fgGreen=$(tput setaf 2)
fgYellow=$(tput setaf 3)
fgCyan=$(tput setaf 6)
bold=$(tput bold)
reset=$(tput sgr0)
if [[ "${DYNAMIC_SCREEN_WIDTH}" = true ]]; then
screen_width=$(tput cols)
fi
}
# -------------------------------------------------------------------------------- #
# Error #
# -------------------------------------------------------------------------------- #
# A simple wrapper function to show something was an error. #
# -------------------------------------------------------------------------------- #
function error()
{
notify 'error' "${@}"
}
# -------------------------------------------------------------------------------- #
# Warning #
# -------------------------------------------------------------------------------- #
# A simple wrapper function to show something was a warning. #
# -------------------------------------------------------------------------------- #
function warn()
{
notify 'warning' "${@}"
}
# -------------------------------------------------------------------------------- #
# Success #
# -------------------------------------------------------------------------------- #
# A simple wrapper function to show something was a success. #
# -------------------------------------------------------------------------------- #
function success()
{
notify 'success' "${@}"
}
# -------------------------------------------------------------------------------- #
# Info #
# -------------------------------------------------------------------------------- #
# A simple wrapper function to show something is information. #
# -------------------------------------------------------------------------------- #
function info()
{
notify 'info' "${@}"
}
# -------------------------------------------------------------------------------- #
# Notify #
# -------------------------------------------------------------------------------- #
# Handle all types of notification in one place. #
# -------------------------------------------------------------------------------- #
function notify()
{
local type="${1:-}"
shift
local message="${*:-}"
local fgColor
if [[ -n $message ]]; then
case "${type}" in
error)
fgColor="${fgRed}";
;;
warning)
fgColor="${fgYellow}";
;;
success)
fgColor="${fgGreen}";
;;
info)
fgColor="${fgCyan}";
;;
*)
fgColor='';
;;
esac
printf '%s%b%s\n' "${fgColor}${bold}" "${message}" "${reset}" 1>&2
fi
}
# -------------------------------------------------------------------------------- #
# Draw Header #
# -------------------------------------------------------------------------------- #
# Draw a nice header if -p has not been passed. #
# -------------------------------------------------------------------------------- #
function draw_header
{
if [[ "${NO_HEADERS}" = false ]]; then
local config_string_raw config_string
config_string_raw="Config File: $(basename "${CONFIG_FILE}") Grabber Script: $(basename "${GRABBER_SCRIPT}") Docker Container: ${OSNAME}:${TAGNAME} Shell: ${SHELLNAME}"
config_string="${fgGreen}Config File:${reset} $(basename "${CONFIG_FILE}") ${fgGreen}Grabber Script:${reset} $(basename "${GRABBER_SCRIPT}") ${fgGreen}Docker Container:${reset} ${OSNAME}:${TAGNAME} ${fgGreen}Shell:${reset} ${SHELLNAME}"
draw_line
center_text "Package version grabber by Wolf Software Limited"
draw_line
center_text "${config_string}" "${#config_string_raw}"
draw_line
fi
}
# -------------------------------------------------------------------------------- #
# abs #
# -------------------------------------------------------------------------------- #
# Return the absolute value for a given number. #
# -------------------------------------------------------------------------------- #
function abs()
{
(( $1 < 0 )) && echo "$(( $1 * -1 ))" || echo "$1"
}
# -------------------------------------------------------------------------------- #
# Center Text #
# -------------------------------------------------------------------------------- #
# A simple wrapper function to some text centered on the screen. #
# -------------------------------------------------------------------------------- #
function center_text()
{
if [[ -n ${2:-} ]]; then
textsize=${2}
extra=$(abs "$(( textsize - ${#1} ))")
else
textsize=${#1}
extra=0
fi
span=$(( ( (screen_width + textsize) / 2) + extra ))
printf '%*s\n' "${span}" "$1"
}
# -------------------------------------------------------------------------------- #
# Draw Line #
# -------------------------------------------------------------------------------- #
# A simple wrapper function to draw a line on the screen. #
# -------------------------------------------------------------------------------- #
function draw_line()
{
if [[ "${NO_HEADERS}" = false ]]; then
local start=$'\e(0' end=$'\e(B' line='qqqqqqqqqqqqqqqq'
while ((${#line} < screen_width));
do
line+="$line";
done
printf '%s%s%s\n' "$start" "${line:0:screen_width}" "$end"
fi
}
# -------------------------------------------------------------------------------- #
# Check Prerequisites #
# -------------------------------------------------------------------------------- #
# Check to ensure that the prerequisite commmands exist. #
# -------------------------------------------------------------------------------- #
function check_prereqs()
{
local error_count=0
for i in "${PREREQ_COMMANDS[@]}"
do
command=$(command -v "${i}" || true)
if [[ -z $command ]]; then
warning "$i is not in your command path"
error_count=$((error_count+1))
fi
done
if [[ $error_count -gt 0 ]]; then
error "$error_count errors located - fix before re-running";
clean_exit 1;
fi
}
# -------------------------------------------------------------------------------- #
# Clean Exit #
# -------------------------------------------------------------------------------- #
# Unset the traps and exit cleanly, with an optional exit code / message. #
# -------------------------------------------------------------------------------- #
function clean_exit()
{
[[ -n ${2:-} ]] && error "${2}"
exit "${1:-0}"
}
# -------------------------------------------------------------------------------- #
# Enable 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
init_colours
check_prereqs
process_arguments "${@}"
}
# -------------------------------------------------------------------------------- #
# 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. #
# -------------------------------------------------------------------------------- #