LoadByPropertyValue() public static method

Return DBServer by property value
public static LoadByPropertyValue ( string $propName, string $propValue ) : DBServer
$propName string
$propValue string
return DBServer
Ejemplo n.º 1
0
 public function xCreateAction()
 {
     $this->request->defineParams(array('volumeId', 'cloudLocation'));
     $aws = $this->getEnvironment()->aws($this->getParam('cloudLocation'));
     $snapshot = $aws->ec2->snapshot->create($this->getParam('volumeId'));
     if (isset($snapshot->snapshotId)) {
         /* @var $volume \Scalr\Service\Aws\Ec2\DataType\VolumeData */
         $volume = $aws->ec2->volume->describe($snapshot->volumeId)->get(0);
         if (count($volume->attachmentSet) && !empty($volume->attachmentSet[0]->instanceId)) {
             $instanceId = $volume->attachmentSet[0]->instanceId;
             try {
                 $dBServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $instanceId);
                 $dBFarm = $dBServer->GetFarmObject();
             } catch (Exception $e) {
             }
             if (isset($dBServer) && isset($dBFarm)) {
                 $comment = sprintf(_("Created on farm '%s', server '%s' (Instance ID: %s)"), $dBFarm->Name, $dBServer->serverId, $instanceId);
             }
         } else {
             $comment = '';
         }
         $this->db->Execute("\n                INSERT INTO ebs_snaps_info\n                SET snapid = ?,\n                    comment = ?,\n                    dtcreated = NOW(),\n                    region = ?\n            ", array($snapshot->snapshotId, $comment, $this->getParam('cloudLocation')));
         $this->response->data(array('data' => array('snapshotId' => $snapshot->snapshotId)));
     } else {
         throw new Exception("Unable to create snapshot. Please try again later.");
     }
 }
Ejemplo n.º 2
0
 public function xListVolumesAction()
 {
     $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'volumeId', 'direction' => 'ASC')), 'volumeId'));
     $csClient = Scalr_Service_Cloud_Cloudstack::newCloudstack($this->environment->getPlatformConfigValue(Modules_Platforms_Cloudstack::API_URL), $this->environment->getPlatformConfigValue(Modules_Platforms_Cloudstack::API_KEY), $this->environment->getPlatformConfigValue(Modules_Platforms_Cloudstack::SECRET_KEY));
     $volumes = $csClient->listVolumes($this->getParam('cloudLocation'));
     $vols = array();
     foreach ($volumes as $pk => $pv) {
         if ($this->getParam('volumeId') && $this->getParam('volumeId') != $pv->id) {
             continue;
         }
         $item = array('volumeId' => $pv->id, 'size' => round($pv->size / 1024 / 1024 / 1024, 2), 'status' => $pv->state, 'attachmentStatus' => $pv->virtualmachineid ? 'attached' : 'available', 'device' => $pv->deviceid, 'instanceId' => $pv->virtualmachineid, 'type' => $pv->type . " ({$pv->storagetype})", 'storage' => $pv->storage);
         $item['autoSnaps'] = $this->db->GetOne("SELECT id FROM autosnap_settings WHERE objectid=? AND object_type=?", array($pv->id, AUTOSNAPSHOT_TYPE::CSVOL)) ? true : false;
         if ($item['instanceId']) {
             try {
                 $dbServer = DBServer::LoadByPropertyValue(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, $item['instanceId']);
                 $item['farmId'] = $dbServer->farmId;
                 $item['farmRoleId'] = $dbServer->farmRoleId;
                 $item['serverIndex'] = $dbServer->index;
                 $item['serverId'] = $dbServer->serverId;
                 $item['farmName'] = $dbServer->GetFarmObject()->Name;
                 $item['mountStatus'] = false;
                 $item['roleName'] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
             } catch (Exception $e) {
             }
         }
         $vols[] = $item;
     }
     $response = $this->buildResponseFromData($vols, array('serverId', 'volumeId', 'farmId', 'farmRoleId', 'storage'));
     $this->response->data($response);
 }
Ejemplo n.º 3
0
 public function xListVolumesAction()
 {
     $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'volumeId', 'direction' => 'ASC')), 'volumeId'));
     $platformName = $this->getParam('platform');
     if (!$platformName) {
         throw new Exception("Cloud should be specified");
     }
     $client = $this->environment->openstack($platformName, $this->getParam('cloudLocation'));
     $volumes = $client->volume->listVolumes(true);
     $vols = array();
     foreach ($volumes as $pk => $pv) {
         if ($this->getParam('volumeId') && $this->getParam('volumeId') != $pv->id) {
             continue;
         }
         $item = array('volumeId' => $pv->id, 'size' => $pv->size, 'status' => $pv->status, 'attachmentStatus' => isset($pv->attachments[0]) ? 'attached' : 'available', 'device' => isset($pv->attachments[0]) ? $pv->attachments[0]->device : "", 'instanceId' => isset($pv->attachments[0]) ? $pv->attachments[0]->server_id : "", 'type' => $pv->volume_type, 'availability_zone' => $pv->availability_zone);
         if ($item['instanceId']) {
             try {
                 $dbServer = DBServer::LoadByPropertyValue(OPENSTACK_SERVER_PROPERTIES::SERVER_ID, $item['instanceId']);
                 $item['farmId'] = $dbServer->farmId;
                 $item['farmRoleId'] = $dbServer->farmRoleId;
                 $item['serverIndex'] = $dbServer->index;
                 $item['serverId'] = $dbServer->serverId;
                 $item['farmName'] = $dbServer->GetFarmObject()->Name;
                 $item['mountStatus'] = false;
                 $item['roleName'] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
             } catch (Exception $e) {
             }
         }
         $vols[] = $item;
     }
     $response = $this->buildResponseFromData($vols, array('serverId', 'volumeId', 'farmId', 'farmRoleId'));
     $this->response->data($response);
 }
Ejemplo n.º 4
0
 public function xCreateAction()
 {
     $this->request->defineParams(array('volumeId', 'cloudLocation'));
     $amazonEC2Client = Scalr_Service_Cloud_Aws::newEc2($this->getParam('cloudLocation'), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::PRIVATE_KEY), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::CERTIFICATE));
     $res = $amazonEC2Client->CreateSnapshot($this->getParam('volumeId'));
     if ($res->snapshotId) {
         $r = $amazonEC2Client->DescribeVolumes($res->volumeId);
         $info = $r->volumeSet->item;
         if ($info->attachmentSet->item->instanceId) {
             try {
                 $dBServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, (string) $info->attachmentSet->item->instanceId);
                 $dBFarm = $dBServer->GetFarmObject();
             } catch (Exception $e) {
             }
             if ($dBServer && $dBFarm) {
                 $comment = sprintf(_("Created on farm '%s', server '%s' (Instance ID: %s)"), $dBFarm->Name, $dBServer->serverId, (string) $info->attachmentSet->item->instanceId);
             }
         } else {
             $comment = "";
         }
         $this->db->Execute("INSERT INTO ebs_snaps_info SET snapid=?, comment=?, dtcreated=NOW(), region=?", array($res->snapshotId, $comment, $this->getParam('cloudLocation')));
         $this->response->data(array('data' => array('snapshotId' => $res->snapshotId)));
     } else {
         throw new Exception("Scalr unable to create snapshot. Please try again later.");
     }
 }
