Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 public function __construct(array $options = array())
 {
     if (!isset($options['default_cache_config'])) {
         $options['default_cache_config'] = 'cache/aws';
     }
     if (!isset($options['key']) && Kwf_Config::getValue('aws.key')) {
         $options['key'] = Kwf_Config::getValue('aws.key');
     }
     if (!isset($options['secret']) && Kwf_Config::getValue('aws.secret')) {
         $options['secret'] = Kwf_Config::getValue('aws.secret');
     }
     parent::__construct($options);
 }
Exemplo n.º 3
0
        $counts["{$region}.{$ami}"] = 0;
    }
}
$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'];
<?php

# Simple configuration;
$OLD_CERTIFICATE = 'old-certificate-name';
$NEW_CERTIFICATE = 'new-certificate-name';
require '/usr/share/php/AWSSDKforPHP/sdk.class.php';
$ec2 = new AmazonEC2();
$elb = new AmazonELB();
$iam = new AmazonIAM();
$response = $iam->get_server_certificate($NEW_CERTIFICATE);
$response_available_regions = $ec2->describe_regions();
$available_regions = array(AmazonELB::REGION_US_E1, AmazonELB::REGION_US_W1, AmazonELB::REGION_EU_W1, AmazonELB::REGION_APAC_SE1, AmazonELB::REGION_APAC_SE2, AmazonELB::REGION_APAC_NE1, AmazonELB::REGION_SA_E1);
foreach ($available_regions as $region) {
    $elb->set_region($region);
    $list_of_elbs = $elb->describe_load_balancers();
    $elbs = $list_of_elbs->body->to_stdClass();
    foreach ($elbs->DescribeLoadBalancersResult->LoadBalancerDescriptions->member as $single_elb) {
        echo $single_elb->LoadBalancerName . "\n";
        foreach ($single_elb->ListenerDescriptions->member as $single_listener) {
            if ($single_listener->SSLCertificateId != "") {
                $pos = strpos($single_listener->SSLCertificateId, $OLD_CERTIFICATE);
                if ($pos === false) {
                    echo "\t- No need to replace: " . $single_listener->SSLCertificateId . "\n";
                } else {
                    echo "\t- Replacing certificate for port " . $single_listener->LoadBalancerPort . "...";
                    $elb->set_load_balancer_listener_ssl_certificate($single_elb->LoadBalancerName, $single_listener->LoadBalancerPort, $response->body->GetServerCertificateResult->ServerCertificate->ServerCertificateMetadata->Arn);
                    echo " Done!\n";
                }
            }
        }
    }
/*%******************************************************************************************%*/
// SETUP
// Enable full-blown error reporting. http://twitter.com/rasmus/status/7448448829
error_reporting(-1);
// Set HTML headers
header("Content-type: text/html; charset=utf-8");
// Include the SDK
require_once '../sdk.class.php';
/*%******************************************************************************************%*/
// THE GOALS & PREPARATION
/*
	1. The goal of this exercise is to retrieve a list of all image IDs that are prefixed with "aki-".
	2. We should end up with an indexed array of string values (just the image IDs).
*/
// Instantiate the AmazonEC2 class
$ec2 = new AmazonEC2();
// Get the response from a call to the DescribeImages operation.
$response = $ec2->describe_images();
/*%******************************************************************************************%*/
// THE LONG WAY
// Prepare to collect AKIs.
$akis = array();
// Loop through the response...
foreach ($response->body->imagesSet->item as $item) {
    // Stringify the value
    $image_id = (string) $item->imageId;
    // Filter the value against a PCRE regular expression.
    if (preg_match('/aki/i', $image_id)) {
        // If the name matches our pattern, add it to the list.
        $akis[] = $image_id;
    }
Exemplo n.º 6
0
 * of the License is located at
 *
 *       http://aws.amazon.com/apache2.0/
 *
 * or in the "license.txt" file accompanying this file. This file is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the
 * License.
 *
 * Modified by Jeffrey S. Haemer <*****@*****.**>
 */
error_reporting(E_ALL);
require_once 'AWSSDKforPHP/sdk.class.php';
// Create the EC2 access object
$ec2 = new AmazonEC2();
// 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);
Exemplo n.º 7
0
 * of the License is located at
 *
 *       http://aws.amazon.com/apache2.0/
 *
 * or in the "license.txt" file accompanying this file. This file is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the
 * License.
 *
 * Modified by Jeffrey S. Haemer <*****@*****.**>
 */
error_reporting(E_ALL);
require_once 'AWSSDKforPHP/sdk.class.php';
// Create the EC2 access object
$ec2 = new AmazonEC2();
// Run an instance
$options = array('KeyName' => "testkey", 'InstanceType' => "t1.micro", 'Placement.AvailabilityZone' => "us-east-1a");
$res = $ec2->run_instances("ami-08728661", 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);
Exemplo n.º 8
0
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;
}
Exemplo n.º 9
0
define('LEFT_MARGIN', 16);
define('RIGHT_MARGIN', 16);
define('TOP_MARGIN', 16);
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;
Exemplo n.º 10
0
 /**
  * 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);
 }
Exemplo n.º 11
0
/**
* 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;
}
Exemplo n.º 12
0
 /**
  * 
  * Enter description here ...
  * @param unknown_type $accessKey
  * @param unknown_type $accessKeyId
  * @param unknown_type $serviceUrl
  * @param unknown_type $serviceUriPrefix
  * @param unknown_type $serviceProtocol
  * @return Scalr_Service_Cloud_Eucalyptus_Client
  */
 public static function newEc2($region, $privateKey, $certificate)
 {
     $ec2 = AmazonEC2::GetInstance(AWSRegions::GetAPIURL($region));
     $ec2->SetAuthKeys($privateKey, $certificate);
     return $ec2;
 }
Exemplo n.º 13
0
 * specific language governing permissions and limitations under the
 * License.
 *
 * Modified by Jeffrey S. Haemer <*****@*****.**>
 */
error_reporting(E_ALL);
require_once 'AWSSDKforPHP/sdk.class.php';
require_once 'include/book.inc.php';
// Check arguments
if ($argc < 3) {
    exit("Usage: " . $argv[0] . " \"message\" VOLUMEID...\n");
}
// Get message
$message = $argv[1];
// Create access objects
$sdb = new AmazonSDB();
$ec2 = new AmazonEC2();
// Process each volume
for ($i = 2; $i < $argc; $i++) {
    $volId = $argv[$i];
    // Create snapshot
    $res1 = $ec2->create_snapshot($volId, "{$volId}: {$message}");
    if ($res1->isOK()) {
        $snapId = $res1->body->snapshotId;
        $startTime = $res1->body->startTime;
        $key = $volId . '_' . $startTime;
        $attrs = array('VolId' => $volId, 'Message' => $message, 'StartTime' => $startTime);
        $res2 = $sdb->put_attributes(BOOK_SNAP_LOG_DOMAIN, $key, $attrs, true);
    }
}
exit(0);
Exemplo n.º 14
0
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the
 * License.
 */
error_reporting(E_ALL);
require_once 'sdk.class.php';
require_once 'include/book.inc.php';
// Check arguments
if ($argc < 3) {
    exit("Usage: " . $argv[0] . " \"message\" VOLUMEID...\n");
}
// Get message
$message = $argv[1];
// Create access objects
$sdb = new AmazonSDB();
$ec2 = new AmazonEC2();
// Process each volume
for ($i = 2; $i < $argc; $i++) {
    $volId = $argv[$i];
    // Create snapshot
    $res1 = $ec2->create_snapshot($volId, $message);
    if ($res1->isOK()) {
        $snapId = $res1->body->snapshotId;
        $startTime = $res1->body->startTime;
        $key = $volId . '_' . $startTime;
        $attrs = array('VolId' => $volId, 'Message' => $message, 'StartTime' => $startTime);
        $res2 = $sdb->put_attributes(BOOK_SNAP_LOG_DOMAIN, $key, $attrs, true);
    }
}
exit(0);
Exemplo n.º 15
0
<?php

require_once $rundir . '/aws-sdk-for-php/sdk.class.php';
require_once $rundir . '/config.inc.php';
$ec2 = new AmazonEC2();
// get instance list and create config
$config = "{$config_begin_seperater}\n";
foreach ($regions as $region) {
    // describe instances in the region.
    $ec2->set_region($region);
    $instances = $ec2->describe_instances();
    if (!$instances->isOK()) {
        continue;
    }
    // create config
    foreach ($instances->body->reservationSet->children() as $reservationItem) {
        foreach ($reservationItem->instancesSet->children() as $instanceItem) {
            $group_name = $region;
            $node_name = $instanceItem->dnsName;
            foreach ($instanceItem->tagSet->item as $val) {
                if ($val->key == 'Name') {
                    $val->value = str_replace('_', '.', $val->value);
                    $node_name = $val->value . '.' . $instanceItem->dnsName;
                } else {
                    if ($val->key == 'Group') {
                        $group_name = $val->value . '.' . $region;
                    }
                }
            }
            $node_ip = $use_public_dns ? $instanceItem->dnsName : $instanceItem->privateIpAddress;
            $config .= create_munin_config($node_ip, $node_name, $group_name);