2023-02-01 19:44:05 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-03-06 20:42:17 +00:00
|
|
|
set -e
|
2023-02-01 19:44:05 +00:00
|
|
|
cd "$(dirname "$0")"
|
|
|
|
|
2023-03-11 16:12:19 +00:00
|
|
|
# this script destroys all resources in the current project.
|
2023-03-02 14:46:17 +00:00
|
|
|
|
|
|
|
if lxc remote get-default | grep -q "local"; then
|
|
|
|
echo "ERROR: you are on the local lxc remote. Nothing to destroy"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-03-10 16:14:55 +00:00
|
|
|
echo "WARNING: this script backs up your existing remote and saves all data locally in the SSME."
|
|
|
|
echo " Then, all your VMs are destroyed on the remote resulting is destruction of user data."
|
|
|
|
echo " But then we re-create everything using the new codebase, then restore user data to the"
|
|
|
|
echo " newly provisioned VMs."
|
|
|
|
|
2023-02-01 19:44:05 +00:00
|
|
|
RESPONSE=
|
2023-03-10 16:14:55 +00:00
|
|
|
read -r -p "Are you sure you want to continue (y/n): ": RESPONSE
|
2023-02-01 19:44:05 +00:00
|
|
|
if [ "$RESPONSE" != "y" ]; then
|
|
|
|
echo "STOPPING."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2023-03-14 15:37:33 +00:00
|
|
|
USER_TARGET_PROJECT=
|
|
|
|
|
|
|
|
# grab any modifications from the command line.
|
|
|
|
for i in "$@"; do
|
|
|
|
case $i in
|
|
|
|
--project=*)
|
|
|
|
USER_TARGET_PROJECT="${i#*=}"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
echo "Unexpected option: $1"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2023-02-01 19:44:05 +00:00
|
|
|
. ../defaults.sh
|
2023-03-02 14:46:17 +00:00
|
|
|
|
2023-03-09 14:55:40 +00:00
|
|
|
. ./remote_env.sh
|
2023-02-01 19:44:05 +00:00
|
|
|
|
2023-03-13 17:40:07 +00:00
|
|
|
for PROJECT_CHAIN in ${DEPLOYMENT_STRING//,/ }; do
|
|
|
|
NO_PARENS="${PROJECT_CHAIN:1:${#PROJECT_CHAIN}-2}"
|
|
|
|
PROJECT_PREFIX=$(echo "$NO_PARENS" | cut -d'|' -f1)
|
|
|
|
BITCOIN_CHAIN=$(echo "$NO_PARENS" | cut -d'|' -f2)
|
2023-03-14 15:37:33 +00:00
|
|
|
|
|
|
|
PROJECT_NAME="$PROJECT_PREFIX-$BITCOIN_CHAIN"
|
|
|
|
PROJECT_PATH="$PROJECTS_DIR/$PROJECT_NAME"
|
|
|
|
|
|
|
|
# if the user sets USER_TARGET_PROJECT, let's ensure the project exists.
|
|
|
|
if [ -n "$USER_TARGET_PROJECT" ]; then
|
|
|
|
if ! lxc project list | grep -q "$USER_TARGET_PROJECT"; then
|
|
|
|
echo "ERROR: the project does not exist! Nothing to destroy."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$PROJECT_NAME" != "$USER_TARGET_PROJECT" ]; then
|
|
|
|
echo "INFO: Skipping project '$PROJECT_NAME' since the system owner has used the --project switch."
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
export PROJECT_NAME="$PROJECT_NAME"
|
|
|
|
export PROJECT_PATH="$PROJECT_PATH"
|
2023-03-13 17:40:07 +00:00
|
|
|
|
|
|
|
. ./project_env.sh
|
|
|
|
|
|
|
|
if ! lxc info | grep "project:" | grep -q "$PROJECT_NAME"; then
|
|
|
|
if lxc project list | grep -q "$PROJECT_NAME"; then
|
|
|
|
lxc project switch "$PROJECT_NAME"
|
|
|
|
fi
|
2023-03-09 14:58:16 +00:00
|
|
|
fi
|
2023-02-01 19:44:05 +00:00
|
|
|
|
2023-03-13 17:40:07 +00:00
|
|
|
for VM in www btcpayserver; do
|
|
|
|
LXD_NAME="$VM-${DOMAIN_NAME//./-}"
|
2023-02-01 19:44:05 +00:00
|
|
|
|
2023-03-13 17:40:07 +00:00
|
|
|
if lxc list | grep -q "$LXD_NAME"; then
|
|
|
|
lxc delete -f "$LXD_NAME"
|
2023-02-01 19:44:05 +00:00
|
|
|
|
2023-03-13 17:40:07 +00:00
|
|
|
# remove the ssh known endpoint else we get warnings.
|
|
|
|
ssh-keygen -f "$SSH_HOME/known_hosts" -R "$LXD_NAME"
|
|
|
|
fi
|
2023-02-01 19:44:05 +00:00
|
|
|
|
2023-03-13 17:40:07 +00:00
|
|
|
if lxc profile list | grep -q "$LXD_NAME"; then
|
|
|
|
lxc profile delete "$LXD_NAME"
|
|
|
|
fi
|
|
|
|
done
|
2023-03-09 14:58:16 +00:00
|
|
|
|
2023-03-13 17:40:07 +00:00
|
|
|
if lxc network list -q | grep -q ss-ovn; then
|
|
|
|
lxc network delete ss-ovn
|
|
|
|
fi
|
2023-03-09 14:58:16 +00:00
|
|
|
|
2023-03-13 17:40:07 +00:00
|
|
|
if ! lxc info | grep "project:" | grep -q default; then
|
|
|
|
lxc project switch default
|
|
|
|
fi
|
2023-03-09 14:58:16 +00:00
|
|
|
|
2023-03-13 17:40:07 +00:00
|
|
|
if lxc project list | grep -q "$PROJECT_NAME"; then
|
|
|
|
lxc project delete "$PROJECT_NAME"
|
|
|
|
fi
|
2023-03-09 14:58:16 +00:00
|
|
|
|
2023-03-13 17:40:07 +00:00
|
|
|
# delete the base image so it can be created.
|
|
|
|
if lxc list | grep -q "$BASE_IMAGE_VM_NAME"; then
|
|
|
|
lxc delete -f "$BASE_IMAGE_VM_NAME"
|
|
|
|
# remove the ssh known endpoint else we get warnings.
|
|
|
|
ssh-keygen -f "$SSH_HOME/known_hosts" -R "$LXD_NAME"
|
|
|
|
fi
|
|
|
|
done
|