Ejemplo n.º 5
0
Archivo: Ips.php Proyecto: mheydt/scalr
 public function xListIpsAction()
 {
     $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'ipId', 'direction' => 'ASC')), 'ipId'));
     $platformName = $this->getParam('platform');
     if (!$platformName) {
         throw new Exception("Cloud should be specified");
     }
     $platform = PlatformFactory::NewPlatform($platformName);
     $cs = $this->environment->cloudstack($platformName);
     $ccProps = $this->environment->cloudCredentials($platformName)->properties;
     $accountName = $ccProps[\Scalr\Model\Entity\CloudCredentialsProperty::CLOUDSTACK_ACCOUNT_NAME];
     $domainId = $ccProps[\Scalr\Model\Entity\CloudCredentialsProperty::CLOUDSTACK_DOMAIN_ID];
     $requestData = new ListIpAddressesData();
     $requestData->account = $accountName;
     $requestData->domainid = $domainId;
     $requestData->zoneid = $this->getParam('cloudLocation');
     $ipAddresses = $cs->listPublicIpAddresses($requestData);
     $systemIp = $ccProps[\Scalr\Model\Entity\CloudCredentialsProperty::CLOUDSTACK_SHARED_IP . ".{$this->getParam('cloudLocation')}"];
     $ips = array();
     if (!empty($ipAddresses)) {
         foreach ($ipAddresses as $pk => $pv) {
             if ($this->getParam('ipId') && $this->getParam('ipId') != $pv->id) {
                 continue;
             }
             if ($pv->ipaddress == $systemIp) {
                 $pv->purpose = 'ScalrShared';
             }
             if ($pv->isstaticnat && !$pv->issystem) {
                 $pv->purpose = 'ElasticIP';
             }
             if ($pv->isstaticnat && $pv->issystem) {
                 $pv->purpose = 'PublicIP';
             }
             $item = array('ipId' => $pv->id, 'dtAllocated' => $pv->allocated, 'networkName' => $pv->associatednetworkname, 'purpose' => $pv->purpose ? $pv->purpose : "Not used", 'ip' => $pv->ipaddress, 'state' => $pv->state, 'instanceId' => $pv->virtualmachineid, 'fullinfo' => $pv, 'farmId' => false);
             if ($item['instanceId']) {
                 try {
                     $dbServer = DBServer::LoadByPropertyValue(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, $item['instanceId']);
                     $item['farmId'] = $dbServer->farmId;
                     $item['farmRoleId'] = $dbServer->farmRoleId;
                     $item['serverIndex'] = $dbServer->index;
                     $item['serverId'] = $dbServer->serverId;
                     $item['farmName'] = $dbServer->GetFarmObject()->Name;
                     $item['roleName'] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
                 } catch (Exception $e) {
                 }
             }
             $ips[] = $item;
         }
     }
     $response = $this->buildResponseFromData($ips, array('serverId', 'ipId', 'ip', 'farmId', 'farmRoleId'));
     $this->response->data($response);
 }
Ejemplo n.º 6
0
 public function xListIpsAction()
 {
     $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'ipId', 'direction' => 'ASC')), 'ipId'));
     $platformName = $this->getParam('platform');
     if (!$platformName) {
         throw new Exception("Cloud should be specified");
     }
     //$platform = PlatformFactory::NewPlatform($platformName);
     //$networkType = $platform->getConfigVariable(OpenstackPlatformModule::NETWORK_TYPE, $this->environment, false);
     $openstack = $this->environment->openstack($platformName, $this->getParam('cloudLocation'));
     //!FIXME dicsydel remove debug
     if ($openstack->hasService(OpenStack::SERVICE_NETWORK)) {
         var_dump($openstack->network->floatingIps->list()->toArray());
     }
     var_dump($openstack->servers->floatingIps->list()->toArray());
     var_dump($openstack->servers->listFloatingIpPools()->toArray());
     var_dump($openstack->network->listNetworks()->toArray());
     exit;
     $ips = array();
     foreach ($ipAddresses->publicipaddress as $pk => $pv) {
         if ($this->getParam('ipId') && $this->getParam('ipId') != $pv->id) {
             continue;
         }
         //!FIXME $systemIp variable has not been defined
         if ($pv->ipaddress == $systemIp) {
             $pv->purpose = 'ScalrShared';
         }
         if ($pv->isstaticnat && !$pv->issystem) {
             $pv->purpose = 'ElasticIP';
         }
         if ($pv->isstaticnat && $pv->issystem) {
             $pv->purpose = 'PublicIP';
         }
         $item = array('ipId' => $pv->id, 'dtAllocated' => $pv->allocated, 'networkName' => $pv->associatednetworkname, 'purpose' => $pv->purpose ? $pv->purpose : "Not used", 'ip' => $pv->ipaddress, 'state' => $pv->state, 'instanceId' => $pv->virtualmachineid, 'fullinfo' => $pv, 'farmId' => false);
         if ($item['instanceId']) {
             try {
                 $dbServer = DBServer::LoadByPropertyValue(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, $item['instanceId']);
                 $item['farmId'] = $dbServer->farmId;
                 $item['farmRoleId'] = $dbServer->farmRoleId;
                 $item['serverIndex'] = $dbServer->index;
                 $item['serverId'] = $dbServer->serverId;
                 $item['farmName'] = $dbServer->GetFarmObject()->Name;
                 $item['roleName'] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
             } catch (Exception $e) {
             }
         }
         $ips[] = $item;
     }
     $response = $this->buildResponseFromData($ips, array('serverId', 'ipId', 'ip', 'farmId', 'farmRoleId'));
     $this->response->data($response);
 }
Ejemplo n.º 7
0
 protected function ValidateRequestByFarmHash($farmid, $instanceid, $authhash)
 {
     try {
         $DBFarm = DBFarm::LoadByID($farmid);
         $DBServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $instanceid);
     } catch (Exception $e) {
         if (!$DBServer) {
             throw new Exception(sprintf(_("Cannot verify the instance you are making request from. Make sure that farmid, instance-id and auth-hash parameters are specified.")));
         }
     }
     if ($DBFarm->Hash != $authhash || $DBFarm->ID != $DBServer->farmId) {
         throw new Exception(sprintf(_("Cannot verify the instance you are making request from. Make sure that farmid (%s), instance-id (%s) and auth-hash (%s) parameters are valid."), $farmid, $instanceid, $authhash));
     }
     return $DBServer;
 }
