Skip to content

Commit

Permalink
add power_off mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gbolo committed Aug 24, 2017
1 parent 269cbba commit aa486ba
Showing 1 changed file with 81 additions and 6 deletions.
87 changes: 81 additions & 6 deletions src/api/power_failure.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
$vc_id=$_GET['vcid'];
}

$mode="power_on";
if ( isset($_GET['mode']) ) {
$mode=$_GET['mode'];
}

if ( $mode != "power_off" ) {
$mode="power_on";
}

// functions required
function select_vms($vc_id, $power_state){
try {
Expand Down Expand Up @@ -61,6 +70,9 @@ function get_vcenter($vc_id, $vc_array) {
return $vc;
}


// START MAIN ------------------------------------------------------------------

// Load MYSQL connection details
require_once( 'lib/mysql_config.php' );
// set up PDO
Expand All @@ -82,9 +94,16 @@ function get_vcenter($vc_id, $vc_array) {
echo <<<EOL
#
# This vmware PowerCli script is auto generated by vsummary.
# The purpose of this powershell script is to power on all VMs which were previously on.
# The purpose of this powershell script is to power on (or off) all VMs which were previously on.
# USE WITH CAUTION AND IN THE EVENT OF A POWER OUTAGE!
#
# DEFAULT MODE IS: power_on
# to switch to power_off mode, call this page with querystring mode=power_off
# example: http(s)://<VSUMMARY_URL>>/api/power_failure.php?vcid=<ID_OF_VCENTER>&mode=power_off
#
# NOTICE WHEN IN power_off MODE:
# !! YOU MUST REMOVE THE TWO LINES WHICH POWEROFF THE VCENTER VM IF THE VCENTER VM IS PART OF THE LIST !!
#
# AVAILABLE VCENTER SERVERS:
Expand All @@ -103,11 +122,25 @@ function get_vcenter($vc_id, $vc_array) {

echo "\n\n\n";
$vc = get_vcenter($vc_id, $vcenters);
echo "# TARGET VCENTER: \n# short_name: ${vc['short_name']}\n# fqdn: ${vc['fqdn']}\n# id: ${vc['id']} \n\n";

if ( !isset( $vc['id'] ) ) {
echo "\n# ^^ ERROR ON SELECTION. CLICK ON AN AVAILABLE VCENTER LINK ABOVE TO OUTPUT SCRIPT ^^";
http_response_code(200);
exit(0);
}

$vms = select_vms($vc_id, "poweredOn");
$vms_total = count($vms);

echo "# TARGET MODE: $mode \n";
echo "# TARGET VCENTER: \n# short_name: ${vc['short_name']}\n# fqdn: ${vc['fqdn']}\n# id: ${vc['id']} \n\n";

if ( $vms_total < 1 ) {
echo "\n# !! THE TARGET VCENTER HAD NO VMS TO POWER ON DURING LAST POLL !!";
http_response_code(200);
exit(0);
}

echo <<<EOL
#
Expand Down Expand Up @@ -145,14 +178,41 @@ function get_vcenter($vc_id, $vc_array) {
} else {
Write-Host "VM is already on. Skipping..."
}
}
Function ShutDown-VM-Gracefully( [string]\$moref, [string]\$waitSec ) {
\$vm = Get-VM -Id \$moref
if (\$vm.PowerState -eq "PoweredOn") {
Stop-VMGuest -VM \$vm -Confirm:\$false
Write-Host "Waiting \$time_to_wait_for_shutdown seconds for graceful shutdown before procceeding to next VM..."
Start-Sleep -s \$waitSec
} else {
Write-Host "VM is already off. Skipping..."
}
}
Function ShutDown-VM-Forcefully( [string]\$moref ) {
\$vm = Get-VM -Id \$moref
if (\$vm.PowerState -eq "PoweredOn") {
Stop-VM -VM \$vm -Kill -Confirm:\$false
} else {
Write-Host "VM is already off. Skipping..."
}
}
#
# Main
#
\$proceed = Read-Host "Powering on $vms_total VM(s) on ${vc['fqdn']} [${vc['short_name']}]. Are you sure? (y/n)"
\$time_to_wait_for_shutdown = 5
\$scriptMode = "${mode}"
if (\$scriptMode -eq 'power_off') {
Broadcast_Msg "!! POWER OFF MODE DETECTED !!"
Write-Host "YOU MUST REMOVE THE TWO LINES WHICH POWEROFF THE VCENTER VM IF THE VCENTER VM IS PART OF THIS SCRIPT!!"
Write-Host "FAILURE TO DO SO WILL RESULT IN THIS SCRIPT NOT COMPLETING!!"
}
\$proceed = Read-Host "Operation $mode on $vms_total VM(s) on ${vc['fqdn']} [${vc['short_name']}]. Are you sure? (y/n)"
if (\$proceed -ne 'y') {
Exit
}
Expand All @@ -163,16 +223,31 @@ function get_vcenter($vc_id, $vc_array) {
Broadcast_Msg "Connecting to vCenter: ${vc['fqdn']} [${vc['short_name']}]"
Connect-to-vcenter "${vc['fqdn']}"
# STARTING THE VMS (Total VMs: $vms_total)
# $mode THE VMS (Total VMs: $vms_total)
EOL;


foreach ($vms as $vm) {
echo "# VM NAME: ${vm['name']}\n";
echo "Broadcast_Msg \"Powering On VM: ${vm['name']}\"\n";
echo "PowerOn-VM \"VirtualMachine-${vm['moref']}\" \n\n";
if ( $mode == "power_on" ) {
echo "Broadcast_Msg \"Powering On VM: ${vm['name']}\"\n";
echo "PowerOn-VM \"VirtualMachine-${vm['moref']}\" \n\n";
} elseif ( $mode == "power_off" ) {
echo "Broadcast_Msg \"Powering DOWN VM Gracefully: ${vm['name']}\"\n";
echo "ShutDown-VM-Gracefully \"VirtualMachine-${vm['moref']}\" \$time_to_wait_for_shutdown \n\n";
}
}

# do another loop for VMs that did not power down gracefully
if ( $mode == "power_off" ) {
echo "\n\n\n\n";
foreach ($vms as $vm) {
echo "# VM NAME: ${vm['name']}\n";
echo "Broadcast_Msg \"Powering DOWN VM FORCEFULLY: ${vm['name']}\"\n";
echo "ShutDown-VM-Forcefully \"VirtualMachine-${vm['moref']}\" \n\n";
}
}

echo 'Broadcast_Msg "Script Has Completed"';
Expand Down

0 comments on commit aa486ba

Please sign in to comment.