From e3e27df51989068ad38bdd9e5a2e018208c147d4 Mon Sep 17 00:00:00 2001 From: ReachableCEO Date: Sat, 25 Nov 2023 18:44:05 +0000 Subject: [PATCH] Nov 25, 2023, 12:44 PM --- .../RackRentalVirtualPowerMgmt/README.md | 1 + .../esxi-PowerCycle.ps1 | 10 ++ .../esxi-PowerOff.ps1 | 9 ++ .../esxi-PowerOn.ps1 | 9 ++ .../kvm-PowerCycle.sh | 12 ++ .../kvm-PowerOff.sh | 11 ++ .../RackRentalVirtualPowerMgmt/kvm-PowerOn.sh | 10 ++ .../nocpowervtps.sh | 136 ++++++++++++++++++ 8 files changed, 198 insertions(+) create mode 100644 shell-frags/RackRentalVirtualPowerMgmt/README.md create mode 100644 shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerCycle.ps1 create mode 100644 shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerOff.ps1 create mode 100644 shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerOn.ps1 create mode 100644 shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerCycle.sh create mode 100644 shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerOff.sh create mode 100644 shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerOn.sh create mode 100644 shell-frags/RackRentalVirtualPowerMgmt/nocpowervtps.sh diff --git a/shell-frags/RackRentalVirtualPowerMgmt/README.md b/shell-frags/RackRentalVirtualPowerMgmt/README.md new file mode 100644 index 0000000..6971caf --- /dev/null +++ b/shell-frags/RackRentalVirtualPowerMgmt/README.md @@ -0,0 +1 @@ +# power-vtps diff --git a/shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerCycle.ps1 b/shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerCycle.ps1 new file mode 100644 index 0000000..d0a74e1 --- /dev/null +++ b/shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerCycle.ps1 @@ -0,0 +1,10 @@ +param( +[string]$vcenter, +[string]$vm +) + +Get-Module -ListAvailable PowerCLI* | Import-Module -Verbose:$false > /dev/null +Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false > /dev/null +Connect-ViServer -Server $vcenter -User tplab\powervtps -Password h7x14vFGkJr5 -Verbose:$false > /dev/null +Stop-VM -RunAsync -VM "$vm" -Confirm:$false -Verbose:$false > /dev/null +Start-VM -RunAsync -VM "$vm" -Confirm:$false -Verbose:$false > /dev/null \ No newline at end of file diff --git a/shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerOff.ps1 b/shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerOff.ps1 new file mode 100644 index 0000000..b227a46 --- /dev/null +++ b/shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerOff.ps1 @@ -0,0 +1,9 @@ +param( +[string]$vcenter, +[string]$vm +) + +Get-Module -ListAvailable PowerCLI* | Import-Module -Verbose:$false > /dev/null +Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false > /dev/null +Connect-ViServer -Server $vcenter -User tplab\powervtps -Password h7x14vFGkJr5 -Verbose:$false > /dev/null +Stop-VM -RunAsync -VM "$vm" -Confirm:$false -Verbose:$false > /dev/null diff --git a/shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerOn.ps1 b/shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerOn.ps1 new file mode 100644 index 0000000..180098f --- /dev/null +++ b/shell-frags/RackRentalVirtualPowerMgmt/esxi-PowerOn.ps1 @@ -0,0 +1,9 @@ +param( +[string]$vcenter, +[string]$vm +) + +Get-Module -ListAvailable PowerCLI* | Import-Module -Verbose:$false > /dev/null +Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false > /dev/null +Connect-ViServer -Server $vcenter -User tplab\powervtps -Password h7x14vFGkJr5 -Verbose:$false > /dev/null +Start-VM -RunAsync -VM "$vm" -Confirm:$false -Verbose:$false > /dev/null \ No newline at end of file diff --git a/shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerCycle.sh b/shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerCycle.sh new file mode 100644 index 0000000..5cb212f --- /dev/null +++ b/shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerCycle.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +VMHOSTFULL="$1" +USER_SUPPLIED_VMNAME="$2" +SSH_ALIAS="ssh -q" + +VIRSH_ALIAS="virsh -c qemu+ssh://root@$VMHOSTFULL/system?no_verify=1" + + + +$VIRSH_ALIAS destroy $USER_SUPPLIED_VMNAME +$VIRSH_ALIAS start $USER_SUPPLIED_VMNAME \ No newline at end of file diff --git a/shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerOff.sh b/shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerOff.sh new file mode 100644 index 0000000..cec39f7 --- /dev/null +++ b/shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerOff.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +VMHOSTFULL="$1" +USER_SUPPLIED_VMNAME="$2" +SSH_ALIAS="ssh -q" + +VIRSH_ALIAS="virsh -c qemu+ssh://root@$VMHOSTFULL/system?no_verify=1" + + + +$VIRSH_ALIAS destroy $USER_SUPPLIED_VMNAME \ No newline at end of file diff --git a/shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerOn.sh b/shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerOn.sh new file mode 100644 index 0000000..93462d5 --- /dev/null +++ b/shell-frags/RackRentalVirtualPowerMgmt/kvm-PowerOn.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +VMHOSTFULL="$1" +USER_SUPPLIED_VMNAME="$2" +SSH_ALIAS="ssh -q" + +VIRSH_ALIAS="virsh -c qemu+ssh://root@$VMHOSTFULL/system?no_verify=1" + + +$VIRSH_ALIAS start $USER_SUPPLIED_VMNAME \ No newline at end of file diff --git a/shell-frags/RackRentalVirtualPowerMgmt/nocpowervtps.sh b/shell-frags/RackRentalVirtualPowerMgmt/nocpowervtps.sh new file mode 100644 index 0000000..082ab64 --- /dev/null +++ b/shell-frags/RackRentalVirtualPowerMgmt/nocpowervtps.sh @@ -0,0 +1,136 @@ +#!/bin/bash + +############################################################################### +#function declarations +############################################################################### + +usage() +#Usage message +{ + + echo "$0 needs to be invoked with two required argument: +---------------------------------------------------------------------------- + --jig ottov + --action (One of: on,off,cycle) +----------------------------------------------------------------------------" + +exit 0 +} + +error_out() +#Error reporting code +{ +echo "A critical error has occured. Please see above line for portion that failed." +echo "This script will now exit..." +exit 1 +} + +bail_out() +#Exit code +{ +echo "Exiting...." +exit 0 +} + + + +############################################################################### +#Start execution +############################################################################### + +if [[ $# -eq 0 ]]; then +usage +fi + +if [ "$1" = "-help" ]; then +usage +fi + +COUNTER=0 +ARGS=("$@") +while [ $COUNTER -lt $# ] +do + arg=${ARGS[$COUNTER]} + let COUNTER=COUNTER+1 + nextArg=${ARGS[$COUNTER]} + argKey="$arg" + argVal="$nextArg" + skipNext=1 + + case "$argKey" in + --jig) + export USER_SUPPLIED_VMNAME="$argVal" + ;; + --action) + export USER_SUPPLIED_ACTION="$argVal" + ;; + -h|--help|-help|--h) + usage + exit + + esac +done + +#Check for proper inputs + +#Check for vmName +echo "Checking if DUT name was provided..." +if [ -z "$USER_SUPPLIED_VMNAME" ]; then +error_out +else + echo "Using DUT $USER_SUPPLIED_VMNAME" +fi + +#Check for action +echo "Checking if action was provided..." +if [ -z $USER_SUPPLIED_ACTION ]; then +error_out +else + echo "Performing action $USER_SUPPLIED_ACTION" +fi + +echo "All preflight checks passsed. Proceeding with power operation." + + +export LMOUT="$(labman device $USER_SUPPLIED_VMNAME)" +export VCENTER_FQDN="$(echo "$LMOUT" | grep -w 'VM Management Server' |awk -F ':' '{print $2}'|sed 's/ //g')" +export VMHOSTFULL="$(echo "$LMOUT" | grep -w 'VM Host' |awk -F ':' '{print $2}'|sed 's/ //g')" + + + +if [ $(echo $VMHOSTFULL | grep esxi -c) -eq 1 ]; then + + if [ "$USER_SUPPLIED_ACTION" = on ] ; then + echo "Powering $USER_SUPPLIED_VMNAME in $VCENTER_FQDN on..." + powershell /local/powervtps/nocpowervtps/esxi-PowerOn.ps1 $VCENTER_FQDN $USER_SUPPLIED_VMNAME + fi + + if [ "$USER_SUPPLIED_ACTION" = off ] ; then + echo "Powering $USER_SUPPLIED_VMNAME in $VCENTER_FQDN off..." + powershell /local/powervtps/nocpowervtps/esxi-PowerOff.ps1 $VCENTER_FQDN $USER_SUPPLIED_VMNAME + fi + + if [ "$USER_SUPPLIED_ACTION" = cycle ] ; then + echo "Cycling $USER_SUPPLIED_VMNAME in $VCENTER_FQDN..." + powershell /local/powervtps/nocpowervtps/esxi-PowerCycle.ps1 $VCENTER_FQDN $USER_SUPPLIED_VMNAME + fi + +fi + +if [ $(echo $VMHOSTFULL | grep kvm -c) -eq 1 ]; then + + if [ "$USER_SUPPLIED_ACTION" = on ] ; then + echo "Powering $USER_SUPPLIED_VMNAME on $VMHOSTFULL on..." + bash /local/powervtps/nocpowervtps/kvm-PowerOn.sh $VMHOSTFULL $USER_SUPPLIED_VMNAME + fi + + if [ "$USER_SUPPLIED_ACTION" = off ] ; then + echo "Powering $USER_SUPPLIED_VMNAME on $VMHOSTFULL off..." + bash /local/powervtps/nocpowervtps/kvm-PowerOff.sh $VMHOSTFULL $USER_SUPPLIED_VMNAME + fi + + if [ "$USER_SUPPLIED_ACTION" = cycle ] ; then + echo "Cycling $USER_SUPPLIED_VMNAME on $VMHOSTFULL..." + bash /local/powervtps/nocpowervtps/kvm-PowerCycle.sh $VMHOSTFULL $USER_SUPPLIED_VMNAME + fi +fi \ No newline at end of file