Ejemplo n.º 8
0
 public function xListEipsAction()
 {
     $amazonEC2Client = Scalr_Service_Cloud_Aws::newEc2($this->getParam('cloudLocation'), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::PRIVATE_KEY), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::CERTIFICATE));
     // Rows
     $aws_response = $amazonEC2Client->DescribeAddresses();
     $rowz = $aws_response->addressesSet->item;
     if ($rowz instanceof stdClass) {
         $rowz = array($rowz);
     }
     foreach ($rowz as &$pv) {
         $item = array('ipaddress' => $pv->publicIp, 'instance_id' => $pv->instanceId);
         $info = $this->db->GetRow("SELECT * FROM elastic_ips WHERE ipaddress=?", array($pv->publicIp));
         if ($info) {
             $item['farm_id'] = $info['farmid'];
             $item['farm_roleid'] = $info['farm_roleid'];
             $item['server_id'] = $info['server_id'];
             $item['indb'] = true;
             $item['server_index'] = $info['instance_index'];
             //WORKAROUND: EIPS not imported correclty from 1.2 to 2.0
             if (!$item['server_id'] && $info['state'] == 1) {
                 try {
                     $DBServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $item['instance_id']);
                     $item['server_id'] = $DBServer->serverId;
                 } catch (Exception $e) {
                 }
             }
             if ($item['farm_roleid']) {
                 try {
                     $DBFarmRole = DBFarmRole::LoadByID($item['farm_roleid']);
                     $item['role_name'] = $DBFarmRole->GetRoleObject()->name;
                     $item['farm_name'] = $DBFarmRole->GetFarmObject()->Name;
                 } catch (Exception $e) {
                 }
             }
         } else {
             try {
                 $DBServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $pv->instanceId);
                 $item['server_id'] = $DBServer->serverId;
                 $item['farm_id'] = $DBServer->farmId;
             } catch (Exception $e) {
             }
         }
         $pv = $item;
     }
     $response = $this->buildResponseFromData($rowz);
     $this->response->data($response);
 }
Ejemplo n.º 9
0
Archivo: Ips.php Proyecto: recipe/scalr
 public function xListIpsAction()
 {
     $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'ipId', 'direction' => 'ASC')), 'ipId'));
     $platformName = $this->getParam('platform');
     if (!$platformName) {
         throw new Exception("Cloud should be specified");
     }
     $platform = PlatformFactory::NewPlatform($platformName);
     $cs = Scalr_Service_Cloud_Cloudstack::newCloudstack($platform->getConfigVariable(Modules_Platforms_Cloudstack::API_URL, $this->environment), $platform->getConfigVariable(Modules_Platforms_Cloudstack::API_KEY, $this->environment), $platform->getConfigVariable(Modules_Platforms_Cloudstack::SECRET_KEY, $this->environment), $platformName);
     $accountName = $platform->getConfigVariable(Modules_Platforms_Cloudstack::ACCOUNT_NAME, $this->getEnvironment(), false);
     $domainId = $platform->getConfigVariable(Modules_Platforms_Cloudstack::DOMAIN_ID, $this->getEnvironment(), false);
     $ipAddresses = $cs->listPublicIpAddresses(null, $accountName, null, $domainId, null, null, null, null, null, null, $this->getParam('cloudLocation'));
     $systemIp = $platform->getConfigVariable(Modules_Platforms_Cloudstack::SHARED_IP . "." . $this->getParam('cloudLocation'), $this->environment);
     $ips = array();
     foreach ($ipAddresses->publicipaddress as $pk => $pv) {
         if ($this->getParam('ipId') && $this->getParam('ipId') != $pv->id) {
             continue;
         }
         if ($pv->ipaddress == $systemIp) {
             $pv->purpose = 'ScalrShared';
         }
         if ($pv->isstaticnat && !$pv->issystem) {
             $pv->purpose = 'ElasticIP';
         }
         if ($pv->isstaticnat && $pv->issystem) {
             $pv->purpose = 'PublicIP';
         }
         $item = array('ipId' => $pv->id, 'dtAllocated' => $pv->allocated, 'networkName' => $pv->associatednetworkname, 'purpose' => $pv->purpose ? $pv->purpose : "Not used", 'ip' => $pv->ipaddress, 'state' => $pv->state, 'instanceId' => $pv->virtualmachineid, 'fullinfo' => $pv, 'farmId' => false);
         if ($item['instanceId']) {
             try {
                 $dbServer = DBServer::LoadByPropertyValue(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, $item['instanceId']);
                 $item['farmId'] = $dbServer->farmId;
                 $item['farmRoleId'] = $dbServer->farmRoleId;
                 $item['serverIndex'] = $dbServer->index;
                 $item['serverId'] = $dbServer->serverId;
                 $item['farmName'] = $dbServer->GetFarmObject()->Name;
                 $item['roleName'] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
             } catch (Exception $e) {
             }
         }
         $ips[] = $item;
     }
     $response = $this->buildResponseFromData($ips, array('serverId', 'ipId', 'ip', 'farmId', 'farmRoleId'));
     $this->response->data($response);
 }
Ejemplo n.º 10
0
 public function xListEipsAction()
 {
     $aws = $this->environment->aws($this->getParam('cloudLocation'));
     $addressList = $aws->ec2->address->describe();
     $rowz = array();
     /* @var $address Ec2DataType\AddressData */
     foreach ($addressList as $address) {
         $item = array('ipaddress' => $address->publicIp, 'allocation_id' => $address->allocationId, 'domain' => $address->domain, 'instance_id' => $address->instanceId === null ? '' : $address->instanceId);
         $info = $this->db->GetRow("SELECT * FROM elastic_ips WHERE ipaddress=? LIMIT 1", array($address->publicIp));
         if ($info) {
             $item['farm_id'] = $info['farmid'];
             $item['farm_roleid'] = $info['farm_roleid'];
             $item['server_id'] = $info['server_id'];
             $item['indb'] = true;
             $item['server_index'] = $info['instance_index'];
             //WORKAROUND: EIPS not imported correclty from 1.2 to 2.0
             if (!$item['server_id'] && $info['state'] == 1) {
                 try {
                     $DBServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $item['instance_id']);
                     $item['server_id'] = $DBServer->serverId;
                 } catch (Exception $e) {
                 }
             }
             if ($item['farm_roleid']) {
                 try {
                     $DBFarmRole = DBFarmRole::LoadByID($item['farm_roleid']);
                     $item['role_name'] = $DBFarmRole->GetRoleObject()->name;
                     $item['farm_name'] = $DBFarmRole->GetFarmObject()->Name;
                 } catch (Exception $e) {
                 }
             }
         } else {
             try {
                 $DBServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $address->instanceId);
                 $item['server_id'] = $DBServer->serverId;
                 $item['farm_id'] = $DBServer->farmId;
             } catch (Exception $e) {
             }
         }
         $rowz[] = $item;
     }
     $response = $this->buildResponseFromData($rowz);
     $this->response->data($response);
 }
