distrobuilder-create/distrobuilder-create.sh

88 lines
2.4 KiB
Bash
Raw Normal View History

2021-09-11 08:12:36 +00:00
#!/bin/bash
set -e
# check dependencies
count=0
for dependency in distrobuilder lxc; do
if [[ ! -x "$(command -v $dependency)" ]]; then
echo "$dependency missing"
count=$((count + 1))
fi
done
# exit script if any dependencies missing
if [[ "$count" -ne 0 ]]; then
exit 1
fi
#
# variables
#
DISTROBUILDER_CONF_DIRECTORY="${DISTROBUILDER_CONF_DIRECTORY:-/etc/distrobuilder-create}"
DISTROBUILDER_STATE_DIRECTORY="${DISTROBUILDER_STATE_DIRECTORY:-/var/lib/distrobuilder-create}"
DISTROBUILDER_ARGS=(build-lxd --compression zstd)
IMAGE_NAME="${1:-archlinux}"
IMAGE_RELEASE="${IMAGE_RELEASE:-$(date +%Y.%m).01}"
IMAGE_SSH_PUBKEY="${IMAGE_SSH_PUBKEY:-ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMKobplkqDaKJc+Cp5YNPW3Wsg5Gps+pPY22AX1B93TS deploy@mainframe}"
IMAGE_DESCRIPTION="${IMAGE_DESCRIPTION:-${IMAGE_NAME} (${IMAGE_RELEASE})}"
IMAGE_TYPES=('ct' 'vm')
#
# general housekeeping
#
# check configuration
if [[ ! -d "$DISTROBUILDER_CONF_DIRECTORY" ]]; then
mkdir -vp "$DISTROBUILDER_CONF_DIRECTORY"
elif [[ ! -f "$DISTROBUILDER_CONF_DIRECTORY/$IMAGE_NAME.yml" ]]; then
echo "$IMAGE_NAME.yml does not exist"
exit 1
fi
# ensure clean slate
if [[ ! -d "$DISTROBUILDER_STATE_DIRECTORY/$IMAGE_NAME-$IMAGE_RELEASE" ]]; then
mkdir -vp "$DISTROBUILDER_STATE_DIRECTORY/$IMAGE_NAME-$IMAGE_RELEASE"
else
rm -vrf "$DISTROBUILDER_STATE_DIRECTORY/$IMAGE_NAME-$IMAGE_RELEASE/"*
fi
# create container/virtual-machine directories
cd "$DISTROBUILDER_STATE_DIRECTORY/$IMAGE_NAME-$IMAGE_RELEASE"
mkdir -v "${IMAGE_TYPES[@]}"
#
# image creation
#
# create configuration
sed \
-e "s/@RELEASE@/$IMAGE_RELEASE/" \
-e "s/@SSH_PUBLIC_KEY@/$IMAGE_SSH_PUBKEY/" \
-e "s/@DESCRIPTION@/$IMAGE_DESCRIPTION/" \
"$DISTROBUILDER_CONF_DIRECTORY/$IMAGE_NAME.yml" \
>"$DISTROBUILDER_STATE_DIRECTORY/$IMAGE_NAME-$IMAGE_RELEASE/$IMAGE_NAME.yml"
# create container & virtual-machine images
cd "$DISTROBUILDER_STATE_DIRECTORY/$IMAGE_NAME-$IMAGE_RELEASE"
for IMAGE_TYPE in "${IMAGE_TYPES[@]}"; do
# add VM specific args
if [ "$IMAGE_TYPE" == "vm" ]; then
DISTROBUILDER_ARGS+=(--vm)
fi
cd "$IMAGE_TYPE"
sudo distrobuilder \
"${DISTROBUILDER_ARGS[@]}" \
"$DISTROBUILDER_STATE_DIRECTORY/$IMAGE_NAME-$IMAGE_RELEASE/$IMAGE_NAME.yml"
cd ..
done
# upload images to local lxd instance
sudo lxc image import \
ct/{lxd.tar.zst,rootfs.squashfs} \
--alias "base-$IMAGE_NAME-ct"
sudo lxc image import \
vm/{lxd.tar.zst,disk.qcow2} \
--alias "base-$IMAGE_NAME-vm"