AJ ONeal 61ed5f223d
feat(cmd/form2mail): production contact-form server
Wires formmailer + ipcohort + geoip + gitshallow into a long-running
HTTP server. Adds graceful SIGTERM via signal.NotifyContext +
http.Server.Shutdown so deploys complete in seconds instead of waiting
for the systemd 90s SIGKILL. Trusts RFC1918 CIDRs (loopback + 10/8 +
172.16/12 + 192.168/16) for X-Forwarded-For so the upstream TLS router
can pass through the real client IP.

Includes a rename-old-first deploy script (avoids ETXTBSY on running
ELFs), HTML success/error templates that hot-reload from disk on each
request, and a silent-drop spam trap for .ru sender addresses.
2026-04-30 16:52:59 -06:00

43 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
# Cross-compile form2mail for linux-amd64 and deploy to <user>@<host>.
# Usage: scripts/deploy.sh <user>@<host> [service-name]
#
# Uses the rename-old-first pattern (avoids scp ETXTBSY on running ELFs),
# restarts via systemctl, verifies --version runs from the new inode, then
# removes the .old backup. Leaves .old in place if verification fails so
# rollback is `mv ~/bin/<bin>.old ~/bin/<bin>`.
set -eu
g_remote="${1:?usage: $0 <user>@<host> [service-name]}"
g_service="${2:-form2mail}"
g_bin="form2mail"
cd "$(dirname "$0")/.."
echo "Building ${g_bin} for linux-amd64..."
GOOS=linux GOARCH=amd64 go build -o "${g_bin}-linux" .
echo "Renaming old binary on ${g_remote}..."
ssh "${g_remote}" "mv ~/bin/${g_bin} ~/bin/${g_bin}.old"
echo "Copying new binary..."
scp "${g_bin}-linux" "${g_remote}:~/bin/${g_bin}"
echo "Restarting ${g_service}..."
ssh "${g_remote}" "sudo systemctl restart ${g_service}"
sleep 2
echo "Verifying..."
if ! ssh "${g_remote}" "systemctl is-active ${g_service}" > /dev/null; then
echo " FAILED: ${g_service} not active. Logs:"
ssh "${g_remote}" "sudo journalctl -u ${g_service} -n 20 --no-pager"
echo " Backup still at ~/bin/${g_bin}.old — roll back with:"
echo " ssh ${g_remote} 'mv ~/bin/${g_bin}.old ~/bin/${g_bin} && sudo systemctl restart ${g_service}'"
exit 1
fi
echo "Removing backup..."
ssh "${g_remote}" "rm ~/bin/${g_bin}.old"
echo "OK: ${g_service} deployed and active."