forked from charmed-kubernetes/jenkins
/
utilities.sh
152 lines (141 loc) · 3.69 KB
/
utilities.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
# Define some common utility functions that are used by other scripts here.
# The maximum amount of seconds to wait in a loop.
MAXIMUM_WAIT_SECONDS=3600
# Download requires the url and the output directory or name.
function download() {
local url=$1
local destination=$2
echo "Downloading ${url}"
# -f fail silently
# -L Follow location redirects
# --retry three times
# -o write output to a file
curl -f -L --retry 3 ${url} -o ${destination}
# Print out the size and sha256 hash sum of the archive.
echo "$(ls -hl ${destination} | cut -d ' ' -f 5) $(basename ${destination})"
echo "$(sha256sum_file ${destination}) $(basename ${destination})"
}
# The check_time function requires two parameters start_time and max_seconds.
function check_time() {
local start_time=$1
local maximum_seconds=$2
local current_time=`date +"%s"`
local difference=$(expr ${current_time} - ${start_time})
# When the difference is greater than maximum seconds, exit this script.
if [ ${difference} -gt ${maximum_seconds} ]; then
echo "The process is taking more than ${maximum_seconds} seconds!"
# End this script because too much time has passed.
exit 3
fi
}
# Run a command in a loop waiting for specific output use MAXIMUM_WAIT_SECONDS.
function run_and_wait() {
local cmd=$1
local match=$2
local sleep_seconds=${3:-5}
local start_time=`date +"%s"`
# Run the command in a loop looking for output.
until $(${cmd} | grep -q "${match}"); do
# Check the time so this does not loop forever.
check_time ${start_time} ${MAXIMUM_WAIT_SECONDS}
sleep ${sleep_seconds}
done
}
# A platform neutral way to get the md5 hash sum of a file.
function md5sum_file() {
if which md5sum > /dev/null 2>&1; then
md5sum "$1" | awk '{ print $1 }'
else
md5 -q "$1"
fi
}
# A platform neutral way to get the sha1 hash sum of a file.
function sha1sum_file() {
if which sha1sum > /dev/null 2>&1; then
sha1sum "$1" | awk '{ print $1 }'
else
shasum -a1 "$1" | awk '{ print $1 }'
fi
}
# A platform neutral way to get the sha256 hash sum of a file.
function sha256sum_file() {
if which sha256sum > /dev/null 2>&1; then
sha256sum "$1" | awk '{ print $1 }'
else
shasum -a256 "$1" | awk '{ print $1 }'
fi
}
# Create an archive and print the hash values for that file.
function create_archive() {
local directory=$1
shift
local archive=$1
shift
# The rest of the arguments are for the tar command.
local files="$@"
# Change to the target directory.
cd ${directory}
tar -cvzf ${archive} ${files}
echo "$(ls -hl ${archive} | cut -d ' ' -f 5) $(basename ${archive})"
echo "$(md5sum_file ${archive}) $(basename ${archive})"
echo "$(sha1sum_file ${archive}) $(basename ${archive})"
echo "$(sha256sum_file ${archive}) $(basename ${archive})"
cd -
}
# Return the operating system of the host system.
function get_os() {
local os
# Get the kernel name.
case "$(uname -s)" in
Darwin)
os=darwin
;;
Linux)
os=linux
;;
*)
echo "Unsupported OS. Must be Linux or Mac OS X." >&2
exit 1
;;
esac
echo ${os}
}
# Return the architecture of the host system.
function get_arch() {
local arch
# Get the machine hardware name.
case "$(uname -m)" in
x86_64*)
arch=amd64
;;
i?86_64*)
arch=amd64
;;
amd64*)
arch=amd64
;;
aarch64*)
arch=arm64
;;
arm64*)
arch=arm64
;;
arm*)
arch=arm
;;
i?86*)
arch=x86
;;
s390x*)
arch=s390x
;;
ppc64le*)
arch=ppc64le
;;
*)
echo "Unsupported arch. Must be x86_64, 386, arm, arm64, s390x or ppc64le." >&2
exit 1
;;
esac
echo "${arch}"
}