#!/usr/bin/env bash # teardown.sh — Elimina el entorno de una rama (al borrar la rama o a mano). # usage: teardown.sh set -euo pipefail BRANCH="${1:?usage: $0 }" DOMAIN="localesp.es" log() { printf '\033[1;34m[teardown]\033[0m %s\n' "$*"; } slugify() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g; s/^-+//; s/-+$//'; } SLUG="$(slugify "$BRANCH")" UNIT="localesp-$SLUG" HOST="$SLUG.$DOMAIN" DEST="/opt/localesp/$SLUG" log "$HOST ($DEST, $UNIT)" systemctl disable --now "$UNIT" 2>/dev/null || true rm -f "/etc/systemd/system/$UNIT.service" systemctl daemon-reload if [ -d "/etc/letsencrypt/live/$HOST" ]; then log "borrando cert $HOST" certbot delete --cert-name "$HOST" -n 2>/dev/null || true fi HOST_RE="$(printf '%s' "$HOST" | sed 's/\./\\./g')" VHOST="$(grep -rlE "server_name[[:space:]]+$HOST_RE[[:space:]]*;" /etc/nginx/conf.d/*.conf 2>/dev/null | head -1 || true)" if [ -n "$VHOST" ]; then log "borrando vhost $VHOST" rm -f "$VHOST" nginx -t && systemctl reload nginx fi rm -rf "$DEST" log "entorno eliminado ✓"