forked from rackerlabs/auter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auter.aptModule
121 lines (104 loc) · 5.13 KB
/
auter.aptModule
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
# This is a script that is intended to only be called by /usr/bin/auter and
# contains linux pagage manager specifc code for auter.
# This is the apt-get version of this script intended for Ubuntu/Debian
# Exit if this script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "ERROR: This script is used by auter and should not be executed directly. Exiting"
fi
function prepare_updates() {
# Run any pre-prep scripts
for SCRIPT in "${PREPREPSCRIPTDIR}"/*; do
run_script "${SCRIPT}" "Pre-Prep"
done
if [[ "${PREDOWNLOADUPDATES}" == "yes" ]]; then
if [[ $(man ${PACKAGE_MANAGER} | grep -c download-only) -gt 0 ]]; then
${PACKAGE_MANAGER} update &>/dev/null
# Check if there are any errors when checking for updates
local ERROR_COUNT=$(${PACKAGE_MANAGER} -u upgrade --assume-no ${PACKAGEMANAGEROPTIONS} | grep -c '^[WE]:')
local AVAILABLE_PACKAGE_COUNT=$(${PACKAGE_MANAGER} -u upgrade --assume-no ${PACKAGEMANAGEROPTIONS} | awk '/upgraded,.*newly installed,/ {sum=$1+$3} END {print sum}')
if [[ ${ERROR_COUNT} -eq 0 ]]; then
# If there are packages to be installed then download them.
if [[ "${AVAILABLE_PACKAGE_COUNT}" -gt 0 ]]; then
sleep $(($RANDOM % ${MAXDELAY}))
if [[ "${ONLYINSTALLFROMPREP}" == "yes" ]]; then
[[ -d "${DOWNLOADDIR}/${CONFIGSET}" ]] || mkdir -p "${DOWNLOADDIR}/${CONFIGSET}"
DOWNLOADOPTION="-o dir::cache::archives=${DOWNLOADDIR}/${CONFIGSET}"
rm -f "${DOWNLOADDIR}"/"${CONFIGSET}"/*.deb
DOWNLOADLOGMSG=" to ${DOWNLOADDIR}/${CONFIGSET}"
fi
DEBIAN_FRONTEND=noninteractive
PREPOUTPUT=$(${PACKAGE_MANAGER} ${PACKAGEMANAGEROPTIONS} ${DOWNLOADOPTION} --download-only dist-upgrade -y 2>&1)
if [[ $(echo ${PREPOUTPUT} | grep -c '^[WE]:') -gt 0 ]]; then
logit "ERROR: There were errors returned by \`${PACKAGE_MANAGER} ${PACKAGEMANAGEROPTIONS} ${DOWNLOADOPTION} --download-only dist-upgrade -y\`. Exiting."
else
logit "INFO: Updates downloaded${DOWNLOADLOGMSG}"
fi
else [[ "${AVAILABLE_PACKAGE_COUNT}" -eq 0 ]]
logit "INFO: No updates are available to be downloaded."
fi
else
logit "ERROR: There were errors returned by \`${PACKAGE_MANAGER} -u upgrade --assume-no ${PACKAGEMANAGEROPTIONS}\`. Exiting."
fi
else
logit "WARNING: downloadonly option is not available"
fi
else
PREPOUTPUT=$(${PACKAGE_MANAGER} ${PACKAGEMANAGEROPTIONS} -s dist-upgrade)
fi
[[ "${PREPOUTPUT}" ]] && echo "${PREPOUTPUT}" > ${DATADIR}/last-prep-${CONFIGSET}
# Run any post-prep scripts
for SCRIPT in "${POSTPREPSCRIPTDIR}"/*; do
run_script "${SCRIPT}" "Post-Prep"
done
}
function apply_updates() {
# Set the list of debs to be installed
if [[ "${ONLYINSTALLFROMPREP}" == "yes" ]]; then
if [[ $(ls -1 ${DOWNLOADDIR}/${CONFIGSET}/*.deb 2>/dev/null | wc -l) -gt 0 ]]; then
AVAILABLE_PACKAGE_COUNT=$(${PACKAGE_MANAGER} -u --just-print install --assume-no ${PACKAGEMANAGEROPTIONS} ${DOWNLOADDIR}/${CONFIGSET}/*.deb | awk '/upgraded,.*newly installed,/ {sum=$1+$3} END {print sum}')
DEBS="${DOWNLOADDIR}/${CONFIGSET}/*.deb"
else
AVAILABLE_PACKAGE_COUNT=0
fi
# When passing DEBs to apt-get, the update verb won't install any that aren't already
# installed (i.e. dependencies of other packages). Instead we need to use install.
UPDATEACTION="install"
else
local ERROR_COUNT=$(${PACKAGE_MANAGER} -u upgrade --assume-no ${PACKAGEMANAGEROPTIONS} | grep -c '^[WE]:')
local AVAILABLE_PACKAGE_COUNT=$(${PACKAGE_MANAGER} -u upgrade --assume-no ${PACKAGEMANAGEROPTIONS} | awk '/upgraded,.*newly installed,/ {sum=$1+$3} END {print sum}')
UPDATEACTION="upgrade"
fi
if [[ ${ERROR_COUNT} -eq 0 ]]; then
if [[ "${AVAILABLE_PACKAGE_COUNT}" -gt 0 ]]; then
for SCRIPT in "${PREAPPLYSCRIPTDIR}"/*; do
run_script "${SCRIPT}" "Pre-Apply"
done
sleep $(($RANDOM % ${MAXDELAY}))
logit "INFO: Applying updates"
local PACKAGES_BEFORE=$(dpkg --list)
# We don't want to allow the user to interrupt a yum/dnf/apt transaction or Bad Things Happen.
echo "Trying to update"
trap '' SIGINT SIGTERM
RUN_OUTPUT=$(${PACKAGE_MANAGER} ${UPDATEACTION} ${PACKAGEMANAGEROPTIONS} ${DEBS})
echo "${RUN_OUTPUT}" &>${DATADIR}/last-apply-output-${CONFIGSET}
default_signal_handling
local PACKAGES_AFTER=$(dpkg --list)
if [[ "${PACKAGES_BEFORE}" == "${PACKAGES_AFTER}" ]]; then
logit "WARNING: No updates were applied. $(echo "${RUN_OUTPUT}" | grep 'upgraded,.*installed,')"
quit 3
fi
logit "INFO: Updates complete. You may need to reboot for some updates to take effect"
log_last_run
for SCRIPT in "${POSTAPPLYSCRIPTDIR}"/*; do
run_script "${SCRIPT}" "Post-Apply"
done
[[ "${AUTOREBOOT}" == "yes" ]] && reboot_server
else
logit "INFO: No updates are available to be applied."
log_last_run
fi
else
logit "ERROR: Exit status ${RC} returned by \`${PACKAGE_MANAGER} -u upgrade --assume-no ${PACKAGEMANAGEROPTIONS}\`. Exiting."
quit 3
fi
}