2023-03-06 19:30:56 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-03-13 17:38:14 +00:00
|
|
|
set -eu
|
2023-03-06 19:30:56 +00:00
|
|
|
cd "$(dirname "$0")"
|
|
|
|
|
2023-03-09 14:53:07 +00:00
|
|
|
bash -c "./stub_lxc_profile.sh --lxd-hostname=$BASE_IMAGE_VM_NAME"
|
2023-03-06 19:30:56 +00:00
|
|
|
|
|
|
|
# let's download our base image.
|
|
|
|
if ! lxc image list --format csv --columns l | grep -q "$UBUNTU_BASE_IMAGE_NAME"; then
|
2023-03-20 16:58:33 +00:00
|
|
|
# if the image if cached locally, import it from disk, otherwise download it from ubuntu
|
|
|
|
IMAGE_IDENTIFIER=$(find "$SS_JAMMY_PATH" | grep ".qcow2" | head -n1 | cut -d "." -f1)
|
|
|
|
METADATA_FILE="$SS_JAMMY_PATH/meta-$IMAGE_IDENTIFIER.tar.xz"
|
|
|
|
IMAGE_FILE="$SS_JAMMY_PATH/$IMAGE_IDENTIFIER.qcow2"
|
|
|
|
if [ -d "$SS_JAMMY_PATH" ] && [ -f "$METADATA_FILE" ] && [ -f "$IMAGE_FILE" ]; then
|
|
|
|
lxc image import "$METADATA_FILE" "$IMAGE_FILE" --alias "$UBUNTU_BASE_IMAGE_NAME"
|
2023-03-18 15:03:53 +00:00
|
|
|
else
|
|
|
|
# copy the image down from canonical.
|
2023-03-18 15:06:36 +00:00
|
|
|
lxc image copy "images:$BASE_LXC_IMAGE" "$REMOTE_NAME": --alias "$UBUNTU_BASE_IMAGE_NAME" --public --vm --auto-update
|
2023-03-18 15:03:53 +00:00
|
|
|
fi
|
2023-03-06 19:30:56 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If the lxc VM does exist, then we will delete it (so we can start fresh)
|
2023-03-08 22:12:36 +00:00
|
|
|
if lxc list --format csv -q | grep -q "$UBUNTU_BASE_IMAGE_NAME"; then
|
2023-03-06 19:30:56 +00:00
|
|
|
# if there's no snapshot, we dispense with the old image and try again.
|
2023-03-08 22:12:36 +00:00
|
|
|
if ! lxc info "$BASE_IMAGE_VM_NAME" | grep -q "$UBUNTU_BASE_IMAGE_NAME"; then
|
2023-03-06 19:30:56 +00:00
|
|
|
lxc delete "$BASE_IMAGE_VM_NAME" --force
|
|
|
|
ssh-keygen -f "$SSH_HOME/known_hosts" -R "$BASE_IMAGE_VM_NAME"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# the base image is ubuntu:22.04.
|
|
|
|
lxc init --profile="$BASE_IMAGE_VM_NAME" "$UBUNTU_BASE_IMAGE_NAME" "$BASE_IMAGE_VM_NAME" --vm
|
|
|
|
|
|
|
|
# TODO move this sovereign-stack-base construction VM to separate dedicated IP
|
|
|
|
lxc config set "$BASE_IMAGE_VM_NAME"
|
|
|
|
|
2023-03-18 15:02:03 +00:00
|
|
|
for CHAIN in mainnet testnet; do
|
|
|
|
for DATA in blocks chainstate; do
|
|
|
|
lxc storage volume attach ss-base "$CHAIN-$DATA" "$BASE_IMAGE_VM_NAME" "/home/ubuntu/.ss/cache/bitcoin/$CHAIN/$DATA"
|
|
|
|
done
|
|
|
|
done
|
2023-03-16 19:48:28 +00:00
|
|
|
|
2023-03-06 19:30:56 +00:00
|
|
|
lxc start "$BASE_IMAGE_VM_NAME"
|
|
|
|
|
2023-03-08 22:12:36 +00:00
|
|
|
sleep 15
|
|
|
|
while lxc exec "$BASE_IMAGE_VM_NAME" -- [ ! -f /var/lib/cloud/instance/boot-finished ]; do
|
|
|
|
sleep 1
|
|
|
|
done
|
2023-03-06 19:30:56 +00:00
|
|
|
|
|
|
|
# ensure the ssh service is listening at localhost
|
2023-03-08 22:12:36 +00:00
|
|
|
lxc exec "$BASE_IMAGE_VM_NAME" -- wait-for-it -t 100 127.0.0.1:22
|
2023-03-06 19:30:56 +00:00
|
|
|
|
2023-03-18 15:02:03 +00:00
|
|
|
# If we have any chaninstate or blocks in our SSME, let's push them to the
|
|
|
|
# remote host as a zfs volume that way deployments can share a common history
|
|
|
|
# of chainstate/blocks.
|
|
|
|
for CHAIN in testnet mainnet; do
|
|
|
|
for DATA in blocks chainstate; do
|
2023-03-18 22:12:09 +00:00
|
|
|
# if the storage snapshot doesn't yet exist, create it.
|
|
|
|
if ! lxc storage volume list ss-base -q --format csv -c n | grep -q "$CHAIN-$DATA/snap0"; then
|
|
|
|
DATA_PATH="/home/ubuntu/.ss/cache/bitcoin/$CHAIN/$DATA"
|
|
|
|
if [ -d "$DATA_PATH" ]; then
|
|
|
|
COMPLETE_FILE_PATH="$DATA_PATH/complete"
|
|
|
|
if lxc exec "$BASE_IMAGE_VM_NAME" -- [ ! -f "$COMPLETE_FILE_PATH" ]; then
|
2023-03-19 17:43:48 +00:00
|
|
|
lxc file push --recursive --project=default "$DATA_PATH/" "$BASE_IMAGE_VM_NAME""$DATA_PATH/"
|
2023-03-18 22:12:09 +00:00
|
|
|
lxc exec "$BASE_IMAGE_VM_NAME" -- su ubuntu - bash -c "echo $(date) > $COMPLETE_FILE_PATH"
|
2023-03-19 17:43:48 +00:00
|
|
|
lxc exec "$BASE_IMAGE_VM_NAME" -- chown -R 999:999 "$DATA_PATH/$DATA"
|
2023-03-18 22:12:09 +00:00
|
|
|
else
|
|
|
|
echo "INFO: it appears as though $CHAIN/$DATA has already been initialized. Continuing."
|
|
|
|
fi
|
2023-03-18 15:02:03 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
2023-03-16 19:48:28 +00:00
|
|
|
|
2023-03-06 19:30:56 +00:00
|
|
|
# stop the VM and get a snapshot.
|
|
|
|
lxc stop "$BASE_IMAGE_VM_NAME"
|
2023-03-08 22:12:36 +00:00
|
|
|
lxc snapshot "$BASE_IMAGE_VM_NAME" "$UBUNTU_BASE_IMAGE_NAME"
|
|
|
|
|
2023-03-06 19:30:56 +00:00
|
|
|
fi
|
2023-03-06 20:26:08 +00:00
|
|
|
|
2023-03-16 20:42:58 +00:00
|
|
|
echo "INFO: Publishing '$BASE_IMAGE_VM_NAME' as image '$DOCKER_BASE_IMAGE_NAME'. Please wait."
|
2023-03-22 20:32:47 +00:00
|
|
|
lxc publish --public "$BASE_IMAGE_VM_NAME/$UBUNTU_BASE_IMAGE_NAME" --project=default --alias="$DOCKER_BASE_IMAGE_NAME" --compression none
|
2023-03-16 19:48:28 +00:00
|
|
|
|
2023-03-18 22:12:09 +00:00
|
|
|
echo "INFO: Success creating the base image. Deleting artifacts from the build process."
|
2023-03-16 19:48:28 +00:00
|
|
|
lxc delete -f "$BASE_IMAGE_VM_NAME"
|
2023-03-18 15:02:03 +00:00
|
|
|
|
|
|
|
# now let's get a snapshot of each of the blocks/chainstate directories.
|
|
|
|
for CHAIN in testnet mainnet; do
|
|
|
|
for DATA in blocks chainstate; do
|
2023-03-18 22:12:09 +00:00
|
|
|
if ! lxc storage volume list ss-base -q --format csv -c n | grep -q "$CHAIN-$DATA/snap0"; then
|
|
|
|
echo "INFO: Creating a snapshot 'ss-base/$CHAIN-$DATA/snap0'."
|
|
|
|
lxc storage volume snapshot ss-base --project=default "$CHAIN-$DATA"
|
|
|
|
fi
|
2023-03-18 15:02:03 +00:00
|
|
|
done
|
|
|
|
done
|