-
Notifications
You must be signed in to change notification settings - Fork 6
/
remote.sh
454 lines (402 loc) · 14.7 KB
/
remote.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
450
451
452
453
454
#!/bin/bash
# Function to show text in green
print_green() {
local TEXT="$1"
printf "%b%s%b\n" "\e[0;92m" "$TEXT" "\e[0m"
}
# Show how to reach the server
show_startup_info() {
local SHOWN_PORT
if [ "$APACHE_PORT" = 443 ]; then
SHOWN_PORT=8443
else
SHOWN_PORT="$APACHE_PORT"
fi
if [ -z "$TRUSTED_DOMAIN" ]; then
print_green "The server should now be reachable via https://localhost:$SHOWN_PORT/"
else
print_green "The server should now be reachable via https://$TRUSTED_DOMAIN:$SHOWN_PORT/"
fi
}
# Manual install: will skip everything and just start apache
manual_install() {
if [ -n "$MANUAL_INSTALL" ]; then
touch /var/www/server-completed
show_startup_info
exit 0
fi
}
# version_greater A B returns whether A > B
version_greater() {
[ "$(printf '%s\n' "$@" | sort -t '.' -n -k1,1 -k2,2 -k3,3 -k4,4 | head -n 1)" != "$1" ]
}
# Handle node versions
handle_node_version() {
set -x
if [ -f package.json ]; then
local NODE_LINE
NODE_LINE=$(grep '"node":' package.json | head -1)
fi
if [ -n "$NODE_LINE" ] && echo "$NODE_LINE" | grep -q '\^'; then
local NODE_VERSION
NODE_VERSION="$(echo "$NODE_LINE" | grep -oP '\^[0-9]+' | sed 's|\^||' | head -n 1)"
if [ -n "$NODE_VERSION" ] && [ "$NODE_VERSION" -gt 16 ]; then
if [ "$NODE_VERSION" -gt 18 ]; then
# TODO: NODE_VERSION test should check for 18 and not 20 but checking for 20 temporarily so that script is able to proceed
if [ "$NODE_VERSION" -gt 20 ]; then
echo "The node version of $APPID is too new. Need to update the container."
exit 1
fi
set +x
nvm use lts/iron
else
set +x
nvm use lts/hydrogen
fi
else
set +x
nvm use lts/gallium
fi
else
set +x
nvm use lts/gallium
fi
}
# Handle npm versions
handle_npm_version() {
set -x
if [ -f package.json ]; then
local NPM_LINE
NPM_LINE=$(grep '"npm":' package.json | head -1)
fi
if [ -n "$NPM_LINE" ] && echo "$NPM_LINE" | grep -q '\^'; then
local NPM_VERSION
NPM_VERSION="$(echo "$NPM_LINE" | grep -oP '\^[0-9]+' | sed 's|\^||' | head -n 1)"
if [ -n "$NPM_VERSION" ] && [ "$NPM_VERSION" -eq 7 ]; then
set +x
npm i -g npm@latest-7
return
fi
fi
set +x
nvm install-latest-npm
}
install_nextcloud_vue() {
if [ -n "$NEXTCLOUDVUE_BRANCH" ] && [ ! -f "/var/www/nextcloud-vue-completed" ]; then
# Handle case that branch is present in nextcloud repo
if ! echo "$NEXTCLOUDVUE_BRANCH" | grep -q ':'; then
NEXTCLOUDVUE_BRANCH="nextcloud:$NEXTCLOUDVUE_BRANCH"
fi
set -x
local VUE_OWNER="${NEXTCLOUDVUE_BRANCH%%:*}"
local VUE_BRANCH="${NEXTCLOUDVUE_BRANCH#*:}"
local VUE_REPO=nextcloud-vue
if echo "$VUE_BRANCH" | grep -q '@'; then
VUE_REPO="${VUE_BRANCH#*@}"
VUE_BRANCH="${VUE_BRANCH%%@*}"
fi
set +x
mkdir /var/www/nextcloud-vue
cd /var/www/nextcloud-vue || exit
if ! git clone https://github.com/"$VUE_OWNER"/"$VUE_REPO".git --branch "$VUE_BRANCH" --single-branch --depth 1 .; then
echo "Could not clone the requested nextcloud vue branch '$VUE_BRANCH' of '$VUE_OWNER/$VUE_REPO'. Does it exist?"
exit 1
fi
if [ -z "$FULL_INSTANCE_BRANCH" ]; then
# Handle node version
handle_node_version
# Handle npm version
handle_npm_version
echo "Compiling Nextcloud vue..."
if ! npm ci --no-audit || ! npm run dev --if-present; then
echo "Could not compile Nextcloud vue"
exit 1
fi
npm link
fi
touch "/var/www/nextcloud-vue-completed"
fi
}
link_nextcloud_vue() {
if [ -n "$NEXTCLOUDVUE_BRANCH" ]; then
if grep -q '@nextcloud/vue' package.json; then
if ! npm link @nextcloud/vue; then
echo "Could not link nextcloud vue in $1."
exit 1
fi
fi
fi
}
# Handle empty server branch variable
if [ -z "$SERVER_BRANCH" ]; then
export SERVER_BRANCH="nextcloud:master"
fi
# Handle case that branch is present in nextcloud repo
if ! echo "$SERVER_BRANCH" | grep -q ':'; then
export SERVER_BRANCH="nextcloud:$SERVER_BRANCH"
fi
if [ -n "$FULL_INSTANCE_BRANCH" ]; then
export SERVER_BRANCH="nextcloud:$FULL_INSTANCE_BRANCH"
export ACTIVITY_BRANCH="$FULL_INSTANCE_BRANCH"
export BRUTEFORCESETTINGS_BRANCH="$FULL_INSTANCE_BRANCH"
export CIRCLES_BRANCH="$FULL_INSTANCE_BRANCH"
export PDFVIEWER_BRANCH="$FULL_INSTANCE_BRANCH"
export FIRSTRUNWIZARD_BRANCH="$FULL_INSTANCE_BRANCH"
export LOGREADER_BRANCH="$FULL_INSTANCE_BRANCH"
export ANNOUNCEMENTS_BRANCH="$FULL_INSTANCE_BRANCH"
export NOTIFICATIONS_BRANCH="$FULL_INSTANCE_BRANCH"
export PASSWORDPOLICY_BRANCH="$FULL_INSTANCE_BRANCH"
export PHOTOS_BRANCH="$FULL_INSTANCE_BRANCH"
export PRIVACY_BRANCH="$FULL_INSTANCE_BRANCH"
export RECOMMENDATIONS_BRANCH="$FULL_INSTANCE_BRANCH"
export RELATEDRESOURCES_BRANCH="$FULL_INSTANCE_BRANCH"
export SERVERINFO_BRANCH="$FULL_INSTANCE_BRANCH"
export SURVEYCLIENT_BRANCH="$FULL_INSTANCE_BRANCH"
export TEXT_BRANCH="$FULL_INSTANCE_BRANCH"
export TWOFACTORTOTP_BRANCH="$FULL_INSTANCE_BRANCH"
export VIEWER_BRANCH="$FULL_INSTANCE_BRANCH"
fi
# Get NVM code
export NVM_DIR="/var/www/.nvm"
# shellcheck disable=SC1091
. "$NVM_DIR/nvm.sh"
# Get latest changes
if ! [ -f /var/www/server-completed ]; then
set -x
FORK_OWNER="${SERVER_BRANCH%%:*}"
FORK_BRANCH="${SERVER_BRANCH#*:}"
FORK_REPO=server
if echo "$FORK_BRANCH" | grep -q '@'; then
FORK_REPO="${FORK_BRANCH#*@}"
FORK_BRANCH="${FORK_BRANCH%%@*}"
fi
set +x
cd /var/www/nextcloud || exit
if ! git clone https://github.com/"$FORK_OWNER"/"$FORK_REPO".git --branch "$FORK_BRANCH" --single-branch --depth 1 .; then
echo "Could not clone the requested server branch '$FORK_BRANCH' of '$FORK_OWNER/$FORK_REPO'. Does it exist?"
exit 1
fi
# Initiate submodules
git submodule update --init
# Allow to compile the server javascript
if [ -z "$FULL_INSTANCE_BRANCH" ] && [ -n "$COMPILE_SERVER" ]; then
set -x
# shellcheck disable=SC2016
installed_version="$(php -r 'require "/var/www/nextcloud/version.php"; echo implode(".", $OC_Version);')"
if version_greater "$installed_version" "24.0.0.0"; then
# Install Nextcloud vue
install_nextcloud_vue
cd /var/www/nextcloud || exit
# Handle node version
handle_node_version
# Handle npm version
handle_npm_version
echo "Compiling server..."
if ! npm ci --no-audit || ! link_nextcloud_vue server || ! npm run dev --if-present || ! npm run sass --if-present || ! npm run icon --if-present; then
echo "Could not compile server."
exit 1
fi
else
echo "Could not compile server because the version is not higher than 24.0.0"
exit 1
fi
set +x
fi
# Manual install
manual_install
# Install Nextcloud
if ! php -f occ \
maintenance:install \
--database=sqlite \
--admin-user=admin \
--admin-pass=nextcloud; then
echo "Failed to create the instance."
exit 1
fi
php -f occ config:system:set log_type --value "errorlog"
# Set trusted domain if needed
if [ -n "$TRUSTED_DOMAIN" ]; then
if ! php -f occ config:system:set trusted_domains 1 --value="$TRUSTED_DOMAIN"; then
echo "Could not set the trusted domain '$TRUSTED_DOMAIN'"
exit 1
fi
fi
touch /var/www/server-completed
fi
# Manual install
manual_install
# Handle skeleton archive url
if [ -n "$SKELETON_ARCHIVE_URL" ] && ! [ -f "/var/www/skeleton-completed" ]; then
set -x
if ! curl -fsSL "$SKELETON_ARCHIVE_URL" -o /var/www/nextcloud/data/skeletondir.tar.gz; then
echo "Could not get the sekeleton archive url"
exit 1
fi
mkdir -p "/var/www/nextcloud/data/skeletondir"
if ! tar -xf "/var/www/nextcloud/data/skeletondir.tar.gz" -C "/var/www/nextcloud/data/skeletondir"; then
echo "Could not untar the archive. Is it a tar.gz archive?"
exit 1
fi
if ! php -f occ config:system:set skeletondirectory --value="/var/www/nextcloud/data/skeletondir"; then
echo "Could not set the skeletondir"
exit 1
fi
if ! rm -r /var/www/nextcloud/data/admin/files/*; then
echo "Could not remove the default admin files"
exit 1
fi
if ! cp -R /var/www/nextcloud/data/skeletondir/* /var/www/nextcloud/data/admin/files/; then
echo "Could not copy the files to the admin user"
exit 1
fi
if ! php -f occ files:scan admin; then
echo "Could not scan the new files for the admin user."
exit 1
fi
set +x
touch /var/www/skeleton-completed
fi
# Install and enable apps
install_enable_app() {
# Variables
local BRANCH="$1"
local APPID="$2"
# Logic
if [ -n "$BRANCH" ] && ! [ -f "/var/www/$APPID-completed" ]; then
# Go into apps directory
cd /var/www/nextcloud/apps || exit
# Remove app directory
if [ -d ./"$APPID" ]; then
php -f ../occ app:disable "$APPID"
rm -r ./"$APPID"
fi
# Handle case that branch is present in nextcloud repo
if ! echo "$BRANCH" | grep -q ':'; then
BRANCH="nextcloud:$BRANCH"
fi
# Clone repo
set -x
local APP_OWNER="${BRANCH%%:*}"
local APP_BRANCH="${BRANCH#*:}"
local APP_REPO="$APPID"
if echo "$APP_BRANCH" | grep -q '@'; then
APP_REPO="${APP_BRANCH#*@}"
APP_BRANCH="${APP_BRANCH%%@*}"
fi
set +x
if ! git clone https://github.com/"$APP_OWNER"/"$APP_REPO".git --branch "$APP_BRANCH" --single-branch --depth 1 "$APPID"; then
echo "Could not clone the requested branch '$APP_BRANCH' of the $APPID app of '$APP_OWNER/$APP_REPO'. Does it exist?"
exit 1
fi
# Install Nextcloud vue
install_nextcloud_vue
cd /var/www/nextcloud/apps/ || exit
# Go into app directory
cd ./"$APPID" || exit
# if [ "$APPID" = mail ]; then
# wget https://getcomposer.org/download/1.10.22/composer.phar
# chmod +x ./composer.phar
# if ! ./composer.phar install --no-dev; then
# echo "Could not install composer dependencies of the mail app."
# exit 1
# fi
# Install composer dependencies
if [ -f composer.json ]; then
if ! composer install --no-dev; then
echo "Could not install composer dependencies of the $APPID app."
exit 1
fi
fi
if [ -z "$FULL_INSTANCE_BRANCH" ]; then
# Handle node version
handle_node_version
# Handel npm version
handle_npm_version
# Compile apps
if [ -f package.json ]; then
# Link nextcloud vue
link_nextcloud_vue "$APPID"
if ! npm ci --no-audit || ! link_nextcloud_vue "$APPID" || ! npm run dev --if-present; then
echo "Could not compile the $APPID app."
exit 1
fi
fi
fi
# Go into occ directory
cd /var/www/nextcloud || exit
# Enable app
if ! php -f occ app:enable "$APPID"; then
echo "Could not enable the $APPID app."
exit 1
fi
# The app was enabled
touch "/var/www/$APPID-completed"
fi
}
# Compatible apps
install_enable_app "$ACTIVITY_BRANCH" activity
install_enable_app "$ANNOUNCEMENTS_BRANCH" nextcloud_announcements
install_enable_app "$APPROVAL_BRANCH" approval
install_enable_app "$BOOKMARKS_BRANCH" bookmarks
install_enable_app "$BRUTEFORCESETTINGS_BRANCH" bruteforcesettings
install_enable_app "$CALENDAR_BRANCH" calendar
install_enable_app "$CIRCLES_BRANCH" circles
install_enable_app "$CONTACTS_BRANCH" contacts
install_enable_app "$DECK_BRANCH" deck
install_enable_app "$DOWNLOADLIMIT_BRANCH" files_downloadlimit
install_enable_app "$E2EE_BRANCH" end_to_end_encryption
install_enable_app "$FILES_LOCK_BRANCH" files_lock
install_enable_app "$FIRSTRUNWIZARD_BRANCH" firstrunwizard
# shellcheck disable=SC2153
install_enable_app "$FORMS_BRANCH" forms
install_enable_app "$GROUPFOLDERS_BRANCH" groupfolders
install_enable_app "$GUESTS_BRANCH" guests
install_enable_app "$IMPERSONATE_BRANCH" impersonate
install_enable_app "$INTEGRATIONGITHUB_BRANCH" integration_github
install_enable_app "$ISSUTEMPLATE_BRANCH" issuetemplate
install_enable_app "$LOGREADER_BRANCH" logreader
install_enable_app "$MAIL_BRANCH" mail
# shellcheck disable=SC2153
install_enable_app "$MAPS_BRANCH" maps
install_enable_app "$NEWS_BRANCH" news
install_enable_app "$NOTES_BRANCH" notes
install_enable_app "$NOTIFICATIONS_BRANCH" notifications
install_enable_app "$OCS_API_VIEWER_BRANCH" ocs_api_viewer
install_enable_app "$PASSWORDPOLICY_BRANCH" password_policy
install_enable_app "$PDFVIEWER_BRANCH" files_pdfviewer
install_enable_app "$PHOTOS_BRANCH" photos
install_enable_app "$POLLS_BRANCH" polls
install_enable_app "$PRIVACY_BRANCH" privacy
install_enable_app "$RECOMMENDATIONS_BRANCH" recommendations
install_enable_app "$RELATEDRESOURCES_BRANCH" related_resources
install_enable_app "$RIGHTCLICK_BRANCH" files_rightclick
install_enable_app "$SERVERINFO_BRANCH" serverinfo
install_enable_app "$SURVEYCLIENT_BRANCH" survey_client
install_enable_app "$SUSPICIOUSLOGIN_BRANCH" suspicious_login
install_enable_app "$TALK_BRANCH" spreed
install_enable_app "$TASKS_BRANCH" tasks
install_enable_app "$TEXT_BRANCH" text
install_enable_app "$TWOFACTORWEBAUTHN_BRANCH" twofactor_webauthn
install_enable_app "$TWOFACTORTOTP_BRANCH" twofactor_totp
install_enable_app "$VIEWER_BRANCH" viewer
install_enable_app "$ZIPPER_BRANCH" files_zip
# Clear cache
cd /var/www/nextcloud || exit
if ! php -f occ maintenance:repair; then
echo "Could not clear the cache"
exit 1
fi
# Set Xdebug options
if [ -n "$XDEBUG_MODE" ]; then
sed -i 's/^xdebug.mode\s*=\s*.*/xdebug.mode='"$XDEBUG_MODE"'/g' /usr/local/etc/php/conf.d/xdebug.ini
fi
# Set loglevel
if [ -n "$NEXTCLOUD_LOGLEVEL" ]; then
php -f occ config:system:set loglevel --value "$NEXTCLOUD_LOGLEVEL" --type int
if [ "$NEXTCLOUD_LOGLEVEL" = 0 ]; then
php -f occ config:system:set debug --value true --type bool
fi
fi
# Show how to reach the server
show_startup_info
print_green "You can log in with the user 'admin' and its password 'nextcloud'"