public function stopHost($vmDetails)
{
// what are we doing?
$log = usingLog()->startAction("stop VM");
// is the VM actually running?
if (!$this->isRunning($vmDetails)) {
// we've decided not to treat this as an error ... that might
// change in a future release
$log->endAction("VM was already stopped or destroyed");
return;
}
// get our Ec2 client from the SDK
$client = fromAws()->getEc2Client();
// what is our instanceId?
$instanceId = $vmDetails->ec2Instance['InstanceId'];
// let's stop the VM
try {
$log->addStep("stop EC2 VM instance '{$instanceId}'", function () use($client, &$response, $instanceId) {
$response = $client->stopInstances(array("InstanceIds" => array($instanceId)));
});
// now, we need to wait until this instance has stopped
$log->addStep("wait for EC2 VM '{$instanceId}' to finish shutting down", function () use($client, $vmDetails, $response, $instanceId) {
$client->waitUntilInstanceStopped(array('InstanceIds' => array($instanceId), 'waiter.interval' => 10, 'waiter.max_attempts' => 18));
// remember the instance data, to save us time in the future
$vmDetails->ec2Instance = $response['Instances'][0];
});
} catch (Exception $e) {
// something went wrong
$log->endAction("VM failed to stop :(");
throw new E5xx_ActionFailed(__METHOD__, $e->getMessage());
}
// all done - success!
$log->endAction("VM successfully stopped");
}