public function deleteAction() { $this->request->defineParams(array('sshKeyId' => array('type' => 'int'))); $sshKey = Scalr_SshKey::init()->loadById($this->getParam('sshKeyId')); $this->user->getPermissions()->validate($sshKey); if ($sshKey->type == Scalr_SshKey::TYPE_GLOBAL) { if ($sshKey->platform == SERVER_PLATFORMS::EC2) { $aws = $this->getEnvironment()->aws($sshKey->cloudLocation); $aws->ec2->keyPair->delete($sshKey->cloudKeyName); $sshKey->delete(); $this->response->success(); } elseif (PlatformFactory::isOpenstack($sshKey->platform)) { $os = $this->getEnvironment()->openstack($sshKey->platform, $sshKey->cloudLocation); try { $os->servers->keypairs->delete($sshKey->cloudKeyName); } catch (Exception $e) { } $sshKey->delete(); $this->response->success(); } else { $sshKey->delete(); } } else { //TODO: } $this->response->success("SSH key successfully removed"); }
protected function run1($stage) { $this->console->out("Initializing instance_type_name field in servers_history table"); $result = $this->db->Execute("\n SELECT sh.* FROM servers_history sh\n JOIN servers s USING(server_id)\n WHERE sh.instance_type_name IS NULL\n AND sh.type IS NOT NULL\n AND sh.cloud_location IS NOT NULL\n ORDER BY sh.env_id, sh.platform DESC\n "); $env = null; $platform = null; $this->db->BeginTrans(); try { $sql = "UPDATE servers_history sh SET sh.instance_type_name = ? WHERE sh.server_id = ?"; while ($record = $result->FetchRow()) { if (!isset($env) || $env->id != $record['env_id']) { $env = Scalr_Environment::init()->loadById($record['env_id']); $platform = null; } if (in_array($record['platform'], [\SERVER_PLATFORMS::EC2, \SERVER_PLATFORMS::GCE])) { $this->db->Execute($sql, [$record['type'], $record['server_id']]); continue; } if (!isset($platform) || $platform != $record['platform']) { $platform = $record['platform']; $platformModule = PlatformFactory::NewPlatform($record['platform']); $url = $platformModule->getEndpointUrl($env); } $cloudLocationId = CloudLocation::calculateCloudLocationId($record['platform'], $record['cloud_location'], $url); $instanceTypeEntity = CloudInstanceType::findPk($cloudLocationId, $record['type']); /* @var $instanceTypeEntity CloudInstanceType */ if ($instanceTypeEntity) { $this->db->Execute($sql, [$instanceTypeEntity->name, $record['server_id']]); } } $this->db->CommitTrans(); } catch (Exception $e) { $this->db->RollbackTrans(); } }
/** * Gets a new Instance of the adapter * * @param string|CloudCredentials|object $name The name of the adapter, or CloudCredentials entity, or cloud credentials data * * @return ApiEntityAdapter Returns the instance of cloud credentials adapter * * @throws ApiErrorException */ public function adapter($name, array $transform = null) { if (is_object($name)) { // $property = $name instanceof $this->entityClass ? static::$entityDescriminator : static::$objectDiscriminator; $value = empty($transform) ? $name->{$property} : $transform[$name->{$property}]; switch (true) { case PlatformFactory::isOpenstack($value, true): $value = SERVER_PLATFORMS::OPENSTACK; break; case PlatformFactory::isCloudstack($value): $value = SERVER_PLATFORMS::CLOUDSTACK; break; case PlatformFactory::isRackspace($value): $value = SERVER_PLATFORMS::RACKSPACE; break; } if (!isset(static::$inheritanceMap[$value])) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unknown cloud '{$value}'"); } $class = empty(static::$inheritanceMap) ? $value : static::$inheritanceMap[$value]; $name = empty(static::$inheritedNamespace) ? $class : static::$inheritedNamespace . "\\{$class}"; } return parent::adapter($name); }
public function xGrowStorageAction() { $dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId')); $this->user->getPermissions()->validate($dbFarmRole->GetFarmObject()); $behavior = Scalr_Role_Behavior::loadByName($dbFarmRole->GetRoleObject()->getDbMsrBehavior()); $master = $behavior->getMasterServer($dbFarmRole); if ($master) { try { $volume = Scalr_Storage_Volume::init()->loadById($dbFarmRole->GetSetting(Scalr_Db_Msr::VOLUME_ID)); if ($volume->type != MYSQL_STORAGE_ENGINE::EBS && $volume->type != MYSQL_STORAGE_ENGINE::RAID_EBS && $volume->type != 'raid') { throw new Exception("Grow feature available only for EBS and RAID storage types"); } if ($volume->size >= (int) $this->getParam('newSize')) { throw new Exception("New size should be greather than current one ({$volume->size} GB)"); } $volumeConfig = $volume->getConfig(); $platformAccessData = PlatformFactory::NewPlatform($dbFarmRole->Platform)->GetPlatformAccessData($this->environment, $master); $newConfig = new stdClass(); $newConfig->size = $this->getParam('newSize'); $result = $master->scalarizr->mysql->growStorage($volumeConfig, $newConfig, $platformAccessData); // Do not remove. We need to wait a bit before operation will be registered in scalr. sleep(2); $this->response->data(array('operationId' => $result)); } catch (Exception $e) { throw new Exception("Cannot grow storage: {$e->getMessage()}"); } } else { throw new Exception("Impossible to increase storage size. No running master server."); } }
protected function run2($stage) { $this->console->out("Populating new properties"); $platforms = $envs = []; foreach (array_keys(\SERVER_PLATFORMS::GetList()) as $platform) { $platforms[$platform] = PlatformFactory::NewPlatform($platform); } $result = $this->db->Execute("\n SELECT s.server_id, s.`platform`, s.`cloud_location`, s.env_id, s.`type`\n FROM servers AS s\n WHERE s.`status` NOT IN (?, ?) AND s.`type` IS NOT NULL\n ", [Server::STATUS_PENDING_TERMINATE, Server::STATUS_TERMINATED]); while ($row = $result->FetchRow()) { if (!empty($row["type"])) { if (!array_key_exists($row["env_id"], $envs)) { $envs[$row["env_id"]] = \Scalr_Environment::init()->loadById($row["env_id"]); } if ($this->isPlatformEnabled($envs[$row["env_id"]], $row["platform"])) { try { $instanceTypeEntity = $platforms[$row["platform"]]->getInstanceType($row["type"], $envs[$row["env_id"]], $row["cloud_location"]); /* @var $instanceTypeEntity CloudInstanceType */ if ($instanceTypeEntity && (int) $instanceTypeEntity->vcpus > 0) { $this->db->Execute("\n INSERT IGNORE INTO server_properties (`server_id`, `name`, `value`) VALUES (?, ?, ?)\n ", [$row["server_id"], Server::INFO_INSTANCE_VCPUS, $instanceTypeEntity->vcpus]); } } catch (\Exception $e) { $this->console->warning("Can't get access to %s, error: %s", $row["platform"], $e->getMessage()); } } } } }
/** * Constructor * * @param string $platform Platform * @param DBFarmRole $DBFarmRole optional Farm Role object * @param int $index optional Server index within the Farm Role scope * @param string $role_id optional Identifier of the Role */ public function __construct($platform, DBFarmRole $DBFarmRole = null, $index = null, $role_id = null) { $this->platform = $platform; $this->dbFarmRole = $DBFarmRole; $this->index = $index; $this->roleId = $role_id === null ? $this->dbFarmRole->RoleID : $role_id; if ($DBFarmRole) { $this->envId = $DBFarmRole->GetFarmObject()->EnvID; } //Refletcion $Reflect = new ReflectionClass(DBServer::$platformPropsClasses[$this->platform]); foreach ($Reflect->getConstants() as $k => $v) { $this->platformProps[] = $v; } if ($DBFarmRole) { if (PlatformFactory::isOpenstack($this->platform)) { $this->SetProperties(array(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $DBFarmRole->CloudLocation)); } elseif (PlatformFactory::isCloudstack($this->platform)) { $this->SetProperties(array(CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $DBFarmRole->CloudLocation)); } else { switch ($this->platform) { case SERVER_PLATFORMS::GCE: $this->SetProperties(array(GCE_SERVER_PROPERTIES::CLOUD_LOCATION => $DBFarmRole->CloudLocation)); break; case SERVER_PLATFORMS::EC2: $this->SetProperties(array(EC2_SERVER_PROPERTIES::REGION => $DBFarmRole->CloudLocation)); break; } } } $this->SetProperties(array(SERVER_PROPERTIES::SZR_VESION => '0.20.0')); }
protected function run2($stage) { $farms = $this->db->Execute("SELECT farmid, value FROM farm_settings WHERE name='ec2.vpc.id' AND value != '' AND value IS NOT NULL"); while ($farm = $farms->FetchRow()) { $dbFarm = \DBFarm::LoadByID($farm['farmid']); $roles = $dbFarm->GetFarmRoles(); foreach ($roles as $dbFarmRole) { $vpcSubnetId = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::AWS_VPC_SUBNET_ID); if ($vpcSubnetId && substr($vpcSubnetId, 0, 6) != 'subnet') { $subnets = json_decode($vpcSubnetId); $vpcSubnetId = $subnets[0]; } if ($vpcSubnetId) { try { $platform = PlatformFactory::NewPlatform(\SERVER_PLATFORMS::EC2); $info = $platform->listSubnets(\Scalr_Environment::init()->loadById($dbFarm->EnvID), $dbFarmRole->CloudLocation, $farm['value'], true, $vpcSubnetId); if ($info && $info['type'] != 'public') { $routerRole = $dbFarm->GetFarmRoleByBehavior(\ROLE_BEHAVIORS::VPC_ROUTER); $dbFarmRole->SetSetting(\Scalr_Role_Behavior_Router::ROLE_VPC_SCALR_ROUTER_ID, $routerRole->ID); $this->console->out("Updating router.scalr.farm_role_id property for Farm Role: %s", $dbFarmRole->ID); } } catch (\Exception $e) { continue; } } } } }
protected function run1($stage) { $envs = []; $platform = SERVER_PLATFORMS::GCE; $platformModule = PlatformFactory::NewPlatform($platform); /* @var $platformModule GoogleCEPlatformModule*/ $result = $this->db->Execute("\n SELECT s.`server_id`, s.`cloud_location`, s.`type`, s.`env_id`, sp.`value` AS vcpus\n FROM servers AS s\n LEFT JOIN server_properties sp ON sp.`server_id`= s.`server_id` AND sp.`name` = ?\n WHERE s.`status` NOT IN (?, ?)\n AND s.`type` IS NOT NULL\n AND s.`platform` = ?\n ", [Server::INFO_INSTANCE_VCPUS, Server::STATUS_PENDING_TERMINATE, Server::STATUS_TERMINATED, $platform]); while ($row = $result->FetchRow()) { if (!empty($row["type"]) && empty($row['vcpus'])) { if (!array_key_exists($row["env_id"], $envs)) { $envs[$row["env_id"]] = \Scalr_Environment::init()->loadById($row["env_id"]); } try { $instanceTypeInfo = $platformModule->getInstanceType($row["type"], $envs[$row["env_id"]], $row["cloud_location"]); if ($instanceTypeInfo instanceof CloudInstanceType) { $vcpus = $instanceTypeInfo->vcpus; } else { trigger_error("Value of vcpus for instance type " . $row["type"] . " is missing for platform " . $platform, E_USER_WARNING); $vcpus = 0; } if ((int) $vcpus > 0) { $this->db->Execute("\n INSERT INTO server_properties (`server_id`, `name`, `value`) VALUES (?, ?, ?)\n ON DUPLICATE KEY UPDATE `value` = ?\n ", [$row["server_id"], Server::INFO_INSTANCE_VCPUS, $vcpus, $vcpus]); } } catch (\Exception $e) { $this->console->warning("Can't get access to %s, error: %s", $platform, $e->getMessage()); } } } }
/** * Performs upgrade literally for the stage ONE. * * Implementation of this method performs update steps needs to be taken * to accomplish upgrade successfully. * * If there are any error during an execution of this scenario it must * throw an exception. * * @param int $stage optional The stage number * @throws \Exception */ protected function run1($stage) { $environments = $this->db->Execute("SELECT id FROM client_environments WHERE status='Active'"); while ($env = $environments->FetchRow()) { $environment = \Scalr_Environment::init()->loadById($env['id']); foreach (PlatformFactory::getOpenstackBasedPlatforms() as $platform) { if ($platform == \SERVER_PLATFORMS::RACKSPACENG_UK || $platform == \SERVER_PLATFORMS::RACKSPACENG_US) { continue; } try { if ($environment->isPlatformEnabled($platform)) { $os = $environment->openstack($platform); //It throws an exception on failure $zones = $os->listZones(); $zone = array_shift($zones); $os = $environment->openstack($platform, $zone->name); // Check SG Extension $pars[$this->getOpenStackOption($platform, 'EXT_SECURITYGROUPS_ENABLED')] = (int) $os->servers->isExtensionSupported(ServersExtension::securityGroups()); // Check Floating Ips Extension $pars[$this->getOpenStackOption($platform, 'EXT_FLOATING_IPS_ENABLED')] = (int) $os->servers->isExtensionSupported(ServersExtension::floatingIps()); // Check Cinder Extension $pars[$this->getOpenStackOption($platform, 'EXT_CINDER_ENABLED')] = (int) $os->hasService('volume'); // Check Swift Extension $pars[$this->getOpenStackOption($platform, 'EXT_SWIFT_ENABLED')] = (int) $os->hasService('object-store'); // Check LBaas Extension $pars[$this->getOpenStackOption($platform, 'EXT_LBAAS_ENABLED')] = $os->hasService('network') ? (int) $os->network->isExtensionSupported('lbaas') : 0; $environment->setPlatformConfig($pars); } } catch (\Exception $e) { $this->console->out("Update settings for env: {$env['id']} failed: " . $e->getMessage()); } } } }
public static function getAwsLocations() { $locations = array(); foreach (PlatformFactory::NewPlatform(SERVER_PLATFORMS::EC2)->getLocations($this->environment) as $key => $loc) { $locations[] = array($key, $loc); } return $locations; }
public function viewAction() { if ($this->getParam('platform')) { $locations = self::loadController('Platforms')->getCloudLocations(array($this->getParam('platform')), false); } else { $locations = self::loadController('Platforms')->getCloudLocations(PlatformFactory::getOpenstackBasedPlatforms(), false); } $this->response->page('ui/tools/openstack/volumes/view.js', array('locations' => $locations)); }
/** * Gets AWS instance * * If cloud location is not specified it will use default cloud location for * current User's session * * @param string $cloudLocation optional A Cloud Location * @return Aws Returns AWS initialized for the specified cloud location */ protected function getAws($cloudLocation = null) { if (empty($cloudLocation)) { $p = PlatformFactory::NewPlatform(SERVER_PLATFORMS::EC2); $cloudLocations = $p->getLocations($this->environment); list($cloudLocation) = each($cloudLocations); } return $this->environment->aws($cloudLocation); }
/** * @param jsonData $platforms * @throws Exception */ public function xGetLocationsAction(JsonData $platforms) { $allPlatforms = $this->user->isScalrAdmin() ? array_keys(SERVER_PLATFORMS::GetList()) : $this->getEnvironment()->getEnabledPlatforms(); $result = array(); foreach ($platforms as $platform) { if (in_array($platform, $allPlatforms)) { $result[$platform] = !in_array($platform, array(SERVER_PLATFORMS::GCE, SERVER_PLATFORMS::ECS)) ? PlatformFactory::NewPlatform($platform)->getLocations($this->environment) : array(); } } $this->response->data(array('locations' => $result)); }
public function xGetResourceGroupsAction() { $data = array(); $azure = $this->environment->azure(); //Get Resource groups; $rGroups = $azure->resourceManager->resourceGroup->getList($this->environment->keychain(SERVER_PLATFORMS::AZURE)->properties[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID]); foreach ($rGroups as $rGroup) { $data[] = ['id' => $rGroup->name, 'name' => "{$rGroup->name} ({$rGroup->location})"]; } $p = PlatformFactory::NewPlatform(\SERVER_PLATFORMS::AZURE); $this->response->data(array('data' => ['resourceGroups' => $data, 'cloudLocations' => $p->getLocations($this->environment)])); }
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); }
public function xSaveAction() { $this->request->defineParams(array('members' => array('type' => 'array'))); $platformObject = PlatformFactory::NewPlatform($this->platform); $request = array('pool_id' => $this->getParam('pool_id'), 'weight' => $this->getParam('weight'), 'protocol_port' => $this->getParam('protocol_port'), 'admin_state_up' => $this->getParam('admin_state_up')); foreach ($this->getParam('members') as $memberId) { $details = $this->getClient()->servers->getServerDetails($memberId); $ips = $platformObject->determineServerIps($this->getClient(), $details); $request['address'] = $ips['localIp']; $this->getClient()->network->lbMembers->create($request); } $this->response->success('Member(s) successfully created'); }
protected function run2($stage) { $envIds = $this->db->GetCol('SELECT DISTINCT env_id FROM `comments` WHERE env_id > 0'); $this->console->out("Environments to process: " . count($envIds)); foreach ($envIds as $index => $envId) { if ($this->db->GetOne('SELECT 1 FROM `security_group_rules_comments` WHERE env_id = ? LIMIT 1', array($envId))) { $this->console->out("Skip environment #{$index}(" . $envId . ")"); continue; } try { $env = \Scalr_Environment::init()->loadById($envId); } catch (\Exception $e) { continue; } $locations = PlatformFactory::NewPlatform(\SERVER_PLATFORMS::EC2)->getLocations($env); $container = \Scalr::getContainer(); $container->environment = $env; foreach ($locations as $location => $locatonName) { try { $sgList = $env->aws($location)->ec2->securityGroup->describe(); } catch (\Exception $e) { continue 2; } /* @var $sg SecurityGroupData */ foreach ($sgList as $sg) { $rules = array(); foreach ($sg->ipPermissions as $rule) { /* @var $ipRange IpRangeData */ foreach ($rule->ipRanges as $ipRange) { $rules[] = "{$rule->ipProtocol}:{$rule->fromPort}:{$rule->toPort}:{$ipRange->cidrIp}"; } /* @var $group UserIdGroupPairData */ foreach ($rule->groups as $group) { $ruleSg = $group->userId . '/' . ($group->groupName ? $group->groupName : $group->groupId); $rules[] = "{$rule->ipProtocol}:{$rule->fromPort}:{$rule->toPort}:{$ruleSg}"; } } foreach ($rules as $rule) { $comment = $this->db->GetOne('SELECT comment FROM `comments` WHERE env_id = ? AND sg_name = ? AND rule = ? LIMIT 1', array($envId, $sg->groupName, $rule)); if ($comment) { try { $this->db->Execute("\n INSERT IGNORE `security_group_rules_comments` (`env_id`, `platform`, `cloud_location`, `vpc_id`, `group_name`, `rule`, `comment`)\n VALUES (?, ?, ?, ?, ?, ?, ?)\n ", array($env->id, \SERVER_PLATFORMS::EC2, $location, $sg->vpcId, $sg->groupName, $rule, $comment)); } catch (\Exception $e) { } } } } } $this->console->out("Environment processed: #{$index}(" . $envId . ")"); } }
public function xGetMigrateDetailsAction() { if (!$this->request->getEnvironment()->isPlatformEnabled(SERVER_PLATFORMS::EC2)) { throw new Exception('You can migrate image between regions only on EC2 cloud'); } $platform = PlatformFactory::NewPlatform(SERVER_PLATFORMS::EC2); $locationsList = $platform->getLocations($this->environment); foreach ($locationsList as $location => $name) { if ($location != $this->getParam('cloudLocation')) { $availableDestinations[] = array('cloudLocation' => $location, 'name' => $name); } } $this->response->data(array('sourceRegion' => $this->getParam('cloudLocation'), 'availableDestinations' => $availableDestinations, 'snapshotId' => $this->getParam('snapshotId'))); }
/** * @test * @dataProvider providerBlockDeviceByType * * @param string $instanceType * @param array $expectedBlockDeviceConfiguration */ public function blockDevicesTest($instanceType, array $expectedBlockDeviceConfiguration) { /* @var $pm Ec2PlatformModule */ $pm = PlatformFactory::NewPlatform('ec2'); $reflection = new ReflectionClass(get_class($pm)); $method = $reflection->getMethod('GetBlockDeviceMapping'); $method->setAccessible(true); /* @var $mapping BlockDeviceMappingData[] */ $mapping = $method->invoke($pm, $instanceType, ''); $this->assertEquals(count($expectedBlockDeviceConfiguration), count($mapping), "Wrong count"); foreach ($mapping as $num => $blockDevice) { $this->assertTrue(isset($expectedBlockDeviceConfiguration[$num]), "Invalid device position"); $this->assertEquals($expectedBlockDeviceConfiguration[$num], $blockDevice->deviceName, "Device name mismatch"); } }
protected function run1($stage) { $platform = SERVER_PLATFORMS::EC2; $platformModule = PlatformFactory::NewPlatform($platform); /* @var $platformModule Ec2PlatformModule*/ $instanceTypes = $platformModule->getInstanceTypes(null, null, true); $result = $this->db->Execute("\n SELECT s.server_id, s.`type`, sp.`value` AS vcpus\n FROM servers AS s\n LEFT JOIN server_properties sp ON sp.`server_id`= s.`server_id` AND sp.`name` = ?\n WHERE s.`status` NOT IN (?, ?)\n AND s.`type` IS NOT NULL\n AND s.`platform` = ?\n ", [Server::INFO_INSTANCE_VCPUS, Server::STATUS_PENDING_TERMINATE, Server::STATUS_TERMINATED, $platform]); while ($row = $result->FetchRow()) { if (!empty($row["type"]) && empty($row['vcpus'])) { if (isset($instanceTypes[$row['type']]['vcpus']) && $instanceTypes[$row['type']]['vcpus'] > 0) { $this->db->Execute("\n INSERT INTO server_properties (`server_id`, `name`, `value`) VALUES (?, ?, ?)\n ON DUPLICATE KEY UPDATE `value` = ?\n ", [$row["server_id"], Server::INFO_INSTANCE_VCPUS, $instanceTypes[$row['type']]['vcpus'], $instanceTypes[$row['type']]['vcpus']]); } } } }
public function _cloudCredentialsType($from, $to, $action) { switch ($action) { case static::ACT_CONVERT_TO_OBJECT: /* @var $from Entity\CloudCredentials */ $to->cloudCredentialsType = static::CLOUD_CREDENTIALS_TYPE_OPENSTACK; break; case static::ACT_CONVERT_TO_ENTITY: /* @var $to Entity\CloudCredentials */ $to->cloud = $this->getOpenstackProvider($from); break; case static::ACT_GET_FILTER_CRITERIA: return [['cloud' => ['$in' => PlatformFactory::getCanonicalOpenstackPlatforms()]]]; } }
/** * @param string $cloudLocation * @param string $diskId optional */ public function xListAction($cloudLocation, $diskId = '') { $platform = PlatformFactory::NewPlatform(SERVER_PLATFORMS::GCE); $client = $platform->getClient($this->environment, $cloudLocation); /* @var $client Google_Service_Compute */ $retval = array(); $disks = $client->disks->listDisks($this->environment->cloudCredentials(SERVER_PLATFORMS::GCE)->properties[Entity\CloudCredentialsProperty::GCE_PROJECT_ID], $cloudLocation, $diskId ? ['filter' => "name eq {$diskId}"] : []); foreach ($disks as $disk) { /* @var $disk Google_Service_Compute_Disk */ $item = array('id' => $disk->name, 'description' => $disk->description, 'createdAt' => strtotime($disk->creationTimestamp), 'size' => (int) $disk->sizeGb, 'status' => $disk->status, 'snapshotId' => $disk->sourceSnapshotId); $retval[] = $item; } $response = $this->buildResponseFromData($retval, array('id', 'name', 'description', 'snapshotId', 'createdAt', 'size')); foreach ($response['data'] as &$row) { $row['createdAt'] = Scalr_Util_DateTime::convertTz($row['createdAt']); } $this->response->data($response); }
public function _cloudCredentialsType($from, $to, $action) { switch ($action) { case static::ACT_CONVERT_TO_OBJECT: /* @cloudCredentialsType $from Entity\CloudCredentials */ $to->cloudCredentialsType = static::CLOUD_CREDENTIALS_TYPE_RACKSPACE; break; case static::ACT_CONVERT_TO_ENTITY: /* @var $to Entity\CloudCredentials */ if (!isset($from->isUk)) { $to->cloud = SERVER_PLATFORMS::RACKSPACENG_US; $from->keystoneUrl = static::RACKSPACE_KEYSTONE_URL_US; $this->_keystoneUrl($from, $to, static::ACT_CONVERT_TO_ENTITY); } break; case static::ACT_GET_FILTER_CRITERIA: return [['cloud' => ['$in' => PlatformFactory::getRackspacePlatforms()]]]; } }
public static function onCreateBackupResult(Scalr_Messaging_Msg $message, DBServer $dbServer) { $dbFarmRole = $dbServer->GetFarmRoleObject(); $dbFarmRole->SetSetting(Scalr_Db_Msr::DATA_BACKUP_LAST_TS, time(), DBFarmRole::TYPE_LCL); $dbFarmRole->SetSetting(Scalr_Db_Msr::DATA_BACKUP_IS_RUNNING, 0, DBFarmRole::TYPE_LCL); //$dbFarmRole->SetSetting(Scalr_Db_Msr::DATA_BACKUP_SERVER_ID, ""); if (PlatformFactory::isOpenstack($dbServer->platform)) { $provider = 'cf'; } else { switch ($dbServer->platform) { case SERVER_PLATFORMS::EC2: $provider = 's3'; break; case SERVER_PLATFORMS::GCE: $provider = 'gcs'; break; default: $provider = 'unknown'; break; } } $backup = Scalr_Db_Backup::init(); $backup->service = $message->dbType; $backup->platform = $dbServer->platform; $backup->provider = $provider; $backup->envId = $dbServer->envId; $backup->farmId = $dbServer->farmId; $backup->cloudLocation = $dbServer->GetCloudLocation(); $backup->status = Scalr_Db_Backup::STATUS_AVAILABLE; $total = 0; if (isset($message->backupParts) && is_array($message->backupParts)) { foreach ($message->backupParts as $item) { if (is_object($item) && $item->size) { $backup->addPart(str_replace(array("s3://", "cf://", "gcs://", "swift://"), array("", "", "", ""), $item->path), $item->size); $total = $total + (int) $item->size; } else { $backup->addPart(str_replace(array("s3://", "cf://", "gcs://", "swift://"), array("", "", "", ""), $item), 0); } } } $backup->size = $total; $backup->save(); }
public function xRemoveAction() { $this->request->defineParams(array('ipId' => array('type' => 'json'), 'cloudLocation')); $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')); foreach ($this->getParam('ipId') as $ipId) { if ($networkType == OpenstackPlatformModule::NETWORK_TYPE_QUANTUM) { $openstack->network->floatingIps->delete($ipId); } else { $openstack->servers->floatingIps->delete($ipId); } } $this->response->success('Floating IP(s) successfully removed'); }
public function xListSnapshotsAction() { $this->request->defineParams(array('sort' => array('type' => 'json', 'default' => array('property' => 'id', 'direction' => 'ASC')), 'snapshotId')); $platform = PlatformFactory::NewPlatform(SERVER_PLATFORMS::GCE); $client = $platform->getClient($this->environment); /* @var $client Google_Service_Compute */ $retval = array(); $snaps = $client->snapshots->listSnapshots($this->environment->getPlatformConfigValue(GoogleCEPlatformModule::PROJECT_ID)); foreach ($snaps as $snap) { /* @var $snap Google_Service_Compute_Snapshot */ if ($this->getParam('snapshotId') && $this->getParam('snapshotId') != $snap->name) { continue; } $item = array('id' => $snap->name, 'description' => $snap->description, 'createdAt' => Scalr_Util_DateTime::convertTz(strtotime($snap->creationTimestamp)), 'size' => $snap->diskSizeGb, 'status' => $snap->status); $retval[] = $item; } $response = $this->buildResponseFromData($retval, array('id', 'description')); $this->response->data($response); }
/** * List orphaned servers * * @param string $platform Cloud platform * @param string $cloudLocation optional Cloud location * @param string $query optional Filter parameter * @param string $imageId optional Filter parameter * @param string $vpcId optional Filter parameter * @param string $subnetId optional Filter paramerer */ public function xListAction($platform, $cloudLocation = null, $query = null, $imageId = null, $vpcId = null, $subnetId = null) { $lookup = []; $p = PlatformFactory::NewPlatform($platform); if (!$this->environment->isPlatformEnabled($platform)) { $this->response->failure(sprintf("Platform '%s' is not enabled", $platform)); return; } $filterFields = []; if ($query) { $filterFields[join(',', ['cloudServerId', 'privateIp', 'publicIp'])] = $query; } if ($imageId) { $filterFields['imageId'] = $imageId; } if ($vpcId) { $filterFields['vpcId'] = $vpcId; } if ($subnetId) { $filterFields['subnetId'] = $subnetId; } $orphans = $this->buildResponseFromData($p->getOrphanedServers($this->getEnvironmentEntity(), $cloudLocation), $filterFields); foreach ($orphans["data"] as $i => &$orphan) { $orphan["launchTime"] = \Scalr_Util_DateTime::convertTz($orphan["launchTime"]); $orphan["imageHash"] = null; $orphan["imageName"] = null; if (!is_array($lookup[$orphan["imageId"]])) { $lookup[$orphan["imageId"]] = []; } $lookup[$orphan["imageId"]][] = $orphan; } if (!empty($lookup)) { /* @var $image Scalr\Model\Entity\Image */ foreach (Image::find([["status" => Image::STATUS_ACTIVE], ["id" => ['$in' => array_keys($lookup)]], ['$or' => [['accountId' => null], ['$and' => [['accountId' => $this->getUser()->accountId], ['$or' => [['envId' => null], ['envId' => $this->getEnvironment()->id]]]]]]]]) as $image) { foreach ($lookup[$image->id] as &$orphan) { $orphan['imageHash'] = $image->hash; $orphan['imageName'] = $image->name; } } } $this->response->data($orphans); }
protected function run1($stage) { if (\Scalr::getContainer()->analytics->enabled) { $properties = EnvironmentProperty::find([['name' => Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_BUCKET]]); foreach ($properties as $property) { /* @var $property EnvironmentProperty */ $environment = \Scalr_Environment::init()->loadById($property->envId); $accountType = $environment->getPlatformConfigValue(Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE); if ($accountType == Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_REGULAR) { $region = Aws::REGION_US_EAST_1; } else { $platformModule = PlatformFactory::NewPlatform(\SERVER_PLATFORMS::EC2); /* @var $platformModule Ec2PlatformModule */ $locations = array_keys($platformModule->getLocationsByAccountType($accountType)); $region = reset($locations); } $environment->setPlatformConfig([Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_REGION => $region]); } } }
/** * Decode params from href depending on type of OS * * Possible formats (branch is optioanl): * for windows: /{repo}/[{branch}/]install_scalarizr.ps1 * for linux: /{repo}/{platform}/[{branch}]/install_scalarizr.sh * * @param array $params * @param bool $isLinux * @return array */ protected function parseInstallScriptArgs($params, $isLinux = true) { $result = ['repo' => '', 'platform' => '', 'repoUrls' => []]; $repo = array_shift($params); $platform = $isLinux ? array_shift($params) : ''; array_pop($params); // pop script name from the end of href $branch = implode('/', $params); $repos = $this->getContainer()->config('scalr.scalarizr_update.' . ($branch ? 'devel_repos' : 'repos')); if (in_array($repo, array_keys($repos))) { if ($branch) { // strip illegal chars $branch = preg_replace('/[^A-Za-z\\/0-9_.-]/', '', $branch); $branch = str_replace(array(".", '/'), array('', '-'), $branch); } $repoUrls = $repos[$repo]; if ($branch) { foreach ($repoUrls as $key => &$url) { $url = sprintf($url, $branch); } } if ($isLinux) { if (in_array($platform, $this->getContainer()->config('scalr.allowed_clouds'))) { if (PlatformFactory::isOpenstack($platform)) { $platform = SERVER_PLATFORMS::OPENSTACK; } else { if (PlatformFactory::isCloudstack($platform)) { $platform = SERVER_PLATFORMS::CLOUDSTACK; } } $result['platform'] = $platform; $result['repo'] = $repo; $result['repoUrls'] = $repoUrls; } } else { $result['repo'] = $repo; $result['repoUrls'] = $repoUrls; } } return $result; }
/** * {@inheritdoc} * @see AbstractServerImport::importServer */ protected function importServer() { $aws = $this->farmRole->getFarm()->getEnvironment()->aws($this->farmRole->cloudLocation); try { $instance = $this->instance = $aws->ec2->instance->describe($this->orphaned->cloudServerId)->get(0)->instancesSet->get(0); $this->server->properties[EC2_SERVER_PROPERTIES::AVAIL_ZONE] = $instance->placement->availabilityZone; $this->server->properties[EC2_SERVER_PROPERTIES::ARCHITECTURE] = $instance->architecture; $this->server->cloudLocationZone = $instance->placement->availabilityZone; $this->server->setOs($instance->platform ? $instance->platform : 'linux'); $this->server->properties[EC2_SERVER_PROPERTIES::INSTANCE_ID] = $this->orphaned->cloudServerId; $this->server->properties[EC2_SERVER_PROPERTIES::VPC_ID] = $this->orphaned->vpcId; $this->server->properties[EC2_SERVER_PROPERTIES::SUBNET_ID] = $this->orphaned->subnetId; $this->server->properties[EC2_SERVER_PROPERTIES::REGION] = $this->server->cloudLocation; $this->server->properties[EC2_SERVER_PROPERTIES::AMIID] = $this->server->imageId; $this->server->instanceTypeName = $this->server->type; $p = PlatformFactory::NewPlatform(SERVER_PLATFORMS::EC2); $instanceTypeInfo = $p->getInstanceType($this->orphaned->instanceType, (new \Scalr_Environment())->loadById($this->server->envId), $this->server->cloudLocation); $this->server->properties[Entity\Server::INFO_INSTANCE_VCPUS] = isset($instanceTypeInfo['vcpus']) ? $instanceTypeInfo['vcpus'] : null; } catch (Exception $e) { throw new ServerImportException(sprintf('Scalr was unable to retrieve details for instance %s: %s', $this->orphaned->cloudServerId, $e->getMessage()), $e->getCode(), $e); } }