DataSift\Storyplayer\HostLib\Ec2Vm::startHost PHP Method

startHost() public method

public startHost ( Ec2VmDetails $vmDetails ) : void
$vmDetails Ec2VmDetails
return void
    public function startHost($vmDetails)
    {
        // what are we doing?
        $log = usingLog()->startAction("start VM");
        // is the VM actually running?
        if ($this->isRunning($vmDetails)) {
            // yes it is ... nothing to do
            //
            // we've decided not to treat this as an error ... that might
            // change in a future release
            $log->endAction("VM is already running");
            return;
        }
        // get our Ec2 client from the SDK
        $client = fromAws()->getEc2Client();
        // what is our instanceId?
        $instanceId = $vmDetails->ec2Instance['InstanceId'];
        // let's start the VM
        try {
            $log->addStep("start EC2 VM instance '{$instanceId}'", function () use($client, &$response, $instanceId) {
                $response = $client->startInstances(array("InstanceIds" => array($instanceId)));
            });
            // now, we need to wait until this instance is running
            $log->addStep("wait for EC2 VM '{$instanceId}' to finish booting", function () use($client, $vmDetails, $response, $instanceId) {
                $client->waitUntilInstanceRunning(array('InstanceIds' => array($instanceId), 'waiter.interval' => 10, 'waiter.max_attempts' => 10));
                // 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 start :(");
            throw new E5xx_ActionFailed(__METHOD__, $e->getMessage());
        }
        // yes it did!!
        //
        // now, we need its IP address, which may have changed
        $ipAddress = $this->determineIpAddress($vmDetails);
        // store the IP address for future use
        $vmDetails->ipAddress = $ipAddress;
        // all done
        $log->endAction("VM successfully started; IP address is {$ipAddress}");
    }