Refactor code

This commit is contained in:
whiskerz007 2020-03-20 15:45:08 +01:00
parent 46af8a34b4
commit be78536fed
No known key found for this signature in database
GPG Key ID: A7EEAB6E1F1A6805

View File

@ -28,11 +28,11 @@ function msg() {
echo -e "$TEXT" echo -e "$TEXT"
} }
function cleanup() { function cleanup() {
popd >/dev/null popd >/dev/null
rm -rf $TMP rm -rf $TEMP_DIR
} }
TMP=`mktemp -d` TEMP_DIR=$(mktemp -d)
pushd $TMP >/dev/null pushd $TEMP_DIR >/dev/null
# Select storage location # Select storage location
while read -r line; do while read -r line; do
@ -73,20 +73,20 @@ import requests
url = 'https://api.github.com/repos/home-assistant/hassos/releases/latest' url = 'https://api.github.com/repos/home-assistant/hassos/releases/latest'
r = requests.get(url).json() r = requests.get(url).json()
if 'message' in r: if 'message' in r:
exit() exit()
for asset in r['assets']: for asset in r['assets']:
if asset['name'].endswith('$RELEASE_EXT'): if asset['name'].endswith('$RELEASE_EXT'):
print(asset['browser_download_url']) print(asset['browser_download_url'])
EOF EOF
) )
if [ -z "$URL" ]; then if [ -z "$URL" ]; then
die "Github has returned an error. A rate limit may have been applied to your connection." die "Github has returned an error. A rate limit may have been applied to your connection."
fi fi
# Download Home Assistant disk image archive # Download Home Assistant disk image archive
msg "Downloading disk image..." msg "Downloading disk image..."
wget -q --show-progress $URL wget -q --show-progress $URL
msg "\e[1A\e[0K\e[1A" #Overwrite output from wget echo -en "\e[1A\e[0K" #Overwrite output from wget
FILE=$(basename $URL) FILE=$(basename $URL)
# Extract Home Assistant disk image # Extract Home Assistant disk image
@ -94,16 +94,16 @@ msg "Extracting disk image..."
gunzip -f $FILE gunzip -f $FILE
# Create variables for container disk # Create variables for container disk
STORAGE_TYPE=`pvesm status -storage $STORAGE | awk 'NR>1 {print $2}'` STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
if [ "$STORAGE_TYPE" = "dir" ]; then if [ "$STORAGE_TYPE" = "dir" ]; then
DISK_EXT=".qcow2" DISK_EXT=".qcow2"
DISK_REF="$VMID/" DISK_REF="$VMID/"
IMPORT_OPT="-format qcow2" IMPORT_OPT="-format qcow2"
fi fi
for i in {0,1}; do for i in {0,1}; do
disk="DISK$i" disk="DISK$i"
eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-} eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk} eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
done done
# Create VM # Create VM
@ -114,6 +114,6 @@ qm create $VMID -agent 1 -bios ovmf -name $VM_NAME -net0 virtio,bridge=vmbr0 \
pvesm alloc $STORAGE $VMID $DISK0 128 1>&/dev/null pvesm alloc $STORAGE $VMID $DISK0 128 1>&/dev/null
qm importdisk $VMID ${FILE%".gz"} $STORAGE ${IMPORT_OPT:-} 1>&/dev/null qm importdisk $VMID ${FILE%".gz"} $STORAGE ${IMPORT_OPT:-} 1>&/dev/null
qm set $VMID -bootdisk sata0 -efidisk0 ${DISK0_REF},size=128K \ qm set $VMID -bootdisk sata0 -efidisk0 ${DISK0_REF},size=128K \
-sata0 ${DISK1_REF},size=6G > /dev/null -sata0 ${DISK1_REF},size=6G > /dev/null
info "Completed Successfully! New VM ID is \e[1m$VMID\e[0m." info "Completed Successfully! New VM ID is \e[1m$VMID\e[0m."