Files
hantim-server/scripts/remove-app.sh
T

90 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
GITEA_URL="https://git.timothykim.net"
GITEA_ORG="hantim"
if [ -z "${1:-}" ]; then
echo "Usage: ./scripts/remove-app.sh <domain>"
echo " Example: ./scripts/remove-app.sh hcsuzuki.net"
echo ""
echo "Removes: local files (compose, workflow, nginx conf), Gitea repo."
echo "Does NOT remove: DNS records, SSL certs."
exit 1
fi
APP="$1"
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
if ! [[ "$APP" =~ ^[a-zA-Z0-9][a-zA-Z0-9._-]*$ ]]; then
echo "Error: app name must be alphanumeric (hyphens, dots, underscores allowed)."
exit 1
fi
# --- Check dependencies ---
if ! command -v bw &>/dev/null; then
echo "Error: Bitwarden CLI (bw) is not installed."
exit 1
fi
# --- Get secrets (cached in env vars, or fetch from Bitwarden) ---
if [ -n "${HANTIM_GITEA_TOKEN:-}" ]; then
echo "Using cached secrets from environment."
GITEA_TOKEN="$HANTIM_GITEA_TOKEN"
else
if bw status 2>/dev/null | grep -q '"status":"unauthenticated"'; then
echo "Log in to Bitwarden:"
bw login
fi
if bw status 2>/dev/null | grep -q '"status":"locked"'; then
BW_SESSION=$(bw unlock --raw)
export BW_SESSION
fi
bw sync --session "${BW_SESSION:-}"
GITEA_TOKEN=$(bw get notes hantim-new-app-script --session "${BW_SESSION:-}")
export HANTIM_GITEA_TOKEN="$GITEA_TOKEN"
fi
# --- Delete Gitea repo ---
echo "==> Deleting Gitea repo $GITEA_ORG/$APP..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
-X DELETE "$GITEA_URL/api/v1/repos/$GITEA_ORG/$APP" \
-H "Authorization: token $GITEA_TOKEN")
if [ "$HTTP_CODE" = "204" ]; then
echo " Deleted."
elif [ "$HTTP_CODE" = "404" ]; then
echo " Repo does not exist, skipping."
else
echo " Warning: unexpected response (HTTP $HTTP_CODE)."
fi
# --- Remove local files ---
echo "==> Removing local files..."
REMOVED=()
for f in "docker/$APP" ".gitea/workflows/deploy-$APP.yml" "docker/nginx/conf.d/$APP.conf"; do
if [ -e "$REPO_ROOT/$f" ]; then
git -C "$REPO_ROOT" rm -rf "$f"
REMOVED+=("$f")
fi
done
if [ ${#REMOVED[@]} -gt 0 ]; then
echo "==> Committing and pushing..."
cd "$REPO_ROOT"
git commit -m "remove $APP"
git push
else
echo " No local files to remove."
fi
echo "Done. DNS records and SSL certs were left in place."