|
#!/bin/bash |
|
|
|
# Copyright 2022, Nogweii |
|
# SPDX-License-Identifier: MIT |
|
|
|
set -euo 'pipefail' |
|
|
|
./download_arch_box.sh |
|
version="v$(cat ./Arch-Linux-x86_64-cloudimg.version.txt)" |
|
raw_title="Raw Arch Linux ${version}" |
|
template_title="Arch Linux ${version}" |
|
zone='us-chi1' |
|
|
|
# Bail early if there is a template already |
|
existing_template_uuid=$(upctl storage list --template -o json | jq -r ".[] | select(.title == \"${template_title}\") | .uuid") |
|
if [ -n "${existing_template_uuid}" ]; then |
|
echo "Already exists! Use this template:" |
|
upctl storage show $existing_template_uuid |
|
exit 0 |
|
fi |
|
|
|
# Block execution until the storage device in UpCloud is online |
|
# $1 [string] The UUID of the storage device |
|
function wait_for_storage() { |
|
storage_uuid="${1}" |
|
while [ "$(upctl storage show $storage_uuid -o json | jq -r '.state')" != 'online' ]; do |
|
sleep 1 |
|
done |
|
} |
|
|
|
# Convert the image from qcow2 (qemu's compressed format) to a raw disk image |
|
qemu-img convert Arch-Linux-x86_64-cloudimg.qcow2 Arch-Linux-x86_64-cloudimg.raw |
|
|
|
# Uplaod the raw disk image to UpCloud, which is imported as a disk image |
|
upctl storage import --source-location ./Arch-Linux-x86_64-cloudimg.raw --title "${raw_title}" --zone $zone -o json --force-colours > storage-import.json |
|
raw_uuid=$(jq -r ".uuid" < storage-import.json) |
|
wait_for_storage $raw_uuid |
|
|
|
# Convert the raw disk image into a template in UpCloud |
|
upctl storage templatise "${raw_uuid}" --title "${template_title}" -o json --force-colours > storage-template.json |
|
template_uuid=$(jq -r ".uuid" < storage-template.json) |
|
wait_for_storage $template_uuid |
|
|
|
# Delete the raw disk, don't need it any more |
|
wait_for_storage $raw_uuid |
|
upctl storage delete "${raw_uuid}" |
|
# and clean up the json files generated |
|
rm storage-import.json storage-template.json |