1
0
IaC/scripts/shutdown.sh
michael 7eddbba696 Add k8s shutdown/openup scripts
Add nut ansible roles
Add acme certificate ansible role
2024-01-11 01:11:16 +13:00

74 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
# Set array variables
DATABASE_LIST=(
"nextcloud"
"gitea"
"mastodon-db"
"synapse"
"mas"
"mss"
"coturn"
"keycloak"
"signal-bridge"
"whatsapp-bridge"
"telegram-bridge"
"discord-bridge"
"facebook-bridge"
)
# Notify shutdown commencement
/scripts/notify.sh "Shutdown initiated on host $HOSTNAME."
# Scale keycloak first
kubectl -n public scale deployment keycloak-operator --replicas=0
kubectl -n public scale statefulset keycloak --replicas=0
# Scale all deployments to 0
kubectl -n private scale deployment --all --replicas=0
kubectl -n public scale deployment --selector=delayed.shutdown!=enabled --replicas=0 # Leave ntfy running
kubectl -n db scale deployment --all --replicas=0
# Notify scaling complete
/scripts/notify.sh "Application scale down complete."
# Hibernate all databases
DB_INDEX=1
DB_TOTAL=${#DATABASE_LIST[@]}
for DB in "${DATABASE_LIST[@]}"; do
echo "Hibernating database $DB_INDEX/$DB_TOTAL ($DB)..."
kubectl cnpg hibernate on $DB -n db
DB_INDEX=$((DB_INDEX+1))
done
# Notify hibernation complete
/scripts/notify.sh "Database hibernations complete, initiating final shutdown."
# Scale the last deployments (ntfy + dns)
kubectl -n public scale deployment --selector=delayed.shutdown=enabled --replicas=0
kubectl -n dns scale deployment bind --replicas=0
# Cordon node
kubectl cordon kube00
# Drain remaining pods
kubectl drain kube00 --ignore-daemonsets --delete-local-data
# Shutdown upsmon to notify the UPS primary that secondary shutdown has finished
systemctl stop nut-monitor.service
# shutdown
poweroff