#!/bin/sh
VERSION=251218
SCRIPT=$(readlink -f $0)
SCRIPTNAME=$(basename $SCRIPT)
SCRIPTDIR=$(dirname $SCRIPT)
if [ "$1" = "-v" ] ; then echo "$VERSION $SCRIPTNAME $SCRIPTDIR";exit 0; fi
if [ -e "$SCRIPTDIR/esc.sh" ] ; then . "$SCRIPTDIR/esc.sh"; fi
LOG=$LOGDIR/log-$SCRIPTNAME.log

if [ -z "$1" ] ; then
        echo "FILE      - file name to use"
        echo "post      - call this post upgrade to disable maintenance etc."
        echo
        echo "searching in local directory:"
        ls $SCRIPTDIR/*.zip
        exit
else
        file=$1
        len=$(echo "$file" | wc -c)
        start=$(expr $len - 4)
        ext=${file:$start:3}
fi

if [ "$1" == "post" ] ; then
        vmware -vl
        echo "disabling maintenance mode..."
        esxcli system maintenanceMode set --enable false
        echo "starting all configured autostart VMs according to the configured order..."
        allvmsids=$(vim-cmd vmsvc/getallvms | awk '$3 ~ /^\[/ {print $1"|"$2}')
        for id in $(vim-cmd hostsvc/autostartmanager/get_autostartseq | grep -B4 "powerOn" | grep "key =" | cut -d":" -f2 | grep -o "[0-9]\+")
        do
                echo "powering on $id -> $(echo "$allvmsids" | grep -e "^$id|") ..."
                vim-cmd vmsvc/power.on $id
        done
        exit

fi

if [ "$ext" != "zip" ] ; then
        echo "not a valid package name (zip?)"
        exit
fi

firstchar=${file:0:1}
if  [ "$firstchar" != "/" ] ; then
        full=$SCRIPTDIR/$file
        echo "full path modified to:$full"
else
        full=$file
fi

if [ ! -e "$full" ] ; then
        echo "[ERROR] the file was not found in the filesystem"
        exit 1
fi

echo
echo "this will shutdown all VMs, enable the maintenance mode , upgrade your server and reboot"
echo
echo -n "proceed? (y/N): "
read answer
if [ "$answer" != "y" ] ; then exit; fi
echo "shutting down all running VMs..."
#https://knowledge.broadcom.com/external/article/308457/powering-off-an-unresponsive-virtual-mac.html
# TODO extend, see details here to check open tasks and cancel them if any
# vim-cmd vmsvc/get.tasklist VMID
# vim-cmd vimsvc/task_cancel task_id
# vim-cmd vmsvc/power.off VMID # hard off if shutiing down fails
for idvm in $(vim-cmd vmsvc/getallvms | awk '$3 ~ /^\[/ {print $1"|"$2}' | sort -n) ; do
        id=$(echo $idvm | cut -d"|" -f1)
        name=$(echo $idvm | cut -d"|" -f2)
        vim-cmd vmsvc/power.getstate $id | grep on >/dev/null
        if [ $? == 0 ] ; then
                echo "shutting down [$id] $name ..."
                vim-cmd vmsvc/power.shutdown $id
                echo $?
        fi
done
echo "enabling maintenance mode..."
esxcli system maintenanceMode set --enable true
echo -n "checking if maintenance mode is activ:"
vim-cmd /hostsvc/hostsummary | grep inMaintenanceMode | grep true >/dev/null
if [ $? == 0 ] ; then
        echo "yes"
        echo "staring upgrade, be patient..."
        esxcli software vib install -d $full
        echo "now rebooting..."
        esxcli system shutdown reboot -r 'reboot after install of $file'
else
        echo "no"
        echo -n "do you want to enable it and reboot now? (y/N): "
        read answer
        if [ "$answer" == "y" ] ; then
                esxcli system maintenanceMode set --enable true
                #after reboot mm is off? how to persist?
                esxcli system shutdown reboot -r 'enable maintenance mode for upgrade'
        fi
fi
exit

