示例#1
0
 /**
  * Request spot instance(s)
  *
  * @param $region Region in which to fire up the instance
  * @param $price maximum price to pay for spot instance
  * @param $opts EC2 options
  * @return CFResponse
  */
 public function requestSpotInstances($region, $price, $opts)
 {
     $ec2 = new AmazonEC2();
     $ec2->set_region($region);
     $response = $ec2->request_spot_instances($price, $opts);
     return $response;
 }
示例#2
0
function EC2_ScaleUp($location, $ec2Config)
{
    $instancesNeeded = 0;
    $file = @fopen("./ec2/testers.{$location}.dat", 'c+');
    if ($file) {
        if (flock($file, LOCK_EX)) {
            $config = parse_ini_file('./settings/ec2.ini', true);
            if (isset($config[$ec2Config])) {
                // see how many tests are currently pending for the given location
                $locations = parse_ini_file('./settings/locations.ini', true);
                BuildLocations($locations);
                if (strlen($locations[$location]['localDir'])) {
                    $files = glob($locations[$location]['localDir'] . '/*.*', GLOB_NOSORT);
                    $backlog = count($files);
                    $instances = json_decode(stream_get_contents($file), true);
                    if (!$instances) {
                        $instances = array();
                    }
                    $count = count($instances);
                    if ($backlog > 0 && !$count) {
                        $instancesNeeded = 1;
                    } else {
                        $ratio = $config[$ec2Config]['ratio'];
                        $max = $config[$ec2Config]['max'];
                        if ($ratio && $count < $max) {
                            $needed = (int) ($backlog / $ratio);
                            $needed = min($needed, $max);
                            $needed = max($needed, $count);
                            $instancesNeeded = $needed - $count;
                        }
                    }
                }
            }
            if ($instancesNeeded) {
                $price = trim($config[$ec2Config]['price']);
                $ami = trim($config[$ec2Config]['ami']);
                $region = trim($config[$ec2Config]['region']);
                $size = 'm1.small';
                if (strlen($config[$ec2Config]['size'])) {
                    $size = trim($config[$ec2Config]['size']);
                }
                $userData = trim($config[$ec2Config]['user_data']);
                if (strlen($price) && strlen($ami) && strlen($region) && strlen($size) && strlen($userData)) {
                    require_once './ec2/sdk.class.php';
                    $ec2 = new AmazonEC2($config[$ec2Config]['key'], $config[$ec2Config]['secret']);
                    if ($ec2) {
                        $ec2->set_region($region);
                        $response = $ec2->request_spot_instances($price, array('InstanceCount' => (int) $instancesNeeded, 'Type' => 'one-time', 'LaunchSpecification' => array('ImageId' => $ami, 'InstanceType' => 'm1.small', 'UserData' => base64_encode($userData))));
                        if ($response->isOK()) {
                            // add empty instances to our list, the actual ID's will be filled in as they come online
                            // and checked periodically
                            for ($i = 0; $i < $instancesNeeded; $i++) {
                                $instances[] = array();
                            }
                            fseek($file, 0);
                            ftruncate($file, 0);
                            fwrite($file, json_encode($instances));
                        }
                    }
                }
            }
            flock($file, LOCK_UN);
        }
        fclose($file);
    }
}