fix: prepare the existing HTTP site for SSH releases
continuous-integration/drone/push Build is passing Details

This commit is contained in:
mihir 2026-11-20 13:00:30 +05:30
parent 6bd1940f5e
commit f4dd351f87
4 changed files with 80 additions and 32 deletions

View File

@ -6,42 +6,33 @@ script, and activates the release through SSH. The sole deployment secret is
## One-time server setup ## One-time server setup
Obtain this branch's files on server1 using your existing repository access, Server inspection on 19 November 2026 found an HTTP-only site at
or transfer the Nginx snippet from your workstation. From the repository directory, `/etc/nginx/sites-enabled/ManiCandles.midastix.com.conf`, with its old root at
run the following as `mihir`. `/var/www/ManiCandles.midastix.com`. Certbot is installed. The setup script is
specific to that configuration and refuses to overwrite a site with TLS settings.
Obtain this branch's files on server1 using your existing repository access.
From the repository directory, run the following as `mihir`.
```sh ```sh
sudo install -d -o mihir -g "$(id -gn mihir)" -m 0755 \ sudo sh scripts/setup-static-host.sh
/var/www/manicandles /var/www/manicandles/releases /var/www/manicandles/incoming sudo certbot --nginx -d manicandles.midastix.com --redirect
sudo install -d -m 0755 /etc/nginx/snippets sudo nginx -t
sudo install -m 0644 deploy/nginx-locations.conf /etc/nginx/snippets/manicandles.conf curl --fail --show-error https://manicandles.midastix.com/
command -v flock
command -v curl
``` ```
Identify the existing Nginx site file for `manicandles.midastix.com`. Alternatively, copy `scripts/setup-static-host.sh` and
`deploy/nginx-locations.conf` together to `/tmp/` on server1, and run
`sudo sh /tmp/setup-static-host.sh`, followed by the Certbot and verification
commands above. The snippet must be beside the setup script for this layout.
```sh Setup backs up the original site under `/var/backups`, prepares writable release
sudo nginx -T 2>&1 | grep -E '^# configuration file|server_name.*manicandles' directories, installs the snippet, validates Nginx, and reloads it. Validation or
``` reload failure restores the saved site file. If the old root contains an index,
setup retains it as the initial `current` release and a rollback target. If no
Back up that site file before editing it. Inside its existing HTTPS `server` old index exists, the frontend awaits its first deployment. DNS must point to
block for `manicandles.midastix.com`, replace the old `root`, `index`, and server1 and public port 80 must be reachable for certificate issuance. Complete
`location` directives with this include. Preserve its `listen`, `server_name`, Certbot's email and agreement prompts yourself. Verify HTTPS before merging.
certificate paths, TLS settings, and HTTP-to-HTTPS redirect. Resolve any duplicate
security header definitions against the snippet.
```nginx
include /etc/nginx/snippets/manicandles.conf;
```
Validate and reload. If validation fails, restore the saved site file before
continuing. Reload preserves existing connections. The frontend will begin
serving when the first release supplies `/var/www/manicandles/current`.
```sh
sudo nginx -t && sudo systemctl reload nginx
```
Save the complete matching private key in Drone's `manicandles_deploy_key` Save the complete matching private key in Drone's `manicandles_deploy_key`
secret. Its public key must be authorized for `mihir` on server1. Keep the secret. Its public key must be authorized for `mihir` on server1. Keep the

View File

@ -25,7 +25,7 @@ test "$(cat "$release/release.txt")" = "$release_id"
# A slow older pipeline must not replace a newer successful release. # A slow older pipeline must not replace a newer successful release.
last_build=0 last_build=0
if [ -L current ]; then if [ -L current ] && [ -f current/release.txt ]; then
last_release=$(cat current/release.txt) last_release=$(cat current/release.txt)
last_build=${last_release%%-*} last_build=${last_release%%-*}
fi fi

View File

@ -0,0 +1,46 @@
#!/bin/sh
set -eu
test "$(id -u)" = 0 || { echo 'Run this setup script with sudo.' >&2; exit 1; }
script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd)
snippet="$script_dir/../deploy/nginx-locations.conf"
if [ ! -f "$snippet" ]; then snippet="$script_dir/nginx-locations.conf"; fi
test -s "$snippet"
site=$(readlink -f /etc/nginx/sites-enabled/ManiCandles.midastix.com.conf)
test -f "$site"
if grep -Eq 'listen[[:space:]].*443|ssl_certificate' "$site"; then
echo 'This site already contains TLS settings. Review its configuration before running setup.' >&2
exit 1
fi
nginx -t
command -v curl
command -v flock
command -v certbot
root=/var/www/manicandles
install -d -o mihir -g "$(id -gn mihir)" -m 0755 "$root" "$root/releases" "$root/incoming"
if [ ! -e "$root/current" ] && [ ! -L "$root/current" ] && [ -s /var/www/ManiCandles.midastix.com/index.html ]; then
ln -s /var/www/ManiCandles.midastix.com "$root/current"
chown -h mihir:"$(id -gn mihir)" "$root/current"
fi
install -d -m 0755 /etc/nginx/snippets /var/backups
backup="/var/backups/manicandles-nginx-$(date +%Y%m%d%H%M%S)-$$.conf"
cp -p "$site" "$backup"
if [ -e /etc/nginx/snippets/manicandles.conf ] && ! cmp -s "$snippet" /etc/nginx/snippets/manicandles.conf; then
echo 'A different deployment snippet exists. Review the existing setup before continuing.' >&2
exit 1
fi
install -m 0644 "$snippet" /etc/nginx/snippets/manicandles.conf
cat > "$site" <<'NGINX'
server {
listen 80;
server_name manicandles.midastix.com;
include /etc/nginx/snippets/manicandles.conf;
}
NGINX
if ! nginx -t || ! systemctl reload nginx; then
cp -p "$backup" "$site"
nginx -t && systemctl reload nginx
echo "Setup failed. Restored $backup" >&2
exit 1
fi
echo "Site prepared. Original configuration saved at $backup"
echo 'Next run: sudo certbot --nginx -d manicandles.midastix.com --redirect'

View File

@ -51,4 +51,15 @@ test "$(cat "$fixture/site/current/release.txt")" = 2-bbb
rm "$fixture/site/wrong-marker" rm "$fixture/site/wrong-marker"
deploy 1-aaa deploy 1-aaa
test "$(cat "$fixture/site/current/release.txt")" = 2-bbb test "$(cat "$fixture/site/current/release.txt")" = 2-bbb
# The existing HTTP site has no release metadata and must remain a rollback target.
rm "$fixture/site/current"
mkdir -p "$fixture/site/legacy"
echo legacy > "$fixture/site/legacy/index.html"
ln -s "$fixture/site/legacy" "$fixture/site/current"
touch "$fixture/site/fail-health"
if deploy 3-ccc; then echo 'Expected rollback to legacy site' >&2; exit 1; fi
test "$(cat "$fixture/site/current/index.html")" = legacy
rm "$fixture/site/fail-health"
deploy 3-ccc
test "$(cat "$fixture/site/previous/index.html")" = legacy
printf 'Deployment activation, rollback and stale-build checks passed.\n' printf 'Deployment activation, rollback and stale-build checks passed.\n'