97 lines
3.6 KiB
Bash
Executable File
97 lines
3.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -eu
|
|
cd "$(dirname "$0")"
|
|
|
|
## 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
|
|
cat >> "$SSH_HOME/config" <<-EOF
|
|
|
|
${SSH_STRING}
|
|
HostName ${FQDN}
|
|
User ubuntu
|
|
EOF
|
|
###
|
|
|
|
fi
|
|
|
|
ssh-keygen -f "$SSH_HOME/known_hosts" -R "$FQDN"
|
|
|
|
# if the machine doesn't exist, we create it.
|
|
if ! lxc list --format csv | grep -q "$LXD_VM_NAME"; then
|
|
|
|
# create a base image if needed and instantiate a VM.
|
|
if [ -z "$MAC_ADDRESS_TO_PROVISION" ]; then
|
|
echo "ERROR: You MUST define a MAC Address for all your machines by setting WWW_SERVER_MAC_ADDRESS, BTCPAYSERVER_MAC_ADDRESS in your site definition."
|
|
echo "INFO: IMPORTANT! You MUST have DHCP Reservations for these MAC addresses. You also need records established the DNS."
|
|
exit 1
|
|
fi
|
|
|
|
# create the docker volume
|
|
VM_ID=w
|
|
if [ "$VIRTUAL_MACHINE" = btcpayserver ]; then
|
|
VM_ID="b"
|
|
fi
|
|
|
|
DOCKER_VOLUME_NAME="$PRIMARY_DOMAIN_IDENTIFIER-$VM_ID""d"
|
|
export DOCKER_VOLUME_NAME="$DOCKER_VOLUME_NAME"
|
|
if ! lxc storage volume list ss-base | grep -q "$DOCKER_VOLUME_NAME"; then
|
|
lxc storage volume create ss-base "$DOCKER_VOLUME_NAME" --type=filesystem
|
|
lxc storage volume set ss-base "$DOCKER_VOLUME_NAME" size="${ROOT_DISK_SIZE_GB}GB"
|
|
fi
|
|
|
|
bash -c "./stub_lxc_profile.sh --vm=$VIRTUAL_MACHINE --lxd-hostname=$LXD_VM_NAME --docker-volume-name=$DOCKER_VOLUME_NAME"
|
|
|
|
# now let's create a new VM to work with.
|
|
#lxc init --profile="$LXD_VM_NAME" "$BASE_IMAGE_VM_NAME" "$LXD_VM_NAME" --vm
|
|
lxc init "$DOCKER_BASE_IMAGE_NAME" "$LXD_VM_NAME" --vm --profile="$LXD_VM_NAME"
|
|
|
|
# let's PIN the HW address for now so we don't exhaust IP
|
|
# and so we can set DNS internally.
|
|
lxc config set "$LXD_VM_NAME" "volatile.enp5s0.hwaddr=$MAC_ADDRESS_TO_PROVISION"
|
|
#lxc config device override "$LXD_VM_NAME" root size="${ROOT_DISK_SIZE_GB}GB"
|
|
|
|
#lxc storage volume attach ss-base "$DOCKER_VOLUME_NAME" "$LXD_VM_NAME"
|
|
|
|
# HOME_VOLUME_NAME="$PRIMARY_DOMAIN_IDENTIFIER-$VM_ID""h"
|
|
# if ! lxc storage volume list ss-base | grep -q "$HOME_VOLUME_NAME"; then
|
|
# lxc storage volume create ss-base "$HOME_VOLUME_NAME" --type=block
|
|
# lxc storage volume set ss-base "$HOME_VOLUME_NAME" size="${ROOT_DISK_SIZE_GB}GB"
|
|
# fi
|
|
|
|
#lxc storage volume attach ss-base "$HOME_VOLUME_NAME" "$LXD_VM_NAME"
|
|
|
|
# if [ "$VIRTUAL_MACHINE" = btcpayserver ]; then
|
|
# # attach any volumes
|
|
# for CHAIN in testnet mainnet; do
|
|
# for DATA in blocks chainstate; do
|
|
# MOUNT_PATH="/$CHAIN-$DATA"
|
|
# lxc config device add "$LXD_VM_NAME" "$CHAIN-$DATA" disk pool=ss-base source="$CHAIN-$DATA" path="$MOUNT_PATH"
|
|
# done
|
|
# done
|
|
# fi
|
|
|
|
lxc start "$LXD_VM_NAME"
|
|
sleep 10
|
|
|
|
bash -c "./wait_for_lxc_ip.sh --lxd-name=$LXD_VM_NAME"
|
|
|
|
# scan the remote machine and install it's identity in our SSH known_hosts file.
|
|
ssh-keyscan -H -t ecdsa "$FQDN" >> "$SSH_HOME/known_hosts"
|
|
|
|
|
|
# create a directory to store backup archives. This is on all new vms.
|
|
ssh "$FQDN" "mkdir -p $REMOTE_HOME/backups"
|
|
|
|
ssh "$FQDN" "sudo chown -R ubuntu:ubuntu $REMOTE_DATA_PATH"
|
|
|
|
if [ "$VIRTUAL_MACHINE" = btcpayserver ]; then
|
|
# send an updated ~/.bashrc so we have quicker access to cli tools
|
|
scp ./btcpayserver/bashrc.txt "ubuntu@$FQDN:$REMOTE_HOME/.bashrc"
|
|
ssh "$BTCPAY_FQDN" "chown ubuntu:ubuntu $REMOTE_HOME/.bashrc"
|
|
ssh "$BTCPAY_FQDN" "chmod 0664 $REMOTE_HOME/.bashrc"
|
|
fi
|
|
fi |