forked from MiczFlor/RPi-Jukebox-RFID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_helpers.sh
347 lines (290 loc) · 10.7 KB
/
02_helpers.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
#!/usr/bin/env bash
### Helpers
show_slow_hardware_message() {
if [[ $(uname -m) == "armv6l" ]]; then
print_c "--------------------------------------------------------------------
| Your hardware is a little slower so this will take a while. |
| Go watch a movie but don't let your computer go to sleep for the |
| SSH connection to remain intact. |
--------------------------------------------------------------------
"
fi
}
# Get key by item number of associated array
get_key_by_item_number() {
local -n array="$1"
local item_number="$2"
local count=0
for key in "${!array[@]}"; do
((count++))
if [ "$count" -eq "$item_number" ]; then
echo "$key"
return
fi
done
}
# $1->start, $2->end
calc_runtime_and_print() {
runtime=$(($2-$1))
((h=${runtime}/3600))
((m=(${runtime}%3600)/60))
((s=${runtime}%60))
echo "Done in ${h}h ${m}m ${s}s"
}
run_with_timer() {
local time_start=$(date +%s);
$1; # Executes the function passed as an argument
run_and_print_lc calc_runtime_and_print time_start $(date +%s)
}
run_with_log_frame() {
local time_start=$(date +%s);
local description="$2"
log "\n\n"
log "#########################################################"
print_lc "${description}"
$1; # Executes the function passed as an argument
local done_in=$(calc_runtime_and_print time_start $(date +%s))
log "\n${done_in} - ${description}"
log "#########################################################"
}
get_architecture() {
local arch=""
if [ "$(uname -m)" = "armv7l" ]; then
arch="armv7"
elif [ "$(uname -m)" = "armv6l" ]; then
arch="armv6"
elif [ "$(uname -m)" = "aarch64" ]; then
arch="arm64"
else
arch="$(uname -m)"
fi
echo $arch
}
is_raspbian() {
if [[ $( . /etc/os-release; printf '%s\n' "$ID"; ) == *"raspbian"* ]]; then
echo true
else
echo false
fi
}
get_debian_version_number() {
source /etc/os-release
echo "$VERSION_ID"
}
get_boot_config_path() {
if [ "$(is_raspbian)" = true ]; then
local debian_version_number=$(get_debian_version_number)
# Bullseye and lower
if [ "$debian_version_number" -le 11 ]; then
echo "/boot/config.txt"
# Bookworm and higher
elif [ "$debian_version_number" -ge 12 ]; then
echo "/boot/firmware/config.txt"
else
echo "unknown"
fi
else
echo "unknown"
fi
}
validate_url() {
local url=$1
wget --spider ${url} >/dev/null 2>&1
return $?
}
download_from_url() {
local url=$1
local output_filename=$2
wget --quiet ${url} -O ${output_filename} || exit_on_error "Download failed"
return $?
}
### Verify helpers
print_verify_installation() {
log "\n
-------------------------------------------------------
Check installation
"
}
# Check if the file(s) exists
verify_files_exists() {
local files="$@"
log " Verify '${files}' exists"
if [[ -z "${files}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
for file in $files
do
test ! -f ${file} && exit_on_error "ERROR: '${file}' does not exists or is not a file!"
done
log " CHECK"
}
# Check if the dir(s) exists
verify_dirs_exists() {
local dirs="$@"
log " Verify '${dirs}' exists"
if [[ -z "${dirs}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
for dir in $dirs
do
test ! -d ${dir} && exit_on_error "ERROR: '${dir}' does not exists or is not a dir!"
done
log " CHECK"
}
# Check if the file(s) has/have the expected owner and modifications
verify_files_chmod_chown() {
local mod_expected=$1
local user_expected=$2
local group_expected=$3
local files="${@:4}"
log " Verify '${mod_expected}' '${user_expected}:${group_expected}' is set for '${files}'"
if [[ -z "${mod_expected}" || -z "${user_expected}" || -z "${group_expected}" || -z "${files}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
for file in $files
do
test ! -f ${file} && exit_on_error "ERROR: '${file}' does not exists or is not a file!"
mod_actual=$(stat --format '%a' "${file}")
user_actual=$(stat -c '%U' "${file}")
group_actual=$(stat -c '%G' "${file}")
test ! "${mod_expected}" -eq "${mod_actual}" && exit_on_error "ERROR: '${file}' actual mod '${mod_actual}' differs from expected '${mod_expected}'!"
test ! "${user_expected}" == "${user_actual}" && exit_on_error "ERROR: '${file}' actual owner '${user_actual}' differs from expected '${user_expected}'!"
test ! "${group_expected}" == "${group_actual}" && exit_on_error "ERROR: '${file}' actual group '${group_actual}' differs from expected '${group_expected}'!"
done
log " CHECK"
}
# Check if the dir(s) has/have the expected owner and modifications
verify_dirs_chmod_chown() {
local mod_expected=$1
local user_expected=$2
local group_expected=$3
local dirs="${@:4}"
log " Verify '${mod_expected}' '${user_expected}:${group_expected}' is set for '${dirs}'"
if [[ -z "${mod_expected}" || -z "${user_expected}" || -z "${group_expected}" || -z "${dirs}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
for dir in $dirs
do
test ! -d ${dir} && exit_on_error "ERROR: '${dir}' does not exists or is not a dir!"
mod_actual=$(stat --format '%a' "${dir}")
user_actual=$(stat -c '%U' "${dir}")
group_actual=$(stat -c '%G' "${dir}")
test ! "${mod_expected}" -eq "${mod_actual}" && exit_on_error "ERROR: '${dir}' actual mod '${mod_actual}' differs from expected '${mod_expected}'!"
test ! "${user_expected}" == "${user_actual}" && exit_on_error "ERROR: '${dir}' actual owner '${user_actual}' differs from expected '${user_expected}'!"
test ! "${group_expected}" == "${group_actual}" && exit_on_error "ERROR: '${dir}' actual group '${group_actual}' differs from expected '${group_expected}'!"
done
log " CHECK"
}
verify_file_contains_string() {
local string="$1"
local file="$2"
log " Verify '${string}' found in '${file}'"
if [[ -z "${string}" || -z "${file}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
if [[ ! $(grep -iw "${string}" "${file}") ]]; then
exit_on_error "ERROR: '${string}' not found in '${file}'"
fi
log " CHECK"
}
verify_file_contains_string_once() {
local string="$1"
local file="$2"
log " Verify '${string}' found in '${file}'"
if [[ -z "${string}" || -z "${file}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
local file_contains_string_count=$(grep -oiw "${string}" "${file}" | wc -l)
if [ "$file_contains_string_count" -lt 1 ]; then
exit_on_error "ERROR: '${string}' not found in '${file}'"
elif [ "$file_contains_string_count" -gt 1 ]; then
exit_on_error "ERROR: '${string}' found more than once in '${file}'"
fi
log " CHECK"
}
verify_service_state() {
local service="$1"
local desired_state="$2"
local option="${3:+$3 }" # optional, dont't quote in next call!
log " Verify service '${option}${service}' is '${desired_state}'"
if [[ -z "${service}" || -z "${desired_state}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
local actual_state=$(systemctl is-active ${option}${service})
if [[ ! "${actual_state}" == "${desired_state}" ]]; then
exit_on_error "ERROR: service '${option}${service}' is not '${desired_state}' (state: '${actual_state}')."
fi
log " CHECK"
}
verify_service_enablement() {
local service="$1"
local desired_enablement="$2"
local option="${3:+$3 }" # optional, dont't quote in next call!
log " Verify service ${option}${service} is ${desired_enablement}"
if [[ -z "${service}" || -z "${desired_enablement}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
local actual_enablement=$(systemctl is-enabled ${option}${service})
if [[ ! "${actual_enablement}" == "${desired_enablement}" ]]; then
exit_on_error "ERROR: service ${option}${service} is not ${desired_enablement} (state: ${actual_enablement})."
fi
log " CHECK"
}
verify_optional_service_enablement() {
local service="$1"
local desired_enablement="$2"
local option="${3:+$3 }" # optional, dont't quote in next call!
log " Verify service ${option}${service} is ${desired_enablement}"
if [[ -z "${service}" || -z "${desired_enablement}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
local actual_enablement=$(systemctl is-enabled ${option}${service}) 2>/dev/null
if [[ -z "${actual_enablement}" ]]; then
log " INFO: optional service ${option}${service} is not installed."
elif [[ "${actual_enablement}" == "static" ]]; then
log " INFO: optional service ${option}${service} is set static."
elif [[ ! "${actual_enablement}" == "${desired_enablement}" ]]; then
exit_on_error "ERROR: service ${option}${service} is not ${desired_enablement} (state: ${actual_enablement})."
fi
log " CHECK"
}
# Reads a textfile and returns all lines as args.
# Does filter out comments, egg-prefixes and version suffixes
# Arguments:
# 1 : textfile to read
get_args_from_file() {
local package_file="$1"
sed 's/.*#egg=//g' ${package_file} | sed -E 's/(#|=|>|<).*//g' | xargs echo
}
# Check if all passed packages are installed. Fail on first missing.
verify_apt_packages() {
local packages="$@"
log " Verify packages are installed: '${packages}'"
if [[ -z "${packages}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
local apt_list_installed=$(apt -qq list --installed 2>/dev/null)
for package in ${packages}
do
if [[ ! $(echo "${apt_list_installed}" | grep -i "^${package}/.*installed") ]]; then
exit_on_error "ERROR: ${package} is not installed"
fi
done
log " CHECK"
}
# Check if all passed modules are installed. Fail on first missing.
verify_pip_modules() {
local modules="$@"
log " Verify modules are installed: '${modules}'"
if [[ -z "${modules}" ]]; then
exit_on_error "ERROR: at least one parameter value is missing!"
fi
local pip_list_installed=$(pip list 2>/dev/null)
for module in ${modules}
do
if [[ ! $(echo "${pip_list_installed}" | grep -i "^${module} ") ]]; then
exit_on_error "ERROR: ${module} is not installed"
fi
done
log " CHECK"
}