Repository navigation
Expand file tree
/
Copy pathStackScript
More file actions
813 lines (737 loc) · 30.3 KB
/
Copy pathStackScript
File metadata and controls
813 lines (737 loc) · 30.3 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#!/bin/bash
#
# Plone in a Box(TM) -- provision a single Ubuntu server running Plone 6
# (backend + Volto frontend + PostgreSQL) behind nginx.
#
# Used as:
# - a Linode StackScript (the UDF lines below become form fields),
# - EC2 user data via src/aws/main.tf, and
# - DigitalOcean Droplet user data via src/digitalocean/main.tf
# (both export the same variables first).
#
# Tested target: Ubuntu 26.04 or 24.04 LTS (amd64 or arm64). Run as root.
#
# <UDF name="ADMIN_PASSWORD" label="Plone admin password, at least 12 characters. Leave empty to have one generated and saved on the server" default="" />
# <UDF name="DOMAIN" label="Domain name for the site, optional. DNS must already point here to get HTTPS" default="" />
# <UDF name="LETSENCRYPT_EMAIL" label="Email for Lets Encrypt certificate notices, required if a domain is set" default="" />
# <UDF name="SITE_TIMEZONE" label="Site time zone" default="UTC" example="America/Chicago" />
# <UDF name="PLONE_VERSION" label="Plone backend image tag" default="6.2" />
# <UDF name="VOLTO_VERSION" label="Volto frontend image tag" default="19" />
# <UDF name="ADDONS" label="Backend add-ons, optional: Python package names separated by spaces, for example collective.easyform. Activate them afterwards in Site Setup, Add-ons" default="" />
# <UDF name="FRONTEND_IMAGE" label="Frontend image, optional: a ready-made Plone in a Box image such as ghcr.io/collective/plone-in-a-box-frontend:19-vlt (Volto Light Theme), or your own. Empty: the standard Volto image" default="" />
#
# After provisioning finishes, open the site and log in as "admin" with the
# password you chose. If you left it empty, a password is generated: log in over
# SSH as root and read /root/plone-credentials.txt.
set -Eeuo pipefail
export DEBIAN_FRONTEND=noninteractive
# Keep a copy of all output in one place on every provider.
LOG_FILE=/var/log/plone-in-a-box.log
exec > >(tee -a "$LOG_FILE") 2>&1
LOG_TEE_PID=$!
# On exit, wait for tee to write the last lines: when setup resumes after a
# restart, systemd stops everything in the service as soon as this script ends.
flush_log() { exec >/dev/null 2>&1; wait "$LOG_TEE_PID" 2>/dev/null || true; }
trap flush_log EXIT
ADMIN_PASSWORD="${ADMIN_PASSWORD:-}"
DOMAIN="${DOMAIN:-}"
LETSENCRYPT_EMAIL="${LETSENCRYPT_EMAIL:-}"
SITE_TIMEZONE="${SITE_TIMEZONE:-UTC}"
PLONE_VERSION="${PLONE_VERSION:-6.2}"
VOLTO_VERSION="${VOLTO_VERSION:-19}"
POSTGRES_VERSION="${POSTGRES_VERSION:-17}"
# Backend add-ons: Python packages that the plone/plone-backend image installs
# with pip each time the backend container starts (its ADDONS variable).
# Split on any whitespace, without expanding wildcards such as "==1.*".
read -ra ADDON_LIST <<< "$(printf '%s' "${ADDONS:-}" | tr '\n\r\t' ' ')"
ADDONS="${ADDON_LIST[*]}"
# Frontend image: replaces plone/plone-frontend:${VOLTO_VERSION}. Ready-made
# Plone in a Box images say in their labels which backend add-ons and site
# behaviors they need; setup installs and activates those too.
FRONTEND_IMAGE="$(printf '%s' "${FRONTEND_IMAGE:-}" | tr -d ' \t\r\n')"
APP_DIR=/opt/plone
CREDENTIALS_FILE=/root/plone-credentials.txt
# Used when setup restarts the server once to finish installing updates and
# then continues after the restart (see "Restart once" below).
RESUME_DIR=/root/.plone-in-a-box-resume
RESUME_UNIT=/etc/systemd/system/plone-in-a-box-resume.service
log() { echo "[plone-in-a-box] $*"; }
# A Python package name with optional extras and version specifiers, such as
# collective.easyform or collective.easyform==4.2.0: nothing pip would read as
# an option, a URL or a file.
valid_addon() {
[[ "$1" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*(\[[A-Za-z0-9._,-]+\])?([~=!<>]=?=?[A-Za-z0-9.*+!_-]+)?(,[~=!<>]=?=?[A-Za-z0-9.*+!_-]+)*$ ]]
}
# A container image reference: [registry[:port]/]path[:tag][@sha256:digest].
valid_image() {
[[ "$1" =~ ^[a-z0-9]+([._-][a-z0-9]+)*(:[0-9]+)?(/[a-z0-9]+([._-]+[a-z0-9]+)*)*(:[A-Za-z0-9_][A-Za-z0-9._-]{0,127})?(@sha256:[a-f0-9]{64})?$ ]]
}
# If this run is the continuation after that restart, make sure it only
# happens once, and remove the saved settings (they include the password).
# The unit file itself stays until the end of setup: removing it and
# reloading systemd while this run is going would make systemd forget the
# service's settings.
if [[ -e "$RESUME_UNIT" ]]; then
log "Continuing setup after the restart"
systemctl disable plone-in-a-box-resume.service || true
rm -f "$RESUME_DIR/settings"
fi
# --- Setup progress page -----------------------------------------------------
# While provisioning runs, nginx serves a page at the server's address that
# shows the current step and refreshes itself; when Plone is ready, the next
# refresh shows the site. If provisioning fails, the page says so.
PROGRESS_DIR=/var/www/plone-in-a-box-setup
PROGRESS_STEPS=(
"Preparing the server"
"Installing Docker"
"Securing the server"
"Downloading Plone (the longest step)"
"Creating your Plone site"
"Starting Plone"
"Finishing up"
)
CURRENT_STEP=0
STARTED_AT="${STARTED_AT:-$(date -u +"%H:%M UTC")}"
write_progress_page() {
# write_progress_page <title> <message html> <body html> <refresh seconds, 0 for none>
[[ -d "$PROGRESS_DIR" ]] || return 0
local refresh=""
if (( $4 > 0 )); then
refresh="<meta http-equiv=\"refresh\" content=\"$4\">"
fi
cat > "$PROGRESS_DIR/index.html.tmp" <<EOF
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8">${refresh}
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>$1 - Plone in a Box</title>
<style>
body{font-family:system-ui,-apple-system,sans-serif;max-width:36rem;margin:3rem auto;padding:0 1rem;color:#1a1a1a;line-height:1.5}
h1{font-size:1.5rem;margin-bottom:.5rem}
ol{list-style:none;padding:0;margin:1.5rem 0}
li{margin:.5rem 0;padding-left:1.75rem;position:relative}
li span{position:absolute;left:0;width:1.25rem;text-align:center}
.done{color:#2e7d32}.now{font-weight:600}.todo{color:#888}.failed{color:#c62828;font-weight:600}
.small{color:#666;font-size:.875rem;margin-top:2rem}
code{background:#f2f2f2;padding:.1rem .3rem;border-radius:3px}
</style></head><body>
<h1>$1</h1>
<p>$2</p>
$3
<p class="small">Plone in a Box™ · started ${STARTED_AT}</p>
</body></html>
EOF
mv "$PROGRESS_DIR/index.html.tmp" "$PROGRESS_DIR/index.html"
}
step_list() {
# step_list <state of the current step: now|failed>
local i html="" cls mark
for i in "${!PROGRESS_STEPS[@]}"; do
if (( i + 1 < CURRENT_STEP )); then
cls="done"; mark="✓"
elif (( i + 1 == CURRENT_STEP )); then
cls=$1
if [[ "$1" == failed ]]; then mark="✗"; else mark="→"; fi
else
cls="todo"; mark=""
fi
html+="<li class=\"${cls}\"><span>${mark}</span>${PROGRESS_STEPS[i]}</li>"
done
echo "<ol>${html}</ol>"
}
progress() {
# progress <step number, starting at 1>
CURRENT_STEP=$1
log "Step ${CURRENT_STEP}/${#PROGRESS_STEPS[@]}: ${PROGRESS_STEPS[CURRENT_STEP-1]}"
write_progress_page "Your Plone site is being set up" \
"This usually takes about 10 minutes. This page refreshes every 15 seconds and turns into your site when it's ready." \
"$(step_list now)" 15
}
on_error() {
log "Setup failed at line $1 (step ${CURRENT_STEP})"
write_progress_page "Setup didn't finish" \
"Something went wrong while setting up Plone. Often it's a temporary download problem: deleting this server and deploying a new one usually fixes it. To see what happened, log in to the server as root and look at <code>${LOG_FILE}</code>." \
"$(step_list failed)" 0
}
trap 'on_error $LINENO' ERR
wait_for() {
# wait_for <description> <timeout-seconds> <command...>
local what=$1 timeout=$2 waited=0
shift 2
until "$@" >/dev/null 2>&1; do
if (( waited >= timeout )); then
log "Timed out after ${timeout}s waiting for ${what}"
return 1
fi
sleep 5
waited=$(( waited + 5 ))
done
log "${what} is ready"
}
# --- System packages ---------------------------------------------------------
# Install nginx first, so the progress page is up within a minute or two.
apt-get update -y
apt-get install -y nginx
mkdir -p "$PROGRESS_DIR"
cat > /etc/nginx/sites-available/plone-in-a-box-setup <<EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root ${PROGRESS_DIR};
add_header Cache-Control "no-store" always;
location / {
try_files /index.html =404;
}
}
EOF
rm -f /etc/nginx/sites-enabled/default
ln -sf /etc/nginx/sites-available/plone-in-a-box-setup /etc/nginx/sites-enabled/plone-in-a-box-setup
progress 1
nginx -t
systemctl reload nginx || systemctl restart nginx
log "Updating system packages"
# full-upgrade, unlike upgrade, also installs updates that need new packages
# (such as a new kernel).
apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold full-upgrade
apt-get install -y ca-certificates curl gnupg ufw openssl
# --- Swap (Plone + Volto + PostgreSQL do not fit comfortably in 1 GB) -------
# Add a 2 GB swap file when memory plus existing swap is under about 3 GB.
# Some providers include a small swap disk (Linode: 512 MB), which isn't
# enough on its own for a 1 GB server.
mem_kb=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
swap_kb=$(awk '/SwapTotal/ {print $2}' /proc/meminfo)
if (( mem_kb + swap_kb < 3000000 )) && [[ ! -e /swapfile ]]; then
log "Adding a 2 GB swap file"
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
grep -q '^/swapfile ' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab
fi
# --- Restart once if the updates need it -------------------------------------
# A new kernel (and some other updates) only takes effect after a restart.
# Restart now, before Plone is installed, and continue this script
# automatically afterwards, so the finished site never goes down for it.
# nginx keeps showing the progress page across the restart.
if [[ -f /var/run/reboot-required && ! -e "$RESUME_DIR/restarted" ]]; then
if [[ -f "$0" ]]; then
log "Restarting the server to finish installing updates; setup continues automatically afterwards"
install -d -m 700 "$RESUME_DIR"
cp "$0" "$RESUME_DIR/setup.sh"
(
umask 077
for var in ADMIN_PASSWORD DOMAIN LETSENCRYPT_EMAIL SITE_TIMEZONE PLONE_VERSION VOLTO_VERSION POSTGRES_VERSION ADDONS FRONTEND_IMAGE STARTED_AT; do
printf 'export %s=%q\n' "$var" "${!var}"
done > "$RESUME_DIR/settings"
)
touch "$RESUME_DIR/restarted"
cat > "$RESUME_UNIT" <<EOF
[Unit]
Description=Continue Plone in a Box setup after the restart
Wants=network-online.target
After=network-online.target
[Service]
# simple, not oneshot: setup takes several minutes, and systemd puts a time
# limit on starting a oneshot service.
Type=simple
ExecStart=/bin/bash -c '. ${RESUME_DIR}/settings && exec /bin/bash ${RESUME_DIR}/setup.sh'
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable plone-in-a-box-resume.service
write_progress_page "Restarting the server" \
"The server is restarting to finish installing updates, and setup then continues by itself. This page refreshes every 15 seconds; if your browser shows an error while the server restarts, reload the page in a minute." \
"$(step_list now)" 15
trap - ERR
systemctl reboot
exit 0
else
log "Updates need a restart, but this script can't be saved to continue afterwards; restart the server after setup"
fi
fi
# --- Docker Engine + Compose plugin (official repository) -------------------
progress 2
log "Installing Docker"
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
# shellcheck disable=SC1091
. /etc/os-release
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu ${VERSION_CODENAME} stable" \
> /etc/apt/sources.list.d/docker.list
apt-get update -y
apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
systemctl enable --now docker
# --- Firewall ----------------------------------------------------------------
# Only SSH and HTTP(S) are reachable from outside. The containers publish
# their ports on 127.0.0.1 only, so Docker's iptables rules don't bypass this.
progress 3
log "Configuring firewall"
ufw allow OpenSSH
ufw allow 'Nginx Full'
ufw --force enable
# --- Secrets -----------------------------------------------------------------
mkdir -p "$APP_DIR"
chmod 700 "$APP_DIR"
if [[ ! -f "$APP_DIR/.env" ]]; then
log "Generating passwords"
POSTGRES_PASSWORD=$(openssl rand -hex 24)
ADMIN_PASSWORD_CHOSEN=0
PASSWORD_NOTE=""
if [[ -n "$ADMIN_PASSWORD" ]]; then
if (( ${#ADMIN_PASSWORD} >= 12 )); then
ADMIN_PASSWORD_CHOSEN=1
else
log "The admin password given is shorter than 12 characters; generating one instead"
PASSWORD_NOTE="The password entered at deploy time was shorter than 12 characters, so this one was generated instead."
ADMIN_PASSWORD=""
fi
fi
if [[ -z "$ADMIN_PASSWORD" ]]; then
ADMIN_PASSWORD=$(openssl rand -base64 18 | tr -d '/+=')
fi
ADDONS_NOTE=""
for addon in "${ADDON_LIST[@]}"; do
if ! valid_addon "$addon"; then
log "Not installing the backend add-ons: \"$addon\" isn't a Python package name"
ADDONS_NOTE="The add-ons ($ADDONS) were not installed: \"$addon\" isn't a Python package name."
ADDONS=""
break
fi
done
FRONTEND_NOTE=""
if [[ -n "$FRONTEND_IMAGE" ]] && ! valid_image "$FRONTEND_IMAGE"; then
log "Not using the frontend image \"$FRONTEND_IMAGE\": it isn't an image name; using the standard Volto image"
FRONTEND_NOTE="The frontend image \"$FRONTEND_IMAGE\" isn't an image name, so the site uses the standard Volto image."
FRONTEND_IMAGE=""
fi
umask 077
cat > "$APP_DIR/.env" <<EOF
PLONE_VERSION=${PLONE_VERSION}
VOLTO_VERSION=${VOLTO_VERSION}
POSTGRES_VERSION=${POSTGRES_VERSION}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ADDONS='${ADDONS}'
FRONTEND_IMAGE='${FRONTEND_IMAGE}'
EOF
umask 022
NEW_ADMIN_PASSWORD_SET=0
else
log "Re-using existing $APP_DIR/.env"
ADMIN_PASSWORD=""
ADMIN_PASSWORD_CHOSEN=0
PASSWORD_NOTE=""
NEW_ADMIN_PASSWORD_SET=1
ADDONS_NOTE=""
ADDONS=$(sed -n "s/^ADDONS='\(.*\)'\$/\1/p" "$APP_DIR/.env")
FRONTEND_NOTE=""
FRONTEND_IMAGE=$(sed -n "s/^FRONTEND_IMAGE='\(.*\)'\$/\1/p" "$APP_DIR/.env")
fi
# Docker Compose prefers an exported variable over the .env file; .env is what
# the site keeps using after setup, so let Compose read it from there.
export -n ADDONS FRONTEND_IMAGE
# --- Docker Compose stack ----------------------------------------------------
cat > "$APP_DIR/docker-compose.yml" <<'EOF'
name: plone
services:
frontend:
# FRONTEND_IMAGE in .env replaces the standard Volto image.
image: ${FRONTEND_IMAGE:-plone/plone-frontend:${VOLTO_VERSION}}
environment:
RAZZLE_INTERNAL_API_PATH: http://backend:8080/Plone
restart: unless-stopped
ports:
- "127.0.0.1:3000:3000"
depends_on:
backend:
condition: service_healthy
backend:
image: plone/plone-backend:${PLONE_VERSION}
environment:
RELSTORAGE_DSN: "dbname='plone' user='plone' host='db' password='${POSTGRES_PASSWORD}'"
# Add-ons are installed with pip each time the container starts, held to
# the versions this Plone release was tested with.
ADDONS: ${ADDONS:-}
PIP_PARAMS: "-c /app/constraints.txt --no-cache-dir"
# The image's own health check, but with time for installing add-ons
# before Plone starts: failed checks only count after five minutes.
healthcheck:
start_period: 5m
restart: unless-stopped
ports:
- "127.0.0.1:8080:8080"
volumes:
# The image ships an "inituser" file containing admin:admin, and Zope
# re-applies it on every start while there is only one Zope user, which
# would silently reset the admin password whenever the container is
# recreated. An empty file disables that.
- ./inituser:/app/inituser:ro
depends_on:
db:
condition: service_healthy
db:
image: postgres:${POSTGRES_VERSION}
environment:
POSTGRES_USER: plone
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: plone
restart: unless-stopped
volumes:
- data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U plone -d plone"]
interval: 5s
timeout: 5s
retries: 20
# No published ports: PostgreSQL is only reachable from the other containers.
volumes:
data: {}
EOF
: > "$APP_DIR/inituser"
# Creates the Zope "admin" user or resets its password. Before the Plone site
# exists, the root user folder is a plain Zope User Folder; creating the site
# replaces it with a PluggableAuthService (PAS), whose generic user-folder API
# doesn't list or update users reliably, so PAS is handled through its user
# manager plugin. The new password is verified before committing.
cat > "$APP_DIR/set_admin_password.py" <<'EOF'
import os
import sys
import transaction
app = globals()["app"]
uf = app.acl_users
password = os.environ["NEW_ADMIN_PASSWORD"]
if hasattr(uf, "plugins"):
# PluggableAuthService
managers = [
p for p in uf.objectValues()
if hasattr(p, "updateUserPassword") and hasattr(p, "listUserIds")
]
if not managers:
sys.exit("No user manager plugin found in the root acl_users")
manager = managers[0]
if "admin" in manager.listUserIds():
manager.updateUserPassword("admin", password)
else:
manager.addUser("admin", "admin", password)
for roles in uf.objectValues():
if hasattr(roles, "assignRoleToPrincipal"):
roles.assignRoleToPrincipal("Manager", "admin")
ok = manager.authenticateCredentials({"login": "admin", "password": password})
else:
# Plain Zope User Folder
if "admin" in uf.getUserNames():
uf._doChangeUser("admin", password, ["Manager"], [])
else:
uf._doAddUser("admin", password, ["Manager"], [])
ok = uf.authenticate("admin", password, None) is not None
if not ok:
transaction.abort()
sys.exit("The new admin password could not be verified; nothing was changed")
transaction.commit()
print("Zope admin user is set up")
EOF
# Activates the add-ons a frontend image needs and enables its behaviors on the
# Plone Site type, so its settings (logo, colours, footer) appear on the site.
cat > "$APP_DIR/activate_frontend_addons.py" <<'EOF'
import os
import sys
import transaction
from AccessControl.SecurityManagement import newSecurityManager
from plone.base.utils import get_installer
from zope.component.hooks import setSite
app = globals()["app"]
site = app["Plone"]
setSite(site)
admin = app.acl_users.getUser("admin") or site.acl_users.getUser("admin")
newSecurityManager(None, admin.__of__(app.acl_users))
installer = get_installer(site)
for name in os.environ.get("SITE_ADDONS", "").split():
if not installer.is_product_installed(name):
if not installer.install_product(name):
transaction.abort()
sys.exit(f"Couldn't activate {name}")
print(f"Activated {name}")
fti = site.portal_types["Plone Site"]
behaviors = list(fti.behaviors)
for name in os.environ.get("SITE_BEHAVIORS", "").split():
if name not in behaviors:
behaviors.append(name)
fti.behaviors = tuple(behaviors)
transaction.commit()
print("Frontend add-ons are set up")
EOF
# Command for resetting the admin password later: sudo plone-reset-admin-password
cat > /usr/local/bin/plone-reset-admin-password <<'EOF'
#!/bin/bash
# Reset the password of the Plone (Zope) "admin" user.
# Usage: sudo plone-reset-admin-password
# (asks for the new password; press Enter to have one generated)
set -euo pipefail
if [[ $EUID -ne 0 ]]; then
echo "Please run this with sudo: sudo plone-reset-admin-password" >&2
exit 1
fi
if [[ ! -t 0 ]]; then
echo "This command asks for the new password, so it needs a terminal." >&2
echo "Over SSH, add -t: ssh -t root@<server> plone-reset-admin-password" >&2
exit 1
fi
read -rsp "New admin password (at least 12 characters, or press Enter to generate one): " NEW_PASSWORD
echo
if [[ -z "$NEW_PASSWORD" ]]; then
NEW_PASSWORD=$(openssl rand -base64 18 | tr -d '/+=')
GENERATED=1
else
if (( ${#NEW_PASSWORD} < 12 )); then
echo "That password is shorter than 12 characters; nothing was changed." >&2
exit 1
fi
read -rsp "Type it again: " AGAIN
echo
if [[ "$NEW_PASSWORD" != "$AGAIN" ]]; then
echo "The passwords don't match; nothing was changed." >&2
exit 1
fi
GENERATED=0
fi
if ! OUTPUT=$(NEW_ADMIN_PASSWORD="$NEW_PASSWORD" docker compose --project-directory /opt/plone \
run --rm --no-deps -e NEW_ADMIN_PASSWORD \
-v /opt/plone/set_admin_password.py:/app/scripts/set_admin_password.py:ro \
backend run /app/scripts/set_admin_password.py 2>&1) \
|| ! grep -q "Zope admin user is set up" <<<"$OUTPUT"; then
echo "Changing the password failed; nothing was changed. Last lines of output:" >&2
tail -5 <<<"$OUTPUT" >&2
exit 1
fi
if [[ "$GENERATED" == 1 ]]; then
echo "The admin password is now: $NEW_PASSWORD"
echo "Store it in a password manager."
else
echo "The admin password has been changed."
fi
EOF
chmod 755 /usr/local/bin/plone-reset-admin-password
cd "$APP_DIR"
compose() { docker compose --project-directory "$APP_DIR" "$@"; }
progress 4
# A frontend image that can't be pulled (misspelled, private, gone) is left
# out, so the site still starts with the standard Volto image. A ready-made
# image's labels name the backend add-ons and site behaviors it needs.
IMAGE_ADDONS="" IMAGE_BEHAVIORS="" IMAGE_ACTIVATED=0
if [[ -n "$FRONTEND_IMAGE" ]]; then
log "Pulling the frontend image ${FRONTEND_IMAGE}"
if docker pull --quiet "$FRONTEND_IMAGE"; then
label() { docker image inspect -f "{{index .Config.Labels \"org.plone-in-a-box.$1\"}}" "$FRONTEND_IMAGE" 2>/dev/null || true; }
for addon in $(label backend-addons); do
if valid_addon "$addon"; then
IMAGE_ADDONS+="${IMAGE_ADDONS:+ }$addon"
fi
done
# Not again if setup runs a second time with the same .env.
missing=""
for addon in $IMAGE_ADDONS; do
[[ " $ADDONS " == *" $addon "* ]] || missing+="${missing:+ }$addon"
done
for behavior in $(label site-behaviors); do
if [[ "$behavior" =~ ^[A-Za-z0-9._-]+$ ]]; then
IMAGE_BEHAVIORS+="${IMAGE_BEHAVIORS:+ }$behavior"
fi
done
if [[ -n "$IMAGE_ADDONS" ]]; then
log "The frontend image needs the backend add-ons ${IMAGE_ADDONS}"
if [[ -n "$missing" ]]; then
ADDONS="${ADDONS:+$ADDONS }${missing}"
sed -i "s/^ADDONS=.*/ADDONS='${ADDONS}'/" "$APP_DIR/.env"
fi
fi
else
log "The frontend image ${FRONTEND_IMAGE} couldn't be pulled; using the standard Volto image"
FRONTEND_NOTE="The frontend image ${FRONTEND_IMAGE} couldn't be pulled, so the site uses the standard Volto image. The reason is in /var/log/plone-in-a-box.log."
FRONTEND_IMAGE=""
sed -i "s/^FRONTEND_IMAGE=.*/FRONTEND_IMAGE=''/" "$APP_DIR/.env"
fi
fi
log "Pulling images (Plone ${PLONE_VERSION}, ${FRONTEND_IMAGE:-Volto ${VOLTO_VERSION}}, PostgreSQL ${POSTGRES_VERSION})"
compose pull --quiet
# A misspelled or incompatible add-on would stop the backend from starting,
# so try installing the add-ons in a throwaway container first, and leave
# them out if that fails.
if [[ -n "$ADDONS" ]]; then
log "Checking that the backend add-ons install: ${ADDONS}"
if ! compose run --rm --no-deps backend true; then
log "The backend add-ons couldn't be installed (see the pip messages above); continuing without them"
ADDONS_NOTE="The add-ons ($ADDONS) couldn't be installed, so the site runs without them. The reason is in /var/log/plone-in-a-box.log."
ADDONS=""
sed -i "s/^ADDONS=.*/ADDONS=''/" "$APP_DIR/.env"
# The frontend image needs its backend half; without it, use the standard one.
if [[ -n "$IMAGE_ADDONS" ]]; then
log "The frontend image ${FRONTEND_IMAGE} needs those add-ons; using the standard Volto image"
FRONTEND_NOTE="The frontend image ${FRONTEND_IMAGE} needs backend add-ons that couldn't be installed, so the site uses the standard Volto image."
FRONTEND_IMAGE="" IMAGE_ADDONS="" IMAGE_BEHAVIORS=""
sed -i "s/^FRONTEND_IMAGE=.*/FRONTEND_IMAGE=''/" "$APP_DIR/.env"
fi
fi
fi
progress 5
log "Starting PostgreSQL"
compose up -d --wait db
if [[ "$NEW_ADMIN_PASSWORD_SET" == 0 ]]; then
log "Setting the Zope admin password"
NEW_ADMIN_PASSWORD="$ADMIN_PASSWORD" compose run --rm --no-deps \
-e NEW_ADMIN_PASSWORD -e ADDONS= \
-v "$APP_DIR/set_admin_password.py:/app/scripts/set_admin_password.py:ro" \
backend run /app/scripts/set_admin_password.py
fi
log "Creating the Plone site (skipped if it already exists)"
compose run --rm --no-deps -e ADDONS= \
-e SITE_ID=Plone \
-e TIMEZONE="$SITE_TIMEZONE" \
backend create-site
if [[ "$NEW_ADMIN_PASSWORD_SET" == 0 && ( -n "$IMAGE_ADDONS" || -n "$IMAGE_BEHAVIORS" ) ]]; then
log "Activating what the frontend image needs: ${IMAGE_ADDONS} ${IMAGE_BEHAVIORS}"
site_addons=""
for addon in $IMAGE_ADDONS; do site_addons+=" ${addon%%[[~=!<>]*}"; done
if compose run --rm --no-deps -e SITE_ADDONS="$site_addons" -e SITE_BEHAVIORS="$IMAGE_BEHAVIORS" \
-v "$APP_DIR/activate_frontend_addons.py:/app/scripts/activate_frontend_addons.py:ro" \
backend run /app/scripts/activate_frontend_addons.py; then
IMAGE_ACTIVATED=1
else
log "Activating them failed (see above); activate them in Site Setup > Add-ons and enable the behaviors in Site Setup > Content Types > Plone Site"
FRONTEND_NOTE="Activating the add-ons the frontend image needs failed: activate ${IMAGE_ADDONS} in Site Setup > Add-ons, and enable ${IMAGE_BEHAVIORS} in Site Setup > Content Types > Plone Site > Behaviors."
fi
fi
# Add-ons left for the admin to activate: not those setup activated for the
# frontend image.
ADDONS_TODO="$ADDONS"
if [[ "$IMAGE_ACTIVATED" == 1 ]]; then
ADDONS_TODO=""
for addon in $ADDONS; do
[[ " $IMAGE_ADDONS " == *" $addon "* ]] || ADDONS_TODO+="${ADDONS_TODO:+ }$addon"
done
fi
if [[ -z "$ADDONS" ]]; then
ADDONS_LINE=""
elif [[ -z "$ADDONS_TODO" ]]; then
ADDONS_LINE="Backend add-ons: ${ADDONS} (activated for the frontend image)"
elif [[ "$ADDONS_TODO" == "$ADDONS" ]]; then
ADDONS_LINE="Backend add-ons: ${ADDONS} (activate them in Site Setup > Add-ons)"
else
ADDONS_LINE="Backend add-ons: ${ADDONS} (activate ${ADDONS_TODO} in Site Setup > Add-ons; the others are activated for the frontend image)"
fi
progress 6
log "Starting Plone"
compose up -d
wait_for "Plone backend" 300 curl -fsS http://127.0.0.1:8080/ok
wait_for "Volto frontend" 300 curl -fsS -o /dev/null http://127.0.0.1:3000/
# --- nginx -------------------------------------------------------------------
progress 7
log "Configuring nginx"
SERVER_NAME="${DOMAIN:-_}"
rm -f /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/plone-in-a-box-setup
cat > /etc/nginx/sites-available/plone <<EOF
upstream backend {
server 127.0.0.1:8080;
}
upstream frontend {
server 127.0.0.1:3000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name ${SERVER_NAME};
client_max_body_size 100m;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
# REST API calls go straight to the Plone backend.
location ~ /\+\+api\+\+(\$|/.*) {
rewrite ^/\+\+api\+\+(\$|/.*) /VirtualHostBase/\$scheme/\$host:\$server_port/Plone/++api++/VirtualHostRoot/\$1 break;
proxy_pass http://backend;
}
# Downloads and image scales are served by the backend too.
location ~ /(@@download|@@images|@@ical_view) {
rewrite ^(.*) /VirtualHostBase/\$scheme/\$host:\$server_port/Plone/VirtualHostRoot\$1 break;
proxy_pass http://backend;
}
# Long-lived caching for Volto's static build assets.
location ~* \.(js|css|eot|ttf|otf|woff|woff2)\$ {
add_header Cache-Control "public";
expires 1y;
proxy_pass http://frontend;
}
location / {
proxy_pass http://frontend;
}
}
EOF
ln -sf /etc/nginx/sites-available/plone /etc/nginx/sites-enabled/plone
nginx -t
systemctl reload nginx
SITE_URL="http://$(curl -fsS -4 https://ifconfig.me 2>/dev/null || hostname -I | awk '{print $1}')"
# --- HTTPS (optional) --------------------------------------------------------
if [[ -n "$DOMAIN" ]]; then
SITE_URL="http://${DOMAIN}"
if [[ -z "$LETSENCRYPT_EMAIL" ]]; then
log "DOMAIN is set but LETSENCRYPT_EMAIL is empty; skipping HTTPS"
else
log "Requesting a Let's Encrypt certificate for ${DOMAIN}"
apt-get install -y certbot python3-certbot-nginx
if certbot --nginx --non-interactive --agree-tos --redirect \
-m "$LETSENCRYPT_EMAIL" -d "$DOMAIN"; then
SITE_URL="https://${DOMAIN}"
else
log "certbot failed (is DNS for ${DOMAIN} pointing at this server?). The site is still available over HTTP; re-run: certbot --nginx -d ${DOMAIN}"
fi
fi
fi
# --- Done ----------------------------------------------------------------------
if [[ "$NEW_ADMIN_PASSWORD_SET" == 0 ]]; then
if [[ "$ADMIN_PASSWORD_CHOSEN" == 1 ]]; then
PASSWORD_LINE="(the password you entered when deploying)"
else
PASSWORD_LINE="${ADMIN_PASSWORD}"
fi
umask 077
cat > "$CREDENTIALS_FILE" <<EOF
Plone in a Box(TM)
Site: ${SITE_URL}
Username: admin
Password: ${PASSWORD_LINE}
${PASSWORD_NOTE}
${ADDONS_LINE}${ADDONS_NOTE}
${FRONTEND_IMAGE:+Frontend image: ${FRONTEND_IMAGE}}${FRONTEND_NOTE}
Store the password in a password manager, then delete this file.
Stack files: ${APP_DIR} (docker compose --project-directory ${APP_DIR} ps)
EOF
umask 022
fi
# Show the site address on the server's console login screen (Linode LISH,
# DigitalOcean Recovery Console, AWS serial console), for anyone who never uses SSH.
cat >> /etc/issue <<EOF
Plone in a Box(TM) is ready: ${SITE_URL}
Log in to the site as "admin".
EOF
rm -rf "$RESUME_DIR"
rm -f "$RESUME_UNIT"
if [[ -f /var/run/reboot-required ]]; then
log "Some updates need a restart to take effect; restart the server when convenient (sudo reboot). Plone starts again by itself."
fi
if [[ -n "$ADDONS_LINE" ]]; then
log "$ADDONS_LINE"
fi
if [[ -n "$ADDONS_NOTE" ]]; then
log "$ADDONS_NOTE"
fi
if [[ -n "$FRONTEND_IMAGE" ]]; then
log "Frontend image: ${FRONTEND_IMAGE}"
fi
if [[ -n "$FRONTEND_NOTE" ]]; then
log "$FRONTEND_NOTE"
fi
if [[ "$ADMIN_PASSWORD_CHOSEN" == 1 ]]; then
log "Finished. Site: ${SITE_URL} -- log in as admin with the password you chose"
else
log "Finished. Site: ${SITE_URL} -- admin credentials are in ${CREDENTIALS_FILE}"
fi