Пример #1
0
/**
* Tests are pending for the given location, start instances as necessary
* 
* @param mixed $location
*/
function EC2_StartInstanceIfNeeded($ami)
{
    $target = 1;
    // just support 1 instance at a time for now
    $needed = false;
    $lock = Lock('ec2-instances', true, 120);
    if ($lock) {
        $instances = json_decode(file_get_contents('./tmp/ec2-instances.dat'), true);
        if (!$instances || !is_array($instances)) {
            $instances = array();
        }
        if (!isset($instances[$ami])) {
            $instances[$ami] = array();
        }
        if (!isset($instances[$ami]['count']) || !is_numeric($instances[$ami]['count'])) {
            $instances[$ami]['count'] = 0;
        }
        if ($instances[$ami]['count'] < $target) {
            $needed = true;
        }
        if ($needed) {
            // figure out the user data string to use for the instance
            $host = GetSetting('host');
            if (!$host && isset($_SERVER['HTTP_HOST']) && strlen($_SERVER['HTTP_HOST'])) {
                $host = $_SERVER['HTTP_HOST'];
            }
            if (!$host && GetSetting('ec2')) {
                $host = file_get_contents('http://169.254.169.254/latest/meta-data/hostname');
            }
            $key = GetSetting('location_key');
            $locations = LoadLocationsIni();
            $urlblast = '';
            $wptdriver = '';
            $loc = '';
            $region = null;
            if ($locations && is_array($locations)) {
                foreach ($locations as $location => $config) {
                    if (isset($config['ami']) && $config['ami'] == $ami) {
                        if (isset($config['region'])) {
                            $region = trim($config['region']);
                        }
                        if (isset($config['key']) && strlen($config['key'])) {
                            $key = trim($config['key']);
                        }
                        if (strlen($loc)) {
                            $loc .= ',';
                        }
                        $loc .= $location;
                        if (isset($config['urlblast'])) {
                            if (strlen($urlblast)) {
                                $urlblast .= ',';
                            }
                            $urlblast .= $location;
                        } else {
                            if (strlen($wptdriver)) {
                                $wptdriver .= ',';
                            }
                            $wptdriver .= $location;
                        }
                    }
                }
            }
            if (strlen($loc) && isset($region)) {
                $user_data = "wpt_server={$host}";
                if (strlen($urlblast)) {
                    $user_data .= " wpt_location={$urlblast}";
                }
                if (strlen($wptdriver)) {
                    $user_data .= " wpt_loc={$wptdriver}";
                }
                if (isset($key) && strlen($key)) {
                    $user_data .= " wpt_key={$key}";
                }
                $size = GetSetting('ec2_instance_size');
                if (!$size) {
                    $size = 'm1.medium';
                }
                if (EC2_LaunchInstance($region, $ami, $size, $user_data, $loc)) {
                    $instances[$ami]['count']++;
                    file_put_contents('./tmp/ec2-instances.dat', json_encode($instances));
                }
            }
        }
        Unlock($lock);
    }
}
Пример #2
0
/**
* Start an EC2 Agent instance given the AMI
* 
* @param mixed $ami
*/
function EC2_StartInstance($ami)
{
    $started = false;
    // figure out the user data string to use for the instance
    $key = GetSetting('location_key');
    $locations = LoadLocationsIni();
    $urlblast = '';
    $wptdriver = '';
    $loc = '';
    $region = null;
    $size = GetSetting('ec2_instance_size');
    if ($locations && is_array($locations)) {
        foreach ($locations as $location => $config) {
            if (isset($config['ami']) && $config['ami'] == $ami) {
                if (isset($config['region'])) {
                    $region = trim($config['region']);
                }
                if (isset($config['size'])) {
                    $size = trim($config['size']);
                }
                if (isset($config['key']) && strlen($config['key'])) {
                    $key = trim($config['key']);
                }
                if (strlen($loc)) {
                    $loc .= ',';
                }
                $loc .= $location;
                if (isset($config['urlblast'])) {
                    if (strlen($urlblast)) {
                        $urlblast .= ',';
                    }
                    $urlblast .= $location;
                } else {
                    if (strlen($wptdriver)) {
                        $wptdriver .= ',';
                    }
                    $wptdriver .= $location;
                }
            }
        }
    }
    if (strlen($loc) && isset($region)) {
        $host = GetSetting('host');
        if (!$host && isset($_SERVER['HTTP_HOST']) && strlen($_SERVER['HTTP_HOST'])) {
            $host = $_SERVER['HTTP_HOST'];
        }
        if ((!$host || $host == '127.0.0.1' || $host == 'localhost') && GetSetting('ec2')) {
            $host = file_get_contents('http://169.254.169.254/latest/meta-data/public-ipv4');
            if (!isset($host) || !strlen($host)) {
                $host = file_get_contents('http://169.254.169.254/latest/meta-data/public-hostname');
            }
            if (!isset($host) || !strlen($host)) {
                $host = file_get_contents('http://169.254.169.254/latest/meta-data/hostname');
            }
        }
        $user_data = "wpt_server={$host}";
        if (strlen($urlblast)) {
            $user_data .= " wpt_location={$urlblast}";
        }
        if (strlen($wptdriver)) {
            $user_data .= " wpt_loc={$wptdriver}";
        }
        if (isset($key) && strlen($key)) {
            $user_data .= " wpt_key={$key}";
        }
        if (!$size) {
            $size = 'm3.medium';
        }
        $started = EC2_LaunchInstance($region, $ami, $size, $user_data, $loc);
    } else {
        EC2LogError("Region ({$region}) or Location ({$loc}) invalid in EC2_StartInstance");
    }
    return $started;
}