1
1
Fork 1
sovereign-stack/deployment/down.sh

132 lines
3.3 KiB
Bash
Raw Permalink 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")"
if lxc remote get-default -q | grep -q "local"; then
echo "ERROR: you are on the local lxc remote. Nothing to take down"
exit 1
fi
KEEP_DOCKER_VOLUME=true
2023-08-12 16:22:34 +00:00
OTHER_SITES_LIST=
2023-08-12 16:14:00 +00:00
SKIP_BTCPAYSERVER=false
SKIP_WWW=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
# grab any modifications from the command line.
for i in "$@"; do
case $i in
2023-08-12 16:22:34 +00:00
--purge)
2023-04-04 15:27:27 +00:00
KEEP_DOCKER_VOLUME=false
shift
;;
2023-08-12 16:14:00 +00:00
--skip-btcpayserver)
SKIP_BTCPAYSERVER=true
shift
;;
--skip-wwwserver)
SKIP_WWW=true
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-08-12 16:14:00 +00:00
SERVERS=
if [ "$SKIP_BTCPAYSERVER" = false ]; then
SERVERS="btcpayserver"
fi
if [ "$SKIP_WWW" = false ]; then
SERVERS="www $SERVERS"
fi
2023-09-06 02:01:57 +00:00
if [ "$SKIP_LNPLAY_SERVER" = false ]; then
SERVERS="lnplayserver $SERVERS"
2023-08-12 16:14:00 +00:00
fi
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
for VIRTUAL_MACHINE in $SERVERS; do
2023-04-04 15:27:27 +00:00
LXD_NAME="$VIRTUAL_MACHINE-${PRIMARY_DOMAIN//./-}"
if lxc list | grep -q "$LXD_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-08-21 00:47:27 +00:00
if [ "$VIRTUAL_MACHINE" = www ] && [ "$BACKUP_WWW_APPS" = true ]; then
APP_LIST="letsencrypt ghost nextcloud gitea nostr"
echo "INFO: Backing up WWW apps."
for APP in $APP_LIST; do
bash -c "$(pwd)/project/www/backup_www.sh --app=$APP"
done
fi
2023-04-05 15:11:11 +00:00
lxc stop "$LXD_NAME"
lxc delete "$LXD_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-04-04 15:27:27 +00:00
if lxc profile list | grep -q "$LXD_NAME"; then
lxc profile delete "$LXD_NAME"
fi
if [ "$KEEP_DOCKER_VOLUME" = false ]; then
# destroy the docker volume
VM_ID=w
if [ "$VIRTUAL_MACHINE" = btcpayserver ]; then
VM_ID="b"
2023-09-06 02:01:57 +00:00
elif [ "$VIRTUAL_MACHINE" = lnplayserver ]; then
2023-08-12 16:19:44 +00:00
VM_ID="c"
2023-04-04 15:27:27 +00:00
fi
# d for docker; b for backup; s for ss-data
for DATA in d b s; do
VOLUME_NAME="$PRIMARY_DOMAIN_IDENTIFIER-$VM_ID""$DATA"
if lxc storage volume list ss-base -q | grep -q "$VOLUME_NAME"; then
RESPONSE=
read -r -p "Are you sure you want to delete the '$VOLUME_NAME' volume intended for '$LXD_NAME'?": RESPONSE
if [ "$RESPONSE" = "y" ]; then
lxc storage volume delete ss-base "$VOLUME_NAME"
fi
fi
done
2023-08-12 16:22:34 +00:00
else
2023-08-12 16:14:00 +00:00
# we maintain the volumes
# TODO make a snapshot on all the zfs storage volumes.
echo "TODO: create snapshot of ZFS volumes and pull them to mgmt machine."
2023-04-04 15:27:27 +00:00
fi
done
if lxc network list -q | grep -q ss-ovn; then
lxc network delete ss-ovn
2023-08-21 00:47:27 +00:00
fi