forked from rackerlabs/auter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auter
executable file
·282 lines (239 loc) · 8.5 KB
/
auter
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
#!/bin/bash
#
# auter is a yum-cron type package which implements automatic updates on an
# individual server with features such as predownloading packages and reboots.
#
#
# Copyright 2016 Rackspace, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
readonly AUTERVERSION="0.11"
readonly SCRIPTDIR="/etc/auter"
readonly DATADIR="/var/lib/auter"
readonly LOCKFILE="${DATADIR}/enabled"
readonly PIDFILE="/var/run/auter/auter.pid"
# Set default options - these can be overridden in the config file or with a command line argument
AUTOREBOOT="no"
PACKAGEMANAGEROPTIONS=""
PREDOWNLOADUPDATES="yes"
ONLYINSTALLFROMPREP="no"
CONFIGFILE="/etc/auter/auter.conf"
DOWNLOADDIR="/var/cache/auter"
MAXDELAY=3600
CONFIGSET="default"
PREPREPSCRIPTDIR="${SCRIPTDIR}/pre-prep.d"
POSTPREPSCRIPTDIR="${SCRIPTDIR}/post-prep.d"
PREAPPLYSCRIPTDIR="${SCRIPTDIR}/pre-apply.d"
POSTAPPLYSCRIPTDIR="${SCRIPTDIR}/post-apply.d"
PREREBOOTSCRIPTDIR="${SCRIPTDIR}/pre-reboot.d"
POSTREBOOTSCRIPTDIR="${SCRIPTDIR}/post-reboot.d"
function default_signal_handling() {
trap 'rm -f "${PIDFILE}"' SIGINT SIGTERM
}
# The man page is generated in part from the print_help() text by running:
# help2man --include=auter.help2man --no-info ./auter > auter.man
function print_help() {
echo "
Usage: auter [--enable|--disable|--status] [--prep] [--apply] [--reboot] [--postreboot] [--config=<configfile>] [OPTION]
Actions:
--enable Enable auter
--disable Disable auter. Also deletes unused pidfile if it exists
--status Show whether enabled or disabled
--prep Pre-download updates before applying
--apply Apply updates, and reboot if AUTOREBOOT=yes
--reboot Reboot system including pre/post reboot scripts
--postreboot Run post reboot script
Options:
--config=FILE Specify the full path to an auter config file. Defaults to /etc/auter/auter.conf
-h, --help Show this help text
-v, --version Show the version
"
}
function logit() {
# If running on a tty, print to screen
$(tty -s) && echo "$1"
logger -p info -t auter "$1"
}
function run_script {
SCRIPT=$1
PHASE=$2
if [[ -x "${SCRIPT}" ]] && [[ -f "${SCRIPT}" ]]; then
logit "INFO: Running ${PHASE} script ${SCRIPT}"
${SCRIPT}
local RC=$?
if [[ "${RC}" -ne 0 ]]; then
logit "ERROR: ${PHASE} script ${SCRIPT} exited with non-zero exit code ${RC}. Aborting auter run."
quit 8
fi
elif [[ -f "${SCRIPT}" ]]; then
logit "ERROR: ${PHASE} script ${SCRIPT} exists but the execute bit is not set. Skipping."
fi
}
# Check whether yum, or dnf is available
function check_package_manager() {
if [[ -x /usr/bin/dnf ]]; then
echo dnf
elif [[ -x /usr/bin/yum ]]; then
echo yum
elif [[ -x /usr/bin/apt-get ]]; then
echo apt-get
else
logit "ERROR: Cannot find yum, dnf or apt-get"
exit 7
fi
}
function reboot_server() {
for SCRIPT in "${PREREBOOTSCRIPTDIR}"/*; do
run_script "${SCRIPT}" "Pre-Reboot"
done
if [[ $(ls "${POSTREBOOTSCRIPTDIR}/" 2>/dev/null) ]]; then
logit "INFO: Adding post-reboot-hook to run scripts under ${POSTREBOOTSCRIPTDIR} to /etc/cron.d/auter-postreboot-${CONFIGSET}"
echo -e "@reboot root /usr/bin/auter --postreboot --config ${CONFIGFILE}" > /etc/cron.d/auter-postreboot-${CONFIGSET}
fi
logit "INFO: Rebooting server"
/sbin/shutdown -r +2 "auter: System reboot to apply updates" &
}
function post_reboot() {
logit "INFO: Removed post-reboot hook: /etc/cron.d/auter-postreboot-${CONFIGSET}"
rm -f "/etc/cron.d/auter-postreboot-${CONFIGSET}"
$(tty -s) || sleep 300
for SCRIPT in "${POSTREBOOTSCRIPTDIR}"/*; do
run_script "${SCRIPT}" "Post-Reboot"
done
}
function print_status() {
if [[ -f "${LOCKFILE}" ]] && [[ -f "${PIDFILE}" ]]; then
if kill -0 $(cat ${PIDFILE}) &>/dev/null; then
echo "auter is currently enabled and running"
else
echo "auter is currently enabled and pid file exists but process is dead"
fi
elif [[ -f "${LOCKFILE}" ]] && [[ ! -f "${PIDFILE}" ]]; then
echo "auter is currently enabled and not running"
else
echo "auter is currently disabled"
fi
}
# Needed to cleanup our PID file. The only argument is the exit code to use.
function quit() {
if [[ -f "${PIDFILE}" ]]; then
rm -f "${PIDFILE}"
fi
exit $1
}
function log_last_run() {
local LASTRUNTIME=$(date -Iseconds)
logit "INFO: Auter successfully ran at ${LASTRUNTIME}"
}
# Main
# Make sure we trap signals and clean up the PID before exiting
default_signal_handling
ARGS=$@
OPTS=$(getopt -n "$0" -o h,v --long prep,apply,enable,disable,reboot,postreboot,version,help,status,config: -- "$@")
if [[ $? -ne 0 ]]; then
echo "Try '$0 --help' for more information."
exit
fi
eval set -- "$OPTS"
while true ; do
case "$1" in
-h|--help) print_help ; exit 0; shift;;
-v|--version) echo "auter ${AUTERVERSION}"; exit 0 ; shift;;
--config) unset CONFIGSET ; CONFIGFILE=$2 ; CUSTOMCONFIG=1 ; shift 2;;
--prep) PREP=1 ; shift;;
--apply) APPLY=1 ; shift;;
--reboot) REBOOTCALL=1 ; shift;;
--postreboot) POSTREBOOT=1 ; shift;;
--enable) ENABLE=1 ; shift;;
--disable) DISABLE=1 ; shift;;
--status) print_status ; exit 0 ; shift;;
--) shift ; break ;;
esac
done
[[ "${PREP}${APPLY}${REBOOTCALL}${POSTREBOOT}${ENABLE}${DISABLE}" == "" ]] && (print_help && exit 1)
readonly PACKAGE_MANAGER=$(check_package_manager)
# Do this after option processing so --help and --status still work.
if [[ "$(whoami)" != "root" ]]; then
echo "Script must be run as root"
exit 5
fi
if [[ ! -d "${DATADIR}" ]]; then
logit "FATAL ERROR: auter DATADIR ${DATADIR} does not exist."
exit 5
fi
if [[ "${ENABLE}" ]] ; then
touch "${LOCKFILE}"
echo "DO NOT DELETE THIS FILE. This file is automatically generated by auter. To disable auter, run auter --disable instead." > "${LOCKFILE}"
logit "INFO: auter enabled"
exit 0
fi
if [[ "${DISABLE}" ]] ; then
rm -f "${LOCKFILE}"
if [[ -f "${PIDFILE}" ]] && ! kill -0 $(cat ${PIDFILE}) &>/dev/null; then
rm -f "${PIDFILE}"
logit "INFO: auter disabled and cleared pid file"
else
logit "INFO: auter disabled"
fi
exit 0
fi
if [[ ! -f "${LOCKFILE}" ]]; then
logit "WARNING: auter disabled. Please run auter --enable to enable automatic updates."
exit 4
fi
# PID file checking to make sure multiple copies of auter don't run at once.
PIDDIR=$(dirname "${PIDFILE}")
if [[ ! -d "${PIDDIR}" ]]; then
install -m 755 -o root -g root -d "${PIDDIR}"
fi
# Note: ALL script exits after this block must use the quit() function instead so the PIDfile is cleaned up.
if [[ -f "${PIDFILE}" ]]; then
logit "ERROR: auter is already running or ${PIDFILE} exists."
exit 6
else
echo "$$" > "${PIDFILE}"
fi
if [[ -f "${CONFIGFILE}" ]]; then
source "${CONFIGFILE}"
elif [[ "${CUSTOMCONFIG}" ]]; then
logit "ERROR: Custom config file ${CONFIGFILE} does not exist"
quit 5
else
logit "WARNING: Using default config values."
fi
# CONFIGSET needs to be set if we're using a custom configuration file.
if [[ -z "${CONFIGSET}" ]]; then
logit "ERROR: You must specify the CONFIGSET variable in custom config file ${CONFIGFILE} to avoid naming collisions"
quit 5
fi
if [[ "${ONLYINSTALLFROMPREP}" == "yes" ]]; then
if [[ ! -d "${DOWNLOADDIR}/${CONFIGSET}" ]]; then
install -m 755 -o root -g root -d ${DOWNLOADDIR}/${CONFIGSET}
elif [[ $(stat -c %G%U%a "${DOWNLOADDIR}") != rootroot[0-9][0-9][0,1,4,5] ]]; then
logit "ERROR: ${DOWNLOADDIR}/${CONFIGSET} does not have the correct permissions."
quit 3
fi
fi
logit "INFO: Running with: $0 ${ARGS}"
[[ "${MAXDELAY}" -lt 1 ]] && MAXDELAY=1
$(tty -s) && MAXDELAY=1 && logit "INFO: Running in an interactive shell, disabling all random sleeps"
# There is an explicit quit here to avoid auter automatically running any
# other unexpected functions.
[[ "${POSTREBOOT}" ]] && post_reboot && quit 0
# Source the module for the specific package manager.
. /usr/lib/auter/auter.module
# The following 2 functions are provided by the previously sourced /usr/lib/auter/auter.module
[[ "${PREP}" ]] && prepare_updates
[[ "${APPLY}" ]] && apply_updates
[[ "${REBOOTCALL}" ]] && reboot_server
quit 0