109 lines
3.5 KiB
Bash
109 lines
3.5 KiB
Bash
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
cd "$(dirname "$0")/.."
|
|
state_dir=/home/koden/.local/state/oktp-deploy
|
|
env_file="$state_dir/metrics.env"
|
|
mkdir -p "$state_dir"
|
|
exec 9>"$state_dir/deploy.lock"
|
|
flock -x 9
|
|
test -r "$env_file" || { echo "Missing $env_file" >&2; exit 1; }
|
|
sha=$(git rev-parse HEAD)
|
|
image="oktp-docs:$sha"
|
|
|
|
is_latest() {
|
|
local remote_sha
|
|
remote_sha=$(git ls-remote https://sulaco.nodered.ru:5333/COD2/OKTP_SERVER.git refs/heads/main | cut -f1)
|
|
[[ "$remote_sha" == "$sha" ]]
|
|
}
|
|
if ! is_latest; then
|
|
echo 'Cannot confirm this commit is latest main; skipping deployment.'
|
|
exit 0
|
|
fi
|
|
|
|
docker build --label "org.opencontainers.image.revision=$sha" -t "$image" .
|
|
|
|
probe="oktp-docs-check-${sha:0:12}-$$"
|
|
backup="oktp-docs-backup-$(date +%s)-$$"
|
|
old_saved=false
|
|
new_created=false
|
|
complete=false
|
|
|
|
cleanup() {
|
|
result=$?
|
|
trap - EXIT
|
|
docker rm -f "$probe" >/dev/null 2>&1 || true
|
|
if [[ "$complete" != true && "$old_saved" == true ]]; then
|
|
if [[ "$new_created" == true ]]; then
|
|
docker rm -f oktp-docs >/dev/null 2>&1 || true
|
|
fi
|
|
if docker rename "$backup" oktp-docs && docker start oktp-docs; then
|
|
echo 'Previous container restored.' >&2
|
|
else
|
|
echo "Rollback failed; inspect container $backup manually." >&2
|
|
fi
|
|
elif [[ "$complete" != true && "$new_created" == true ]]; then
|
|
docker rm -f oktp-docs >/dev/null 2>&1 || true
|
|
fi
|
|
exit "$result"
|
|
}
|
|
trap cleanup EXIT
|
|
trap 'exit 130' INT
|
|
trap 'exit 143' TERM
|
|
|
|
# Check the candidate before stopping the current website.
|
|
docker run -d --name "$probe" --env-file "$env_file" \
|
|
-e SITE_BIND=0.0.0.0 \
|
|
-e METRICS_URL=http://host.docker.internal:9108/metrics \
|
|
--add-host=host.docker.internal:host-gateway "$image" >/dev/null
|
|
docker exec "$probe" python3 -c '
|
|
import time
|
|
from urllib.request import urlopen
|
|
from urllib.error import URLError
|
|
for attempt in range(20):
|
|
try:
|
|
for path in ("/", "/app.js", "/style.css"):
|
|
with urlopen("http://127.0.0.1:8080" + path, timeout=2) as response:
|
|
assert response.status == 200
|
|
break
|
|
except URLError:
|
|
time.sleep(0.5)
|
|
else:
|
|
raise SystemExit("Candidate did not start")
|
|
'
|
|
docker rm -f "$probe" >/dev/null
|
|
if ! is_latest; then
|
|
echo 'A newer commit exists or Gitea is unavailable; keeping current site.'
|
|
exit 0
|
|
fi
|
|
|
|
if docker container inspect oktp-docs >/dev/null 2>&1; then
|
|
# AutoRemove containers cannot be preserved for rollback after stopping.
|
|
if [[ $(docker inspect --format '{{.HostConfig.AutoRemove}}' oktp-docs) == true ]]; then
|
|
echo 'Recreate oktp-docs without --rm before enabling automatic deployment.' >&2
|
|
exit 1
|
|
fi
|
|
docker rename oktp-docs "$backup"
|
|
old_saved=true
|
|
docker stop "$backup" >/dev/null
|
|
fi
|
|
|
|
docker create --name oktp-docs --restart unless-stopped \
|
|
-p 8080:8080 --add-host=host.docker.internal:host-gateway \
|
|
--env-file "$env_file" -e SITE_BIND=0.0.0.0 \
|
|
-e METRICS_URL=http://host.docker.internal:9108/metrics "$image" >/dev/null
|
|
new_created=true
|
|
docker start oktp-docs >/dev/null
|
|
curl --fail --silent --show-error --retry 10 --retry-connrefused \
|
|
--retry-delay 1 --max-time 3 http://127.0.0.1:8080/ >/dev/null
|
|
complete=true
|
|
docker tag "$image" oktp-docs:latest
|
|
if [[ "$old_saved" == true ]]; then
|
|
docker rm "$backup" >/dev/null
|
|
fi
|
|
echo "Deployed $sha at http://oktpserver.local:8080"
|
|
# A stopped agent should not roll back an otherwise healthy documentation site.
|
|
if ! curl --fail --silent --max-time 5 http://127.0.0.1:8080/api/metrics >/dev/null; then
|
|
echo 'Warning: site is healthy, but the host metrics agent is unavailable.' >&2
|
|
fi
|