Beispiel #1
0
 public function getEc2Client()
 {
     // the client to return
     static $ec2Client = null;
     if (!$ec2Client) {
         // what are we doing?
         $log = usingLog()->startAction("create AWS client for EC2");
         // get the Aws client factory
         $awsFactory = fromAws()->getAwsClientFactory();
         // create the EC2 client
         $ec2Client = $awsFactory->get('ec2');
         $log->endAction();
     }
     // all done
     return $ec2Client;
 }
 public function markAllVolumesAsDeleteOnTermination()
 {
     $this->requiresValidHost(__METHOD__);
     // what are we doing?
     $log = usingLog()->startAction("mark all volumes on EC2 VM '{$this->instanceName}' to be deleted on termination");
     // create a list of all of the volumes we're going to modify
     $ebsVolumes = array();
     foreach ($this->instance['BlockDeviceMappings'] as $origEbsVolume) {
         $ebsVolume = array('DeviceName' => $origEbsVolume['DeviceName'], 'Ebs' => array('DeleteOnTermination' => true));
         $ebsVolumes[] = $ebsVolume;
     }
     // get the AWS EC2 client to work with
     $ec2Client = fromAws()->getEc2Client();
     // let's mark all of the volumes as needing to be deleted
     // on termination
     $ec2Client->modifyInstanceAttribute(array('InstanceId' => $this->instance['InstanceId'], 'BlockDeviceMappings' => $ebsVolumes));
     // now, we need to make sure that actually worked
     $this->instance = fromEc2()->getInstance($this->instanceName);
     // var_dump("\n\n\nAFTER MODIFY INSTANCE ATTRIBUTE\n\n");
     // var_dump($this->instance);
     // that should be that
     $log->endAction();
 }
Beispiel #3
0
 public function getInstance($instanceName)
 {
     // what are we doing?
     $log = usingLog()->startAction("get data for EC2 VM '{$instanceName}'");
     // get the client
     $client = fromAws()->getEc2Client();
     // get the list of running instances
     $result = $client->describeInstances();
     // loop through, and see if the instance is running
     foreach ($result['Reservations'] as $reservation) {
         foreach ($reservation['Instances'] as $instance) {
             $foundInstanceName = '';
             if (!isset($instance['Tags'])) {
                 // no tags at all means no name to match against
                 continue;
             }
             // skip terminated instances
             if ($instance['State']['Code'] == 48) {
                 continue;
             }
             foreach ($instance['Tags'] as $tag) {
                 if ($tag['Key'] == 'Name') {
                     $foundInstanceName = $tag['Value'];
                 }
             }
             if ($instanceName == $foundInstanceName) {
                 // we're done here
                 $instanceId = $instance['InstanceId'];
                 $log->endAction("instance is running as ID '{$instanceId}'");
                 return $instance;
             }
         }
     }
     // if we get here, the instance currently does not exist at EC2
     $log->endAction("instance does not exist");
     return null;
 }
Beispiel #4
0
 /**
  *
  * @param  Ec2VmDetails $vmDetails
  * @return void
  */
 public function destroyHost($vmDetails)
 {
     // what are we doing?
     $log = usingLog()->startAction("destroy VM");
     // get our Ec2 client from the SDK
     $client = fromAws()->getEc2Client();
     // what is our instanceId?
     $instanceId = $vmDetails->ec2Instance['InstanceId'];
     // let's destroy the VM
     try {
         $log->addStep("destroy EC2 VM instance '{$instanceId}'", function () use($client, &$response, $instanceId) {
             $response = $client->terminateInstances(array("InstanceIds" => array($instanceId)));
         });
         // now, we need to wait until this instance has been terminated
         $log->addStep("wait for EC2 VM '{$instanceId}' to finish terminating", function () use($client, $vmDetails, $response, $instanceId) {
             $client->waitUntilInstanceTerminated(array('InstanceIds' => array($instanceId), 'waiter.interval' => 10, 'waiter.max_attempts' => 10));
         });
     } catch (Exception $e) {
         // something went wrong
         $log->endAction("VM failed to terminate :(");
         throw new E5xx_ActionFailed(__METHOD__, $e->getMessage());
     }
     // if we get here, we need to forget about this VM
     usingHostsTable()->removeHost($vmDetails->hostId);
     // all done
     $log->endAction();
 }