function __construct($instance_id, $region) { $ec2 = new AmazonEC2(); switch ($region) { case 'tokyo': $ec2->set_region(AmazonEC2::REGION_APAC_NE1); break; case 'singapore': $ec2->set_region(AmazonEC2::REGION_APAC_SE1); break; case 'ireland': $ec2->set_region(AmazonEC2::REGION_EU_W1); break; case 'california': $ec2->set_region(AmazonEC2::REGION_US_W1); break; case 'virginia': $ec2->set_region(AmazonEC2::REGION_US_E1); break; default: echo 'default'; break; } $this->ec2 = $ec2; $this->instance_id = $instance_id; $this->availability_zone = self::fetch_availability_zone($this->instance_id); }
$instanceType = 'm1.small'; if (isset($instanceSize)) { $instanceType = $instanceSize; } // we only terminate instances at the top of the hour, but we can add instances at other times $addOnly = true; $minute = (int) gmdate('i'); if ($minute < 5 || $minute > 55) { $addOnly = false; } $now = time(); echo "Fetching list of running instances...\n"; $ec2 = new AmazonEC2($keyID, $secret); if ($ec2) { foreach ($regions as $region => &$amiData) { $ec2->set_region($region); // clean up any orphaned volumes $volumes = $ec2->describe_volumes(); if (isset($volumes)) { foreach ($volumes->body->volumeSet->item as $item) { if ($item->status == 'available') { $id = strval($item->volumeId); $ec2->delete_volume($id); } } } foreach ($amiData as $ami => &$regionData) { $location = $regionData['location']; echo "\n{$region} ({$location}):\n"; // load the valid testers in this location $testers = array();
/** * Get the number of instance of a particular AMI running in a give region. * @param $region * @param $ami * @return int Number of instances of AMI $ami running in region $region */ public function getInstanceCount($region, $ami) { $ec2 = new AmazonEC2(); $ec2->set_region($region); $response = $ec2->describe_instances(array('Filter' => array(array('Name' => 'image-id', 'Value' => $ami)))); if ($response->status != "200") { logOutput("[ERROR] [WptmEC2.class] EC2 Error [" . $response->body->Errors->Error->Message . "] while getting instance count", "ec2Processor.log"); } return sizeof($response->body->reservationSet->item); }
function EC2_LaunchInstance($region, $ami, $size, $user_data, $loc) { $ret = false; $key = GetSetting('ec2_key'); $secret = GetSetting('ec2_secret'); if ($key && $secret) { $ec2 = new AmazonEC2($key, $secret); $ec2->set_region($region); $response = $ec2->run_instances($ami, 1, 1, array('InstanceType' => $size, 'UserData' => base64_encode($user_data))); if ($response->isOK()) { $ret = true; if (isset($loc) && strlen($loc) && isset($response->body->instancesSet->item->instanceId)) { $instance_id = (string) $response->body->instancesSet->item->instanceId; $ec2->create_tags($instance_id, array(array('Key' => 'Name', 'Value' => 'WebPagetest Agent'), array('Key' => 'WPTLocations', 'Value' => $loc))); } } } return $ret; }
/** * Check to see if the instance is in the process of being terminated * (also add it to the list of known instances if we didn't know about it) * * @param mixed $location * @param mixed $ec2Config * @param mixed $instance */ function EC2_CheckInstance($location, $ec2Config, $instanceID) { $active = false; $file = @fopen("./ec2/testers.{$location}.dat", 'c+'); if ($file) { if (flock($file, LOCK_EX)) { $instances = json_decode(stream_get_contents($file), true); if (count($instances)) { foreach ($instances as &$instance) { if ($instance['id'] == $instanceID) { $active = true; break; } } } // check in case we don't already know about this instance (and it's not shutting down) if (!$active) { $config = parse_ini_file('./settings/ec2.ini', true); if (isset($config[$ec2Config])) { $region = $config[$ec2Config]['region']; $ami = $config[$ec2Config]['ami']; require_once './ec2/sdk.class.php'; $ec2 = new AmazonEC2($config[$ec2Config]['key'], $config[$ec2Config]['secret']); if ($ec2 && strlen($region) && strlen($ami)) { $ec2->set_region($region); UpdateInstanceList($ec2, $instances, $ami); if (count($instances)) { foreach ($instances as &$instance) { if ($instance['id'] == $instanceID) { $active = true; break; } } // write out the updated list of instances fseek($file, 0); ftruncate($file, 0); fwrite($file, json_encode($instances)); } } } } flock($file, LOCK_UN); } fclose($file); } return $active; }