Ejemplo n.º 11
0
 public function xCreateAction()
 {
     $this->request->restrictAccess(Acl::RESOURCE_AWS_SNAPSHOTS, Acl::PERM_AWS_SNAPSHOTS_MANAGE);
     $this->request->defineParams(array('volumeId', 'cloudLocation', 'description'));
     $aws = $this->getEnvironment()->aws($this->getParam('cloudLocation'));
     $snapshot = $aws->ec2->snapshot->create($this->getParam('volumeId'), $this->getParam('description'));
     if (isset($snapshot->snapshotId)) {
         /* @var $volume \Scalr\Service\Aws\Ec2\DataType\VolumeData */
         $volume = $aws->ec2->volume->describe($snapshot->volumeId)->get(0);
         if (!empty($volume->tagSet) && $volume->tagSet->count()) {
             try {
                 //We need to do sleep due to eventual consistency on EC2
                 sleep(2);
                 //Set tags (copy them from the original EBS volume)
                 $snapshot->createTags($volume->tagSet);
             } catch (Exception $e) {
                 //We want to hear from the cases when it cannot set tag to snapshot
                 trigger_error(sprintf("Cound not set tag to snapshot: %s", $e->getMessage()), E_USER_WARNING);
             }
         }
         if (count($volume->attachmentSet) && !empty($volume->attachmentSet[0]->instanceId)) {
             $instanceId = $volume->attachmentSet[0]->instanceId;
             try {
                 $dBServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $instanceId);
                 $dBFarm = $dBServer->GetFarmObject();
             } catch (Exception $e) {
             }
             if (isset($dBServer) && isset($dBFarm)) {
                 $comment = sprintf(_("Created on farm '%s', server '%s' (Instance ID: %s)"), $dBFarm->Name, $dBServer->serverId, $instanceId);
             }
         } else {
             $comment = '';
         }
         $this->db->Execute("\n                INSERT INTO ebs_snaps_info\n                SET snapid = ?,\n                    comment = ?,\n                    dtcreated = NOW(),\n                    region = ?\n            ", array($snapshot->snapshotId, $comment, $this->getParam('cloudLocation')));
         $this->response->data(array('data' => array('snapshotId' => $snapshot->snapshotId)));
     } else {
         throw new Exception("Unable to create snapshot. Please try again later.");
     }
 }
Ejemplo n.º 12
0
 public function xListVolumesAction()
 {
     $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'volumeId', 'direction' => 'ASC')), 'volumeId'));
     $platformName = $this->getParam('platform');
     if (!$platformName) {
         throw new Exception("Cloud should be specified");
     }
     $cs = $this->environment->cloudstack($platformName);
     $requestData = new ListVolumesData();
     $requestData->zoneid = $this->getParam('cloudLocation');
     $requestData->listall = true;
     $volumes = $cs->volume->describe($requestData);
     $vols = array();
     if (!empty($volumes)) {
         foreach ($volumes as $pk => $pv) {
             if ($this->getParam('volumeId') && $this->getParam('volumeId') != $pv->id) {
                 continue;
             }
             $item = array('volumeId' => $pv->id, 'size' => round($pv->size / 1024 / 1024 / 1024, 2), 'status' => $pv->state, 'attachmentStatus' => $pv->virtualmachineid ? 'attached' : 'available', 'device' => $pv->deviceid, 'instanceId' => $pv->virtualmachineid, 'type' => $pv->type . " ({$pv->storagetype})", 'storage' => $pv->storage);
             $item['autoSnaps'] = $this->db->GetOne("SELECT id FROM autosnap_settings WHERE objectid=? AND object_type=? LIMIT 1", array($pv->id, AUTOSNAPSHOT_TYPE::CSVOL)) ? true : false;
             if ($item['instanceId']) {
                 try {
                     $dbServer = DBServer::LoadByPropertyValue(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, $item['instanceId']);
                     $item['farmId'] = $dbServer->farmId;
                     $item['farmRoleId'] = $dbServer->farmRoleId;
                     $item['serverIndex'] = $dbServer->index;
                     $item['serverId'] = $dbServer->serverId;
                     $item['farmName'] = $dbServer->GetFarmObject()->Name;
                     $item['mountStatus'] = false;
                     $item['roleName'] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
                 } catch (Exception $e) {
                 }
             }
             $vols[] = $item;
         }
     }
     $response = $this->buildResponseFromData($vols, array('serverId', 'volumeId', 'farmId', 'farmRoleId', 'storage'));
     $this->response->data($response);
 }
