$busy = false; if (array_key_exists('test', $info) && strlen($info['test'])) { $busy = true; } if ($info['locCount'] < $locCount && !$busy && $lifetime > 600) { echo "{$id} needs to be rebooted\n"; $reboot[] = $id; } } if (count($reboot)) { $ec2->reboot_instances($reboot); } // get the list of current running ec2 instances $terminate = array(); $count = 0; $response = $ec2->describe_instances(); $activeCount = 0; $idleCount = 0; $offlineCount = 0; if ($response->isOK()) { foreach ($response->body->reservationSet->item as $item) { foreach ($item->instancesSet->item as $instance) { if ($instance->imageId == $ami && $instance->instanceState->code <= 16) { $id = (string) $instance->instanceId; if (array_key_exists($id, $testers) || $addOnly) { if ($testers[$id]['offline']) { $offlineCount++; } elseif (strlen($testers[$id]['test']) || $addOnly) { $activeCount++; } else { $idleCount++;
// Run an instance $options = array('KeyName' => "testkey", 'InstanceType' => "m1.small"); $res = $ec2->run_instances("ami-48aa4921", 1, 1, $options); if (!$res->isOK()) { exit("Could not launch instance: " . $res->body->Errors->Error->Message . "\n"); } // Get the Id and Availability Zone of the instance $instances = $res->body->instancesSet; $instanceId = (string) $instances->item->instanceId; $availabilityZone = (string) $instances->item->placement->availabilityZone; print "Launched instance {$instanceId} " . "in availability zone {$availabilityZone}.\n"; // Wait for the instance's state to change to running // before attaching volumes do { $options = array('InstanceId.1' => $instanceId); $res = $ec2->describe_instances($options); $instances = $res->body->reservationSet->item->instancesSet; $state = $instances->item->instanceState->name; $running = $state == 'running'; if (!$running) { print "Instance is currently in " . "state {$state}, waiting 10 seconds\n"; sleep(10); } } while (!$running); // Allocate an Elastic IP address $res = $ec2->allocate_address(); if (!$res->isOK()) { exit("Could not allocate public IP address.\n"); } // Get the allocated Elastic IP address $publicIP = (string) $res->body->publicIp;
/** * 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_GetRunningInstances() { $now = time(); $instances = array(); $key = GetSetting('ec2_key'); $secret = GetSetting('ec2_secret'); if ($key && $secret) { $ec2 = new AmazonEC2($key, $secret); $regions = array(); $response = $ec2->describe_regions(); if (isset($response) && $response->isOK()) { foreach ($response->body->regionInfo->item as $region) { $regions[] = (string) $region->regionName; } } foreach ($regions as $region) { $ec2->set_region($region); $response = $ec2->describe_instances(); if (isset($response) && $response->isOK()) { foreach ($response->body->reservationSet->item as $item) { foreach ($item->instancesSet->item as $instance) { $wptLocations = null; if (isset($instance->tagSet)) { foreach ($instance->tagSet->item as $tag) { if ($tag->key == 'WPTLocations') { $wptLocations = explode(',', $tag->value); break; } } } if (isset($wptLocations)) { $launchTime = strtotime((string) $instance->launchTime); $elapsed = $now - $launchTime; $instances[] = array('region' => $region, 'id' => (string) $instance->instanceId, 'ami' => (string) $instance->imageId, 'state' => (int) $instance->instanceState->code, 'launchTime' => (string) $instance->launchTime, 'launched' => $launchTime, 'runningTime' => $elapsed, 'locations' => $wptLocations); } } } } } } // update the AMI counts we are tracking locally if (count($instances)) { $lock = Lock('ec2-instances', true, 120); if ($lock) { $amis = array(); foreach ($instances as $instance) { if (isset($instance['ami']) && strlen($instance['ami']) && is_numeric($instance['state']) && $instance['state'] <= 16) { if (!isset($amis[$instance['ami']])) { $amis[$instance['ami']] = array('count' => 0); } $amis[$instance['ami']]['count']++; } } file_put_contents('./tmp/ec2-instances.dat', json_encode($amis)); Unlock($lock); } } return $instances; }
define('BOTTOM_MARGIN', 16); define('TEXT_MARGIN', 4); define('TEXT_LINE_HEIGHT', 14); define('INSTANCE_WIDTH', 128); define('INSTANCE_HEIGHT', 64); define('VOLUME_WIDTH', 96); define('VOLUME_HEIGHT', 64); define('SNAP_WIDTH', 96); define('SNAP_HEIGHT', 64); define('VOLUME_GAP', 16); define('SNAP_GAP', 16); // Create the access objects $ec2 = new AmazonEC2(); $s3 = new AmazonS3(); // Get the EC2 instances, EBS volumes, and snapshots $resInstances = $ec2->describe_instances(); $resVolumes = $ec2->describe_volumes(); $resSnapshots = $ec2->describe_snapshots(); // Check for errors if (!$resInstances->isOK() || !$resVolumes->isOK() || !$resSnapshots->isOK()) { exit("Error retrieving system information."); } // Create an object to represent the EC2 region $region = new Region('us-east-1'); // Add each EC2 instance to the region foreach ($resInstances->body->reservationSet->item as $itemSet) { foreach ($itemSet->instancesSet->item as $item) { $instanceId = (string) $item->instanceId; $state = (string) $item->instanceState->name; $instanceType = (string) $item->instanceType; $availabilityZone = (string) $item->placement->availabilityZone;