forked from rackerlabs/auter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auter.aptModule
175 lines (149 loc) · 7.47 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
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
#!/bin/bash
# This is a script that is intended to only be called by /usr/bin/auter and
# contains linux package manager specific 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 check_package_manager_lock() {
# Set default values if the variables are undefined
[[ $PACKAGEMANAGERLOCKRETRIES ]] || PACKAGEMANAGERLOCKRETRIES=5
[[ $PACKAGEMANAGERLOCKWAITTIME ]] || PACKAGEMANAGERLOCKWAITTIME=60
# This is a place holder function pending code for a check for apt-get locks
}
function prepare_updates() {
# Run any pre-prep scripts
for _script in "$PREPREPSCRIPTDIR"/*; do
run_script "$_script" "Pre-Prep"
done
prepoutput="$(date '+%F %T')\\n"
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 available_package_count
error_count=$("$PACKAGE_MANAGER" -u upgrade --assume-no "${PACKAGEMANAGEROPTIONS[@]}" | grep -c '^[WE]:')
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_delay=$((RANDOM % MAXDELAY))
[[ $sleep_delay -gt 1 ]] && logit "INFO: Sleeping for $sleep_delay seconds"
sleep $sleep_delay
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
declare -x 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."
prepoutput+="\\nSTATUS:FAILED:Errors returned by package manager"
else
logit "INFO: Updates downloaded$DOWNLOADLOGMSG"
prepoutput+="\\nSTATUS:SUCCESS:Package download complete"
fi
else [[ "$available_package_count" -eq 0 ]]
logit "INFO: No updates are available to be downloaded."
prepoutput+="STATUS:SUCCESS:No updates available"
fi
else
logit "ERROR: There were errors returned by \`$PACKAGE_MANAGER -u upgrade --assume-no ${PACKAGEMANAGEROPTIONS[*]}\`. Exiting."
prepoutput+="\\nSTATUS:FAILED:Errors returned by package manager"
fi
else
if [[ "$ONLYINSTALLFROMPREP" == "yes" ]]; then
logit "ERROR: downloadoption set to 'yes' but the '--downloadonly' option is not available in the current version of $PACKAGE_MANAGER"
quit 3
else
logit "WARNING: downloadonly option is not available"
prepoutput+="\\nSTATUS:Download only not available"
fi
fi
else
prepoutput+=$("$PACKAGE_MANAGER" "${PACKAGEMANAGEROPTIONS[@]}" -s dist-upgrade 2>&1)
fi
rotate_file "$DATADIR/last-prep-output-$CONFIGSET"
[[ "$prepoutput" ]] && echo -e "$prepoutput" > "$DATADIR/last-prep-output-$CONFIGSET"
# Run any post-prep scripts
for _script in "$POSTPREPSCRIPTDIR"/*; do
run_script "$_script" "Post-Prep"
done
}
function apply_updates() {
# Prevent dialog box as we are not running the update in interactive mode
declare -x debian_frontend=noninteractive
applyoutput="$(date '+%F %T')\\n"
# Set the list of debs to be installed
if [[ "$ONLYINSTALLFROMPREP" == "yes" ]]; then
local available_packages available_package_count error_count
if [[ $(find "$DOWNLOADDIR/$CONFIGSET" -name "*.deb" | wc -l) -gt 0 ]]; then
available_packages=$("$PACKAGE_MANAGER" -u --just-print install --assume-no "${PACKAGEMANAGEROPTIONS[@]}" "${DOWNLOADDIR}/${CONFIGSET}"/*.deb 2>&1)
echo "$available_packages" >"$DATADIR/last-apply-output-$CONFIGSET"
available_package_count=$(echo "$available_packages" | 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
available_packages=$($PACKAGE_MANAGER -u upgrade --assume-no "${PACKAGEMANAGEROPTIONS[@]}" 2>&1)
echo "$available_packages" >"$DATADIR/last-apply-output-$CONFIGSET"
error_count=$(echo "$available_packages" | grep -c '^[WE]:')
available_package_count=$(echo "$available_packages" | 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
local packages_before packages_after
# Sleep before running pre-scripts and updates
sleep_delay=$((RANDOM % MAXDELAY))
[[ $sleep_delay -gt 1 ]] && logit "INFO: Sleeping for $sleep_delay seconds"
sleep $sleep_delay
for _script in "$PREAPPLYSCRIPTDIR"/*; do
run_script "$_script" "Pre-Apply"
done
logit "INFO: Applying updates"
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
if applyoutput=$("$PACKAGE_MANAGER" "$updateaction" "${PACKAGEMANAGEROPTIONS[@]}" -y "${debs[@]}" 2>&1); then
applyoutput+="\\nSTATUS:SUCCESS:Package updates applied"
else
applyoutput+="\\nSTATUS:FAILED:Package updates failed"
fi
rotate_file "$DATADIR/last-apply-output-$CONFIGSET"
echo -e "$applyoutput" &>"$DATADIR/last-apply-output-$CONFIGSET"
default_signal_handling
packages_after=$(dpkg --list)
if [[ "$packages_before" == "$packages_after" ]]; then
logit "WARNING: No updates were applied. $(echo "$applyoutput" | 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
# Excluding this check because the REBOOTCALL variable is used by the main auter script
# shellcheck disable=SC2034
[[ "$AUTOREBOOT" == "yes" ]] && REBOOTCALL=1
else
logit "INFO: No updates are available to be applied."
applyoutput+="\\nSTATUS:SUCCESS:No updates available"
echo -e "$applyoutput" > "$DATADIR/last-apply-output-$CONFIGSET"
log_last_run
fi
else
logit "ERROR: Exit status $RC returned by \`$PACKAGE_MANAGER -u upgrade --assume-no ${PACKAGEMANAGEROPTIONS[*]}\`. Exiting."
applyoutput+="\\nSTATUS:FAILED:Updates failed with status $RC"
echo -e "$applyoutput" > "$DATADIR/last-apply-output-$CONFIGSET"
quit 3
fi
}