1
1
sovereign-stack/deployment/deploy_vm.sh

130 lines
4.5 KiB
Bash
Raw Normal View History

2023-04-07 14:23:04 +00:00
#!/bin/bash
2023-09-15 23:28:48 +00:00
set -exu
2023-04-07 14:23:04 +00:00
cd "$(dirname "$0")"
2023-04-07 18:02:24 +00:00
. ./base.sh
2023-04-07 14:23:04 +00:00
## This is a weird if clause since we need to LEFT-ALIGN the statement below.
SSH_STRING="Host ${FQDN}"
if ! grep -q "$SSH_STRING" "$SSH_HOME/config"; then
########## BEGIN
2023-04-12 14:09:47 +00:00
cat >> "$SSH_HOME/config" <<-EOF
2023-04-07 14:23:04 +00:00
${SSH_STRING}
HostName ${FQDN}
User ubuntu
EOF
###
fi
# if the machine doesn't exist, we create it.
2023-11-29 19:04:24 +00:00
if ! incus list --format csv | grep -q "$INCUS_VM_NAME"; then
2023-04-07 14:23:04 +00:00
# create a base image if needed and instantiate a VM.
if [ -z "$MAC_ADDRESS_TO_PROVISION" ]; then
2023-08-12 16:25:23 +00:00
echo "ERROR: You MUST define a MAC Address for all your machines in your project definition."
2023-04-07 14:23:04 +00:00
echo "INFO: IMPORTANT! You MUST have DHCP Reservations for these MAC addresses. You also need records established the DNS."
exit 1
fi
# TODO ensure we are only GROWING the volume--never shrinking per zfs volume docs.
VM_ID=
BACKUP_DISK_SIZE_GB=
SSDATA_DISK_SIZE_GB=
DOCKER_DISK_SIZE_GB=
if [ "$VIRTUAL_MACHINE" = www ]; then
2023-08-12 16:14:00 +00:00
if [ "$SKIP_WWW" = true ]; then
exit 0
fi
2023-04-07 14:23:04 +00:00
VM_ID="w"
BACKUP_DISK_SIZE_GB="$WWW_BACKUP_DISK_SIZE_GB"
SSDATA_DISK_SIZE_GB="$WWW_SSDATA_DISK_SIZE_GB"
DOCKER_DISK_SIZE_GB="$WWW_DOCKER_DISK_SIZE_GB"
fi
if [ "$VIRTUAL_MACHINE" = btcpayserver ]; then
2023-08-12 16:14:00 +00:00
if [ "$SKIP_BTCPAYSERVER" = true ]; then
exit 0
fi
2023-04-07 14:23:04 +00:00
VM_ID="b"
BACKUP_DISK_SIZE_GB="$BTCPAYSERVER_BACKUP_DISK_SIZE_GB"
SSDATA_DISK_SIZE_GB="$BTCPAYSERVER_SSDATA_DISK_SIZE_GB"
DOCKER_DISK_SIZE_GB="$BTCPAYSERVER_DOCKER_DISK_SIZE_GB"
fi
2023-09-06 02:01:57 +00:00
if [ "$VIRTUAL_MACHINE" = lnplayserver ]; then
if [ "$SKIP_LNPLAY_SERVER" = true ]; then
2023-08-12 16:14:00 +00:00
exit 0
fi
VM_ID="c"
BACKUP_DISK_SIZE_GB="$BTCPAYSERVER_BACKUP_DISK_SIZE_GB"
SSDATA_DISK_SIZE_GB="$BTCPAYSERVER_SSDATA_DISK_SIZE_GB"
DOCKER_DISK_SIZE_GB="$BTCPAYSERVER_DOCKER_DISK_SIZE_GB"
fi
2023-04-07 14:23:04 +00:00
DOCKER_VOLUME_NAME="$PRIMARY_DOMAIN_IDENTIFIER-$VM_ID""d"
2023-09-22 23:46:07 +00:00
if ! incus storage volume list ss-base | grep -q "$DOCKER_VOLUME_NAME"; then
incus storage volume create ss-base "$DOCKER_VOLUME_NAME" --type=block
2023-04-07 14:23:04 +00:00
fi
# TODO ensure we are only GROWING the volume--never shrinking
2023-09-22 23:46:07 +00:00
incus storage volume set ss-base "$DOCKER_VOLUME_NAME" size="${DOCKER_DISK_SIZE_GB}GB"
2023-04-07 14:23:04 +00:00
SSDATA_VOLUME_NAME="$PRIMARY_DOMAIN_IDENTIFIER-$VM_ID""s"
2023-09-22 23:46:07 +00:00
if ! incus storage volume list ss-base | grep -q "$SSDATA_VOLUME_NAME"; then
incus storage volume create ss-base "$SSDATA_VOLUME_NAME" --type=filesystem
2023-04-07 14:23:04 +00:00
fi
# TODO ensure we are only GROWING the volume--never shrinking per zfs volume docs.
2023-09-22 23:46:07 +00:00
incus storage volume set ss-base "$SSDATA_VOLUME_NAME" size="${SSDATA_DISK_SIZE_GB}GB"
2023-04-07 14:23:04 +00:00
BACKUP_VOLUME_NAME="$PRIMARY_DOMAIN_IDENTIFIER-$VM_ID""b"
2023-09-22 23:46:07 +00:00
if ! incus storage volume list ss-base | grep -q "$BACKUP_VOLUME_NAME"; then
incus storage volume create ss-base "$BACKUP_VOLUME_NAME" --type=filesystem
2023-04-07 14:23:04 +00:00
fi
2023-09-22 23:46:07 +00:00
incus storage volume set ss-base "$BACKUP_VOLUME_NAME" size="${BACKUP_DISK_SIZE_GB}GB"
2023-04-07 14:23:04 +00:00
2023-11-30 02:33:45 +00:00
bash -c "./stub_profile.sh --vm=$VIRTUAL_MACHINE --incus-hostname=$INCUS_VM_NAME --ss-volume-name=$SSDATA_VOLUME_NAME --backup-volume-name=$BACKUP_VOLUME_NAME"
2023-04-07 14:23:04 +00:00
# now let's create a new VM to work with.
2023-11-29 19:04:24 +00:00
#incus init -q --profile="$INCUS_VM_NAME" "$BASE_IMAGE_VM_NAME" "$INCUS_VM_NAME" --vm
incus init "$DOCKER_BASE_IMAGE_NAME" "$INCUS_VM_NAME" --vm --profile="$INCUS_VM_NAME"
2023-04-07 14:23:04 +00:00
# let's PIN the HW address for now so we don't exhaust IP
# and so we can set DNS internally.
2023-11-29 19:04:24 +00:00
incus config set "$INCUS_VM_NAME" "volatile.enp5s0.hwaddr=$MAC_ADDRESS_TO_PROVISION"
2023-04-07 14:23:04 +00:00
# attack the docker block device.
2023-11-29 19:04:24 +00:00
incus storage volume attach ss-base "$DOCKER_VOLUME_NAME" "$INCUS_VM_NAME"
2023-04-07 14:23:04 +00:00
# if [ "$VIRTUAL_MACHINE" = btcpayserver ]; then
# # attach any volumes
# for CHAIN in testnet mainnet; do
# for DATA in blocks chainstate; do
# MOUNT_PATH="/$CHAIN-$DATA"
2023-11-29 19:04:24 +00:00
# incus config device add "$INCUS_VM_NAME" "$CHAIN-$DATA" disk pool=ss-base source="$CHAIN-$DATA" path="$MOUNT_PATH"
2023-04-07 14:23:04 +00:00
# done
# done
# fi
2023-11-29 19:04:24 +00:00
incus start "$INCUS_VM_NAME"
2023-04-07 14:23:04 +00:00
sleep 10
2023-11-30 02:33:45 +00:00
bash -c "./wait_for_ip.sh --incus-name=$INCUS_VM_NAME"
2023-04-07 14:23:04 +00:00
# scan the remote machine and install it's identity in our SSH known_hosts file.
2023-04-12 14:05:25 +00:00
ssh-keyscan -H "$FQDN" >> "$SSH_HOME/known_hosts"
2023-04-07 14:23:04 +00:00
ssh "$FQDN" "sudo chown ubuntu:ubuntu $REMOTE_DATA_PATH"
ssh "$FQDN" "sudo chown -R ubuntu:ubuntu $REMOTE_BACKUP_PATH"
fi