-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
executable file
·81 lines (75 loc) · 2.09 KB
/
update.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
#!/bin/bash
#
# Update all the packages
readonly PATH_TO_SCRIPT="$(dirname "$(readlink "${0}")")"
# import utility functions
. "${PATH_TO_SCRIPT}"/../../utils/utils.sh
# import configuration
. "${PATH_TO_SCRIPT}"/package-management.conf
usage() {
echo 'Usage: update [OPTIONS]'
echo 'OPTIONS:'
echo ' --help Display this help message'
echo ' -h, --shutdown Shutdown the system after update'
echo ' -r, --reboot Reboot the system after update'
echo ' -f, --force Force shutdown/reboot if the update failed'
echo "Note 1: if both --shutdown and --reboot are provided, --shutdown will\
be used."
echo "Note 2: by default, the system will not be shutdown/rebooted if the\
update failed."
echo " Use --force to override this behavior."
}
#default arguments value
shutdown=false
reboot=false
force=false
# read arguments
while [[ "$#" -gt 0 ]]; do
case ${1} in
--help) usage; exit 0;;
-h|--shutdown) shutdown=true;;
-r|--reboot) reboot=true;;
-f|--force) force=true;;
*) utils::err "Unknown parameter passed: ${1}"; usage; exit 1;;
esac;
shift;
done
echo 'Update all packages'
# ask for super user
utils::ask_sudo
# error tracker
error=0
# iterate on all package managers and update
for manager in $(find "${PATH_TO_SCRIPT}" -mindepth 1 -type d); do
# only update the chosen package managers
chosen=UPDATE_$(basename "${manager}")
if [[ ${!chosen} = true ]]; then
utils::exec_cmd "$(basename "${manager}")-update" \
"Update $(basename "${manager}") packages"
((error|=$?))
else
echo "$(basename "${manager}") packages update skipped as defined in" \
"config file"
fi
done
# Done
if [[ "${error}" -eq 0 ]]; then
echo 'Update finished!'
# shutdown/reboot if needed
if [[ "${shutdown}" = true ]]; then
shutdown -h now
elif [[ "${reboot}" = true ]]; then
shutdown -r now
fi
else
utils::err 'Update incomplete.'
# shutdown/reboot if needed
if [[ "${force}" = true ]]; then
if [[ "${shutdown}" = true ]]; then
shutdown -h now
elif [[ "${reboot}" = true ]]; then
shutdown -r now
fi
fi
exit 1
fi