2020-03-20 12:50:32 +01:00
|
|
|
#!/usr/bin/env bash
|
2020-03-20 13:16:38 +01:00
|
|
|
|
|
|
|
# Setup script environment
|
|
|
|
set -o errexit #Exit immediately if a pipeline returns a non-zero status
|
|
|
|
set -o errtrace #Trap ERR from shell functions, command substitutions, and commands from subshell
|
|
|
|
set -o nounset #Treat unset variables as an error
|
|
|
|
set -o pipefail #Pipe will exit with last non-zero status if applicable
|
2019-03-15 01:18:01 +01:00
|
|
|
shopt -s expand_aliases
|
|
|
|
alias die='EXIT=$? LINE=$LINENO error_exit'
|
|
|
|
trap die ERR
|
2020-03-20 13:16:38 +01:00
|
|
|
trap cleanup EXIT
|
|
|
|
|
2019-03-15 01:18:01 +01:00
|
|
|
function error_exit() {
|
2020-03-20 13:47:49 +01:00
|
|
|
trap - ERR
|
|
|
|
local DEFAULT='Unknown failure occured.'
|
|
|
|
local REASON="\e[97m${1:-$DEFAULT}\e[39m"
|
|
|
|
local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
|
|
|
|
msg "$FLAG $REASON"
|
|
|
|
exit $EXIT
|
|
|
|
}
|
2020-03-20 14:39:06 +01:00
|
|
|
function info() {
|
|
|
|
local REASON="$1"
|
|
|
|
local FLAG="\e[36m[INFO]\e[39m"
|
|
|
|
msg "$FLAG $REASON"
|
|
|
|
}
|
2020-03-20 13:47:49 +01:00
|
|
|
function msg() {
|
|
|
|
local TEXT="$1"
|
|
|
|
echo -e "$TEXT"
|
2019-03-15 01:18:01 +01:00
|
|
|
}
|
|
|
|
function cleanup() {
|
|
|
|
popd >/dev/null
|
|
|
|
rm -rf $TMP
|
|
|
|
}
|
|
|
|
TMP=`mktemp -d`
|
|
|
|
pushd $TMP >/dev/null
|
2019-02-18 15:10:47 +01:00
|
|
|
|
2020-03-20 14:10:14 +01:00
|
|
|
# Select storage location
|
2019-03-15 01:18:01 +01:00
|
|
|
STORAGE=${1:-local-lvm}
|
|
|
|
pvesm list $STORAGE >& /dev/null ||
|
|
|
|
die "'$STORAGE' is not a valid storage ID."
|
|
|
|
pvesm status -content images -storage $STORAGE >&/dev/null ||
|
|
|
|
die "'$STORAGE' does not allow 'Disk image' to be stored."
|
|
|
|
STORAGE_TYPE=`pvesm status -storage $STORAGE | awk 'NR>1 {print $2}'`
|
2020-03-20 14:39:06 +01:00
|
|
|
info "Using '$STORAGE' for storage location."
|
2020-03-20 14:10:14 +01:00
|
|
|
|
|
|
|
# Get the next guest VM/LXC ID
|
2020-03-20 14:42:03 +01:00
|
|
|
VMID=$(pvesh get /cluster/nextid)
|
2020-03-20 14:39:06 +01:00
|
|
|
info "Container ID is $VMID."
|
2020-03-20 14:10:14 +01:00
|
|
|
|
|
|
|
# Get latest Home Assistant disk image archive URL
|
2020-03-20 14:39:06 +01:00
|
|
|
msg "Getting URL for latest Home Assistant disk image..."
|
2019-11-30 04:35:34 +01:00
|
|
|
RELEASE_EXT=vmdk.gz
|
2019-02-18 15:10:47 +01:00
|
|
|
URL=$(cat<<EOF | python3
|
|
|
|
import requests
|
|
|
|
url = 'https://api.github.com/repos/home-assistant/hassos/releases/latest'
|
|
|
|
r = requests.get(url).json()
|
|
|
|
if 'message' in r:
|
2019-03-15 01:18:01 +01:00
|
|
|
exit()
|
2019-02-18 15:10:47 +01:00
|
|
|
for asset in r['assets']:
|
2019-11-30 04:35:34 +01:00
|
|
|
if asset['name'].endswith('$RELEASE_EXT'):
|
2019-03-15 01:18:01 +01:00
|
|
|
print(asset['browser_download_url'])
|
2019-02-18 15:10:47 +01:00
|
|
|
EOF
|
2019-03-15 01:18:01 +01:00
|
|
|
)
|
2019-02-18 15:10:47 +01:00
|
|
|
if [ -z "$URL" ]; then
|
2019-03-15 01:18:01 +01:00
|
|
|
die "Github has returned an error. A rate limit may have been applied to your connection."
|
|
|
|
fi
|
2020-03-20 14:10:14 +01:00
|
|
|
|
|
|
|
# Download Home Assistant disk image archive
|
2020-03-20 14:39:06 +01:00
|
|
|
msg "Downloading disk image..."
|
2019-03-15 01:18:01 +01:00
|
|
|
wget -q --show-progress $URL
|
2020-03-20 14:39:06 +01:00
|
|
|
msg "\e[1A\e[0K\e[1A" #Overwrite output from wget
|
2019-03-15 01:18:01 +01:00
|
|
|
FILE=$(basename $URL)
|
2020-03-20 14:10:14 +01:00
|
|
|
|
|
|
|
# Extract Home Assistant disk image
|
2020-03-20 14:39:06 +01:00
|
|
|
msg "Extracting disk image..."
|
2019-03-15 01:18:01 +01:00
|
|
|
gunzip -f $FILE
|
2020-03-20 14:10:14 +01:00
|
|
|
|
|
|
|
# Create variables for container disk
|
2019-03-15 01:18:01 +01:00
|
|
|
if [ "$STORAGE_TYPE" = "dir" ]; then
|
|
|
|
DISK_EXT=".qcow2"
|
|
|
|
DISK_REF="$VMID/"
|
|
|
|
IMPORT_OPT="-format qcow2"
|
|
|
|
fi
|
|
|
|
for i in {0,1}; do
|
|
|
|
disk="DISK$i"
|
2020-03-20 13:42:27 +01:00
|
|
|
eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
|
|
|
|
eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
|
2019-03-15 01:18:01 +01:00
|
|
|
done
|
2020-03-20 14:10:14 +01:00
|
|
|
|
|
|
|
# Create VM
|
2020-03-20 14:39:06 +01:00
|
|
|
msg "Creating VM..."
|
2019-11-30 04:35:34 +01:00
|
|
|
qm create $VMID -bios ovmf -name $(sed -e "s/\_//g" -e "s/.${RELEASE_EXT}//" <<< $FILE) \
|
2019-03-15 01:18:01 +01:00
|
|
|
-net0 virtio,bridge=vmbr0 -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
|
|
|
|
pvesm alloc $STORAGE $VMID $DISK0 128 1>&/dev/null
|
2020-03-20 13:42:27 +01:00
|
|
|
qm importdisk $VMID ${FILE%".gz"} $STORAGE ${IMPORT_OPT:-} 1>&/dev/null
|
2019-03-15 01:18:01 +01:00
|
|
|
qm set $VMID -bootdisk sata0 -efidisk0 ${DISK0_REF},size=128K \
|
|
|
|
-sata0 ${DISK1_REF},size=6G > /dev/null
|
2020-03-20 14:10:14 +01:00
|
|
|
|
2020-03-20 14:39:06 +01:00
|
|
|
info "Completed Successfully! New VM ID is \e[1m$VMID\e[0m."
|