-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2029 from liberapay/aws-upgrade
This branch upgrades the Liberapay webapp in several ways. The production servers will now run Amazon Linux v2 instead of the old v1, Python 3.8 instead of 3.6 (closes #1703), `gunicorn` instead of Apache's `httpd` with `mod_wsgi`, and `cloudflared` instead of an Amazon load balancer. This last point fixes #1093 and will zero out a part of our AWS bill, but those savings won't lower the overall bill because on the other hand I've increased the resources allocated to the database and webapp. Although it wasn't part of the plan, this branch can be considered a step towards #1727.
- Loading branch information
Showing
33 changed files
with
271 additions
and
313 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hooks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
tunnel: liberapay-prod | ||
credentials-file: /etc/cloudflared/liberapay-prod.json | ||
|
||
ingress: | ||
- service: unix:/var/app/current/socket |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
[Unit] | ||
Description=Cloudflare Tunnel daemon for web app #%i | ||
After=network.target webapp@%i.service | ||
Wants=network.target | ||
BindsTo=webapp@%i.service | ||
|
||
[Service] | ||
Type=notify | ||
ExecStart=/usr/local/bin/cloudflared --no-autoupdate tunnel --config /etc/cloudflared/cloudflared.conf run | ||
User=cloudflared | ||
Group=cloudflared | ||
Restart=on-failure | ||
RestartSec=5s | ||
|
||
CapabilityBoundingSet= | ||
AmbientCapabilities= | ||
PrivateUsers=true | ||
|
||
NoNewPrivileges=true | ||
LimitNOFILE=1048576 | ||
UMask=0077 | ||
|
||
ProtectSystem=strict | ||
ProtectHome=true | ||
PrivateTmp=true | ||
PrivateDevices=true | ||
ProtectHostname=true | ||
ProtectClock=true | ||
ProtectKernelTunables=true | ||
ProtectKernelModules=true | ||
ProtectKernelLogs=true | ||
ProtectControlGroups=true | ||
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 | ||
RestrictNamespaces=true | ||
LockPersonality=true | ||
MemoryDenyWriteExecute=true | ||
RestrictRealtime=true | ||
RestrictSUIDSGID=true | ||
RemoveIPC=true | ||
|
||
SystemCallFilter=@system-service | ||
SystemCallFilter=~@privileged @resources | ||
SystemCallArchitectures=native |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/sh | ||
|
||
export PGHOST=$(/opt/elasticbeanstalk/bin/get-config environment -k PGHOST) | ||
export PGPORT=$(/opt/elasticbeanstalk/bin/get-config environment -k PGPORT) | ||
export PGDATABASE=$(/opt/elasticbeanstalk/bin/get-config environment -k PGDATABASE) | ||
export PGUSER=$(/opt/elasticbeanstalk/bin/get-config environment -k PGUSER) | ||
export PGPASSWORD=$(/opt/elasticbeanstalk/bin/get-config environment -k PGPASSWORD) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[Unit] | ||
Description=Web application daemon #%i | ||
Requires=webapp@%i.socket | ||
|
||
[Service] | ||
User=webapp | ||
Group=webapp | ||
Type=notify | ||
WorkingDirectory=/var/app/current/ | ||
EnvironmentFile=/opt/elasticbeanstalk/deployment/env | ||
Sockets=webapp@%i.socket | ||
ExecStart=/var/app/venv/staging-LQM1lest/bin/python app.py --bind fd:3 | ||
|
||
Restart=always | ||
|
||
StandardOutput=syslog | ||
StandardError=syslog | ||
SyslogIdentifier=web | ||
|
||
# When stopping, send the initial SIGTERM to the main process only. | ||
KillMode=mixed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[Unit] | ||
Description=Socket of web application daemon #%i | ||
|
||
[Socket] | ||
ListenStream=/var/app/%i/socket | ||
Service=webapp@%i.service | ||
SocketUser=webapp | ||
SocketGroup=cloudflared | ||
SocketMode=0660 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/bin/bash | ||
|
||
# Tell bash to be strict and log everything | ||
set -eux | ||
|
||
# Compute the deployment ID | ||
deploy_id=$(($(cat /var/app/_deploy_id 2>/dev/null || echo 0) + 1)) | ||
max_deploy_id=$((deploy_id + 99)) | ||
while systemctl is-active --quiet webapp@$deploy_id.service; do | ||
let deploy_id++ | ||
if [ $deploy_id -gt $max_deploy_id ]; then | ||
echo "this script appears to be stuck in an infinite loop, exiting" | ||
exit 1 | ||
fi | ||
done | ||
|
||
# Rename the app directory | ||
app_dir=$(pwd) | ||
rm -rf /var/app/$deploy_id | ||
mv $app_dir /var/app/$deploy_id | ||
ln -s /var/app/$deploy_id $app_dir | ||
|
||
# Start the new instance and its proxy | ||
systemctl start webapp@$deploy_id.service cloudflared@$deploy_id.service | ||
|
||
# Save the new deployment ID | ||
echo $deploy_id >/var/app/_deploy_id | ||
|
||
# Stop the old instance(s) and their proxies | ||
let i=1 | ||
while [ $i -lt $deploy_id ]; do | ||
systemctl stop cloudflared@$i.service | ||
systemctl stop webapp@$i.service | ||
systemctl stop webapp@$i.socket | ||
rm -rf /var/app/$i | ||
let i++ | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/bash | ||
|
||
# Tell bash to be strict and log everything | ||
set -eux | ||
|
||
# Install libffi-devel for misaka, and htop for when I want to look at what's going on | ||
yum install -y libffi-devel htop | ||
# Install PostgreSQL client tools and libraries | ||
amazon-linux-extras install -y postgresql11 | ||
|
||
# Automatically set the PG* environment variables so that `psql` connects to the liberapay database by default | ||
install -m 644 -o root -g root -t /etc/profile.d .platform/files/pgenv.sh | ||
|
||
# Install the systemd service files for the webapp and cloudflared | ||
install -m 644 -o root -g root -t /etc/systemd/system .platform/files/[email protected] | ||
install -m 644 -o root -g root -t /etc/systemd/system .platform/files/[email protected] | ||
install -m 644 -o root -g root -t /etc/systemd/system .platform/files/[email protected] | ||
systemctl daemon-reload | ||
|
||
# Install cloudflared, directly from GitHub | ||
if ! which cloudflared 2>/dev/null || [ $(cloudflared version) != "cloudflared version 2021.5.8 "* ]; then | ||
wget https://github.com/cloudflare/cloudflared/releases/download/2021.5.8/cloudflared-linux-amd64 | ||
hash=$(sha256sum cloudflared-linux-amd64 | cut -d' ' -f1) | ||
expected_hash=224cd850cb042a5da1d15432063ed04bf8764241de769338e65c44639ed6c28e | ||
if [ $hash != $expected_hash ]; then | ||
echo "cloudflared binary downloaded from GitHub doesn't match expected hash: $hash != $expected_hash" | ||
exit 1 | ||
fi | ||
install -m 755 -o root -g root cloudflared-linux-amd64 /usr/local/bin/cloudflared | ||
rm cloudflared-linux-amd64 | ||
fi | ||
|
||
# Create the cloudflared system user and group | ||
groupadd -r cloudflared || true | ||
useradd -r -g cloudflared cloudflared || true | ||
|
||
# Install the Cloudflare Tunnel configuration and credentials files | ||
install -o cloudflared -g cloudflared -m 755 -d /etc/cloudflared | ||
install -o cloudflared -g cloudflared -m 644 -t /etc/cloudflared .platform/files/cloudflared.conf | ||
if ! [ -f /etc/cloudflared/liberapay-prod.json ]; then | ||
aws s3 cp s3://serverfiles.liberapay.org/liberapay-prod.json liberapay-prod.json | ||
install -o cloudflared -g cloudflared -m 644 -t /etc/cloudflared liberapay-prod.json | ||
rm liberapay-prod.json | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash -eu | ||
|
||
/var/app/venv/staging-*/bin/pip install --require-hashes -r requirements_base.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash -eu | ||
|
||
sudo -u webapp -E PYTHONPATH=. /var/app/venv/staging-*/bin/python liberapay/wireup.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: sleep 7d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.