git.lirion.de

Of git, get, and gud

summaryrefslogtreecommitdiffstats
path: root/bin/nextcloud-upgrade
blob: c77182b98f34403006802fb05c7187960e39d878 (plain)
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
#!/usr/bin/env bash

# AUTHOR: mail_redacted_for_web
# LICENCE: LGPL-3.0-only

# Error codes: 131-159 (130 is reserved for sigint et al!)

[ -z "$1" ] && printf 'Usage: %b VERSION\n' "$(basename "$0")" >&2 && exit 101
# shellcheck disable=SC1091
source /etc/lirion/nextcloud.conf || exit 102
# shellcheck disable=SC1091
source /usr/lib/lirion/ln-initfunctions || exit 103

/usr/bin/sudo -u "$NCUSER" /usr/bin/whoami > /dev/null || exit 133
/usr/bin/sudo /usr/bin/whoami > /dev/null || exit 133

lnbegin "File inventory"
if ! /usr/bin/sudo -u "$NCUSER" /usr/bin/stat -tL "${NCDLTGT}/nextcloud-${1}.tar.bz2" > /dev/null 2>&1; then
	lnfail "archive not found"
	exit 131
elif ! /usr/bin/sudo -u "$NCUSER" /usr/bin/stat -tL "${NCDLTGT}/nextcloud-${1}.tar.bz2.sha512" >/dev/null 2>&1; then
	lnfail "checksum file not found"
	exit 131
elif ! /usr/bin/sudo -u "$NCUSER" /usr/bin/stat -tL "${NCDLTGT}/nextcloud-${1}.tar.bz2.asc" >/dev/null 2>&1; then
	lnfail "signature file not found"
	exit 131
fi
lnok
# TODO: implement script parameter skipping this - if we execute right after nextcloud-download,
# this is redundant as the latter also verifies. We need this only on separate execution (so
# by default as well).
lnbegin "Verifying download"
lnprog 'GPG'
if ! /usr/bin/sudo -u "$NCUSER" /usr/bin/gpg --no-default-keyring --keyring /var/www/keyrings/nextcloud-security.gpg \
		--quiet --verify "${NCDLTGT}/nextcloud-${1}.tar.bz2.asc" "${NCDLTGT}/nextcloud-${1}.tar.bz2" 2>/dev/null
then
	lnfail 'GPG failed'
	exit 132
fi
lnok

lnbegin 'Setting maintenance mode'
if ! /usr/bin/sudo -u "$NCUSER" "$NCPHPBIN" "${NCAPPLDIR}/occ" 'maintenance:mode' '--on' >/dev/null 2>&1; then
	lnfail 'enabling failed'
	exit 133
fi
lnok
lnbegin "Grace period with maintenance mode"
while [ "$NCGRACE" -ge 1 ]; do
	lnprog "$NCGRACE"
	NCGRACE="$((NCGRACE-1))"
	sleep 1
done
lnok

lnbegin 'Move old Nextcloud folder'
lnprog 'folder backup removal'
if ! /usr/bin/sudo rm -rf "${NCAPPLDIR}.bak" >/dev/null 2>&1; then
	lnfail 'folder backup removal failed'
	exit 134
fi
lnprog 'folder backup'
if ! /usr/bin/sudo /usr/bin/mv -v "${NCAPPLDIR}" "${NCAPPLDIR}.bak" >/dev/null 2>&1; then
	lnfail 'folder backup failed'
	exit 135
fi
lnok
lnbegin 'NC download extraction'
if ! /usr/bin/sudo /usr/bin/install -dm'0750' -o"$NCUSER" -g"$NCUSER" "$NCAPPLDIR" > /dev/null 2>&1; then
	lnfail 'folder creation failed'
	exit 136
elif ! MYTMP="$(/usr/bin/sudo -u "$NCUSER" /usr/bin/mktemp -d -p /tmp nextcloud.XXXXXX 2>/dev/null)"; then
	lnfail 'temp creation failed'
	exit 137
elif ! /usr/bin/sudo -u "$NCUSER" /usr/bin/tar -C "$MYTMP" --checkpoint-action='.' --checkpoint=16384 \
		-xjf "${NCDLTGT}/nextcloud-${1}.tar.bz2"; then
	lnfail 'tarball extraction failed'
	exit 138
elif [ "$(/usr/bin/sudo -u "$NCUSER" /usr/bin/find "$MYTMP" -mindepth 1 -maxdepth 1 -name 'nextcloud' 2>/dev/null | wc -w)" -ne 1 ]; then
	lnfail 'subfolder nextcloud not found'
	exit 139
