forked from rackerlabs/auter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auter.yumdnfModule
127 lines (108 loc) · 5.1 KB
/
auter.yumdnfModule
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
# 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 yum/dnf version of this script intended for redhat/fedora/CentOS
# 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 [[ $(${PACKAGE_MANAGER} --help | grep -c downloadonly) -gt 0 ]]; then
local RC=$(${PACKAGE_MANAGER} check-update ${PACKAGEMANAGEROPTIONS} &>/dev/null; echo $?)
# If check-update has an exit code of 100, updates are available.
if [[ "${RC}" -eq 100 ]]; then
# Run any pre-prep scripts
sleep $(($RANDOM % ${MAXDELAY}))
if [[ "${ONLYINSTALLFROMPREP}" == "yes" ]]; then
DOWNLOADOPTION="--downloaddir=${DOWNLOADDIR}/${CONFIGSET}"
rm -f "${DOWNLOADDIR}"/"${CONFIGSET}"/*.rpm
# DNF doesn't support downloaddir, so instead we download to the default
# location and copy out the way
if [[ "${PACKAGE_MANAGER}" == "dnf" ]]; then
find /var/cache/dnf -name "*.rpm" -exec rm -f {} \;
DOWNLOADOPTION=""
fi
DOWNLOADLOGMSG=" to ${DOWNLOADDIR}/${CONFIGSET}"
fi
${PACKAGE_MANAGER} ${PACKAGEMANAGEROPTIONS} ${DOWNLOADOPTION} update --downloadonly -y &>${DATADIR}/last-prep-output-${CONFIGSET}
if [[ $? -eq 0 ]]; then
if [[ "${ONLYINSTALLFROMPREP}" == "yes" && "${PACKAGE_MANAGER}" == "dnf" ]]; then
find /var/cache/dnf -name "*.rpm" -exec mv {} ${DOWNLOADDIR}/${CONFIGSET} \;
fi
logit "INFO: Updates downloaded${DOWNLOADLOGMSG}"
else
logit "ERROR: Updates could not be pre-downloaded${DOWNLOADLOGMSG}. See the ${DATADIR}/last-prep-output-${CONFIGSET} file for details."
fi
elif [[ "${RC}" -eq 1 ]]; then
logit "ERROR: Exit status ${RC} returned by \`${PACKAGE_MANAGER} ${PACKAGEMANAGEROPTIONS} ${DOWNLOADOPTION} update --downloadonly -y\`. Exiting."
else
logit "INFO: No updates are available to be downloaded."
fi
else
logit "WARNING: downloadonly option is not available"
fi
else
${PACKAGE_MANAGER} ${PACKAGEMANAGEROPTIONS} check-update &>${DATADIR}/last-prep-output-${CONFIGSET}
fi
# Run any post-prep scripts
for SCRIPT in "${POSTPREPSCRIPTDIR}"/*; do
run_script "${SCRIPT}" "Post-Prep"
done
}
function apply_updates() {
# Set the list of rpms to be installed
if [[ "${ONLYINSTALLFROMPREP}" == "yes" ]]; then
# Default RC to 0 which means no updates are available
local RC=0
# Check if there are updates staged for this configset
if [[ $(ls -1 ${DOWNLOADDIR}/${CONFIGSET}/*.rpm 2>/dev/null| wc -l) -gt 0 ]]; then
# Save the staged rpms to a variable
RPMS="${DOWNLOADDIR}/${CONFIGSET}/*.rpm"
# Check if the updates staged are still required
"${PACKAGE_MANAGER}" check-update $(rpm -qp ${RPMS}) &>/dev/null
local RC=$?
fi
# When passing RPM files to dnf/yum, 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 RC=$(${PACKAGE_MANAGER} check-update ${PACKAGEMANAGEROPTIONS} &>/dev/null; echo $?)
UPDATEACTION="update"
fi
# If check-update has an exit code of 100, updates are available.
if [[ "${RC}" -eq 100 ]]; then
for SCRIPT in "${PREAPPLYSCRIPTDIR}"/*; do
run_script "${SCRIPT}" "Pre-Apply"
done
sleep $(($RANDOM % ${MAXDELAY}))
logit "INFO: Applying updates"
local HISTORY_BEFORE=$(${PACKAGE_MANAGER} history list)
# We don't want to allow the user to interrupt a yum/dnf transaction or Bad Things Happen.
trap '' SIGINT SIGTERM
${PACKAGE_MANAGER} ${UPDATEACTION} -y ${PACKAGEMANAGEROPTIONS} ${RPMS} &>${DATADIR}/last-apply-output-${CONFIGSET}
default_signal_handling
local HISTORY_AFTER=$(${PACKAGE_MANAGER} history list)
for SCRIPT in "${POSTAPPLYSCRIPTDIR}"/*; do
run_script "${SCRIPT}" "Post-Apply"
done
if [[ "${HISTORY_BEFORE}" == "${HISTORY_AFTER}" ]]; then
logit "ERROR: Updates failed. See the ${DATADIR}/last-prep-output-${CONFIGSET}-$(date +\%F) file for details. Exiting."
cp ${DATADIR}/last-apply-output-${CONFIGSET} ${DATADIR}/last-apply-output-${CONFIGSET}-$(date +\%F)
quit 3
fi
local TRANSACTIONID=$(${PACKAGE_MANAGER} history info 2>&1 | grep "Transaction ID")
logit "INFO: Updates complete (${PACKAGE_MANAGER} ${TRANSACTIONID}). You may need to reboot for some updates to take effect"
log_last_run
[[ "${AUTOREBOOT}" == "yes" ]] && reboot_server
elif [[ "${RC}" -eq 0 ]]; then
logit "INFO: No updates are available to be applied."
log_last_run
else
logit "ERROR: Exit status ${RC} returned by \`${PACKAGE_MANAGER} check-update ${PACKAGEMANAGEROPTIONS}\`. Exiting."
quit 3
fi
}