1
1
sovereign-stack/deployment/down.sh

141 lines
3.5 KiB
Bash
Raw Normal View History

2023-04-04 15:27:27 +00:00
#!/bin/bash
# https://www.sovereign-stack.org/ss-down/
2023-08-21 00:47:27 +00:00
set -exu
2023-04-04 15:27:27 +00:00
cd "$(dirname "$0")"
2023-09-22 23:46:07 +00:00
if incus remote get-default -q | grep -q "local"; then
echo "ERROR: you are on the local incus remote. Nothing to take down"
2023-04-04 15:27:27 +00:00
exit 1
fi
2023-12-19 14:53:01 +00:00
KEEP_ZFS_STORAGE_VOLUMES=true
2023-08-12 16:22:34 +00:00
OTHER_SITES_LIST=
2023-12-14 17:47:16 +00:00
SKIP_BTCPAY_SERVER=false
SKIP_WWW_SERVER=false
2023-09-06 02:01:57 +00:00
SKIP_LNPLAY_SERVER=false
2023-08-21 00:47:27 +00:00
BACKUP_WWW_APPS=true
2023-04-04 15:27:27 +00:00
2023-12-17 21:51:21 +00:00
WWW_SERVER_MAC_ADDRESS=
BTCPAY_SERVER_MAC_ADDRESS=
LNPLAY_SERVER_MAC_ADDRESS=
2023-04-04 15:27:27 +00:00
# grab any modifications from the command line.
for i in "$@"; do
case $i in
2023-08-12 16:22:34 +00:00
--purge)
2023-12-19 14:53:01 +00:00
KEEP_ZFS_STORAGE_VOLUMES=false
2023-04-04 15:27:27 +00:00
shift
;;
2023-08-12 16:14:00 +00:00
--skip-btcpayserver)
2023-12-14 17:47:16 +00:00
SKIP_BTCPAY_SERVER=true
2023-08-12 16:14:00 +00:00
shift
;;
--skip-wwwserver)
2023-12-14 17:47:16 +00:00
SKIP_WWW_SERVER=true
2023-08-12 16:14:00 +00:00
shift
;;
2023-09-06 02:01:57 +00:00
--skip-lnplayserver)
SKIP_LNPLAY_SERVER=true
2023-08-12 16:14:00 +00:00
shift
;;
2023-04-04 15:27:27 +00:00
*)
echo "Unexpected option: $1"
exit 1
;;
esac
done
2023-04-07 18:02:24 +00:00
. ./deployment_defaults.sh
2023-04-04 15:27:27 +00:00
. ./remote_env.sh
. ./project_env.sh
# let's bring down services on the remote deployment if necessary.
export DOMAIN_NAME="$PRIMARY_DOMAIN"
export SITE_PATH="$SITES_PATH/$PRIMARY_DOMAIN"
source "$SITE_PATH/site.conf"
source ./project/domain_env.sh
2023-08-12 16:21:15 +00:00
source ./domain_list.sh
2023-12-19 14:53:01 +00:00
2023-12-17 21:51:21 +00:00
SERVERS=
2023-12-19 14:53:01 +00:00
if [ "$SKIP_WWW_SERVER" = false ] && [ -n "$WWW_SERVER_MAC_ADDRESS" ]; then
SERVERS="www $SERVERS"
2023-12-17 21:51:21 +00:00
fi
2023-12-19 14:53:01 +00:00
if [ "$SKIP_BTCPAY_SERVER" = false ] && [ -n "$BTCPAY_SERVER_MAC_ADDRESS" ]; then
SERVERS="btcpayserver"
2023-12-17 21:51:21 +00:00
fi
if [ "$SKIP_LNPLAY_SERVER" = false ] && [ -n "$LNPLAY_SERVER_MAC_ADDRESS" ]; then
SERVERS="lnplayserver $SERVERS"
fi
2023-12-19 14:53:01 +00:00
2023-08-12 16:21:15 +00:00
for VIRTUAL_MACHINE in $SERVERS; do
2023-11-29 19:04:24 +00:00
INCUS_VM_NAME="$VIRTUAL_MACHINE-${PRIMARY_DOMAIN//./-}"
2023-04-04 15:27:27 +00:00
2023-11-29 19:04:24 +00:00
if incus list | grep -q "$INCUS_VM_NAME"; then
2023-08-12 16:21:15 +00:00
bash -c "./stop.sh --server=$VIRTUAL_MACHINE"
2023-04-04 15:27:27 +00:00
2023-11-29 19:04:24 +00:00
incus stop "$INCUS_VM_NAME"
2023-04-05 15:11:11 +00:00
2023-11-29 19:04:24 +00:00
incus delete "$INCUS_VM_NAME"
2023-04-04 15:27:27 +00:00
fi
2023-04-07 18:02:24 +00:00
# remove the ssh known endpoint else we get warnings.
ssh-keygen -f "$SSH_HOME/known_hosts" -R "$VIRTUAL_MACHINE.$PRIMARY_DOMAIN" | exit
2023-11-29 19:04:24 +00:00
if incus profile list | grep -q "$INCUS_VM_NAME"; then
incus profile delete "$INCUS_VM_NAME"
2023-04-04 15:27:27 +00:00
fi
2023-12-19 14:53:01 +00:00
if [ "$KEEP_ZFS_STORAGE_VOLUMES" = false ]; then
2023-04-04 15:27:27 +00:00
# d for docker; b for backup; s for ss-data
2023-12-12 20:09:07 +00:00
for DATA in docker backup ss-data; do
VOLUME_NAME="$VIRTUAL_MACHINE-$DATA"
2023-09-22 23:46:07 +00:00
if incus storage volume list ss-base -q | grep -q "$VOLUME_NAME"; then
2023-04-04 15:27:27 +00:00
RESPONSE=
2023-11-29 19:04:24 +00:00
read -r -p "Are you sure you want to delete the '$VOLUME_NAME' volume intended for '$INCUS_VM_NAME'?": RESPONSE
2023-04-04 15:27:27 +00:00
if [ "$RESPONSE" = "y" ]; then
2023-09-22 23:46:07 +00:00
incus storage volume delete ss-base "$VOLUME_NAME"
2023-04-04 15:27:27 +00:00
fi
fi
done
fi
done
BACKUP_WWW_APPS=true
echo "BACKUP_WWW_APPS: $BACKUP_WWW_APPS"
echo "SERVERS: $SERVERS"
echo "BACKUP_WWW_APPS: $BACKUP_WWW_APPS"
# let's grab a snapshot of the
if [ "$BACKUP_WWW_APPS" = true ]; then
#SNAPSHOT_ID=$(cat /dev/urandom | tr -dc 'a-aA-Z' | fold -w 6 | head -n 1)
#incus storage volume snapshot create ss-base www-ss-data "$SNAPSHOT_ID"
BACKUP_LOCATION="$HOME/ss/backups"
mkdir -p "$BACKUP_LOCATION"
incus storage volume export ss-base "www-ss-data" "$BACKUP_LOCATION/project-$(incus project list --format csv | grep "(current)" | awk '{print $1}')_www-ss-data_""$(date +%s)"".tar.gz"
#incus storage volume snapshot delete ss-base "www-ss-data" "$SNAPSHOT_ID"
fi
2023-09-22 23:46:07 +00:00
if incus network list -q | grep -q ss-ovn; then
incus network delete ss-ovn
2023-08-21 00:47:27 +00:00
fi