Ejemplo n.º 13
0
 public function xGetCloudServersListAction()
 {
     $this->request->defineParams(array('platform', 'cloudLocation'));
     if (!$this->environment->isPlatformEnabled($this->getParam('platform'))) {
         throw new Exception(sprintf('Cloud %s is not enabled for current environment', $this->getParam('platform')));
     }
     $results = array();
     $platform = PlatformFactory::NewPlatform($this->getParam('platform'));
     //TODO: Added support for GCE
     if ($this->getParam('platform') == SERVER_PLATFORMS::GCE) {
         $gce = $platform->getClient($this->environment, $this->getParam('cloudLocation'));
         $result = $gce->instances->listInstances($this->environment->getPlatformConfigValue(GoogleCEPlatformModule::PROJECT_ID), $this->getParam('cloudLocation'), array());
         if (is_array($result->items)) {
             foreach ($result->items as $server) {
                 if ($server->status != 'RUNNING') {
                     continue;
                 }
                 $ips = $platform->determineServerIps($gce, $server);
                 $itm = array('id' => $server->name, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $this->getParam('cloudLocation'), 'isImporting' => false, 'isManaged' => false);
                 //Check is instance already importing
                 try {
                     $dbServer = DBServer::LoadByPropertyValue(GCE_SERVER_PROPERTIES::SERVER_NAME, $server->name);
                     if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) {
                         if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                             $itm['isImporting'] = true;
                         } else {
                             $itm['isManaged'] = true;
                         }
                         $itm['serverId'] = $dbServer->serverId;
                     }
                 } catch (Exception $e) {
                 }
                 $results[] = $itm;
             }
         }
     } elseif (PlatformFactory::isOpenstack($this->getParam('platform'))) {
         $client = $this->environment->openstack($this->getParam('platform'), $this->getParam('cloudLocation'));
         $r = $client->servers->list(true);
         do {
             foreach ($r as $server) {
                 if ($server->status != 'ACTIVE') {
                     continue;
                 }
                 $ips = $platform->determineServerIps($client, $server);
                 $itm = array('id' => $server->id, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $this->getParam('cloudLocation'), 'isImporting' => false, 'isManaged' => false);
                 //Check is instance already importing
                 try {
                     $dbServer = DBServer::LoadByPropertyValue(OPENSTACK_SERVER_PROPERTIES::SERVER_ID, $server->id);
                     if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) {
                         if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                             $itm['isImporting'] = true;
                         } else {
                             $itm['isManaged'] = true;
                         }
                         $itm['serverId'] = $dbServer->serverId;
                     }
                 } catch (Exception $e) {
                 }
                 $results[] = $itm;
             }
         } while (false !== ($r = $r->getNextPage()));
     } elseif (PlatformFactory::isCloudstack($this->getParam('platform'))) {
         $client = $this->environment->cloudstack($this->getParam('platform'));
         $platform = PlatformFactory::NewPlatform($this->getParam('platform'));
         $r = $client->instance->describe(array('zoneid' => $this->getParam('cloudLocation')));
         if (count($r) > 0) {
             foreach ($r as $server) {
                 $ips = $platform->determineServerIps($client, $server);
                 $itm = array('id' => $server->id, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $this->getParam('cloudLocation'), 'isImporting' => false, 'isManaged' => false);
                 //Check is instance already importing
                 try {
                     $dbServer = DBServer::LoadByPropertyValue(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, $server->id);
                     if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) {
                         if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                             $itm['isImporting'] = true;
                         } else {
                             $itm['isManaged'] = true;
                         }
                         $itm['serverId'] = $dbServer->serverId;
                     }
                 } catch (Exception $e) {
                 }
                 $results[] = $itm;
             }
         }
     } elseif ($this->getParam('platform') == SERVER_PLATFORMS::EC2) {
         $client = $this->environment->aws($this->getParam('cloudLocation'))->ec2;
         $nextToken = null;
         do {
             if (isset($r)) {
                 $nextToken = $r->getNextToken();
             }
             $r = $client->instance->describe(null, null, $nextToken);
             if (count($r)) {
                 foreach ($r as $reservation) {
                     /* @var $reservation Scalr\Service\Aws\Ec2\DataType\ReservationData */
                     foreach ($reservation->instancesSet as $instance) {
                         /* @var $instance Scalr\Service\Aws\Ec2\DataType\InstanceData */
                         if ($instance->instanceState->name != 'running') {
                             continue;
                         }
                         $itm = array('id' => $instance->instanceId, 'localIp' => $instance->privateIpAddress, 'publicIp' => $instance->ipAddress, 'zone' => $instance->placement->availabilityZone, 'isImporting' => false, 'isManaged' => false);
                         //Check is instance already importing
                         try {
                             $dbServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $instance->instanceId);
                             if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) {
                                 if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                                     $itm['isImporting'] = true;
                                 } else {
                                     $itm['isManaged'] = true;
                                 }
                                 $itm['serverId'] = $dbServer->serverId;
                             }
                         } catch (Exception $e) {
                         }
                         $results[] = $itm;
                     }
                 }
             }
         } while ($r->getNextToken());
     } elseif ($this->getParam('platform') == SERVER_PLATFORMS::EUCALYPTUS) {
         $client = $this->environment->eucalyptus($this->getParam('cloudLocation'))->ec2;
         $r = $client->instance->describe(null, null, $nextToken);
         if (count($r)) {
             foreach ($r as $reservation) {
                 /* @var $reservation Scalr\Service\Aws\Ec2\DataType\ReservationData */
                 foreach ($reservation->instancesSet as $instance) {
                     /* @var $instance Scalr\Service\Aws\Ec2\DataType\InstanceData */
                     if ($instance->instanceState->name != 'running') {
                         continue;
                     }
                     $itm = array('id' => $instance->instanceId, 'localIp' => $instance->privateIpAddress, 'publicIp' => $instance->ipAddress, 'zone' => $instance->placement->availabilityZone, 'isImporting' => false, 'isManaged' => false);
                     //Check is instance already importing
                     try {
                         $dbServer = DBServer::LoadByPropertyValue(EUCA_SERVER_PROPERTIES::INSTANCE_ID, $instance->instanceId);
                         if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) {
                             if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                                 $itm['isImporting'] = true;
                             } else {
                                 $itm['isManaged'] = true;
                             }
                             $itm['serverId'] = $dbServer->serverId;
                         }
                     } catch (Exception $e) {
                     }
                     $results[] = $itm;
                 }
             }
         }
     }
     $this->response->data(array('data' => $results));
 }
Ejemplo n.º 14
0
 public function xListVolumesAction()
 {
     $aws = $this->getEnvironment()->aws($this->getParam('cloudLocation'));
     $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'volumeId', 'direction' => 'DESC')), 'volumeId'));
     if ($this->getParam('volumeId')) {
         $filter = array(array('name' => VolumeFilterNameType::volumeId(), 'value' => $this->getParam('volumeId')));
     } else {
         $filter = null;
     }
     // Rows
     $volumeList = $aws->ec2->volume->describe(null, $filter);
     $vols = array();
     /* @var $pv VolumeData */
     foreach ($volumeList as $pv) {
         /* @var $att AttachmentSetResponseData */
         if (count($pv->attachmentSet)) {
             $att = $pv->attachmentSet[0];
         } else {
             $att = null;
         }
         $tags = array();
         foreach ($pv->tagSet as $tag) {
             /* @var $tag ResourceTagSetData */
             $tg = "{$tag->key}";
             if ($tag->value) {
                 $tg .= "={$tag->value}";
             }
             $tags[] = $tg;
         }
         $item = array('volumeId' => $pv->volumeId, 'size' => (int) $pv->size, 'snapshotId' => $pv->snapshotId, 'availZone' => $pv->availabilityZone, 'type' => $pv->volumeType, 'status' => $pv->status, 'attachmentStatus' => $att !== null ? $att->status : null, 'device' => $att !== null ? $att->device : null, 'instanceId' => $att !== null ? $att->instanceId : null, 'tags' => implode(',', $tags));
         $item['autoSnaps'] = $this->db->GetOne("SELECT id FROM autosnap_settings WHERE objectid=? AND object_type=? LIMIT 1", array($pv->volumeId, AUTOSNAPSHOT_TYPE::EBSSnap)) ? true : false;
         if (!empty($item['instanceId'])) {
             try {
                 $dbServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $item['instanceId']);
                 if ($dbServer) {
                     $item['farmId'] = $dbServer->farmId;
                     $item['farmRoleId'] = $dbServer->farmRoleId;
                     $item['serverIndex'] = $dbServer->index;
                     $item['serverId'] = $dbServer->serverId;
                     $item['farmName'] = $dbServer->GetFarmObject()->Name;
                     $item['mountStatus'] = false;
                     $item['roleName'] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
                     /* Waiting for bugfix on scalarizr side
                        if ($dbServer->IsSupported('2.5.4')) {
                            $item['mounts'] = $dbServer->scalarizr->system->mounts();
                        }
                        */
                 }
             } catch (\Exception $e) {
             }
         }
         $vols[] = $item;
     }
     $response = $this->buildResponseFromData($vols, array('instanceId', 'volumeId', 'snapshotId', 'farmId', 'farmRoleId', 'availZone', 'type'));
     $this->response->data($response);
 }
