public function GetServersList(Scalr_Environment $environment, $region, $skipCache = false) { if (!$region) { return array(); } if (!$this->instancesListCache[$environment->id][$region] || $skipCache) { $EC2Client = Scalr_Service_Cloud_Aws::newEc2($region, $environment->getPlatformConfigValue(self::PRIVATE_KEY), $environment->getPlatformConfigValue(self::CERTIFICATE)); try { $results = $EC2Client->DescribeInstances(); $results = $results->reservationSet; } catch (Exception $e) { throw new Exception(sprintf("Cannot get list of servers for platfrom ec2: %s", $e->getMessage())); } if ($results->item) { if ($results->item->reservationId) { $this->instancesListCache[$environment->id][$region][(string) $results->item->instancesSet->item->instanceId] = (string) $results->item->instancesSet->item->instanceState->name; } else { foreach ($results->item as $item) { $this->instancesListCache[$environment->id][$region][(string) $item->instancesSet->item->instanceId] = (string) $item->instancesSet->item->instanceState->name; } } } } return $this->instancesListCache[$environment->id][$region]; }
/** * Gets the list of available locations * * @param \Scalr_Environment $environment * @return \Scalr_Environment Returns the list of available locations looks like array(location => description) */ public function getLocations(\Scalr_Environment $environment = null) { $accountType = null; if ($environment instanceof \Scalr_Environment) { $accountType = $environment->keychain(SERVER_PLATFORMS::EC2)->properties[Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE]; } return $this->getLocationsByAccountType($accountType); }
/** * Sets the values for the specified platform properties * * @deprecated by cloud credentials * @param array $pars Associative array of the keys -> value * @param \Scalr_Environment $env The environment object * @param string $encrypted optional This parameter is already ignored * @param string $cloudLocation The cloud location */ public function setConfigVariable($pars, \Scalr_Environment $env, $encrypted = true, $cloudLocation = '') { $config = array(); foreach ($pars as $key => $v) { $index = $this->platform ? "{$this->platform}.{$key}" : $key; $config[$index] = $v; } $env->setPlatformConfig($config, $encrypted, $cloudLocation); }
/** * Gets the list of available locations * * @return array Returns the list of available locations looks like array(location => description) */ public function getLocations(\Scalr_Environment $environment = null) { if ($environment instanceof \Scalr_Environment && $this instanceof Ec2PlatformModule) { if ($environment->getPlatformConfigValue(Ec2PlatformModule::ACCOUNT_TYPE) == Ec2PlatformModule::ACCOUNT_TYPE_GOV_CLOUD) { return array(Aws::REGION_US_GOV_WEST_1 => 'AWS / us-gov-west-1 (GovCloud US)'); } } return array(Aws::REGION_US_EAST_1 => 'AWS / us-east-1 (N. Virginia)', Aws::REGION_US_WEST_1 => 'AWS / us-west-1 (N. California)', Aws::REGION_US_WEST_2 => 'AWS / us-west-2 (Oregon)', Aws::REGION_EU_WEST_1 => 'AWS / eu-west-1 (Ireland)', Aws::REGION_SA_EAST_1 => 'AWS / sa-east-1 (Sao Paulo)', Aws::REGION_AP_SOUTHEAST_1 => 'AWS / ap-southeast-1 (Singapore)', Aws::REGION_AP_SOUTHEAST_2 => 'AWS / ap-southeast-2 (Sydney)', Aws::REGION_AP_NORTHEAST_1 => 'AWS / ap-northeast-1 (Tokyo)'); }
/** * {@inheritdoc} * @see PHPUnit_Framework_TestCase::setUp() */ protected function setUp() { parent::setUp(); $this->container = \Scalr::getContainer(); $this->environment = new \Scalr_Environment(); if (!$this->isSkipFunctionalTests()) { $this->environment->loadById(\Scalr::config('scalr.phpunit.envid')); $this->container->environment = $this->environment; } }
/** * {@inheritdoc} * @see \Scalr\Modules\PlatformModuleInterface::hasCloudPrices() */ public function hasCloudPrices(\Scalr_Environment $env) { if (!$this->container->analytics->enabled) { return false; } $url = $env->getPlatformConfigValue(static::API_URL); if (empty($url)) { return false; } return $this->container->analytics->prices->hasPriceForUrl(\SERVER_PLATFORMS::NIMBULA, $url) ?: $url; }
public function testEnvironment() { if (!$this->getUser()->isAccountOwner()) { $this->markTestSkipped('Specified user cannot create new environments.'); } $createdEnvId = 0; // remove previous test envs $env = new \Scalr_Environment(); $result = $env->loadByFilter(array('clientId' => $this->getEnvironment()->clientId, 'name' => self::getTestName(self::ENV_NAME))); if (count($result)) { foreach ($result as $e) { $obj = new \Scalr_Environment(); $obj->loadById($e['id']); $obj->delete(); } } // create new $content = $this->request('/environments/xCreate', array('name' => self::getTestName(self::ENV_NAME))); $this->assertTrue($content['success']); if ($content['env']) { $createdEnvId = $content['env']['id']; } // create failure $content = $this->request('/environments/xCreate', array('name' => '')); $this->assertFalse($content['success']); // get info about test env $content = $this->request('/environments/' . $createdEnvId . '/xGetInfo'); $this->assertTrue($content['success']); $this->assertArrayHasKey('environment', $content); $this->assertArrayHasKey('id', $content['environment']); $this->assertArrayHasKey('name', $content['environment']); $this->assertArrayHasKey('params', $content['environment']); $this->assertArrayHasKey('enabledPlatforms', $content['environment']); // get info about env failure $content = $this->request('/environments/' . '-3' . '/xGetInfo'); $this->assertFalse($content['success']); // check new env in list $content = $this->request('/environments/xListEnvironments'); $this->assertTrue($content['success']); $this->assertArrayHasKey('data', $content); $flag = false; foreach ($content['data'] as $value) { if ($value['name'] == self::getTestName(self::ENV_NAME)) { $flag = true; } } $this->assertTrue($flag, 'Created environment not found in list'); // test platforms save $this->envTestEc2($createdEnvId); $content = $this->request('/environments/xRemove', array('envId' => $createdEnvId)); $this->assertTrue($content['success']); }
public function viewAction() { $users = array(); foreach ($this->env->getTeams() as $teamId) { $team = Scalr_Account_Team::init()->loadById($teamId); foreach ($team->getUsers() as $user) { if (!isset($users[$user['id']])) { $users[$user['id']] = array('id' => $user['id'], 'name' => !empty($user['fullname']) ? $user['fullname'] : $user['email'], 'email' => $user['email'], 'teams' => array()); } $users[$user['id']]['teams'][] = array('id' => $team->id, 'name' => $team->name); } } $this->response->page('ui/account2/environments/accessmap.js', array('definitions' => Acl::getResources(true), 'users' => array_values($users), 'env' => array('id' => $this->env->id, 'name' => $this->env->name))); }
/** * {@inheritdoc} * @see \Scalr\Modules\PlatformModuleInterface::getInstanceTypes() */ public function getInstanceTypes(\Scalr_Environment $env = null, $cloudLocation = null, $details = false) { if (!$env instanceof \Scalr_Environment) { throw new \InvalidArgumentException(sprintf("Method %s requires environment to be specified.", __METHOD__)); } $ret = array(); $cs = $env->cloudstack($this->platform); $products = $cs->listAvailableProductTypes(); if (count($products) > 0) { foreach ($products as $product) { $ret[(string) $product->serviceofferingid] = (string) $product->serviceofferingdesc; } } return $ret; }
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()); } } } }
/** * Gets a list of environments by key * * @param string $query Search query * @return array Returns array of environments */ private function getEnvironmentsList($query = null) { $envs = []; $environments = $this->user->getEnvironments($query); if (count($environments) > 0) { $iterator = ChartPeriodIterator::create('month', gmdate('Y-m-01'), null, 'UTC'); //It calculates usage for all provided enviroments $usage = $this->getContainer()->analytics->usage->getFarmData($this->user->getAccountId(), [], $iterator->getStart(), $iterator->getEnd(), [TagEntity::TAG_ID_ENVIRONMENT, TagEntity::TAG_ID_FARM]); //It calculates usage for previous period same days $prevusage = $this->getContainer()->analytics->usage->getFarmData($this->user->getAccountId(), [], $iterator->getPreviousStart(), $iterator->getPreviousEnd(), [TagEntity::TAG_ID_ENVIRONMENT, TagEntity::TAG_ID_FARM]); foreach ($environments as $env) { if (isset($usage['data'][$env['id']]['data'])) { $envs[$env['id']]['topSpender'] = $this->getFarmTopSpender($usage['data'][$env['id']]['data']); } else { $envs[$env['id']]['topSpender'] = null; } $envs[$env['id']]['name'] = $env['name']; $envs[$env['id']]['envId'] = $env['id']; $ccId = \Scalr_Environment::init()->loadById($env['id'])->getPlatformConfigValue(\Scalr_Environment::SETTING_CC_ID); if (!empty($ccId)) { $envs[$env['id']]['ccId'] = $ccId; $envs[$env['id']]['ccName'] = CostCentreEntity::findPk($ccId)->name; } $totalCost = round(isset($usage['data'][$env['id']]) ? $usage['data'][$env['id']]['cost'] : 0, 2); $prevCost = round(isset($prevusage['data'][$env['id']]) ? $prevusage['data'][$env['id']]['cost'] : 0, 2); $envs[$env['id']] = $this->getWrappedUsageData(['iterator' => $iterator, 'usage' => $totalCost, 'prevusage' => $prevCost]) + $envs[$env['id']]; } } return array_values($envs); }
/** * 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 function getAccountEnvironmentsList() { $environments = $this->user->getEnvironments(); $result = array(); foreach ($environments as &$row) { $env = Scalr_Environment::init()->loadById($row['id']); $row['platforms'] = $env->getEnabledPlatforms(); $row['teams'] = array(); if ($this->getContainer()->config->get('scalr.auth_mode') == 'ldap') { $row['teamIds'] = array(); } foreach ($env->getTeams() as $teamId) { if ($this->getContainer()->config->get('scalr.auth_mode') == 'ldap') { $team = new Scalr_Account_Team(); $team->loadById($teamId); $row['teams'][] = $team->name; $row['teamIds'][] = $teamId; } else { $row['teams'][] = $teamId; } } $row['dtAdded'] = Scalr_Util_DateTime::convertTz($env->dtAdded); $row['status'] = $env->status; if ($this->getContainer()->analytics->enabled) { $row['ccId'] = $env->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID); } $result[] =& $row; } return $result; }
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()); } } } } }
/** * Set test names for objects */ protected function setUp() { parent::setUp(); if ($this->isSkipFunctionalTests()) { $this->markTestSkipped(); } $testEnvId = \Scalr::config('scalr.phpunit.envid'); try { $this->testEnv = \Scalr_Environment::init()->loadById($testEnvId); } catch (Exception $e) { $this->markTestSkipped('Test Environment does not exist.'); } if (!$this->testEnv || !$this->testEnv->isPlatformEnabled(\SERVER_PLATFORMS::AZURE)) { $this->markTestSkipped('Azure platform is not enabled.'); } $this->azure = $this->testEnv->azure(); $this->subscriptionId = $this->azure->getEnvironment()->cloudCredentials(SERVER_PLATFORMS::AZURE)->properties[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID]; $this->resourceGroupName = 'test3-resource-group-' . $this->getInstallationId(); $this->availabilitySetName = 'test3-availability-set-' . $this->getInstallationId(); $this->vmName = 'test3-virtual-machine-' . $this->getInstallationId(); $this->vnName = 'test3-virtual-network-' . $this->getInstallationId(); $this->nicName = 'test3-network-interface' . $this->getInstallationId(); $this->publicIpName = 'myPublicIP3'; $this->storageName = 'teststorage3' . $this->getInstallationId(); $this->sgName = 'test3-security-group' . $this->getInstallationId(); }
/** * Gets the list of available locations * * @return array Returns the list of available locations looks like array(location => description) */ public function getLocations(\Scalr_Environment $environment = null) { $retval = array(Aws::REGION_US_EAST_1 => 'us-east-1 (N. Virginia)', Aws::REGION_US_WEST_1 => 'us-west-1 (N. California)', Aws::REGION_US_WEST_2 => 'us-west-2 (Oregon)', Aws::REGION_EU_WEST_1 => 'eu-west-1 (Ireland)', Aws::REGION_EU_CENTRAL_1 => 'eu-central-1 (Frankfurt)', Aws::REGION_SA_EAST_1 => 'sa-east-1 (Sao Paulo)', Aws::REGION_AP_SOUTHEAST_1 => 'ap-southeast-1 (Singapore)', Aws::REGION_AP_SOUTHEAST_2 => 'ap-southeast-2 (Sydney)', Aws::REGION_AP_NORTHEAST_1 => 'ap-northeast-1 (Tokyo)'); if ($environment instanceof \Scalr_Environment && $this instanceof Ec2PlatformModule) { if ($environment->getPlatformConfigValue(Ec2PlatformModule::ACCOUNT_TYPE) == Ec2PlatformModule::ACCOUNT_TYPE_GOV_CLOUD) { return [Aws::REGION_US_GOV_WEST_1 => 'us-gov-west-1 (GovCloud US)']; } if ($environment->getPlatformConfigValue(Ec2PlatformModule::ACCOUNT_TYPE) == Ec2PlatformModule::ACCOUNT_TYPE_CN_CLOUD) { return [Aws::REGION_CN_NORTH_1 => 'cn-north-1 (China)']; } } else { // For admin (when no environment defined) we need to show govcloud and chinacloud locations to be able to manage images. $retval = array_merge($retval, [Aws::REGION_CN_NORTH_1 => 'cn-north-1 (China)', Aws::REGION_US_GOV_WEST_1 => 'us-gov-west-1 (GovCloud US)']); } return $retval; }
public static function setUpBeforeClass() { parent::setUpBeforeClass(); if (self::isSkippedFunctionalTest(self::TEST_TYPE_UI)) { return; } $db = \Scalr::getDb(); self::deleteTestVariables(); $envId = \Scalr::config('scalr.phpunit.envid'); if (!$envId) { return; } $env = \Scalr_Environment::init()->loadById($envId); self::$vars[ScopeInterface::SCOPE_SCALR] = new Scalr_Scripting_GlobalVariables(); self::$vars[ScopeInterface::SCOPE_ACCOUNT] = new Scalr_Scripting_GlobalVariables($env->clientId, 0, ScopeInterface::SCOPE_ACCOUNT); self::$vars[ScopeInterface::SCOPE_ENVIRONMENT] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_ENVIRONMENT); self::$args[ScopeInterface::SCOPE_SCALR] = self::$args[ScopeInterface::SCOPE_ACCOUNT] = self::$args[ScopeInterface::SCOPE_ENVIRONMENT] = [0, 0, 0, '']; /* @var $farm Farm */ $farm = Farm::findOne([['envId' => $env->id]]); if ($farm) { self::$vars[ScopeInterface::SCOPE_FARM] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_FARM); self::$args[ScopeInterface::SCOPE_FARM] = [0, $farm->id, 0, '']; /* @var $farmRole FarmRole */ $farmRole = FarmRole::findOne([['farmId' => $farm->id]]); if ($farmRole) { self::$vars[ScopeInterface::SCOPE_ROLE] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_ROLE); self::$args[ScopeInterface::SCOPE_ROLE] = [$farmRole->roleId, 0, 0, '']; self::$vars[ScopeInterface::SCOPE_FARMROLE] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_FARMROLE); self::$args[ScopeInterface::SCOPE_FARMROLE] = [$farmRole->roleId, $farm->id, $farmRole->id, '']; } } }
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; } } } } }
public function init() { $this->env = Scalr_Environment::init()->loadById($this->getParam(Scalr_UI_Controller_Environments::CALL_PARAM_NAME)); $this->user->getPermissions()->validate($this->env); if (!($this->user->getType() == Scalr_Account_User::TYPE_ACCOUNT_OWNER || $this->user->isTeamUserInEnvironment($this->env->id, Scalr_Account_Team::PERMISSIONS_OWNER))) { throw new Scalr_Exception_InsufficientPermissions(); } }
protected function run1($stage) { $envIds = $this->db->Execute("SELECT `id` FROM `client_environments`"); $platformVariables = static::getCloudsCredentialProperties(); foreach ($envIds as $row) { $environment = \Scalr_Environment::init()->loadById($row['id']); $platforms = []; foreach (array_keys(SERVER_PLATFORMS::getList()) as $platform) { if ($environment->getPlatformConfigValue($platform . '.is_enabled', false)) { $platforms[] = $platform; } } foreach ($platforms as $platform) { try { switch ($platform) { case SERVER_PLATFORMS::RACKSPACE: foreach (['rs-ORD1', 'rs-LONx'] as $location) { $cloudCredentials = new Entity\CloudCredentials(); $cloudCredentials->accountId = $environment->getAccountId(); $cloudCredentials->envId = $environment->id; $cloudCredentials->cloud = "{$location}.{$platform}"; $cloudCredentials->name = "{$environment->id}-{$environment->getAccountId()}-{$cloudCredentials->cloud}-" . \Scalr::GenerateUID(true); foreach ($platformVariables[$platform] as $name => $newName) { $value = $environment->getPlatformConfigValue($name, true, $location); if ($value === null) { $value = false; } $cloudCredentials->properties[$newName] = $value; } $cloudCredentials->save(); $cloudCredentials->bindToEnvironment($environment); } break; default: $cloudCredentials = new Entity\CloudCredentials(); $cloudCredentials->accountId = $environment->getAccountId(); $cloudCredentials->envId = $environment->id; $cloudCredentials->cloud = $platform; $cloudCredentials->name = "{$environment->id}-{$environment->getAccountId()}-{$platform}-" . \Scalr::GenerateUID(true); $cloudCredentials->status = Entity\CloudCredentials::STATUS_ENABLED; foreach ($platformVariables[$platform] as $name => $newName) { $value = $environment->getPlatformConfigValue($name); if ($value === null) { $value = false; } $cloudCredentials->properties[$newName] = $value; } $cloudCredentials->save(); $cloudCredentials->bindToEnvironment($environment); break; } } catch (Exception $e) { $this->console->error(get_class($e) . " in {$e->getFile()} on line {$e->getLine()}: " . $e->getMessage()); error_log(get_class($e) . " in {$e->getFile()} at line {$e->getLine()}: {$e->getMessage()}\n{$e->getTraceAsString()}"); } } } }
/** * Auto-snapshoting * {@inheritdoc} * @see \Scalr\System\Pcntl\ProcessInterface::OnStartForking() */ public function OnStartForking() { $db = \Scalr::getDb(); // selects rows where the snapshot's time has come to create new snapshot. $resultset = $db->Execute("\n SELECT * FROM autosnap_settings\n WHERE (`dtlastsnapshot` < NOW() - INTERVAL `period` HOUR OR `dtlastsnapshot` IS NULL)\n AND objectid != '0'\n AND object_type = ?\n ", array(AUTOSNAPSHOT_TYPE::RDSSnap)); while ($snapshotsSettings = $resultset->FetchRow()) { try { $environment = Scalr_Environment::init()->loadById($snapshotsSettings['env_id']); $aws = $environment->aws($snapshotsSettings['region']); // Check instance. If instance wasn't found then delete current recrod from settings table try { $aws->rds->dbInstance->describe($snapshotsSettings['objectid']); } catch (Exception $e) { if (stristr($e->getMessage(), "not found") || stristr($e->getMessage(), "not a valid") || stristr($e->getMessage(), "security token included in the request is invalid")) { $db->Execute("\n DELETE FROM autosnap_settings WHERE id = ?\n ", array($snapshotsSettings['id'])); } $this->Logger->error(sprintf(_("RDS instance %s was not found. " . "Auto-snapshot settings for this instance will be deleted. %s."), $snapshotsSettings['objectid'], $e->getMessage())); throw $e; } // snapshot random unique name $snapshotId = "scalr-auto-" . dechex(microtime(true) * 10000) . rand(0, 9); try { // Create new snapshot $aws->rds->dbSnapshot->create($snapshotsSettings['objectid'], $snapshotId); // update last snapshot creation date in settings $db->Execute("\n UPDATE autosnap_settings\n SET last_snapshotid=?, dtlastsnapshot=NOW() WHERE id=?\n ", array($snapshotId, $snapshotsSettings['id'])); // create new snapshot record in DB $db->Execute("\n INSERT INTO rds_snaps_info\n SET snapid = ?,\n comment = ?,\n dtcreated = NOW(),\n region = ?,\n autosnapshotid = ?\n ", array($snapshotId, _("Auto snapshot"), $snapshotsSettings['region'], $snapshotsSettings['id'])); } catch (Exception $e) { $this->Logger->warn(sprintf(_("Could not create RDS snapshot: %s."), $e->getMessage())); } // Remove old snapshots if ($snapshotsSettings['rotate'] != 0) { $oldSnapshots = $db->GetAll("\n SELECT * FROM rds_snaps_info\n WHERE autosnapshotid = ?\n ORDER BY id ASC\n ", array($snapshotsSettings['id'])); if (count($oldSnapshots) > $snapshotsSettings['rotate']) { while (count($oldSnapshots) > $snapshotsSettings['rotate']) { // takes the oldest snapshot ... $deletingSnapshot = array_shift($oldSnapshots); try { // and deletes it from amazon and from DB $aws->rds->dbSnapshot->delete($deletingSnapshot['snapid']); $db->Execute("\n DELETE FROM rds_snaps_info WHERE id = ?\n ", array($deletingSnapshot['id'])); } catch (Exception $e) { if (stristr($e->getMessage(), "not found") || stristr($e->getMessage(), "not a valid")) { $db->Execute("\n DELETE FROM rds_snaps_info WHERE id = ?\n ", array($deletingSnapshot['id'])); } $this->Logger->error(sprintf(_("DBsnapshot %s for RDS instance %s was not found and cannot be deleted . %s."), $deletingSnapshot['snapid'], $snapshotsSettings['objectid'], $e->getMessage())); } } } } } catch (Exception $e) { $this->Logger->warn(sprintf(_("Cannot create snapshot for RDS Instance %s. %s"), $snapshotsSettings['objectid'], $e->getMessage())); } } }
public function getContent($params = array()) { $environments = $this->user->getEnvironments(); foreach ($environments as &$env) { $environment = Scalr_Environment::init()->loadById($env['id']); $env['farmsCount'] = $environment->getFarmsCount(); $env['serversCount'] = $environment->getRunningServersCount(); } return ['environments' => $environments]; }
/** * {@inheritdoc} * @see \Scalr\Modules\Platforms\Openstack\OpenstackPlatformModule::getLocations() */ public function getLocations(\Scalr_Environment $environment = null) { if (!$environment) { return array('ORD' => 'Rackspace US / ORD', 'DFW' => 'Rackspace US / DFW', 'IAD' => 'Rackspace US / IAD', 'SYD' => 'Rackspace US / SYD'); } else { try { $client = $environment->openstack($this->platform, "fakeRegion"); $zones = $client->listZones(); $endpoints = $client->getConfig()->getAuthToken()->getRegionEndpoints(); foreach ($zones as $zone) { if (isset($endpoints['compute'][$zone->name])) { $retval[$zone->name] = "Rackspace US / {$zone->name}"; } } } catch (\Exception $e) { return array(); } return $retval; } }
public function xSaveEucalyptusAction() { $this->request->defineParams(array('clouds' => array('type' => 'json'))); $pars = array(); $enabled = false; $clouds = $this->getParam('clouds'); $cloudsDeleted = array(); if (count($clouds)) { $enabled = true; foreach ($clouds as $cloud) { $pars[$cloud][EucalyptusPlatformModule::ACCOUNT_ID] = $this->checkVar(EucalyptusPlatformModule::ACCOUNT_ID, 'string', "Account ID required", $cloud); $pars[$cloud][EucalyptusPlatformModule::ACCESS_KEY] = $this->checkVar(EucalyptusPlatformModule::ACCESS_KEY, 'string', "Access Key required", $cloud); $pars[$cloud][EucalyptusPlatformModule::EC2_URL] = $this->checkVar(EucalyptusPlatformModule::EC2_URL, 'string', "EC2 URL required", $cloud); $pars[$cloud][EucalyptusPlatformModule::S3_URL] = $this->checkVar(EucalyptusPlatformModule::S3_URL, 'string', "S3 URL required", $cloud); $pars[$cloud][EucalyptusPlatformModule::SECRET_KEY] = $this->checkVar(EucalyptusPlatformModule::SECRET_KEY, 'password', "Secret Key required", $cloud); $pars[$cloud][EucalyptusPlatformModule::PRIVATE_KEY] = $this->checkVar(EucalyptusPlatformModule::PRIVATE_KEY, 'file', "x.509 Private Key required", $cloud); $pars[$cloud][EucalyptusPlatformModule::CERTIFICATE] = $this->checkVar(EucalyptusPlatformModule::CERTIFICATE, 'file', "x.509 Certificate required", $cloud); $pars[$cloud][EucalyptusPlatformModule::CLOUD_CERTIFICATE] = $this->checkVar(EucalyptusPlatformModule::CLOUD_CERTIFICATE, 'file', "x.509 Cloud Certificate required", $cloud); } } // clear old cloud locations foreach ($this->db->GetAll('SELECT * FROM client_environment_properties WHERE env_id = ? AND name LIKE "eucalyptus.%" AND `group` != "" GROUP BY `group', $this->env->id) as $key => $value) { if (!in_array($value['group'], $clouds)) { $cloudsDeleted[] = $value['group']; } } if (count($this->checkVarError)) { $this->response->failure(); $this->response->data(array('errors' => $this->checkVarError)); } else { $this->db->BeginTrans(); try { $this->env->enablePlatform(SERVER_PLATFORMS::EUCALYPTUS, $enabled); foreach ($cloudsDeleted as $key => $cloud) { $this->db->Execute('DELETE FROM client_environment_properties WHERE env_id = ? AND `group` = ? AND name LIKE "eucalyptus.%"', array($this->env->id, $cloud)); } foreach ($pars as $cloud => $prs) { $this->env->setPlatformConfig($prs, true, $cloud); } if (!$this->user->getAccount()->getSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED)) { $this->user->getAccount()->setSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED, time()); } $this->response->success(_('Environment saved')); $this->response->data(array('enabled' => $enabled)); } catch (Exception $e) { $this->db->RollbackTrans(); throw new Exception(_('Failed to save Eucalyptus settings')); } $this->db->CommitTrans(); } }
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 Run() { global $db; $time = microtime(true); $images = $db->Execute("SELECT * FROM role_images WHERE architecture IS NULL AND platform = 'ec2' ORDER BY id DESC"); while ($image = $images->FetchRow()) { $role = DBRole::loadById($image['role_id']); if ($role->clientId == 0) { continue; } $environemnt = Scalr_Environment::init()->loadById($role->envId); try { $acrh = $environemnt->aws($image['cloud_location'])->ec2->image->describe($image['image_id'])->get(0)->architecture; $db->Execute("UPDATE role_images SET architecture = ? WHERE id = ?", array($acrh, $image['id'])); } catch (Exception $e) { if (stristr($e->getMessage(), "does not exist") && stristr($e->getMessage(), 'The image id')) { //$db->Execute("DELETE FROM role_images WHERE id = ?", array($image['id'])); print "Removed {$image['image_id']} because: {$e->getMessage()}\n"; continue; } elseif (stristr($e->getMessage(), "AWS was not able to validate the provided access credentials")) { continue; } elseif (stristr($e->getMessage(), "You are not subscribed to this service")) { continue; } elseif (stristr($e->getMessage(), "Invalid id")) { $db->Execute("DELETE FROM role_images WHERE id = ?", array($image['id'])); continue; } var_dump($e->getMessage()); exit; } } $db->Execute("ALTER TABLE `roles` DROP `is_stable` , DROP `approval_state` , DROP `szr_version` ;"); $db->Execute("ALTER TABLE `roles` DROP `architecture` ;"); $db->Execute("ALTER TABLE `roles` ADD `os_family` VARCHAR( 30 ) NULL ,\n ADD `os_generation` VARCHAR( 10 ) NULL ,\n ADD `os_version` VARCHAR( 10 ) NULL\n "); //SSL certs refactoring $db->Execute("ALTER TABLE `apache_vhosts` ADD `ssl_cert_id` INT( 11 ) NULL"); $db->Execute("CREATE TABLE IF NOT EXISTS `services_ssl_certs` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `env_id` int(11) NOT NULL,\n `name` varchar(40) NOT NULL,\n `ssl_pkey` text NULL,\n `ssl_cert` text NULL,\n `ssl_cabundle` text NULL,\n PRIMARY KEY (`id`)\n ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;"); $vhosts = $db->Execute("SELECT * FROM apache_vhosts WHERE is_ssl_enabled = '1'"); while ($vhost = $vhosts->FetchRow()) { $db->Execute("INSERT INTO services_ssl_certs SET\n env_id = ?,\n name = ?,\n ssl_pkey = ?,\n ssl_cert = ?,\n ssl_cabundle = ?\n ", array($vhost['env_id'], $vhost['name'], $vhost['ssl_key'], $vhost['ssl_cert'], $vhost['ca_cert'])); $certId = $db->Insert_ID(); $db->Execute("UPDATE apache_vhosts SET ssl_cert_id = ? WHERE id = ?", array($certId, $vhost['id'])); } print "Done.\n"; $t = round(microtime(true) - $time, 2); printf("Upgrade process took %0.2f seconds\n\n\n", $t); }
public function Run() { global $db; $time = microtime(true); $rs = $db->Execute("\n SELECT DISTINCT c1.`env_id`\n FROM `client_environment_properties` c1\n JOIN `client_environment_properties` c2 ON c2.env_id = c1.env_id\n WHERE c1.`name` = 'cloudyn.enabled' AND c1.`value` = '1'\n AND c2.`name` = 'ec2.is_enabled' AND c2.`value` = '1'\n "); foreach ($rs as $row) { $env = Scalr_Environment::init(); $env->loadById($row['env_id']); $awsUsername = sprintf('scalr-cloudyn-%s-%s', $env->id, SCALR_ID); $policyName = sprintf('cloudynpolicy-%s', $env->id); try { $policy = $env->aws->iam->user->getUserPolicy($awsUsername, $policyName); $aPolicy = json_decode($policy, true); $bUpdated = false; if (!empty($aPolicy['Statement'])) { foreach ($aPolicy['Statement'] as $k => $v) { if (!isset($v['Effect']) || $v['Effect'] != 'Allow') { continue; } if (!empty($v['Action']) && is_array($v['Action'])) { $ptr =& $aPolicy['Statement'][$k]['Action']; if (!in_array("rds:List*", $ptr)) { $ptr[] = "rds:List*"; $bUpdated = true; } if (!in_array("s3:GetBucketTagging", $ptr)) { $ptr[] = "s3:GetBucketTagging"; $bUpdated = true; } unset($ptr); if ($bUpdated) { $env->aws->iam->user->putUserPolicy($awsUsername, $policyName, json_encode($aPolicy)); break; } } } } } catch (ClientException $e) { echo $e->getMessage() . "\n"; } unset($env); } print "Done.\n"; $t = round(microtime(true) - $time, 2); printf("Upgrade process took %0.2f seconds\n\n\n", $t); }
public function getContext() { $data = array(); if ($this->user) { $data['user'] = array('userId' => $this->user->getId(), 'clientId' => $this->user->getAccountId(), 'userName' => $this->user->getEmail(), 'gravatarHash' => $this->user->getGravatarHash(), 'envId' => $this->getEnvironment() ? $this->getEnvironmentId() : 0, 'envName' => $this->getEnvironment() ? $this->getEnvironment()->name : '', 'envVars' => $this->getEnvironment() ? $this->getEnvironment()->getPlatformConfigValue(Scalr_Environment::SETTING_UI_VARS) : '', 'type' => $this->user->getType()); if (!$this->user->isScalrAdmin()) { $data['farms'] = $this->db->getAll('SELECT id, name FROM farms WHERE env_id = ? ORDER BY name', array($this->getEnvironmentId())); if ($this->user->getAccountId() != 0) { $data['flags'] = $this->user->getAccount()->getFeaturesList(); $data['user']['userIsTrial'] = $this->user->getAccount()->getSetting(Scalr_Account::SETTING_IS_TRIAL) == '1' ? true : false; } else { $data['flags'] = array(); } $data['flags']['billingExists'] = \Scalr::config('scalr.billing.enabled'); $data['flags']['featureUsersPermissions'] = $this->user->getAccount()->isFeatureEnabled(Scalr_Limits::FEATURE_USERS_PERMISSIONS); $data['flags']['wikiUrl'] = \Scalr::config('scalr.ui.wiki_url'); $data['flags']['supportUrl'] = \Scalr::config('scalr.ui.support_url'); $data['acl'] = $this->request->getAclRoles()->getAllowedArray(true); if ($this->user->isAccountOwner()) { if (!$this->user->getAccount()->getSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED)) { if (count($this->environment->getEnabledPlatforms()) == 0) { $data['flags']['needEnvConfig'] = Scalr_Environment::init()->loadDefault($this->user->getAccountId())->id; } } } $data['environments'] = $this->user->getEnvironments(); if ($this->getEnvironment() && $this->user->isTeamOwner()) { $data['user']['isTeamOwner'] = true; } } $data['platforms'] = array(); $allowedClouds = (array) \Scalr::config('scalr.allowed_clouds'); foreach (SERVER_PLATFORMS::getList() as $platform => $platformName) { if (!in_array($platform, $allowedClouds) || $platform == SERVER_PLATFORMS::UCLOUD && !$this->request->getHeaderVar('Interface-Beta')) { continue; } $data['platforms'][$platform] = array('enabled' => $this->user->isScalrAdmin() ? false : !!$this->environment->isPlatformEnabled($platform), 'name' => $platformName); } } $data['flags']['authMode'] = $this->getContainer()->config->get('scalr.auth_mode'); $data['flags']['specialToken'] = Scalr_Session::getInstance()->getToken(); return $data; }
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]); } } }
/** * Makes clod credentials entity for specified platform * * @param string $platform Cloud credentials platform * @param array $parameters Array of cloud credentials parameters * @param int $status optional Cloud credentials status * * @return Entity\CloudCredentials Returns new cloud credentials entity * * @throws Exception */ public function makeCloudCredentials($platform, $parameters, $status = Entity\CloudCredentials::STATUS_ENABLED) { $cloudCredentials = new Entity\CloudCredentials(); $cloudCredentials->envId = $this->env->id; $cloudCredentials->accountId = $this->env->getAccountId(); $cloudCredentials->cloud = $platform; $cloudCredentials->name = "{$this->env->id}-{$this->env->getAccountId()}-{$platform}-" . \Scalr::GenerateUID(true); $cloudCredentials->status = $status; try { $this->db->BeginTrans(); $cloudCredentials->save(); $cloudCredentials->properties->saveSettings($parameters); $cloudCredentials->bindToEnvironment($this->env); $this->db->CommitTrans(); } catch (Exception $e) { $this->db->RollbackTrans(); throw $e; } $cloudCredentials->cache($this->env->getContainer()); return $cloudCredentials; }