public static function get($autoScalingGroup)
 {
     if (!$autoScalingGroup) {
         throw new Kwf_Exception("autoScalingGroup is requried");
     }
     $ac = new Kwf_Util_Aws_AutoScaling();
     $r = $ac->describe_auto_scaling_groups(array('AutoScalingGroupNames' => $autoScalingGroup));
     if (!$r->isOK()) {
         throw new Kwf_Exception($r->body->asXml());
     }
     $instanceIds = array();
     if ($r->body->DescribeAutoScalingGroupsResult->AutoScalingGroups->member) {
         foreach ($r->body->DescribeAutoScalingGroupsResult->AutoScalingGroups->member->Instances->member as $member) {
             $instanceIds[] = (string) $member->InstanceId;
         }
     }
     if (!$instanceIds) {
         return array();
     }
     $ec2 = new Kwf_Util_Aws_Ec2();
     $r = $ec2->describe_instances(array('InstanceId' => $instanceIds));
     if (!$r->isOK()) {
         throw new Kwf_Exception($r->body->asXml());
     }
     $servers = array();
     foreach ($r->body->reservationSet->item as $reservaionSet) {
         foreach ($reservaionSet->instancesSet->item as $item) {
             $dnsName = (string) $item->dnsName;
             if ($dnsName) {
                 $servers[] = $dnsName;
             }
         }
     }
     return $servers;
 }
 public static function getOther()
 {
     $ec2 = new Kwf_Util_Aws_Ec2();
     $r = $ec2->describe_instances(array('Filter' => array(array('Name' => 'tag:application.id', 'Value' => Kwf_Config::getValue('application.id')), array('Name' => 'tag:config_section', 'Value' => Kwf_Setup::getConfigSection()))));
     if (!$r->isOK()) {
         throw new Kwf_Exception($r->body->asXml());
     }
     $ownHostname = file_get_contents('http://169.254.169.254/latest/meta-data/public-hostname');
     $domains = array();
     foreach ($r->body->reservationSet->item as $resItem) {
         foreach ($resItem->instancesSet->item as $item) {
             $dnsName = (string) $item->dnsName;
             if ($dnsName && $dnsName != $ownHostname) {
                 $domains[] = $dnsName;
             }
         }
     }
     return array_unique($domains);
 }
Exemplo n.º 3
0
 public static function callUtil($method, $params, $options = array())
 {
     $outputType = '';
     if (isset($params['type']) && $params['type'] == 'user') {
         $outputType = 'apc user';
     } else {
         if (isset($params['type']) && $params['type'] == 'file') {
             $outputType = 'optcode';
         }
     }
     $params['password'] = self::_getHttpPassword();
     $skipOtherServers = isset($options['skipOtherServers']) ? $options['skipOtherServers'] : false;
     $config = Kwf_Registry::get('config');
     if (!$config->server->aws || $skipOtherServers) {
         $d = $config->server->domain;
         if (!$d) {
             if (isset($options['outputFn'])) {
                 call_user_func($options['outputFn'], "error: {$outputType}: domain not set");
             }
             return false;
         }
         $domains = array(array('domain' => $d));
         if ($config->server->noRedirectPattern) {
             $domains[0]['alternative'] = str_replace(array('^', '\\', '$'), '', $config->server->noRedirectPattern);
         }
     } else {
         $ec2 = new Kwf_Util_Aws_Ec2();
         $r = $ec2->describe_instances(array('Filter' => array(array('Name' => 'tag:application.id', 'Value' => $config->application->id), array('Name' => 'tag:config_section', 'Value' => Kwf_Setup::getConfigSection()))));
         if (!$r->isOK()) {
             throw new Kwf_Exception($r->body->asXml());
         }
         $domains = array();
         foreach ($r->body->reservationSet->item as $resItem) {
             foreach ($resItem->instancesSet->item as $item) {
                 $dnsName = (string) $item->dnsName;
                 if ($dnsName) {
                     $domains[] = array('domain' => $dnsName);
                 }
             }
         }
     }
     foreach ($domains as $d) {
         $s = microtime(true);
         $urlPart = "http://";
         $baseUrl = Kwf_Setup::getBaseUrl();
         $url = "{$urlPart}{$d['domain']}{$baseUrl}/kwf/util/apc/{$method}";
         $client = new Zend_Http_Client();
         $client->setMethod(Zend_Http_Client::POST);
         $client->setParameterPost($params);
         $client->setConfig(array('timeout' => 60, 'keepalive' => true));
         $client->setUri($url);
         $body = null;
         $outputMessage = 'could not reach web per http';
         try {
             $response = $client->request();
             $result = !$response->isError() && substr($response->getBody(), 0, 2) == 'OK';
             $body = $response->getBody();
             $outputMessage = $body;
         } catch (Exception $e) {
             $result = false;
         }
         $url2 = null;
         if (!$result && isset($d['alternative'])) {
             $url2 = "{$urlPart}{$d['alternative']}{$baseUrl}/kwf/util/apc/{$method}";
             $client = new Zend_Http_Client();
             $client->setMethod(Zend_Http_Client::POST);
             $client->setConfig(array('timeout' => 60, 'keepalive' => true));
             $client->setUri($url2);
             $client->setParameterPost($params);
             try {
                 $response = $client->request();
                 $result = !$response->isError() && substr($response->getBody(), 0, 2) == 'OK';
                 $body = $response->getBody();
                 $outputMessage = $body;
             } catch (Exception $e) {
                 $result = false;
             }
         }
         if (isset($options['outputFn'])) {
             $outputUrl = $url;
             if ($url2) {
                 $outputUrl .= " / {$url2}";
             }
             $time = round((microtime(true) - $s) * 1000);
             if ($result) {
                 call_user_func($options['outputFn'], "{$outputUrl} ({$time}ms) {$outputMessage} ");
             } else {
                 call_user_func($options['outputFn'], "error: {$outputType} {$outputUrl} {$outputMessage}\n\n");
             }
         }
     }
     if (isset($options['returnBody']) && $options['returnBody']) {
         return $body;
     } else {
         return $result;
     }
 }