elif ! /usr/bin/sudo rsync -rlptD "${MYTMP}/nextcloud/" "${NCAPPLDIR}/" >/dev/null 2>&1; then
	lnfail 'folder sync failed'
	exit 140
fi
lnok
function ctrl_c {
	printf '\n'
	lnbegin 'Caught TERM/INT, aborting.'
	lnquit
	if ! /usr/bin/sudo /usr/bin/rm -rf "$MYTMP"; then
		exit 141
	fi
	exit 130
}
trap ctrl_c INT
trap ctrl_c TERM
lnbegin 'Temp dir removal'
if ! /usr/bin/sudo -u "$NCUSER" /usr/bin/rm -rf "$MYTMP" >/dev/null 2>&1; then
	lnfail 142
fi
lnok
lnbegin 'Sync old config/apps'
# do use trailing slashes for folders here, we are rsyncing!
for src in 'config/config.php' 'apps/' 'apps-extras/' 'apps-external/'; do
	lnprog "$src"
	if ! stat -tL "${NCAPPLDIR}.bak/$src" >/dev/null 2>&1; then
		lnprog "$src not existing in backup"
		sleep 0.314159
		continue
	fi
	if ! /usr/bin/sudo /usr/bin/rsync -rulptgoD "${NCAPPLDIR}.bak/$src" "${NCAPPLDIR}/$src" >/dev/null 2>&1; then
		lnfail "syncing $src failed"
		exit 143
	fi
done
lnok
lnbegin "Nextcloud file permissions"
if ! /usr/bin/sudo /usr/bin/chown -R "${NCUSER}:" "${NCAPPLDIR}/" >/dev/null 2>&1 && \
		/usr/bin/sudo /usr/bin/chmod o-rwx "${NCAPPLDIR}/" >/dev/null 2>&1; then
	lnfail
	exit 144
fi
lnok

# TODO: make update check decide on whether to update
lnbegin 'Disabling maintenance mode'
if ! /usr/bin/sudo -u "$NCUSER" "$NCPHPBIN" "${NCAPPLDIR}/occ" 'maintenance:mode' '--off' >/dev/null 2>&1; then
	lnfail 'disabling failed'
	exit 152
fi
lnok
lnbegin "Nextcloud upgrade"
if ! /usr/bin/sudo -u "$NCUSER" /usr/bin/stat -tL "${NCAPPLDIR}/occ" >/dev/null 2>&1 &&\
	[ "$(/usr/bin/sudo -u "$NCUSER" /usr/bin/stat -c '%f' "${NCAPPLDIR}/occ" 2>/dev/null)" != '81a0' ]; then
	lnfail
	exit 145
fi
lnprog "occ upgrade"
if ! /usr/bin/sudo -u "$NCUSER" "$NCPHPBIN" "${NCAPPLDIR}/occ" upgrade -q >/dev/null 2>&1; then
	lnfail
	exit 146
fi
lnprog "occ app:update --all"
if ! /usr/bin/sudo -u "$NCUSER" "$NCPHPBIN" "${NCAPPLDIR}/occ" app:update -q --all >/dev/null 2>&1; then
	lnfail "occ app:update"
	exit 147
fi
lnprog "occ db:add-missing-primary-keys"
if ! /usr/bin/sudo -u "$NCUSER" "$NCPHPBIN" "${NCAPPLDIR}/occ" db:add-missing-primary-keys -q >/dev/null 2>&1; then
	lnfail "occ db:add-missing-primary-keys"
	exit 148
fi
lnprog "occ db:add-missing-columns"
if ! /usr/bin/sudo -u "$NCUSER" "$NCPHPBIN" "${NCAPPLDIR}/occ" db:add-missing-columns -q >/dev/null 2>&1; then
	lnfail "occ db:add-missing-columns"
	exit 149
fi
lnprog "occ db:add-missing-indices"
if ! /usr/bin/sudo -u "$NCUSER" "$NCPHPBIN" "${NCAPPLDIR}/occ" db:add-missing-indices -q >/dev/null 2>&1; then
	lnfail "occ db:add-missing-indices"
	exit 150
fi
lnprog "cron.php"
if ! /usr/bin/sudo -u "$NCUSER" "$NCPHPBIN" -f "${NCAPPLDIR}/cron.php" 2>/dev/null; then
	lnfail "cron.php"
	exit 151
fi
lnok