summaryrefslogtreecommitdiffstats
path: root/satellit_upgrader/updater.template.sh
blob: ebab45776f504c465ff2d3f01727bf0fe3c376ef (plain) (blame)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/bash

# Use special param to make sure we're running under bash (in case user does "sh install.sh")
[ "x$1" != "x--exec-self" ] && exec /bin/bash "$0" --exec-self "$@"

if [ ! -d "/opt/dmsd" ] && [ ! -d "/srv/openslx/www/slx-admin" ]; then
	echo "This system doesn't look like a bwLehrpool Satellite Server"
	exit 1
fi

TEST1=$(which systemctl)
TEST2=$(lsb_release -sc)
if [ -z "$TEST1" ] || [ "x$TEST2" != "xjessie" ]; then
	echo "This Version of the bwLehrpool Satellite Server is too old!"
	exit 1
fi
unset TEST1 TEST2

unset TMPDIR
[ -n "$TMPDIR" ] && exit 1

IGNORE_ERRORS=
while [ $# -gt 0 ]; do
	[ "x$1" = "x--ignore-errors" ] && IGNORE_ERRORS=jup
	shift
done
readonly IGNORE_ERRORS

declare -rg SELFPID=$$
perror () {
	if [ -n "$IGNORE_ERRORS" ]; then
		echo -n -e '\033[01;31m[ERROR]\033[00m '
		echo "$@"
		return 0
	fi
	echo -n -e '\033[01;31m[FATAL]\033[00m '
	echo "$@"
	[ -n "$TMPDIR" ] && [ -d "$TMPDIR" ] && rm -rf -- "$TMPDIR"
	[ "$$" != "$SELFPID" ] && kill "$SELFPID"
	exit 1
}

pwarning () {
	echo -n -e '\033[01;33m[WARNING]\033[00m '
	echo "$@"
}

if [ "$UID" != "0" ]; then
	perror "Must be running as root"
fi

# ** Extract value from text file containing key=value pairs
extractfield () {
	grep -m1 "^\s*$2\b" "$1" | awk -F '=' '{print $2}' | sed 's/\s//g'
}

# ** Wrap mysql command line client so we're always using the deb-sys-maint credentials
mysql () {
	"$(which mysql)" --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8 "$@"
}

# ** Restart given systemd service, warn if it fails but do not bail out
restart_service () {
	if ! systemctl restart "$1"; then
		pwarning "Could not restart service $1 - !! YOU SHOULD REBOOT THE SERVER !!"
		systemctl status "$1"
	fi
}

# ** Recursively set permissions of given directory to 0755/0644 optionally setting owner/group
# ** If directory is not there it will be mkdir'ed.
fixperms () {
	[ -e "$1" ] || perror "normalize_perms: $1 does not exist"
	[ -d "$1" ] || mkdir -p "$1"
	chmod -R a-rw,u+rwX,go+rX "$1"
	if [ -n "$2" ]; then
		chown -R "$2" "$1" || pwarning "Could not change owner of $1 to $2"
	fi
}

# ** Constants - to be patched by the packaging script
declare -rg TARGET_WEBIF_VERSION="%TARGET_WEBIF_VERSION%"
declare -rg TGZ_SLXADMIN="%TGZ_SLXADMIN%"
declare -rg TGZ_DOZMOD="%TGZ_DOZMOD%"
declare -rg TGZ_TASKMANAGER="%TGZ_TASKMANAGER%"
declare -rg TGZ_TFTP="%TGZ_TFTP%"
declare -rg TGZ_IPXE="%TGZ_IPXE%"
declare -rg TGZ_LDADP="%TGZ_LDADP%"
declare -rg PAYLOAD_OFFSET="%PAYLOAD_OFFSET%"

# ** Constants - hardcoded or determined at runtime
declare -rg PATH_SLXADMIN="/srv/openslx/www/slx-admin"
declare -rg PATH_SLXADMINCACHE="/var/cache/slx-admin"
declare -rg PATH_DOZMOD="/opt/dmsd"
declare -rg PATH_TASKMANAGER="/opt/taskmanager"
declare -rg PATH_TFTP="/srv/openslx/tftp"
declare -rg PATH_IPXE="/opt/openslx/ipxe"
declare -rg PATH_LDADP="/opt/ldadp"

# ** Check if constants have been filled, bail out otherwise
if [ -z "$TARGET_WEBIF_VERSION" ] || [[ "$TARGET_WEBIF_VERSION" == %*% ]]; then
	[ -n "$TGZ_SLXADMIN" ] && perror "Bad upgrader: TARGET_WEBIF_VERSION not set"
fi
if [ -z "$PAYLOAD_OFFSET" ] || [[ "$PAYLOAD_OFFSET" == %*% ]]; then
	perror "Bad upgrader: PAYLOAD_OFFSET not set"
fi

# **********************************************************

# Get current webif version
if [ -n "$TGZ_SLXADMIN" ]; then
	declare -rg CURRENT_WEBIF_VERSION=$(mysql -e 'SELECT value FROM openslx.property WHERE name = "webif-version" LIMIT 1' | tail -n 1)
	[ -z "$CURRENT_WEBIF_VERSION" ] && perror "Could not determine current webif version"
	if [ "$TARGET_WEBIF_VERSION" = "missing" ] || [ "$CURRENT_WEBIF_VERSION" -le "$TARGET_WEBIF_VERSION" ]; then
		: # Negate so NaN triggers else branch
	else
		perror "This update seems to be older than the server version you're currently running"
	fi
fi

# **************** Fix tmpdelete cronjob *******************
if [ -f "/etc/cron.daily/tmpdelete.sh" ]; then
	[ -f "/etc/cron.daily/tmpdelete" ] || mv "/etc/cron.daily/tmpdelete.sh" "/etc/cron.daily/tmpdelete"
	rm -f -- "/etc/cron.daily/tmpdelete.sh"
fi

# ************** Extract payload ***************************
declare -rg TMPDIR=$(mktemp -d)
[ -z "$TMPDIR" ] && perror "Could not create temporary directory for installer"
dd "bs=$PAYLOAD_OFFSET" "if=$0" skip=1 | tar -z -x -C "$TMPDIR"
RET=$?
[ "$RET" -ne 0 ] && perror "Extracting installer payload failed with exit code $RET"
declare -rg FILEDIR="$TMPDIR/files"

# **********************************************************

# ********************** Taskmanager ***********************
# Do this first in case one of the later updates depends on the taskmanager
if [ -n "$TGZ_TASKMANAGER" ]; then
	[ -e "$TMPDIR/$TGZ_TASKMANAGER" ] || perror "$TGZ_TASKMANAGER missing from payload"
	echo "* Task manager"
	echo "Extracting new jar and data"
	# Replacement trick (see slxadmin)
	tar -x -C "$PATH_TASKMANAGER" -f "$TMPDIR/$TGZ_TASKMANAGER" || perror "Could not extract $TGZ_TASKMANAGER to $PATH_TASKMANAGER"
	rm -rf -- "$PATH_TASKMANAGER/data" "$PATH_TASKMANAGER/scripts" "$PATH_TASKMANAGER/plugins" "$PATH_TASKMANAGER/taskmanager.sh"
	tar -x -C "$PATH_TASKMANAGER" -f "$TMPDIR/$TGZ_TASKMANAGER"
	fixperms "$PATH_TASKMANAGER" root:root
	chmod -R a+x "$PATH_TASKMANAGER/scripts"
	echo "Restarting service"
	restart_service taskmanager
	echo "Taskmanager upgrade complete"
fi

# ************************** IPXE **************************
if [ -n "$TGZ_IPXE" ]; then
	[ -e "$TMPDIR/$TGZ_IPXE" ] || perror "$TGZ_IPXE missing from payload"
	echo "* iPXE source code"
	echo "Extracting"
	rm -rf -- "$PATH_IPXE"
	mkdir -p "$PATH_IPXE"
	tar -x -C "$PATH_IPXE" -f "$TMPDIR/$TGZ_IPXE" || perror "Could not extract $TGZ_IPXE to $PATH_IPXE"
	fixperms "$PATH_IPXE" taskmanager:taskmanager
	echo "Resetting pxe menu"
	mysql -e 'UPDATE openslx.property SET value = "invalid" WHERE name = "server-ip"' || pwarning "Could not reset pxe menu status; manual regeneration of menu required"
	echo "iPXE upgrade complete"
	# Trigger default compile in the background so the next regen doesn't take too long
	{
		cd "$PATH_IPXE/src" && sudo -n -u taskmanager nice -n 10 make -j2 bin/undionly.kkkpxe > /dev/null 2>&1
	} &
fi

# ************************** SLX-Admin *********************
if [ -n "$TGZ_SLXADMIN" ]; then
	[ -e "$TMPDIR/$TGZ_SLXADMIN" ] || perror "$TGZ_SLXADMIN missing from payload."
	echo "* SLX-Admin: $CURRENT_WEBIF_VERSION -> $TARGET_WEBIF_VERSION"
	# Cheap hack: extract, then delete, extract again to get rid of old unused files. If the extraction fails, we don't leave
	# the user with a broken webif, because we didn't remove the directories yet
	tar tf "$TMPDIR/$TGZ_SLXADMIN" >/dev/null 2>&1 || perror "Could not extract $TGZ_SLXADMIN to $PATH_SLXADMIN"
	rm -rf -- "$PATH_SLXADMIN/"{inc,apis,modules,templates,Mustache,script,style}
	tar -x -C "$PATH_SLXADMIN" -f "$TMPDIR/$TGZ_SLXADMIN"
	rm -f -- "$PATH_SLXADMIN/config.php.example"
	# Enable modules
	(
		mkdir -p "$PATH_SLXADMIN/modules"
		cd "$PATH_SLXADMIN/modules" || perror "Cannot cd to slxadmin"
		for mod in adduser backup baseconfig baseconfig_bwidm baseconfig_bwlp bootstrap_datepicker bootstrap_dialog bootstrap_timepicker bootstrap_multiselect bootstrap_switch dozmod eventlog exams js_chart js_circles js_jqueryui js_moment js_selectize js_vis locations main minilinux news serversetup-bwlp roomplanner session statistics summernote sysconfig syslog systemstatus vmstore webinterface; do
			name=${mod%%-*}
			rm -f -- "$name"
			ln -s "../modules-available/$mod" "$name" || pwarning "Could not activate module '$mod' (alias '$name')"
		done
		# Update DB
		cd "$PATH_SLXADMIN"
		if ! ./install-all > "$TMPDIR/wwwup.log"; then
			cat "$TMPDIR/wwwup.log"
			perror "Updating the web interface database failed"
		fi
		if grep -q 'Result: UPDATE_FAILED' "$TMPDIR/wwwup.log"; then
			cat "$TMPDIR/wwwup.log"
			perror "Updating the web interface database failed"
		fi
		if [ "x$(tail -n -1 "$TMPDIR/wwwup.log")" != "xDone." ]; then
			cat "$TMPDIR/wwwup.log"
			pwarning "slxadmin updater didn't finish with 'Done.'"
		fi
	)
	# New config.php supplied
	if [ -s "$FILEDIR/slxadmin-config.php" ]; then
		# Patch config with new features using old mysql password
		SQLPASS=$(grep -m1 '^define.*CONFIG_SQL_PASS.*;' "${PATH_SLXADMIN}/config.php" | awk -F "'" '{print $4}')
		if [ -z "$SQLPASS" ]; then
			pwarning "Could not extract old slx-admin mysql password - cannot update config :("
		else
			sed "s/%MYSQL_OPENSLX_PASS%/${SQLPASS}/g" "$FILEDIR/slxadmin-config.php" > "${PATH_SLXADMIN}/config.php"
		fi
	fi

	fixperms "$PATH_SLXADMIN" root:root
	chmod 0640 "$PATH_SLXADMIN/config.php"
	chown root:www-data "$PATH_SLXADMIN/config.php"
	# Update version number in DB - doesn't make too much sense like this, restore would overwrite
	mysql -e "INSERT INTO openslx.property (name, value) VALUES ('webif-version', '${TARGET_WEBIF_VERSION}')
		ON DUPLICATE KEY UPDATE value = VALUES(value)" || pwarning "Could not update version number in DB"
	# config.tgz symlink -> db entry
	if [ -L /srv/openslx/www/boot/default/config.tgz ]; then
		CONFTGZ=$(readlink /srv/openslx/www/boot/default/config.tgz | sed "s/'/\\\'/g")
		echo "config.tgz links to '$CONFTGZ'"
		mysql -e "INSERT IGNORE INTO openslx.configtgz_location (locationid, configid) SELECT 0, configid FROM openslx.configtgz WHERE filepath = '$CONFTGZ' LIMIT 1" \
			|| pwarning "Could not convert default config.tgz setting - do so manually"
		rm -f -- /srv/openslx/www/boot/default/config.tgz
	fi
	ln -s -f "$PATH_TFTP" "/srv/openslx/www/tftp" || perror "Could not symlink $PATH_TFTP to /srv/openslx/www/tftp"
	echo "Web interface upgrade complete"
	fixperms "${PATH_SLXADMINCACHE}" www-data:www-data
	
	if [ -h "${PATH_SLXADMINCACHE}/baseconfig-bwidm_settings.json" ]; then
		pwarning "There is already a link or file /srv/openslx/www/slx-admin/modules-available/baseconfig_bwidm/baseconfig/settings.json. Please check manually, it should lead to /srv/openslx/www/slx-admin/modules-available/baseconfig_bwidm/baseconfig/settings.json"
	else
		ln -s "${PATH_SLXADMINCACHE}/baseconfig-bwidm_settings.json" /srv/openslx/www/slx-admin/modules-available/baseconfig_bwidm/baseconfig/settings.json || \
		pwarning "Could not set link ${PATH_SLXADMINCACHE}/baseconfig-bwidm_settings.json to /srv/openslx/www/slx-admin/modules-available/baseconfig_bwidm/baseconfig/settings.json - do so manually"
	fi
fi

#************************* Dozmod *************************
if [ -n "$TGZ_DOZMOD" ]; then
	[ -e "$TMPDIR/$TGZ_DOZMOD" ] || perror "$TGZ_DOZMOD missing from payload."
	echo "* Dozentenmodul"
	echo "Adjusting mysql permissions of user sat"
	mysql -e 'GRANT CREATE, ALTER ON sat.* TO sat@localhost' || perror "Could not GRANT permissions ON sat.* to sat@localhost"
	mysql -e 'GRANT SELECT ON openslx.location TO sat@localhost' || perror "Could not GRANT permissions ON openslx.location to sat@localhost"
	echo "Extracting new jar"
	tar -x -C "$PATH_DOZMOD" -f "$TMPDIR/$TGZ_DOZMOD" || perror "Could not extract $TGZ_DOZMOD to $PATH_DOZMOD"
	rm -rf -- "$PATH_DOZMOD/bwLehrpoolSuite_Server.jar" "$PATH_DOZMOD/control.sh" "$PATH_DOZMOD/flag"
	tar -x -C "$PATH_DOZMOD" -f "$TMPDIR/$TGZ_DOZMOD"
	if mysql -e 'SHOW TABLES' openslx | grep -q '^location$'; then
		echo "Enabling location feature"
		sed -i '/^db.location-table\b/d' "$PATH_DOZMOD/config.properties"
		echo 'db.location-table = openslx.location' >> "$PATH_DOZMOD/config.properties"
	fi
	fixperms "$PATH_DOZMOD" root:images
	echo "Restarting service"
	restart_service dmsd
	echo "Dozentenmodul Server Daemon upgrade complete"
fi

# ************************** TFTP **************************
if [ -n "$TGZ_TFTP" ]; then
	[ -e "$TMPDIR/$TGZ_TFTP" ] || perror "$TGZ_TFTP missing from payload"
	echo "* TFTP"
	echo "Extracting"
	rm -rf -- "$PATH_TFTP/v4" "$PATH_TFTP/v6" "$PATH_TFTP/ipxelinux.0" "$PATH_TFTP/pxelinux.0"
	mkdir -p "$PATH_TFTP"
	tar -x -C "$PATH_TFTP" -f "$TMPDIR/$TGZ_TFTP" || perror "Could not extract $TGZ_TFTP to $PATH_TFTP"
	fixperms "$PATH_TFTP" taskmanager:taskmanager
	echo "Resetting pxe menu"
	mysql -e 'UPDATE openslx.property SET value = "invalid" WHERE name = "server-ip"' || pwarning "Could not reset pxe menu status; manual regeneration of menu required"
	echo "TFTP upgrade complete"
fi

# ********************** lighttpd config *******************
if [ -e "$FILEDIR/lighttpd.conf" ] && [ -e "/etc/lighttpd/lighttpd.conf" ]; then
	echo "* Replacing lighttpd.conf"
	cp "$FILEDIR/lighttpd.conf" "/etc/lighttpd/lighttpd.conf" || perror "Could not replace /etc/lighttpd/lighttpd.conf"
	restart_service lighttpd
	echo "lighttpd config upgrade complete"
fi

# ********************** atftpd.service ********************
if [ -e "$FILEDIR/atftpd.service" ]; then
	echo "* Replacing atftpd.service"
	cp "$FILEDIR/atftpd.service" "/etc/systemd/system/atftpd.service" || perror "Could not replace /etc/systemd/system/atftpd.service"
	ln -s -f "../atftpd.service" "/etc/systemd/system/multi-user.target.wants/"
	systemctl daemon-reload
	restart_service atftpd
	echo "Service file replaced"
fi

# ********************** dmsd.service **********************
if [ -e "$FILEDIR/dmsd.service" ]; then
	echo "* Replacing dmsd.service"
	cp "$FILEDIR/dmsd.service" "/etc/systemd/system/dmsd.service" || perror "Could not replace /etc/systemd/system/dmsd.service"
	ln -s -f "../dmsd.service" "/etc/systemd/system/multi-user.target.wants/"
	systemctl daemon-reload
	restart_service dmsd
	echo "Service file replaced"
fi

# ************************** ldadp *************************
if [ -n "$TGZ_LDADP" ]; then
	[ -e "$TMPDIR/$TGZ_LDADP" ] || perror "$TGZ_LDADP missing from payload"
	echo "* LDAP/AD Proxy"
	OLD=$(md5sum "$PATH_LDADP/ldadp")
	echo "Extracting new binary"
	tar -x -C "$PATH_LDADP" -f "$TMPDIR/$TGZ_LDADP" || perror "Could not extract $TGZ_LDADP to $PATH_LDADP"
	rm -rf -- "$PATH_LDADP/logs"
	mkdir -p "$PATH_LDADP/configs" "$PATH_LDADP/pid"
	fixperms "$PATH_LDADP/configs" taskmanager:ldadp
	chmod -R o-rwx "$PATH_LDADP/configs"
	fixperms "$PATH_LDADP/pid" ldadp:root
	mkdir -p "/var/log/ldadp"
	fixperms "/var/log/ldadp" ldadp:root
	NEW=$(md5sum "$PATH_LDADP/ldadp")
	if [ "x$OLD" != "x$NEW" ]; then
		killall ldadp 2>/dev/null && pwarning "LDAP/AD Proxy was killed. Client logins might not work for a couple of minutes (see Server Status in webif)"
	fi
	echo "LDADP upgrade complete"
fi

# ************************ sudo config *********************
if [ -n "$FILEDIR/tm-sudo-config" ]; then
	cp -f "$FILEDIR/tm-sudo-config" "/etc/sudoers.d/taskmanager" || pwarning "Could not update sudo conf for taskmanager"
	chmod 0440 "/etc/sudoers.d/taskmanager"
fi

for i in 1 1 1 1 2 2 3 4 END; do
	CB=$(sudo -u www-data -n php /srv/openslx/www/slx-admin/api.php cb)
	[ "x$CB" != "xTrue" ] && break
	[ "$i" = "END" ] && break
	sleep $i
done

echo ""
echo "Update complete. It is recommended to reboot the server."
echo ""

exit 0
# File end