devops/bdj/deploy.sh

37 lines
1.4 KiB
Bash
Executable file

#!/usr/bin/env bash
# Build the static site and ensure the edge nginx serves it.
# Content changes need no pod restart (hostPath is live); config changes trigger a reload.
set -euo pipefail
cd "$(dirname "$0")"
BASE=/home/opc/zai-home-base
K="sudo /usr/local/bin/k3s kubectl"
echo "==> building site"
python3 build.py
mkdir -p "$BASE/sites"
rm -rf "$BASE/sites/bestdadjokes.new"
cp -r site "$BASE/sites/bestdadjokes.new"
# ensure SELinux-friendly context if SELinux is enforcing
if command -v getenforce >/dev/null 2>&1 && [ "$(getenforce)" = "Enforcing" ]; then
chcon -Rt container_file_t "$BASE/sites/bestdadjokes.new" 2>/dev/null || true
chcon -Rt container_file_t "$BASE/bdj/nginx/conf.d" 2>/dev/null || true
fi
rm -rf "$BASE/sites/bestdadjokes.old"
[ -d "$BASE/sites/bestdadjokes" ] && mv "$BASE/sites/bestdadjokes" "$BASE/sites/bestdadjokes.old"
mv "$BASE/sites/bestdadjokes.new" "$BASE/sites/bestdadjokes"
rm -rf "$BASE/sites/bestdadjokes.old"
echo "==> applying edge manifest"
$K apply -f k8s/nginx-edge.yaml
POD=$($K get pods -l app=nginx-edge -o jsonpath='{.items[0].metadata.name}')
echo "==> reloading nginx config in pod $POD"
$K exec "$POD" -- nginx -s reload 2>/dev/null || true
echo "==> verifying"
sleep 1
code=$($K exec "$POD" -- wget -q -O- --header="Host: bestdadjokes.lol" http://127.0.0.1/ | head -c 60)
echo "first bytes of homepage: $code"
echo "==> deploy complete"