SSH_COMMAND="/usr/bin/ssh -q -x -o PasswordAuthentication=no -o StrictHostKeyChecking=no -n -l root"
#SSH_COMMAND="/usr/bin/ssh -q -x -n -l root"
START_COMMAND="qm start"
STOP_COMMAND="qm stop"

REBOOT_COMMAND="qm reset"

# Warning: If you select this poweroff command, it'll physically
# power-off the machine, and quite a number of systems won't be remotely
# revivable.
# TODO: Probably should touch a file on the server instead to just
# prevent heartbeat et al from being started after the reboot.
# POWEROFF_COMMAND="echo 'sleep 2; /sbin/poweroff -nf' | SHELL=/bin/sh at now >/dev/null 2>&1"

# Rewrite the hostlist to accept "," as a delimeter for hostnames too.
#hostlist=`echo $hostlist | tr ',' ' '`

is_host_stopped() {
  if
    [ -z "$proxmoxhost" ]
  then
    return 1
  fi
  if
    [ -z "$vmid" ]
  then
    return 1
  fi
  retval=$(/usr/bin/ssh -x -o PasswordAuthentication=no -o StrictHostKeyChecking=no -l root $proxmoxhost qm status $vmid)
  #echo "Retval: $retval"
  if [[ sed-$retval =~ stopped ]]
  then
        #echo "Stopped!"
        return 0
  else
        #echo "Running or unknown."
        return 1
  fi
}


case $1 in
on)
        # Can't really be implemented because ssh cannot power on a system
        # when it is powered off.
        $($SSH_COMMAND $proxmoxhost $START_COMMAND $vmid)
        for j in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
        do
          if
            is_host_stopped $proxmoxhost
          then
            sleep 1
          else
            exit 0
          fi
        done
        exit 1
        ;;
off)
        # Shouldn't really be implemented because if ssh cannot power on a
        # system, it shouldn't be allowed to power it off.
        $($SSH_COMMAND $proxmoxhost $START_COMMAND $vmid)
        sleep 2
        if
           is_host_stopped $proxmoxhost
        then
          exit 0
        else
          exit 1
        fi
        ;;
reset)
        if
          [ -z "$proxmoxhost" ]
        then
          exit 1
        fi
        if
          [ -z "$vmid" ]
        then
          exit 1
        fi
        if
           is_host_stopped $proxmoxhost
        then
          echo "Host $vmid is already turned off."
          # well... Let's call it successful, after all this is only for testing...
          exit 0
        else
          echo "RESET"
          $($SSH_COMMAND $proxmoxhost $STOP_COMMAND $vmid)
          if
            is_host_stopped $proxmoxhost
          then
            $($SSH_COMMAND $proxmoxhost $START_COMMAND $vmid)
            exit 0
          else
            exit 1
          fi
        fi
        exit 1
        ;;
status)
        #root@pve2:/data/glusterfs/vmvol/232GB_B3DF759F2947/brick# ssh pve1 qm status 117
        #status: running

        if
          [ -z "$proxmoxhost" ]
        then
          exit 1
        fi
        if
          [ -z "$vmid" ]
        then
          exit 1
        fi
        retval=$($SSH_COMMAND $proxmoxhost qm status $vmid)

        if [[ sed-$retval =~ status ]]
        then
                # Good, we got a return message from Proxmox host
                exit 0
        else
                # Oh dear... No response from Proxmox host
                exit 1
        fi

        ;;
getconfignames)
        echo "proxmoxhost"
        echo "vmid"
        exit 0
        ;;
getinfo-devid)
        echo "ssh STONITH device"
        exit 0
        ;;
getinfo-devname)
        echo "ssh STONITH external device"
        exit 0
        ;;
getinfo-devdescr)
        echo "ssh-based host reset"
        echo "Used to kill Proxmox VMs"
        exit 0
        ;;
getinfo-devurl)
        echo "http://openssh.org"
        exit 0
        ;;
getinfo-xml)
        cat << SSHXML
<parameters>
<parameter name="proxmoxhost" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
Proxmoxhost
</shortdesc>
<longdesc lang="en">
The host that the STONITH device controls
</longdesc>
</parameter>
<parameter name="vmid" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
vmid
</shortdesc>
<longdesc lang="en">
The Proxmox ID of vm that should be killed
</longdesc>
</parameter>

</parameters>
SSHXML
        exit 0
        ;;
*)
        exit 1
        ;;
esac

