-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller.bash
542 lines (449 loc) · 13.6 KB
/
installer.bash
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#!/bin/bash/env bash
#
# Filename: installer.bash
# Description: Manages libvirt-hooks binaries, scripts and services.
# Author(s): Alex Portell <codeberg.org/portellam> <github.com/portellam>
# Maintainer(s): Alex Portell <codeberg.org/portellam> <github.com/portellam>
# Version: 1.0.0
#
# <traps>
trap 'catch_error' SIGINT SIGTERM ERR
trap 'catch_exit' EXIT
# </traps>
# <params>
declare -r SCRIPT_VERSION="1.0.0"
declare -r REPO_NAME="libvirt-hooks"
declare -r WORKING_DIR="$( dirname $( realpath "${0}" ) )/"
declare -r OPTION="${1}"
SAVEIFS="${IFS}"
IFS=$'\n'
# <summary>Execution Flags</summary>
DO_INSTALL=false
DO_UNINSTALL=false
# <summary>
# Color coding
# Reference URL: 'https://www.shellhacks.com/bash-colors'
# </summary>
SET_COLOR_GREEN='\033[0;32m'
SET_COLOR_RED='\033[0;31m'
SET_COLOR_YELLOW='\033[0;33m'
RESET_COLOR='\033[0m'
# <summary>Append output</summary>
PREFIX_PROMPT="$( basename "${0}" ): "
PREFIX_ERROR="${PREFIX_PROMPT}${SET_COLOR_YELLOW}An error occurred:${RESET_COLOR} "
PREFIX_FAIL="${PREFIX_PROMPT}${SET_COLOR_RED}Failure:${RESET_COLOR} "
PREFIX_PASS="${PREFIX_PROMPT}${SET_COLOR_GREEN}Success:${RESET_COLOR} "
declare -r LIBVIRTD_SERVICE="libvirtd"
declare -r BIN_DEST_PATH="/usr/local/bin/libvirt-hooks/"
declare -r BIN_SOURCE_PATH="${WORKING_DIR}bin/"
declare -r SCRIPT_DEST_PATH="/etc/libvirt/hooks/"
declare -r SCRIPT_SOURCE_RELATIVE_PATH="hooks/"
declare -r SERVICE_DEST_PATH="/etc/systemd/system/"
declare -r SERVICE_SOURCE_PATH="${WORKING_DIR}systemd/"
declare -a BIN_LIST=( )
declare -a SCRIPT_LIST=( )
declare -a SCRIPT_SUBDIR_LIST=( )
declare -a SERVICE_LIST=( )
DO_INSTALL_AUDIO_LOOPBACK=false
AUDIO_LOOPBACK_HOOK_NAME="audio-loopback"
# </params>
# <functions>
function main
{
is_user_superuser || exit 1
add_to_lists &> /dev/null
get_option || exit 1
prompt_install || exit 1
is_pulseaudio_installed
do_install_audio_loopback
if ! "${DO_INSTALL}"; then
uninstall || exit 1
else
are_dependencies_installed || exit 1
install || exit 1
fi
update_services
exit "${?}"
}
function add_to_lists
{
BIN_LIST=( $( find -L "${BIN_SOURCE_PATH}" -maxdepth 1 -type f ) )
SCRIPT_LIST=( $( find -L "${SCRIPT_SOURCE_RELATIVE_PATH}" -type f ) )
SCRIPT_SUBDIR_LIST=( $( find -L "${SCRIPT_SOURCE_RELATIVE_PATH}" -type d ) )
unset SCRIPT_SUBDIR_LIST[0]
readonly SCRIPT_SUBDIR_LIST
SERVICE_LIST=( $( find -L "${SERVICE_SOURCE_PATH}" -maxdepth 1 -type f ) )
}
# <summary>Business logic</summary>
function prompt_install
{
if "${DO_INSTALL}" ||
"${DO_UNINSTALL}"; then
return 0
fi
yes_no_prompt "Install '${REPO_NAME}'?"
case "${?}" in
0 )
DO_INSTALL=true ;;
1 )
return 1 ;;
255 )
DO_UNINSTALL=true ;;
esac
}
function install
{
if ! do_source_files_exist \
|| ! does_destination_path_exist \
|| ! copy_source_files_to_destination \
|| ! set_permissions_for_destination_files; then
print_fail_to_log"Could not install ${REPO_NAME}."
return 1
fi
print_pass_to_log "Installed ${REPO_NAME}."
}
function uninstall
{
if ! delete_destination_files; then
print_fail_to_log"Could not uninstall ${REPO_NAME}."
return 1
fi
print_pass_to_log "Uninstalled ${REPO_NAME}."
}
# <summary>Clean-up</summary>
function reset_ifs
{
IFS="${SAVEIFS}"
}
# <summary>Data-type validation</summary>
function is_string
{
if [[ "${1}" == "" ]]; then
return 1
fi
}
# <summary>Handlers</summary>
function catch_error {
exit 1
}
function catch_exit {
reset_ifs
}
function is_user_superuser
{
if [[ $( whoami ) != "root" ]]; then
print_to_error_log "User is not sudo or root."
return 1
fi
}
function yes_no_prompt
{
local output="${1}"
is_string "${output}" && output+=" "
for counter in $( seq 0 2 ); do
echo -en "${output}[Y/n]: "
read -r -p "" answer
case "${answer}" in
[Yy]* )
return 0 ;;
[Nn]* )
return 255 ;;
* )
echo "Please answer 'Y' or 'N'." ;;
esac
done
return 1
}
# <summary>Loggers</summary>
function print_error_to_log
{
echo -e "${PREFIX_ERROR}${1}" >&2
}
function print_fail_to_log
{
echo -e "${PREFIX_FAIL}${1}" >&2
}
function print_output_to_log
{
echo -e "${PREFIX_PROMPT}${1}" >&1
}
function print_pass_to_log
{
echo -e "${PREFIX_PASS}${1}" >&1
}
function print_usage
{
IFS=$'\n'
local -a output=(
"Usage:\tbash libvirt-hooks [OPTION]"
"Manages ${REPO_NAME} binaries, scripts, and services."
"Version ${SCRIPT_VERSION}.\n"
" -h, --help\t\tPrint this help and exit."
" -i, --install\t\tInstall ${REPO_NAME} to system."
" -u, --uninstall\tUninstall ${REPO_NAME} from system."
)
echo -e "${output[*]}"
unset IFS
}
# <summary>Options logic</summary>
function get_option
{
case "${OPTION}" in
"-u" | "--uninstall" )
DO_UNINSTALL=true ;;
"-i" | "--install" )
DO_INSTALL=true ;;
"" )
return 0 ;;
"-h" | "--help" | * )
print_usage
return 1 ;;
esac
}
# <summary>Copy Source Files to Destination</summary>
function copy_source_files_to_destination
{
copy_binary_files_to_destination || return 1
copy_script_files_to_destination || return 1
copy_service_files_to_destination
}
function copy_binary_files_to_destination
{
for bin in "${BIN_LIST[@]}"; do
local bin_name="$( basename "${bin}" )"
local bin_path="${BIN_DEST_PATH}${bin_name}"
if ! sudo cp --force "${bin}" "${bin_path}" &> /dev/null; then
print_error_to_log "Failed to copy project binaries."
return 1
fi
done
}
function copy_script_files_to_destination
{
for script in "${SCRIPT_LIST[@]}"; do
local script_source_file="${WORKING_DIR}${script}"
local script_dest_file="${SCRIPT_DEST_PATH}${script:6}"
local script_dest_dir="$( dirname "${script_dest_file}" )/"
if ! does_path_exist "${script_dest_dir}" \
|| ! sudo rsync --archive --recursive --verbose "${script_source_file}" "${script_dest_dir}" &> /dev/null; then
print_error_to_log "Failed to copy project script(s)."
return 1
fi
done
}
function copy_service_files_to_destination
{
for service in "${SERVICE_LIST[@]}"; do
local service_name="$( basename "${service}" )"
local service_path="${SERVICE_DEST_PATH}/${service_name}"
if ! sudo cp --force "${service}" "${service_path}" &> /dev/null; then
print_error_to_log "Failed to copy project service(s)."
return 1
fi
done
}
# <summary>Delete Destination Files</summary>
function delete_destination_files
{
delete_binary_files || return 1
delete_script_files || return 1
delete_service_files
}
function delete_binary_files
{
if [[ ! -d "${BIN_DEST_PATH}" ]]; then
return 0
fi
if ! rm --force --recursive "${BIN_DEST_PATH}" &> /dev/null; then
print_error_to_log "Failed to delete project binaries."
return 1
fi
}
function delete_script_files
{
if [[ ! -d "${SCRIPT_DEST_PATH}" ]]; then
return 0
fi
if ! rm --force --recursive ${SCRIPT_DEST_PATH}* &> /dev/null; then
print_error_to_log "Failed to delete project script(s)."
return 1
fi
}
function delete_service_files
{
if [[ ! -d "${SERVICE_DEST_PATH}" ]]; then
return 0
fi
for service in "${SERVICE_LIST[@]}"; do
local service_name="$( basename "${service}" )"
local service_path="${SERVICE_DEST_PATH}${service_name}"
if ! rm --force "${service_path}" &> /dev/null; then
print_error_to_log "Failed to delete project service(s)."
return 1
fi
done
}
function do_source_files_exist
{
do_binary_files_exist || return 1
do_script_files_exist || return 1
do_service_files_exist
}
function do_binary_files_exist
{
for bin in "${BIN_LIST[@]}"; do
if [[ ! -e "${bin}" ]]; then
print_error_to_log "Missing project binaries."
return 1
fi
done
}
function do_script_files_exist
{
for script in "${SCRIPT_LIST[@]}"; do
if [[ ! -e "${script}" ]]; then
print_error_to_log "Missing project scripts."
return 1
fi
done
}
function do_service_files_exist
{
for service in "${SERVICE_LIST[@]}"; do
if [[ ! -e "${service}" ]]; then
print_error_to_log "Missing project services."
return 1
fi
done
}
# <summary>Dependency validation</summary>
function are_dependencies_installed
{
local -r systemd_app="systemd"
if ! command -v "${systemd_app}" &> /dev/null; then
print_error_to_log "Required dependency '${systemd_app}' is not installed."
return 1
fi
local -r output="$( systemctl status "${LIBVIRTD_SERVICE}" )"
if [[ "${output}" == "Unit ${LIBVIRTD_SERVICE}.service could not be found." ]]; then
print_error_to_log "Required service '${LIBVIRTD_SERVICE}' is not installed."
return 1
fi
print_pass_to_log "Dependencies are installed."
}
function do_install_audio_loopback
{
for key in "${!SCRIPT_LIST[@]}"; do
local script="${SCRIPT_LIST["${key}"]}"
if ! "${DO_INSTALL_AUDIO_LOOPBACK}" \
&& is_file_for_pulseaudio "${script}"; then
unset SCRIPT_LIST["${key}"]
fi
done
for key in "${!SERVICE_LIST[@]}"; do
local service="${SERVICE_LIST["${key}"]}"
local service_name="$( basename "${service}" )"
if ! "${DO_INSTALL_AUDIO_LOOPBACK}" \
&& is_file_for_pulseaudio "${service_name}"; then
unset SERVICE_LIST["${key}"]
fi
done
}
function is_pulseaudio_installed
{
if command -v "pulseaudio" &> /dev/null \
&& command -v "pactl" &> /dev/null; then
DO_INSTALL_AUDIO_LOOPBACK=true
fi
}
function is_file_for_pulseaudio
{
case "${1}" in
*"${AUDIO_LOOPBACK_HOOK_NAME}"* )
return 0 ;;
esac
return 1
}
# <summary>Do Destination Paths Exist</summary>
function does_destination_path_exist
{
does_path_exist "${BIN_DEST_PATH}" || return 1
does_script_path_exist || return 1
does_path_exist "${SERVICE_DEST_PATH}" || return 1
}
function does_path_exist
{
local -r path="${1}"
if [[ ! -d "${path}" ]] \
&& ! sudo mkdir --parents "${path}" &> /dev/null; then
print_error_to_log "Could not create directory '${path}'."
return 1
fi
}
function does_script_path_exist
{
does_path_exist "${SCRIPT_DEST_PATH}" || return 1
for script_subdir in "${SCRIPT_SUBDIR_LIST[@]}"; do
script_subdir="${script_subdir:6}"
script_subdir="${SCRIPT_DEST_PATH}${script_subdir}"
does_path_exist "${script_subdir}" || return 1
done
}
# <summary>Services logic</summary>
function update_services
{
if ! systemctl daemon-reload &> /dev/null; then
print_error_to_log "Could not update services."
return 1
fi
if ! systemctl enable "${LIBVIRTD_SERVICE}" &> /dev/null; then
print_error_to_log "Could not enable ${LIBVIRTD_SERVICE}."
return 1
fi
if ! systemctl restart "${LIBVIRTD_SERVICE}" &> /dev/null; then
print_error_to_log "Could not start ${LIBVIRTD_SERVICE}."
return 1
fi
print_pass_to_log "Updated services."
}
# <summary>Set Permissions For Destination Files</summary>
function set_permissions_for_destination_files
{
set_permissions_for_binary_files || return 1
set_permissions_for_script_files || return 1
set_permissions_for_service_files
}
function set_permissions_for_binary_files
{
if ! sudo chown --recursive --silent root:root "${BIN_DEST_PATH}" \
|| ! sudo chmod --recursive --silent +x "${BIN_DEST_PATH}"; then
print_error_to_log "Failed to set file permissions for binaries."
return 1
fi
}
function set_permissions_for_script_files
{
if ! sudo chown --recursive --silent root:root "${SCRIPT_DEST_PATH}" \
|| ! sudo chmod --recursive --silent +x "${SCRIPT_DEST_PATH}"; then
print_error_to_log "Failed to set file permissions for script(s)."
return 1
fi
}
function set_permissions_for_service_files
{
if ! sudo chown --recursive --silent root:root "${SERVICE_DEST_PATH}"; then
print_error_to_log "Failed to set file permissions for service(s)."
return 1
fi
for service in "${SERVICE_LIST[@]}"; do
local this_service_path="${SERVICE_DEST_PATH}$( basename "${service}" )"
if ! sudo chmod --recursive --silent +x "${this_service_path}"; then
print_error_to_log "Failed to set file permissions for service '${service}'."
return 1
fi
done
}
# </functions>
# <code>
main
# </code>