Ejemplo n.º 15
0
 public function xListVolumesAction()
 {
     $aws = $this->getEnvironment()->aws($this->getParam('cloudLocation'));
     $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'volumeId', 'direction' => 'DESC')), 'volumeId'));
     if ($this->getParam('volumeId')) {
         $filter = array(array('name' => VolumeFilterNameType::volumeId(), 'value' => $this->getParam('volumeId')));
     } else {
         $filter = null;
     }
     // Rows
     $volumeList = $aws->ec2->volume->describe(null, $filter);
     $vols = array();
     /* @var $pv VolumeData */
     foreach ($volumeList as $pv) {
         /* @var $att AttachmentSetResponseData */
         if (count($pv->attachmentSet)) {
             $att = $pv->attachmentSet[0];
         } else {
             $att = null;
         }
         $item = array('volumeId' => $pv->volumeId, 'size' => (int) $pv->size, 'snapshotId' => $pv->snapshotId, 'availZone' => $pv->availabilityZone, 'type' => $pv->volumeType, 'status' => $pv->status, 'attachmentStatus' => $att !== null ? $att->status : null, 'device' => $att !== null ? $att->device : null, 'instanceId' => $att !== null ? $att->instanceId : null);
         $item['autoSnaps'] = $this->db->GetOne("SELECT id FROM autosnap_settings WHERE objectid=? AND object_type=? LIMIT 1", array($pv->volumeId, AUTOSNAPSHOT_TYPE::EBSSnap)) ? true : false;
         $dbEbsVolume = false;
         try {
             $dbEbsVolume = DBEBSVolume::loadByVolumeId($pv->volumeId);
             $item['farmId'] = $dbEbsVolume->farmId;
             $item['farmRoleId'] = $dbEbsVolume->farmRoleId;
             $item['serverIndex'] = $dbEbsVolume->serverIndex;
             $item['serverId'] = $dbEbsVolume->serverId;
             $item['mountStatus'] = $dbEbsVolume->mountStatus;
             $item['farmName'] = DBFarm::LoadByID($dbEbsVolume->farmId)->Name;
             $item['roleName'] = DBFarmRole::LoadByID($dbEbsVolume->farmRoleId)->GetRoleObject()->name;
             $item['autoAttach'] = true;
         } catch (\Exception $e) {
         }
         if (!$dbEbsVolume && !empty($item['instanceId'])) {
             try {
                 $dbServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $item['instanceId']);
                 $item['farmId'] = $dbServer->farmId;
                 $item['farmRoleId'] = $dbServer->farmRoleId;
                 $item['serverIndex'] = $dbServer->index;
                 $item['serverId'] = $dbServer->serverId;
                 $item['farmName'] = $dbServer->GetFarmObject()->Name;
                 $item['mountStatus'] = false;
                 $item['roleName'] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
             } catch (\Exception $e) {
             }
         }
         $vols[] = $item;
     }
     $response = $this->buildResponseFromData($vols, array('instanceId', 'volumeId', 'snapshotId', 'farmId', 'farmRoleId', 'availZone', 'type'));
     $this->response->data($response);
 }
Ejemplo n.º 16
0
 /**
  * @param   string  $platform
  * @param   string  $cloudLocation
  * @throws  Exception
  */
 public function xGetCloudServersListAction($platform, $cloudLocation)
 {
     if (!$this->environment->isPlatformEnabled($platform)) {
         throw new Exception(sprintf('Cloud %s is not enabled for current environment', $platform));
     }
     $results = [];
     $platformObj = PlatformFactory::NewPlatform($platform);
     if ($platform == SERVER_PLATFORMS::GCE) {
         $gce = $platformObj->getClient($this->environment);
         $result = $gce->instances->listInstances($this->environment->keychain(SERVER_PLATFORMS::GCE)->properties[CloudCredentialsProperty::GCE_PROJECT_ID], $cloudLocation, []);
         if (is_array($result->items)) {
             foreach ($result->items as $server) {
                 if ($server->status != 'RUNNING') {
                     continue;
                 }
                 $ips = $platformObj->determineServerIps($gce, $server);
                 $itm = ['id' => $server->name, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $cloudLocation, 'isImporting' => false, 'isManaged' => false];
                 //Check is instance already importing
                 try {
                     $dbServer = DBServer::LoadByPropertyValue(GCE_SERVER_PROPERTIES::SERVER_NAME, $server->name);
                     if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) {
                         if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                             $itm['isImporting'] = true;
                         } else {
                             $itm['isManaged'] = true;
                         }
                         $itm['serverId'] = $dbServer->serverId;
                     }
                 } catch (Exception $e) {
                 }
                 $results[] = $itm;
             }
         }
     } else {
         if ($platform == SERVER_PLATFORMS::AZURE) {
             // cloudLocation is resourceGroup
             $t = $this->getEnvironment()->azure()->compute->virtualMachine->getList($this->getEnvironment()->keychain(SERVER_PLATFORMS::AZURE)->properties[CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID], $cloudLocation);
             foreach ($t as $server) {
                 $itm = ['id' => $server->name, 'isImporting' => false, 'isManaged' => false];
                 $nicInfo = $server->properties->networkProfile->networkInterfaces[0]->id;
                 // get id and call
                 if (!empty($nicInfo->properties->ipConfigurations)) {
                     foreach ($nicInfo->properties->ipConfigurations as $ipConfig) {
                         $privateIp = $ipConfig->properties->privateIPAddress;
                         if ($ipConfig->properties->publicIPAddress) {
                             $publicIp = $ipConfig->properties->publicIPAddress->properties->ipAddress;
                             if ($publicIp) {
                                 break;
                             }
                         }
                     }
                 }
                 $itm['localIp'] = $privateIp;
                 $itm['publicIp'] = $publicIp;
                 $itm['zone'] = $server->location;
                 $results[] = $itm;
             }
         } elseif (PlatformFactory::isOpenstack($platform)) {
             $client = $this->environment->openstack($platform, $cloudLocation);
             $r = $client->servers->list(true);
             do {
                 foreach ($r as $server) {
                     if ($server->status != 'ACTIVE') {
                         continue;
                     }
                     $ips = $platformObj->determineServerIps($client, $server);
                     $itm = array('id' => $server->id, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $cloudLocation, 'isImporting' => false, 'isManaged' => false, 'fullInfo' => $server);
                     //Check is instance already importing
                     try {
                         $dbServer = DBServer::LoadByPropertyValue(OPENSTACK_SERVER_PROPERTIES::SERVER_ID, $server->id);
                         if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) {
                             if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                                 $itm['isImporting'] = true;
                             } else {
                                 $itm['isManaged'] = true;
                             }
                             $itm['serverId'] = $dbServer->serverId;
                         }
                     } catch (Exception $e) {
                     }
                     $results[] = $itm;
                 }
             } while (false !== ($r = $r->getNextPage()));
         } elseif (PlatformFactory::isCloudstack($platform)) {
             $client = $this->environment->cloudstack($platform);
             $platformObj = PlatformFactory::NewPlatform($platform);
             $r = $client->instance->describe(array('zoneid' => $cloudLocation));
             if (count($r) > 0) {
                 foreach ($r as $server) {
                     $ips = $platformObj->determineServerIps($client, $server);
                     $itm = array('id' => $server->id, 'localIp' => $ips['localIp'], 'publicIp' => $ips['remoteIp'], 'zone' => $cloudLocation, 'isImporting' => false, 'isManaged' => false);
                     //Check is instance already importing
                     try {
                         $dbServer = DBServer::LoadByPropertyValue(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, $server->id);
                         if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) {
                             if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                                 $itm['isImporting'] = true;
                             } else {
                                 $itm['isManaged'] = true;
                             }
                             $itm['serverId'] = $dbServer->serverId;
                         }
                     } catch (Exception $e) {
                     }
                     $results[] = $itm;
                 }
             }
         } elseif ($platform == SERVER_PLATFORMS::EC2) {
             $client = $this->environment->aws($cloudLocation)->ec2;
             $nextToken = null;
             do {
                 if (isset($r)) {
                     $nextToken = $r->getNextToken();
                 }
                 $r = $client->instance->describe(null, null, $nextToken);
                 if (count($r)) {
                     foreach ($r as $reservation) {
                         /* @var $reservation Scalr\Service\Aws\Ec2\DataType\ReservationData */
                         foreach ($reservation->instancesSet as $instance) {
                             /* @var $instance Scalr\Service\Aws\Ec2\DataType\InstanceData */
                             if ($instance->instanceState->name != 'running') {
                                 continue;
                             }
                             $itm = array('id' => $instance->instanceId, 'localIp' => $instance->privateIpAddress, 'publicIp' => $instance->ipAddress, 'zone' => $instance->placement->availabilityZone, 'isImporting' => false, 'isManaged' => false);
                             //Check is instance already importing
                             try {
                                 $dbServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $instance->instanceId);
                                 if ($dbServer && $dbServer->status != SERVER_STATUS::TERMINATED) {
                                     if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                                         $itm['isImporting'] = true;
                                     } else {
                                         $itm['isManaged'] = true;
                                     }
                                     $itm['serverId'] = $dbServer->serverId;
                                 }
                             } catch (Exception $e) {
                             }
                             $results[] = $itm;
                         }
                     }
                 }
             } while ($r->getNextToken());
         }
     }
     $this->response->data(array('data' => $results));
 }
