-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
service-setup.sh
executable file
·449 lines (409 loc) · 18 KB
/
service-setup.sh
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# ownCloud service setup
WEB_DIR="/var/services/web_packages"
# for backwards compatability
if [ $SYNOPKG_DSM_VERSION_MAJOR -lt 7 ];then
WEB_DIR="/var/services/web"
fi
# Others
PHP="/usr/local/bin/php74"
SQLITE="/bin/sqlite3"
OCROOT="${WEB_DIR}/${SYNOPKG_PKGNAME}"
OCC="${SYNOPKG_PKGDEST}/bin/occ-cmd.sh"
JQ="/bin/jq"
SYNOSVC="/usr/syno/sbin/synoservice"
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
GROUP="http"
fi
set_owncloud_permissions ()
{
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
DIRNAME=$1
DIRDATA="${wizard_owncloud_datadirectory}"
PKGUSER="sc-${SYNOPKG_PKGNAME}"
echo "Setting the correct ownership and permissions of the files and folders in ${DIRNAME}"
# Set the ownership for all files and folders to sc-owncloud:http except the config and data directory
find -L ${DIRNAME} \( -path ./data -o -path ./config \) -prune -o -type d -print0 | xargs -0 chown ${PKGUSER}:${GROUP} 2>&1
find -L ${DIRNAME} \( -path ./data -o -path ./config \) -prune -o -type f -print0 | xargs -0 chown ${PKGUSER}:${GROUP} 2>&1
# Set the ownership for all files and folders to http:http for the config, data and apps directories
chown -R ${GROUP}:${GROUP} ${DIRNAME}/config 2>&1
if [ -n "${DIRDATA}" ]; then
chown -R ${GROUP}:${GROUP} ${DIRDATA} 2>&1
else
chown -R ${GROUP}:${GROUP} ${DIRNAME}/data 2>&1
fi
chown -R ${GROUP}:${GROUP} ${DIRNAME}/apps 2>&1
# Use chmod on files and directories with different permissions
# For all files use 0640
find -L ${DIRNAME} -type f -print0 | xargs -0 chmod 640 2>&1
# For all directories use 0750
find -L ${DIRNAME} -type d -print0 | xargs -0 chmod 750 2>&1
# For external data directory
if [ -n "${DIRDATA}" ]; then
find -L ${DIRDATA} -type f -print0 | xargs -0 chmod 640 2>&1
find -L ${DIRDATA} -type d -print0 | xargs -0 chmod 750 2>&1
fi
# Set the occ command to executable
chmod +x ${DIRNAME}/occ 2>&1
else
echo "Notice: set_owncloud_permissions() is no longer required on DSM7."
fi
}
service_prestart ()
{
# Replace generic service startup, fork process in background
echo "Starting owncloud-daemon at ${SYNOPKG_PKGDEST}/bin" >> ${LOG_FILE}
COMMAND="${SYNOPKG_PKGDEST}/bin/owncloud-daemon"
${COMMAND} >> ${LOG_FILE} 2>&1 &
echo "$!" > "${PID_FILE}"
}
service_preinst ()
{
:
}
service_postinst ()
{
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
# Install the web interface
echo "Installing web interface"
${MKDIR} ${OCROOT}
rsync -aX ${SYNOPKG_PKGDEST}/share/${SYNOPKG_PKGNAME}/ ${OCROOT} 2>&1
# Install web configurations
TEMPDIR="${SYNOPKG_PKGTMP}/web"
${MKDIR} ${TEMPDIR}
WS_CFG_PATH="/usr/syno/etc/packages/WebStation"
WS_CFG_FILE="WebStation.json"
PHP_CFG_FILE="PHPSettings.json"
PHP_PROF_NAME="Default PHP 7.4 Profile"
WS_BACKEND="$(${JQ} -r '.default.backend' ${WS_CFG_PATH}/${WS_CFG_FILE})"
WS_PHP="$(${JQ} -r '.default.php' ${WS_CFG_PATH}/${WS_CFG_FILE})"
CFG_UPDATE="no"
# Check if Apache is the selected back-end
if [ ! "$WS_BACKEND" = "2" ]; then
echo "Set Apache as the back-end server"
${JQ} '.default.backend = 2' ${WS_CFG_PATH}/${WS_CFG_FILE} > ${TEMPDIR}/${WS_CFG_FILE}
${MV} ${WS_CFG_PATH}/${WS_CFG_FILE} ${WS_CFG_PATH}/${WS_CFG_FILE}.bak
rsync -aX ${TEMPDIR}/${WS_CFG_FILE} ${WS_CFG_PATH}/ 2>&1
${RM} ${TEMPDIR}/${WS_CFG_FILE}
${SYNOSVC} --reload nginx
fi
# Check if default PHP profile is selected
if [ -z "$WS_PHP" ]; then
echo "Enable default PHP profile"
# Locate default PHP profile
PHP_PROF_ID="$(${JQ} -r '. | to_entries[] | select(.value | type == "object" and .profile_desc == "'"$PHP_PROF_NAME"'") | .key' "${WS_CFG_PATH}/${PHP_CFG_FILE}")"
${JQ} ".default.php = \"$PHP_PROF_ID\"" "${WS_CFG_PATH}/${WS_CFG_FILE}" > ${TEMPDIR}/${WS_CFG_FILE}
${MV} ${WS_CFG_PATH}/${WS_CFG_FILE} ${WS_CFG_PATH}/${WS_CFG_FILE}.bak
rsync -aX ${TEMPDIR}/${WS_CFG_FILE} ${WS_CFG_PATH}/ 2>&1
${RM} ${TEMPDIR}/${WS_CFG_FILE}
${SYNOSVC} --reload nginx
fi
# Check for ownCloud PHP profile
if ! ${JQ} -e '.["com-synocommunity-packages-owncloud"]' "${WS_CFG_PATH}/${PHP_CFG_FILE}" >/dev/null; then
echo "Add PHP profile for ownCloud"
${JQ} --argfile ocNode ${SYNOPKG_PKGDEST}/web/owncloud.json '.["com-synocommunity-packages-owncloud"] = $ocNode' ${WS_CFG_PATH}/${PHP_CFG_FILE} > ${TEMPDIR}/${PHP_CFG_FILE}
${MV} ${WS_CFG_PATH}/${PHP_CFG_FILE} ${WS_CFG_PATH}/${PHP_CFG_FILE}.bak
rsync -aX ${TEMPDIR}/${PHP_CFG_FILE} ${WS_CFG_PATH}/ 2>&1
${RM} ${TEMPDIR}/${PHP_CFG_FILE}
CFG_UPDATE="yes"
fi
# Check for ownCloud Apache config
if [ ! -f "/usr/local/etc/apache24/sites-enabled/owncloud.conf" ]; then
echo "Add Apache config for ownCloud"
rsync -aX ${SYNOPKG_PKGDEST}/web/owncloud.conf /usr/local/etc/apache24/sites-enabled/ 2>&1
CFG_UPDATE="yes"
fi
# Restart Apache if configs have changed
if [ "$CFG_UPDATE" = "yes" ]; then
echo "Restart Apache to load new configs"
${SYNOSVC} --restart pkgctl-Apache2.4
fi
fi
if [ "${SYNOPKG_PKG_STATUS}" = "INSTALL" ]; then
# Create data directory
${MKDIR} "${wizard_owncloud_datadirectory}"
# Fix permissions
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
set_owncloud_permissions ${OCROOT}
fi
# Setup configuration file
${OCC} maintenance:install \
--database "sqlite" \
--database-name "${SYNOPKG_PKGNAME}" \
--data-dir "${wizard_owncloud_datadirectory}" \
--admin-user "${wizard_owncloud_admin_username}" \
--admin-pass "${wizard_owncloud_admin_password}" 2>&1
# Get the trusted domains
DOMAINS="$(${OCC} config:system:get trusted_domains)"
# Fix trusted domains array
line_number=0
echo "${DOMAINS}" | while read -r line; do
if [ "$(echo "$line" | grep -cE ':5000|:5001')" -gt 0 ]; then
# Remove ":5000" or ":5001" from the line and update the trusted_domains array
new_line=$(echo "$line" | sed -E 's/(:5000|:5001)//')
${OCC} config:system:set trusted_domains $line_number --value="$new_line"
fi
line_number=$((line_number+1))
done
# Add user specified trusted domains
line_number=$(( $(echo -ne "$DOMAINS" | wc -l) + 1 ))
for var in wizard_owncloud_trusted_domain_1 wizard_owncloud_trusted_domain_2 wizard_owncloud_trusted_domain_3; do
val="${!var}"
if [ -n "$val" ]; then
${OCC} config:system:set trusted_domains $line_number --value="$val"
line_number=$((line_number+1))
fi
done
# Add HTTP to HTTPS redirect to Apache configuration file
APACHE_CONF="${OCROOT}/.htaccess"
if [ -f "${APACHE_CONF}" ]; then
echo "RewriteEngine On" >> ${APACHE_CONF}
echo "RewriteCond %{HTTPS} off" >> ${APACHE_CONF}
echo "RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]" >> ${APACHE_CONF}
fi
# Fix permissions
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
set_owncloud_permissions ${OCROOT}
fi
fi
}
service_preuninst ()
{
if [ "${SYNOPKG_PKG_STATUS}" = "UNINSTALL" ]; then
# Check export directory
if [ -n "${wizard_export_path}" ]; then
# Get data directory
DATADIR="$(${OCC} config:system:get datadirectory)"
# Prepare archive structure
TEMPDIR="${SYNOPKG_PKGTMP}/${SYNOPKG_PKGNAME}-backup_$(date +"%Y%m%d")"
${MKDIR} "${TEMPDIR}"
# Check database export
if [ "${wizard_export_database}" = "true" ]; then
echo "Copying previous database from ${DATADIR}"
${MKDIR} "${TEMPDIR}/database"
${SQLITE} "${DATADIR}/${SYNOPKG_PKGNAME}.db" ".backup '${TEMPDIR}/database/${SYNOPKG_PKGNAME}.db'" 2>&1
fi
# Check configuration export
if [ "${wizard_export_configs}" = "true" ]; then
echo "Copying previous configuration from ${OCROOT}"
${MKDIR} "${TEMPDIR}/configs"
${CP} "${OCROOT}/.user.ini" "${TEMPDIR}/configs/"
${CP} "${OCROOT}/.htaccess" "${TEMPDIR}/configs/"
${MKDIR} "${TEMPDIR}/configs/config"
source="${OCROOT}/config"
patterns=(
"*config.php"
"*.json"
)
target="${TEMPDIR}/configs/config"
# Process each pattern of files in the source directory
for pattern in "${patterns[@]}"; do
files=$(find "$source" -type f -name "$pattern")
if [ -n "$files" ]; then
for file in "${files[@]}"; do
file_name=$(basename "$file")
${CP} "$file" "$target/"
done
fi
done
fi
# Check user data export
if [ "${wizard_export_userdata}" = "true" ]; then
echo "Copying previous user data from ${DATADIR}"
dir_name=$(basename "$DATADIR")
${MKDIR} "${TEMPDIR}/$dir_name"
${CP} "${DATADIR}" "${TEMPDIR}/$dir_name/"
fi
# Create backup archive
archive_name="$(basename "$TEMPDIR").tar.gz"
echo "Creating compressed archive of ownCloud data in file $archive_name"
/bin/tar -czvf "${SYNOPKG_PKGTMP}/$archive_name" "$TEMPDIR" 2>&1
# Move archive to export directory
if ${RSYNC} "${SYNOPKG_PKGTMP}/$archive_name" "${wizard_export_path}/"; then
echo "Backup file copied successfully to ${wizard_export_path}."
else
echo "File copy failed. Trying to copy to alternate location..."
if ${RSYNC} "${SYNOPKG_PKGTMP}/$archive_name" "${SYNOPKG_PKGDEST_VOL}/@tmp/"; then
echo "Backup file copied successfully to alternate location (${SYNOPKG_PKGDEST_VOL}/@tmp)."
else
echo "File copy failed. Backup of ownCloud data will not be saved."
fi
fi
fi
fi
}
service_postuninst ()
{
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
# Remove the web interface
${RM} ${OCROOT}
# Remove web configurations
TEMPDIR="${SYNOPKG_PKGTMP}/web"
${MKDIR} ${TEMPDIR}
WS_CFG_PATH="/usr/syno/etc/packages/WebStation"
PHP_CFG_FILE="PHPSettings.json"
CFG_UPDATE="no"
# Check for ownCloud PHP profile
if ${JQ} -e '.["com-synocommunity-packages-owncloud"]' "${WS_CFG_PATH}/${PHP_CFG_FILE}" >/dev/null; then
echo "Removing PHP profile for ownCloud"
${JQ} 'del(.["com-synocommunity-packages-owncloud"])' ${WS_CFG_PATH}/${PHP_CFG_FILE} > ${TEMPDIR}/${PHP_CFG_FILE}
${MV} ${WS_CFG_PATH}/${PHP_CFG_FILE} ${WS_CFG_PATH}/${PHP_CFG_FILE}.bak
rsync -aX ${TEMPDIR}/${PHP_CFG_FILE} ${WS_CFG_PATH}/ 2>&1
${RM} ${TEMPDIR}/${PHP_CFG_FILE}
CFG_UPDATE="yes"
fi
# Check for ownCloud Apache config
if [ -f "/usr/local/etc/apache24/sites-enabled/owncloud.conf" ]; then
echo "Removing Apache config for ownCloud"
${RM} /usr/local/etc/apache24/sites-enabled/owncloud.conf
CFG_UPDATE="yes"
fi
# Restart Apache if configs have changed
if [ "$CFG_UPDATE" = "yes" ]; then
echo "Restart Apache to load new configs"
${SYNOSVC} --restart pkgctl-Apache2.4
fi
fi
}
validate_preupgrade ()
{
# ownCloud upgrades only possible from 8.2.11, 9.0.9, 9.1.X, or 10.X.Y
is_upgrade_possible="no"
valid_versions=("8.2.11" "9.0.9" "9.1.*" "10.*.*")
previous=$(echo ${SYNOPKG_OLD_PKGVER} | cut -d'-' -f1)
for version in "${valid_versions[@]}"; do
if echo "$previous" | grep -q "$version"; then
is_upgrade_possible="yes"
break
fi
done
# No matching ugrade paths found
if [ "$is_upgrade_possible" = "no" ]; then
echo "Please uninstall previous version, no update possible from v${SYNOPKG_OLD_PKGVER}.<br>Remember to save your ${OCROOT}/data files before uninstalling."
exit 1
fi
}
service_save ()
{
# Place server in maintenance mode
${OCC} maintenance:mode --on
# Identify data directory for restore
DATADIR=$(realpath "$(${OCC} config:system:get datadirectory)")
WEBROOT=$(realpath "${OCROOT}")
if echo "$DATADIR" | grep -q "^$WEBROOT"; then
echo "${DATADIR#"$WEBROOT/"}" > "${SYNOPKG_TEMP_UPGRADE_FOLDER}/.datadirectory"
fi
# Backup configuration and data
[ -d ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME} ] && ${RM} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}
echo "Backup existing distribution to ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}"
${MKDIR} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}
rsync -aX ${OCROOT}/ ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME} 2>&1
# Backup server database
[ -d ${SYNOPKG_TEMP_UPGRADE_FOLDER}/db_backup ] && ${RM} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/db_backup
echo "Backup existing server database to ${SYNOPKG_TEMP_UPGRADE_FOLDER}/db_backup"
${MKDIR} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/db_backup
${SQLITE} "${DATADIR}/${SYNOPKG_PKGNAME}.db" ".backup '${SYNOPKG_TEMP_UPGRADE_FOLDER}/db_backup/${SYNOPKG_PKGNAME}-dbbackup_$(date +"%Y%m%d").bak'" 2>&1
}
service_restore ()
{
# Validate data directory for restore
if [ -f ${SYNOPKG_TEMP_UPGRADE_FOLDER}/.datadirectory ]; then
DATAPATH="$(cat ${SYNOPKG_TEMP_UPGRADE_FOLDER}/.datadirectory)"
# Data directory inside owncloud directory and needs to be restored
[ -d ${OCROOT}/${DATAPATH} ] && ${RM} ${OCROOT}/${DATAPATH}
echo "Restore previous data directory from ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/${DATAPATH}"
rsync -aX ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/${DATAPATH} ${OCROOT}/ 2>&1
${RM} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/.datadirectory
fi
# Restore the configuration files
echo "Restore previous configuration from ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}"
source="${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/config"
patterns=(
"*config.php"
"*.json"
)
target="${OCROOT}/config"
# Process each pattern of files in the source directory
for pattern in "${patterns[@]}"; do
files=$(find "$source" -type f -name "$pattern")
if [ -n "$files" ]; then
for file in "${files[@]}"; do
file_name=$(basename "$file")
[ -f $target/$file_name ] && ${MV} $target/$file_name $target/$file_name.bak
rsync -aX "$file" "$target/" 2>&1
done
fi
done
if [ -f ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.user.ini ]; then
[ -f ${OCROOT}/.user.ini ] && ${MV} ${OCROOT}/.user.ini ${OCROOT}/.user.ini.bak
rsync -aX ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.user.ini ${OCROOT}/ 2>&1
fi
if [ -f ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.htaccess ]; then
[ -f ${OCROOT}/.htaccess ] && ${MV} ${OCROOT}/.htaccess ${OCROOT}/.htaccess.bak
rsync -aX ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.htaccess ${OCROOT}/ 2>&1
fi
echo "Restore manually installed apps from ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}"
# Migrate manually installed apps from source to destination directories
dirs=(
"${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/apps"
"${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/apps-external"
)
dest="${OCROOT}"
# Process the subdirectories in each of the source directories
for dir in "${dirs[@]}"; do
dir_name=$(basename "$dir")
sub_dirs=()
for item in "$dir"/*; do
if [ -d "$item" ]; then
sub_dirs+=("$item")
fi
done
if [ ! -d "$dest/$dir_name" ]; then
rsync -aX "$dir" "$dest/" 2>&1
elif [ -n "$sub_dirs" ]; then
for sub_dir in "${sub_dirs[@]}"; do
sub_dir_name=$(basename "$sub_dir")
# Check if the subdirectory is missing from the destination
if [ ! -d "$dest/$dir_name/$sub_dir_name" ]; then
rsync -aX "$sub_dir" "$dest/$dir_name/" 2>&1
fi
done
fi
done
# Fix permissions
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
set_owncloud_permissions ${OCROOT}
fi
# Disable maintenance mode
${OCC} maintenance:mode --off
# Finalize upgrade
${OCC} upgrade
DATADIR=$(realpath "$(${OCC} config:system:get datadirectory)")
# Archive backup server database
echo "Archive backup server database to ${DATADIR}"
if [ -d ${SYNOPKG_TEMP_UPGRADE_FOLDER}/db_backup ]; then
if [ -d ${DATADIR}/db_backup ]; then
i=1
while [ -d "${DATADIR}/db_backup.${i}" ]; do
i=$((i+1))
done
while [ $i -gt 1 ]; do
j=$((i-1))
${MV} "${DATADIR}/db_backup.${j}" "${DATADIR}/db_backup.${i}"
i=$j
done
${MV} "${DATADIR}/db_backup" "${DATADIR}/db_backup.1"
fi
rsync -aX ${SYNOPKG_TEMP_UPGRADE_FOLDER}/db_backup ${DATADIR}/ 2>&1
fi
# Remove upgrade backup files
${RM} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}
${RM} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/db_backup
}
service_preupgrade ()
{
:
}
service_postupgrade ()
{
:
}