Improve error_exit and add msg function

This commit is contained in:
whiskerz007 2020-03-20 13:47:49 +01:00
parent d90c07835b
commit bb5f311829
No known key found for this signature in database
GPG Key ID: A7EEAB6E1F1A6805

View File

@ -11,16 +11,16 @@ trap die ERR
trap cleanup EXIT trap cleanup EXIT
function error_exit() { function error_exit() {
REASON=$1 trap - ERR
MSG="\e[91mERROR: \e[93m$EXIT@" local DEFAULT='Unknown failure occured.'
if [ -z "$REASON" ]; then local REASON="\e[97m${1:-$DEFAULT}\e[39m"
MSG="$MSG$LINE:" local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
REASON="Unknown failure occured." msg "$FLAG $REASON"
else exit $EXIT
MSG="$MSG`echo $(( $LINE - 1 ))`:" }
fi function msg() {
echo -e "$MSG \e[97m$REASON\e[39m\e[49m" local TEXT="$1"
exit $EXIT echo -e "$TEXT"
} }
function cleanup() { function cleanup() {
popd >/dev/null popd >/dev/null
@ -46,10 +46,10 @@ else:
print(int(last_vm)+1) print(int(last_vm)+1)
EOF EOF
) )
echo -e "\n\n\n" \ msg "
"********************************\n" \ ********************************
"* Getting latest HassOS Info *\n" \ * Getting latest HassOS Info *
"********************************\n" ********************************"
RELEASE_EXT=vmdk.gz RELEASE_EXT=vmdk.gz
URL=$(cat<<EOF | python3 URL=$(cat<<EOF | python3
import requests import requests
@ -65,21 +65,21 @@ 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
echo -e "\n\n\n" \ msg "\n\n\n
"********************************\n" \ ********************************
"* Downloading HassOS *\n" \ * Downloading HassOS *
"********************************\n" ********************************"
wget -q --show-progress $URL wget -q --show-progress $URL
FILE=$(basename $URL) FILE=$(basename $URL)
echo -e "\n\n\n" \ msg "\n\n\n
"********************************\n" \ ********************************
"* Extracting HassOS *\n" \ * Extracting HassOS *
"********************************\n" ********************************"
gunzip -f $FILE gunzip -f $FILE
echo -e "\n\n\n" \ msg "\n\n\n
"********************************\n" \ ********************************
"* Creating new VM *\n" \ * Creating new VM *
"********************************\n" ********************************"
if [ "$STORAGE_TYPE" = "dir" ]; then if [ "$STORAGE_TYPE" = "dir" ]; then
DISK_EXT=".qcow2" DISK_EXT=".qcow2"
DISK_REF="$VMID/" DISK_REF="$VMID/"
@ -96,8 +96,8 @@ 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
echo -e "\n\n\n" \ msg "\n\n\n
"********************************\n" \ ********************************
"* Completed Successfully *\n" \ * Completed Successfully *
"* New VM ID is \e[1m$VMID\e[0m *\n" \ * New VM ID is \e[1m$VMID\e[0m *
"********************************\n" ********************************"