Ejemplo n.º 17
0
 $chunks = explode(";", $req_Data);
 foreach ($chunks as $chunk) {
     $dt = explode(":", $chunk);
     $data[$dt[0]] = trim($dt[1]);
 }
 // prepare GET params
 $farm_id = (int) $req_FarmID;
 $hash = preg_replace("/[^A-Za-z0-9]+/", "", $req_Hash);
 $pkg_ver = $req_PkgVer;
 //TODO:
 //Logger::getLogger('EVENT_HANDLER')->info(new FarmLogMessage($farm_id, http_build_query($_REQUEST)));
 //Logger::getLogger('EVENT_HANDLER')->info(http_build_query($_REQUEST));
 // Get farminfo and instanceinfo from database
 $farminfo = $db->GetRow("SELECT id FROM farms WHERE id=? AND hash=? LIMIT 1", array($farm_id, $hash));
 $DBFarm = DBFarm::LoadByID($farminfo['id']);
 $DBServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $req_InstanceID);
 if ($DBServer->farmId != $DBFarm->ID) {
     throw new Exception("Server not found");
 }
 if ($DBServer->GetProperty(SERVER_PROPERTIES::SZR_VESION) != $pkg_ver) {
     $DBServer->SetProperty(SERVER_PROPERTIES::SZR_VESION, $pkg_ver);
 }
 /**
  * Deserialize data from instance
  */
 if (!$DBServer->localIp) {
     $DBServer->localIp = $data["localip"];
 }
 if ($DBServer->localIp == $_SERVER['REMOTE_ADDR'] && $DBServer->platform == SERVER_PLATFORMS::EC2) {
     if (!$DBServer->remoteIp) {
         try {
Ejemplo n.º 18
0
 /**
  * List volumes
  *
  * @param string $cloudLocation          The location of a cloud
  * @param string $volumeId      optional Volume ID
  * @param int    $farmId        optional Farm ID
  * @param int    $farmRoleId    optional Role ID tied to the farm
  */
 public function xListVolumesAction($cloudLocation, $volumeId = null, $farmId = null, $farmRoleId = null)
 {
     $filter = [];
     $filterFields = ["instanceId", "volumeId", "snapshotId", "availZone", "type"];
     $env = $this->getEnvironment();
     $aws = $env->aws($cloudLocation);
     if (!empty($volumeId)) {
         $filter = [["name" => VolumeFilterNameType::volumeId(), "value" => $volumeId]];
     }
     $filteringDecision = $this->request->getCloudResourceFilteringDecision(Acl::RESOURCE_AWS_VOLUMES, SERVER_PLATFORMS::EC2, !empty($farmId) ? $farmId : null);
     if ($filteringDecision->emptySet) {
         //This user hasn't any managed Farm. We should return empty result set.
         $response = $this->buildResponseFromData([], $filterFields);
         return $this->response->data($response);
     } elseif (!empty($filteringDecision->filter)) {
         $filter = array_merge($filter, $filteringDecision->filter);
     }
     // Rows
     $startTime = microtime(true);
     $volumeList = $aws->ec2->volume->describe(null, empty($filter) ? null : $filter);
     $describeTime = round(microtime(true) - $startTime) * 1000;
     $startTime = microtime(true);
     $vols = [];
     $needFilter = !empty($farmId) || !empty($farmRoleId);
     foreach ($volumeList as $pv) {
         /* @var $pv VolumeData */
         /* @var $att AttachmentSetResponseData */
         $att = count($pv->attachmentSet) ? $pv->attachmentSet[0] : null;
         $tags = [];
         $scalrMetaTag = null;
         foreach ($pv->tagSet as $tag) {
             /* @var $tag ResourceTagSetData */
             $tg = "{$tag->key}";
             if ($tag->value) {
                 $tg .= "={$tag->value}";
             }
             if ($tag->key == Scalr_Governance::SCALR_META_TAG_NAME) {
                 $scalrMetaTag = $tag->value;
             }
             $tags[] = $tg;
         }
         if (!$filteringDecision->matchScalrMetaTag($scalrMetaTag)) {
             continue;
         }
         $item = ["volumeId" => $pv->volumeId, "size" => (int) $pv->size, "snapshotId" => $pv->snapshotId, "availZone" => $pv->availabilityZone, "type" => $pv->volumeType, "status" => $pv->status, "attachmentStatus" => $att !== null ? $att->status : null, "device" => $att !== null ? $att->device : null, "instanceId" => $att !== null ? $att->instanceId : null, "tags" => implode(",", $tags), "encrypted" => $pv->encrypted, "kmsKeyId" => $pv->kmsKeyId];
         if (!empty($item["instanceId"])) {
             try {
                 if (isset($this->listCache["instances"][$item["instanceId"]])) {
                     $dbServer = $this->listCache["instances"][$item["instanceId"]];
                 } else {
                     $dbServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $item["instanceId"]);
                     $this->listCache["instances"][$item["instanceId"]] = $dbServer;
                 }
                 if ($dbServer) {
                     $item["farmId"] = $dbServer->farmId;
                     $item["farmRoleId"] = $dbServer->farmRoleId;
                     $item["serverIndex"] = $dbServer->index;
                     $item["serverId"] = $dbServer->serverId;
                     $item["mountStatus"] = false;
                     if (isset($this->listCache["farms"][$item["farmId"]])) {
                         $item["farmName"] = $this->listCache["farms"][$item["farmId"]];
                     } else {
                         $item["farmName"] = $dbServer->GetFarmObject()->Name;
                         $this->listCache["farms"][$item["farmId"]] = $item["farmName"];
                     }
                     if (isset($this->listCache["farmRoles"][$item["farmRoleId"]])) {
                         $item["roleName"] = $this->listCache["farmRoles"][$item["farmRoleId"]];
                     } else {
                         $item["roleName"] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
                         $this->listCache["farmRoles"][$item["farmRoleId"]] = $item["roleName"];
                     }
                     /* Waiting for bugfix on scalarizr side
                        if ($dbServer->IsSupported("2.5.4")) {
                            $item["mounts"] = $dbServer->scalarizr->system->mounts();
                        }
                        */
                 }
             } catch (\Exception $e) {
             }
         }
         if ($needFilter === true) {
             foreach (["farmId", "farmRoleId"] as $var) {
                 if (!empty(${$var}) && (!isset($item[$var]) || $item[$var] != ${$var})) {
                     continue 2;
                 }
             }
         }
         $vols[] = $item;
     }
     $volumesTime = round(microtime(true) - $startTime) * 1000;
     $startTime = microtime(true);
     $response = $this->buildResponseFromData($vols, $filterFields);
     foreach ($response["data"] as &$item) {
         $item["autoSnaps"] = (bool) $this->db->GetOne("SELECT id FROM autosnap_settings WHERE objectid=? AND object_type=? LIMIT 1", [$item["volumeId"], \AUTOSNAPSHOT_TYPE::EBSSnap]);
     }
     $responseTime = round(microtime(true) - $startTime) * 1000;
     $response["performanceMeasurements"] = ["describe" => $describeTime, "volumes" => $volumesTime, "response" => $responseTime];
     $this->response->data($response);
 }
Ejemplo n.º 19
0
 public function xListVolumesAction()
 {
     $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'volumeId', 'direction' => 'DESC')), 'volumeId'));
     $amazonEC2Client = Scalr_Service_Cloud_Aws::newEc2($this->getParam('cloudLocation'), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::PRIVATE_KEY), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::CERTIFICATE));
     // Rows
     $aws_response = $amazonEC2Client->DescribeVolumes();
     $rowz = $aws_response->volumeSet->item;
     if ($rowz instanceof stdClass) {
         $rowz = array($rowz);
     }
     $vols = array();
     foreach ($rowz as $pk => $pv) {
         if ($pv->attachmentSet && $pv->attachmentSet->item) {
             $pv->attachmentSet = $pv->attachmentSet->item;
         }
         if ($this->getParam('volumeId') && $this->getParam('volumeId') != $pv->volumeId) {
             continue;
         }
         $item = array('volumeId' => $pv->volumeId, 'size' => $pv->size, 'snapshotId' => $pv->snapshotId, 'availZone' => $pv->availabilityZone, 'status' => $pv->status, 'attachmentStatus' => $pv->attachmentSet->status, 'device' => $pv->attachmentSet->device, 'instanceId' => $pv->attachmentSet->instanceId);
         $item['autoSnaps'] = $this->db->GetOne("SELECT id FROM autosnap_settings WHERE objectid=? AND object_type=?", array($pv->volumeId, AUTOSNAPSHOT_TYPE::EBSSnap)) ? true : false;
         $DBEBSVolume = false;
         try {
             $DBEBSVolume = DBEBSVolume::loadByVolumeId($pv->volumeId);
             //$sort_key = "{$DBEBSVolume->farmId}_{$DBEBSVolume->farmRoleId}_{$pv->volumeId}";
             $item['farmId'] = $DBEBSVolume->farmId;
             $item['farmRoleId'] = $DBEBSVolume->farmRoleId;
             $item['serverIndex'] = $DBEBSVolume->serverIndex;
             $item['serverId'] = $DBEBSVolume->serverId;
             $item['mountStatus'] = $DBEBSVolume->mountStatus;
             $item['farmName'] = DBFarm::LoadByID($DBEBSVolume->farmId)->Name;
             $item['roleName'] = DBFarmRole::LoadByID($DBEBSVolume->farmRoleId)->GetRoleObject()->name;
             $item['autoAttach'] = true;
         } catch (Exception $e) {
         }
         if (!$DBEBSVolume && $item['instanceId']) {
             try {
                 $dbServer = DBServer::LoadByPropertyValue(EC2_SERVER_PROPERTIES::INSTANCE_ID, $item['instanceId']);
                 $item['farmId'] = $dbServer->farmId;
                 $item['farmRoleId'] = $dbServer->farmRoleId;
                 $item['serverIndex'] = $dbServer->index;
                 $item['serverId'] = $dbServer->serverId;
                 $item['farmName'] = $dbServer->GetFarmObject()->Name;
                 $item['mountStatus'] = false;
                 $item['roleName'] = $dbServer->GetFarmRoleObject()->GetRoleObject()->name;
             } catch (Exception $e) {
             }
         }
         $vols[] = $item;
     }
     $response = $this->buildResponseFromData($vols, array('instanceId', 'volumeId', 'snapshotId', 'farmId', 'farmRoleId', 'availZone'));
     $this->response->data($response);
 }