Files
mesh/deploy/install.sh
T

106 lines
4.6 KiB
Bash

#!/bin/sh
# Install the verified build with data backups and restore on a failed migration.
set -eu
umask 077
STAMP=${1:?backup timestamp required}
source_commit=${2:?source revision required}
expected_commit=${3:?native revision required}
case "$STAMP" in *[!0-9TZ]*|'') exit 1 ;; esac
[ "${#STAMP}" -eq 16 ]
for revision in "$source_commit" "$expected_commit"; do
case "$revision" in *[!0-9a-f]*|'') exit 1 ;; esac
[ "${#revision}" -eq 40 ]
done
mesh_token=$(cat)
rollback_ready=0
service_stopped=0
finish() {
result=$?
trap - 0
if [ "$result" -ne 0 ] && [ "$rollback_ready" -eq 1 ]; then
printf '%s' "$STAMP" | sh /opt/mesh-src/deploy/rollback.sh
elif [ "$result" -ne 0 ] && [ "$service_stopped" -eq 1 ]; then
systemctl start mesh.service
fi
if [ "$result" -ne 0 ]; then
python3 /opt/mesh-src/deploy/migrate-install.py restore "$STAMP"
fi
exit "$result"
}
trap finish 0
python3 /opt/mesh-src/scripts/verify_deployment_inputs.py verify-artifact-manifest \
--output /opt/mesh-src/mesh \
--manifest /opt/mesh-src/build/mesh.manifest.json \
--expected-source-commit "$source_commit" \
--expected-liboqs-commit "$expected_commit"
python3 /opt/mesh-src/deploy/migrate-install.py prepare "$STAMP"
mkdir -p /var/lib/mesh/backups
database=$(python3 /opt/mesh-src/deploy/database-path.py /etc/mesh/mesh.env)
test -f "$database"
printf '%s\n' "$database" > "/var/lib/mesh/backups/mesh.database-path.$STAMP"
cp /opt/mesh/mesh "/var/lib/mesh/backups/mesh.pre-upgrade.$STAMP"
if [ -f /opt/mesh/mesh.manifest.json ]; then
cp /opt/mesh/mesh.manifest.json "/var/lib/mesh/backups/mesh.manifest.$STAMP"
fi
systemctl stop mesh.service
service_stopped=1
sqlite3 "$database" ".backup '/var/lib/mesh/backups/mesh.db.$STAMP'"
test -s "/var/lib/mesh/backups/mesh.db.$STAMP"
install -m 600 /etc/mesh/mesh.env "/var/lib/mesh/backups/mesh.env.$STAMP"
if [ -f /etc/systemd/system/mesh.service.d/20-mesh-mesh.conf ]; then
cp /etc/systemd/system/mesh.service.d/20-mesh-mesh.conf "/var/lib/mesh/backups/mesh.mesh.$STAMP"
else
touch "/var/lib/mesh/backups/mesh.mesh-absent.$STAMP"
fi
rollback_ready=1
# Replace the existing backup maintenance unit only when it was already enabled.
if systemctl is-enabled --quiet daochi-backup-prune.timer || systemctl is-enabled --quiet mesh-backup-prune.timer; then
if systemctl is-enabled --quiet daochi-backup-prune.timer; then
touch "/var/lib/mesh/backups/mesh.legacy-prune-enabled.$STAMP"
if systemctl is-active --quiet daochi-backup-prune.timer; then
touch "/var/lib/mesh/backups/mesh.legacy-prune-active.$STAMP"
fi
systemctl disable --now daochi-backup-prune.timer
fi
install -m 644 /opt/mesh-src/deploy/mesh-backup-prune.service /opt/mesh-src/deploy/mesh-backup-prune.timer /etc/systemd/system/
systemctl daemon-reload
systemctl enable mesh-backup-prune.timer
if [ -f "/var/lib/mesh/backups/mesh.legacy-prune-active.$STAMP" ]; then
systemctl start mesh-backup-prune.timer
fi
fi
install -m 755 /opt/mesh-src/mesh /opt/mesh/mesh.new
install -m 644 /opt/mesh-src/build/mesh.manifest.json /opt/mesh/mesh.manifest.new
mv /opt/mesh/mesh.new /opt/mesh/mesh
mv /opt/mesh/mesh.manifest.new /opt/mesh/mesh.manifest.json
if [ -n "$mesh_token" ]; then
install -d -m 755 /etc/systemd/system/mesh.service.d
printf '%s' "$mesh_token" | python3 -c '
import os, pathlib, sys
value = sys.stdin.read().strip()
if any(c in value for c in "\r\n\0"):
sys.exit("invalid mesh credential line")
value = value.replace("\\", "\\\\").replace(chr(34), "\\" + chr(34)).replace("%", "%%")
path = pathlib.Path("/etc/systemd/system/mesh.service.d/20-mesh-mesh.conf")
temporary = path.with_suffix(".new")
fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
with os.fdopen(fd, "w") as output:
output.write("[Service]\nEnvironment=" + chr(34) + "MESH_NODE_SYNC_TOKEN=" + value + chr(34) + "\n")
os.replace(temporary, path)
'
systemctl daemon-reload
fi
python3 /opt/mesh-src/scripts/migrate-legacy-env.py /etc/mesh/mesh.env --apply --canonical-only
sh /opt/mesh-src/deploy/set-environment.sh /opt/mesh-src/deploy/node.env /etc/mesh/mesh.env
if [ -s /etc/mesh/google-play-service-account.json ]; then
echo MESH_GOOGLE_SERVICE_ACCOUNT_JSON_FILE=/etc/mesh/google-play-service-account.json |
sh /opt/mesh-src/deploy/set-environment.sh - /etc/mesh/mesh.env
fi
# A raised Dew ratio or a new Dew product in deploy/dew-ratio.env
# rewrites the environment; if that fails, the previous node comes back.
sh /opt/mesh-src/deploy/apply-dew-ratio.sh /opt/mesh-src/deploy/dew-ratio.env /etc/mesh/mesh.env /opt/mesh/mesh "$database"
systemctl start mesh.service
sleep 3
systemctl is-active --quiet mesh.service
rollback_ready=0