sovereign-stack/deployment/project/deploy.sh

452 lines
13 KiB
Bash
Raw Normal View History

2021-12-25 18:43:01 +00:00
#!/bin/bash
2023-02-01 19:44:05 +00:00
set -ex
2021-12-25 18:43:01 +00:00
cd "$(dirname "$0")"
2023-03-06 17:47:45 +00:00
LATEST_GIT_COMMIT="$(cat ../../.git/refs/heads/master)"
2023-02-01 20:24:03 +00:00
export LATEST_GIT_COMMIT="$LATEST_GIT_COMMIT"
2023-03-06 17:47:45 +00:00
# check to ensure dependencies are met.
for cmd in wait-for-it dig rsync sshfs lxc; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "This script requires \"${cmd}\" to be installed. Please run 'install.sh'."
exit 1
fi
done
# do a spot check; if we are on production warn.
if lxc remote get-default | grep -q "production"; then
echo "WARNING: You are running command against a production system!"
echo ""
# check if there are any uncommited changes. It's dangerous to
# alter production systems when you have commits to make or changes to stash.
if git update-index --refresh | grep -q "needs update"; then
echo "ERROR: You have uncommited changes! You MUST commit or stash all changes to continue."
exit 1
fi
RESPONSE=
read -r -p " Are you sure you want to continue (y) ": RESPONSE
if [ "$RESPONSE" != "y" ]; then
echo "STOPPING."
exit 1
fi
fi
2021-12-25 18:43:01 +00:00
DOMAIN_NAME=
RUN_CERT_RENEWAL=true
SKIP_WWW=false
2022-08-24 14:11:50 +00:00
RESTORE_WWW=false
2023-03-06 16:47:27 +00:00
BACKUP_CERTS=true
BACKUP_APPS=true
BACKUP_BTCPAY=true
BACKUP_BTCPAY_ARCHIVE_PATH=
2022-10-08 16:44:46 +00:00
RESTORE_BTCPAY=false
2022-10-26 23:33:43 +00:00
SKIP_BTCPAY=false
2021-12-25 18:43:01 +00:00
UPDATE_BTCPAY=false
RECONFIGURE_BTCPAY_SERVER=false
2022-07-27 16:38:33 +00:00
CLUSTER_NAME="$(lxc remote get-default)"
2022-10-22 00:04:03 +00:00
STOP_SERVICES=false
USER_SAYS_YES=false
RESTART_FRONT_END=true
2022-05-24 18:19:26 +00:00
# grab any modifications from the command line.
2021-12-25 18:43:01 +00:00
for i in "$@"; do
case $i in
2022-07-27 16:38:33 +00:00
--restore-www)
2022-08-24 14:11:50 +00:00
RESTORE_WWW=true
2022-10-08 16:44:46 +00:00
BACKUP_APPS=false
2022-05-24 18:19:26 +00:00
RUN_CERT_RENEWAL=false
RESTART_FRONT_END=true
2021-12-25 18:43:01 +00:00
shift
;;
2022-07-27 16:38:33 +00:00
--restore-btcpay)
RESTORE_BTCPAY=true
2022-08-24 14:11:50 +00:00
BACKUP_BTCPAY=false
2022-10-08 16:44:46 +00:00
RUN_CERT_RENEWAL=false
2022-07-27 16:38:33 +00:00
shift
;;
2022-09-28 17:59:01 +00:00
--backup-certs)
BACKUP_CERTS=true
shift
;;
--no-backup-www)
BACKUP_CERTS=false
BACKUP_APPS=false
shift
;;
2022-10-22 00:04:03 +00:00
--stop)
STOP_SERVICES=true
RESTART_FRONT_END=true
2022-10-22 00:04:03 +00:00
shift
;;
--restart-front-end)
RESTART_FRONT_END=true
shift
;;
--domain=*)
DOMAIN_NAME="${i#*=}"
shift
;;
--backup-archive-path=*)
BACKUP_BTCPAY_ARCHIVE_PATH="${i#*=}"
shift
;;
2022-01-14 21:25:45 +00:00
--update-btcpay)
2021-12-25 18:43:01 +00:00
UPDATE_BTCPAY=true
shift
;;
2022-07-27 16:38:33 +00:00
--skip-www)
2022-10-26 23:33:43 +00:00
SKIP_WWW=true
2022-07-27 16:38:33 +00:00
shift
;;
--skip-btcpay)
2022-10-26 23:33:43 +00:00
SKIP_BTCPAY=true
2022-07-27 16:38:33 +00:00
shift
;;
2022-09-28 17:59:01 +00:00
--backup-ghost)
2022-10-08 16:44:46 +00:00
BACKUP_APPS=true
2021-12-25 18:43:01 +00:00
shift
;;
--no-cert-renew)
RUN_CERT_RENEWAL=false
2021-12-25 18:43:01 +00:00
shift
;;
--reconfigure-btcpay)
RECONFIGURE_BTCPAY_SERVER=true
shift
;;
-y)
USER_SAYS_YES=true
shift
;;
2021-12-25 18:43:01 +00:00
*)
2022-08-15 13:42:15 +00:00
echo "Unexpected option: $1"
exit 1
2021-12-25 18:43:01 +00:00
;;
esac
done
if [ "$RESTORE_BTCPAY" = true ] && [ -z "$BACKUP_BTCPAY_ARCHIVE_PATH" ]; then
echo "ERROR: BACKUP_BTCPAY_ARCHIVE_PATH was not set event when the RESTORE_BTCPAY = true. "
exit 1
fi
2022-05-24 18:19:26 +00:00
# set up our default paths.
2023-03-06 17:47:45 +00:00
source ../../defaults.sh
export DOMAIN_NAME="$DOMAIN_NAME"
2022-05-24 18:19:26 +00:00
export REGISTRY_DOCKER_IMAGE="registry:2"
2022-08-24 14:11:50 +00:00
export RESTORE_WWW="$RESTORE_WWW"
2022-10-22 00:04:03 +00:00
export STOP_SERVICES="$STOP_SERVICES"
2022-09-28 17:59:01 +00:00
export BACKUP_CERTS="$BACKUP_CERTS"
2022-10-08 16:44:46 +00:00
export BACKUP_APPS="$BACKUP_APPS"
2022-08-24 14:11:50 +00:00
export RESTORE_BTCPAY="$RESTORE_BTCPAY"
2022-10-26 23:33:43 +00:00
export BACKUP_BTCPAY="$BACKUP_BTCPAY"
2022-09-09 18:00:07 +00:00
export RUN_CERT_RENEWAL="$RUN_CERT_RENEWAL"
2022-07-27 16:38:33 +00:00
export CLUSTER_NAME="$CLUSTER_NAME"
export CLUSTER_PATH="$CLUSTERS_DIR/$CLUSTER_NAME"
export USER_SAYS_YES="$USER_SAYS_YES"
export BACKUP_BTCPAY_ARCHIVE_PATH="$BACKUP_BTCPAY_ARCHIVE_PATH"
export RESTART_FRONT_END="$RESTART_FRONT_END"
2023-02-01 19:44:05 +00:00
# todo convert this to Trezor-T
SSH_PUBKEY_PATH="$SSH_HOME/id_rsa.pub"
export SSH_PUBKEY_PATH="$SSH_PUBKEY_PATH"
if [ ! -f "$SSH_PUBKEY_PATH" ]; then
# generate a new SSH key for the base vm image.
ssh-keygen -f "$SSH_HOME/id_rsa" -t ecdsa -b 521 -N ""
fi
2023-02-01 19:44:05 +00:00
# ensure our cluster path is created.
mkdir -p "$CLUSTER_PATH"
2021-12-25 18:43:01 +00:00
CLUSTER_DEFINITION="$CLUSTER_PATH/cluster_definition"
export CLUSTER_DEFINITION="$CLUSTER_DEFINITION"
#########################################
if [ ! -f "$CLUSTER_DEFINITION" ]; then
2023-02-01 19:44:05 +00:00
echo "ERROR: The cluster definition could not be found. You may need to re-run 'ss-cluster'."
exit 1
fi
2022-11-05 23:47:47 +00:00
source "$CLUSTER_DEFINITION"
2022-05-24 18:19:26 +00:00
2022-07-27 16:38:33 +00:00
# this is our password generation mechanism. Relying on GPG for secure password generation
2022-05-24 18:19:26 +00:00
function new_pass {
2022-07-27 16:38:33 +00:00
gpg --gen-random --armor 1 25
}
2022-05-24 18:19:26 +00:00
2022-09-09 18:00:07 +00:00
2022-08-24 14:11:50 +00:00
function stub_site_definition {
mkdir -p "$SITE_PATH" "$PROJECT_PATH/sites"
# create a symlink from the CLUSTERPATH/sites/DOMAIN_NAME to the ss-sites/domain name
if [ ! -d "$PROJECT_PATH/sites/$DOMAIN_NAME" ]; then
ln -s "$SITE_PATH" "$PROJECT_PATH/sites/$DOMAIN_NAME"
fi
if [ ! -f "$SITE_PATH/site_definition" ]; then
# check to see if the enf file exists. exist if not.
SITE_DEFINITION_PATH="$SITE_PATH/site_definition"
if [ ! -f "$SITE_DEFINITION_PATH" ]; then
# stub out a site_definition with new passwords.
cat >"$SITE_DEFINITION_PATH" <<EOL
#!/bin/bash
2022-07-27 16:38:33 +00:00
export DOMAIN_NAME="${DOMAIN_NAME}"
2022-11-02 18:54:02 +00:00
#export BTCPAY_ALT_NAMES="tip,store,pay,send"
2022-09-28 17:59:01 +00:00
export SITE_LANGUAGE_CODES="en"
export DUPLICITY_BACKUP_PASSPHRASE="$(new_pass)"
export DEPLOY_GHOST=true
2023-03-05 16:49:06 +00:00
export DEPLOY_CLAMS=true
2022-10-22 00:04:03 +00:00
export DEPLOY_NEXTCLOUD=false
2023-02-01 19:44:05 +00:00
export NOSTR_ACCOUNT_PUBKEY=
export DEPLOY_GITEA=false
export GHOST_MYSQL_PASSWORD="$(new_pass)"
export GHOST_MYSQL_ROOT_PASSWORD="$(new_pass)"
export NEXTCLOUD_MYSQL_PASSWORD="$(new_pass)"
export NEXTCLOUD_MYSQL_ROOT_PASSWORD="$(new_pass)"
export GITEA_MYSQL_PASSWORD="$(new_pass)"
export GITEA_MYSQL_ROOT_PASSWORD="$(new_pass)"
EOL
chmod 0744 "$SITE_DEFINITION_PATH"
2023-02-01 19:44:05 +00:00
echo "INFO: we stubbed a new site_definition for you at '$SITE_DEFINITION_PATH'. Go update it!"
exit 1
fi
fi
}
2023-03-02 14:46:17 +00:00
PROJECT_NAME="$PROJECT_PREFIX-$BITCOIN_CHAIN"
2023-02-01 21:02:38 +00:00
PROJECT_PATH="$PROJECTS_DIR/$PROJECT_NAME"
mkdir -p "$PROJECT_PATH" "$CLUSTER_PATH/projects"
2023-03-02 14:46:17 +00:00
export PROJECT_NAME="$PROJECT_NAME"
export PROJECT_PATH="$PROJECT_PATH"
# create a symlink from ./clusterpath/projects/project
2023-02-01 21:02:38 +00:00
if [ ! -d "$CLUSTER_PATH/projects/$PROJECT_NAME" ]; then
ln -s "$PROJECT_PATH" "$CLUSTER_PATH/projects/$PROJECT_NAME"
fi
2023-02-01 21:02:38 +00:00
# create the lxc project as specified by PROJECT_NAME
if ! lxc project list | grep -q "$PROJECT_NAME"; then
2023-02-01 19:44:05 +00:00
echo "INFO: The lxd project specified in the cluster_definition did not exist. We'll create one!"
2023-02-01 21:02:38 +00:00
lxc project create "$PROJECT_NAME"
2023-03-02 14:46:17 +00:00
lxc project set "$PROJECT_NAME" features.networks=true
fi
2023-02-01 19:44:05 +00:00
# # check if we need to provision a new lxc project.
2023-03-02 14:46:17 +00:00
# if ! lxc info | grep "project:" | grep -q "$PROJECT_NAME"; then
# echo "INFO: switch to lxd project '$PROJECT_NAME'."
# lxc project switch "$PROJECT_NAME"
2023-02-01 19:44:05 +00:00
# fi
2022-11-14 01:26:51 +00:00
# check to see if the enf file exists. exist if not.
PROJECT_DEFINITION_PATH="$PROJECT_PATH/project_definition"
if [ ! -f "$PROJECT_DEFINITION_PATH" ]; then
2022-11-15 21:00:08 +00:00
# stub out a project_definition
2022-11-14 01:26:51 +00:00
cat >"$PROJECT_DEFINITION_PATH" <<EOL
#!/bin/bash
# see https://www.sovereign-stack.org/project-definition for more info.
2023-02-01 19:44:05 +00:00
export WWW_SERVER_MAC_ADDRESS=
export BTCPAYSERVER_MAC_ADDRESS=
export PRIMARY_DOMAIN=
#export OTHER_SITES_LIST="domain1.tld,domain2.tld,domain3.tld"
export BTCPAY_SERVER_CPU_COUNT="4"
export BTCPAY_SERVER_MEMORY_MB="4096"
export WWW_SERVER_CPU_COUNT="6"
export WWW_SERVER_MEMORY_MB="4096"
2022-11-14 01:26:51 +00:00
EOL
chmod 0744 "$PROJECT_DEFINITION_PATH"
2023-02-01 19:44:05 +00:00
echo "INFO: we stubbed a new project_defition for you at '$PROJECT_DEFINITION_PATH'. Go update it!"
echo "INFO: Learn more at https://www.sovereign-stack.org/projects/"
2022-11-14 01:26:51 +00:00
exit 1
fi
# source project defition.
source "$PROJECT_DEFINITION_PATH"
2023-02-01 19:44:05 +00:00
if [ -z "$PRIMARY_DOMAIN" ]; then
echo "ERROR: The PRIMARY_DOMAIN is not specified. Check your project_definition."
exit 1
fi
if [ -z "$WWW_SERVER_MAC_ADDRESS" ]; then
echo "ERROR: the WWW_SERVER_MAC_ADDRESS is not specified. Check your project_definition."
exit 1
fi
if [ -z "$BTCPAYSERVER_MAC_ADDRESS" ]; then
echo "ERROR: the BTCPAYSERVER_MAC_ADDRESS is not specified. Check your project_definition."
exit 1
fi
# the DOMAIN_LIST is a complete list of all our domains. We often iterate over this list.
DOMAIN_LIST="${PRIMARY_DOMAIN}"
if [ -n "$OTHER_SITES_LIST" ]; then
DOMAIN_LIST="${DOMAIN_LIST},${OTHER_SITES_LIST}"
fi
export DOMAIN_LIST="$DOMAIN_LIST"
2022-11-14 01:26:51 +00:00
export DOMAIN_COUNT=$(("$(echo "$DOMAIN_LIST" | tr -cd , | wc -c)"+1))
# let's provision our primary domain first.
export DOMAIN_NAME="$PRIMARY_DOMAIN"
export SITE_PATH="$SITES_PATH/$DOMAIN_NAME"
export PRIMARY_WWW_FQDN="$WWW_HOSTNAME.$DOMAIN_NAME"
2023-02-01 21:03:07 +00:00
stub_site_definition
2023-02-01 21:03:33 +00:00
# bring the VMs up under the primary domain name.
2023-03-06 17:47:45 +00:00
export UPDATE_BTCPAY="$UPDATE_BTCPAY"
export RECONFIGURE_BTCPAY_SERVER="$RECONFIGURE_BTCPAY_SERVER"
# iterate over all our server endpoints and provision them if needed.
# www
VPS_HOSTNAME=
for VIRTUAL_MACHINE in www btcpayserver; do
export VIRTUAL_MACHINE="$VIRTUAL_MACHINE"
FQDN=
export SITE_PATH="$SITES_PATH/$DOMAIN_NAME"
source "$SITE_PATH/site_definition"
source ./domain_env.sh
# VALIDATE THE INPUT from the ENVFILE
if [ -z "$DOMAIN_NAME" ]; then
echo "ERROR: DOMAIN_NAME not specified. Use the --domain-name= option."
exit 1
fi
# # switch to the default project
# if ! lxc project list --format csv | grep -a "default (current)"; then
# lxc project switch default
# fi
# Goal is to get the macvlan interface.
LXD_SS_CONFIG_LINE=
if lxc network list --format csv | grep lxdbr0 | grep -q ss-config; then
LXD_SS_CONFIG_LINE="$(lxc network list --format csv | grep lxdbr0 | grep ss-config)"
fi
if [ -z "$LXD_SS_CONFIG_LINE" ]; then
echo "ERROR: the MACVLAN interface has not been specified. You may need to run ss-cluster again."
exit 1
fi
CONFIG_ITEMS="$(echo "$LXD_SS_CONFIG_LINE" | awk -F'"' '{print $2}')"
DATA_PLANE_MACVLAN_INTERFACE="$(echo "$CONFIG_ITEMS" | cut -d ',' -f2)"
export DATA_PLANE_MACVLAN_INTERFACE="$DATA_PLANE_MACVLAN_INTERFACE"
# # switch to the default project to ensure the base image is created.
# if ! lxc project list --format csv | grep -a "default (current)"; then
# lxc project switch default
# fi
# create the lxd base image.
./create_lxc_base.sh
# # now switch to the current chain project.
# if ! lxc project list --format csv | grep -a "$BITCOIN_CHAIN"; then
# lxc project switch "$BITCOIN_CHAIN"
# fi
export MAC_ADDRESS_TO_PROVISION=
export VPS_HOSTNAME="$VPS_HOSTNAME"
export FQDN="$VPS_HOSTNAME.$DOMAIN_NAME"
DDNS_HOST=
if [ "$VIRTUAL_MACHINE" = www ]; then
if [ "$SKIP_WWW" = true ]; then
echo "INFO: Skipping WWW due to command line argument."
continue
fi
VPS_HOSTNAME="$WWW_HOSTNAME"
MAC_ADDRESS_TO_PROVISION="$WWW_SERVER_MAC_ADDRESS"
DDNS_HOST="$WWW_HOSTNAME"
ROOT_DISK_SIZE_GB="$((ROOT_DISK_SIZE_GB + NEXTCLOUD_SPACE_GB))"
elif [ "$VIRTUAL_MACHINE" = btcpayserver ] || [ "$SKIP_BTCPAY" = true ]; then
DDNS_HOST="$BTCPAY_HOSTNAME"
VPS_HOSTNAME="$BTCPAY_HOSTNAME"
MAC_ADDRESS_TO_PROVISION="$BTCPAYSERVER_MAC_ADDRESS"
if [ "$BITCOIN_CHAIN" = mainnet ]; then
ROOT_DISK_SIZE_GB=150
elif [ "$BITCOIN_CHAIN" = testnet ]; then
ROOT_DISK_SIZE_GB=70
fi
elif [ "$VIRTUAL_MACHINE" = "$BASE_IMAGE_VM_NAME" ]; then
DDNS_HOST="$BASE_IMAGE_VM_NAME"
ROOT_DISK_SIZE_GB=8
else
echo "ERROR: VIRTUAL_MACHINE not within allowable bounds."
exit
fi
export DDNS_HOST="$DDNS_HOST"
export FQDN="$DDNS_HOST.$DOMAIN_NAME"
export LXD_VM_NAME="${FQDN//./-}"
export VIRTUAL_MACHINE="$VIRTUAL_MACHINE"
export REMOTE_CERT_DIR="$REMOTE_CERT_BASE_DIR/$FQDN"
export MAC_ADDRESS_TO_PROVISION="$MAC_ADDRESS_TO_PROVISION"
./deploy_vms.sh
if [ "$VIRTUAL_MACHINE" = www ]; then
# this tells our local docker client to target the remote endpoint via SSH
export DOCKER_HOST="ssh://ubuntu@$PRIMARY_WWW_FQDN"
# enable docker swarm mode so we can support docker stacks.
if docker info | grep -q "Swarm: inactive"; then
docker swarm init --advertise-addr enp6s0
fi
fi
done
2023-02-01 21:03:33 +00:00
# let's stub out the rest of our site definitions, if any.
for DOMAIN_NAME in ${OTHER_SITES_LIST//,/ }; do
export DOMAIN_NAME="$DOMAIN_NAME"
export SITE_PATH="$SITES_PATH/$DOMAIN_NAME"
# stub out the site_defition if it's doesn't exist.
stub_site_definition
done
2022-05-24 18:19:26 +00:00
2023-02-01 20:24:03 +00:00
# now let's run the www and btcpay-specific provisioning scripts.
2023-03-05 16:50:22 +00:00
if [ "$SKIP_WWW" = false ]; then
2023-03-06 17:47:45 +00:00
./www/go.sh
2023-02-01 20:24:03 +00:00
ssh ubuntu@"$PRIMARY_WWW_FQDN" "echo $LATEST_GIT_COMMIT > /home/ubuntu/.ss-githead"
fi
export DOMAIN_NAME="$PRIMARY_DOMAIN"
export SITE_PATH="$SITES_PATH/$DOMAIN_NAME"
2023-03-05 16:50:22 +00:00
if [ "$SKIP_BTCPAY" = false ]; then
2023-03-02 14:46:17 +00:00
./btcpayserver/go.sh
2023-02-01 20:24:03 +00:00
ssh ubuntu@"$BTCPAY_FQDN" "echo $LATEST_GIT_COMMIT > /home/ubuntu/.ss-githead"
2023-02-01 19:44:05 +00:00
fi