Add info function and improve script output

This commit is contained in:
whiskerz007 2020-03-20 14:39:06 +01:00
parent f8a261da39
commit 02895331e3
No known key found for this signature in database
GPG Key ID: A7EEAB6E1F1A6805

View File

@ -18,6 +18,11 @@ function error_exit() {
msg "$FLAG $REASON" msg "$FLAG $REASON"
exit $EXIT exit $EXIT
} }
function info() {
local REASON="$1"
local FLAG="\e[36m[INFO]\e[39m"
msg "$FLAG $REASON"
}
function msg() { function msg() {
local TEXT="$1" local TEXT="$1"
echo -e "$TEXT" echo -e "$TEXT"
@ -36,6 +41,7 @@ pvesm list $STORAGE >& /dev/null ||
pvesm status -content images -storage $STORAGE >&/dev/null || pvesm status -content images -storage $STORAGE >&/dev/null ||
die "'$STORAGE' does not allow 'Disk image' to be stored." die "'$STORAGE' does not allow 'Disk image' to be stored."
STORAGE_TYPE=`pvesm status -storage $STORAGE | awk 'NR>1 {print $2}'` STORAGE_TYPE=`pvesm status -storage $STORAGE | awk 'NR>1 {print $2}'`
info "Using '$STORAGE' for storage location."
# Get the next guest VM/LXC ID # Get the next guest VM/LXC ID
VMID=$(cat<<EOF | python3 VMID=$(cat<<EOF | python3
@ -49,12 +55,10 @@ else:
print(int(last_vm)+1) print(int(last_vm)+1)
EOF EOF
) )
info "Container ID is $VMID."
# Get latest Home Assistant disk image archive URL # Get latest Home Assistant disk image archive URL
msg " msg "Getting URL for latest Home Assistant disk image..."
********************************
* Getting latest HassOS Info *
********************************"
RELEASE_EXT=vmdk.gz RELEASE_EXT=vmdk.gz
URL=$(cat<<EOF | python3 URL=$(cat<<EOF | python3
import requests import requests
@ -72,24 +76,15 @@ if [ -z "$URL" ]; then
fi fi
# Download Home Assistant disk image archive # Download Home Assistant disk image archive
msg "\n\n\n msg "Downloading disk image..."
********************************
* Downloading HassOS *
********************************"
wget -q --show-progress $URL wget -q --show-progress $URL
msg "\e[1A\e[0K\e[1A" #Overwrite output from wget
FILE=$(basename $URL) FILE=$(basename $URL)
# Extract Home Assistant disk image # Extract Home Assistant disk image
msg "\n\n\n msg "Extracting disk image..."
********************************
* Extracting HassOS *
********************************"
gunzip -f $FILE gunzip -f $FILE
msg "\n\n\n
********************************
* Creating new VM *
********************************"
# Create variables for container disk # Create variables for container disk
if [ "$STORAGE_TYPE" = "dir" ]; then if [ "$STORAGE_TYPE" = "dir" ]; then
DISK_EXT=".qcow2" DISK_EXT=".qcow2"
@ -103,6 +98,7 @@ for i in {0,1}; do
done done
# Create VM # Create VM
msg "Creating VM..."
qm create $VMID -bios ovmf -name $(sed -e "s/\_//g" -e "s/.${RELEASE_EXT}//" <<< $FILE) \ qm create $VMID -bios ovmf -name $(sed -e "s/\_//g" -e "s/.${RELEASE_EXT}//" <<< $FILE) \
-net0 virtio,bridge=vmbr0 -onboot 1 -ostype l26 -scsihw virtio-scsi-pci -net0 virtio,bridge=vmbr0 -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
pvesm alloc $STORAGE $VMID $DISK0 128 1>&/dev/null pvesm alloc $STORAGE $VMID $DISK0 128 1>&/dev/null
@ -110,8 +106,4 @@ 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
msg "\n\n\n info "Completed Successfully! New VM ID is \e[1m$VMID\e[0m."
********************************
* Completed Successfully *
* New VM ID is \e[1m$VMID\e[0m *
********************************"