/** * Registers suspension error and suspends cloud platform if the timeout is reached * * @param string $errorMessage The error message */ public function registerError($errorMessage) { if (!$this->firstErrorOccurred instanceof EnvironmentProperty) { $this->firstErrorOccurred = $this->initProp($this->firstErrorOccurredProp, time()); $this->firstErrorOccurred->save(); } elseif (empty($this->firstErrorOccurred->value)) { $this->firstErrorOccurred->value = time(); $this->firstErrorOccurred->save(); } elseif ($this->isSuspended()) { //If cloud platform is already suspended we don't keep collecting error messages return; } else { //Checks suspension timeout if (time() - $this->firstErrorOccurred->value > static::PLATFORM_SUSPEND_INTERVAL) { //The cloud platform should be suspended $this->suspend(); } } if ($this->lastErrorMessage instanceof EnvironmentProperty) { $this->lastErrorMessage->value = $errorMessage; } else { $this->lastErrorMessage = $this->initProp($this->lastErrorMessageProp, $errorMessage); } $this->lastErrorMessage->save(); }
protected function run1($stage) { $envProps = EnvironmentProperty::find([['name' => Entity\CloudCredentialsProperty::AWS_ACCOUNT_ID]]); foreach ($envProps as $prop) { /* @var $prop EnvironmentProperty */ if (!is_numeric($prop->value)) { $prop->value = \Scalr::getContainer()->crypto->decrypt($prop->value); $prop->save(); } } }
/** * Gets environment properties list * * TODO: need to implement proper properties handling @see \Scalr_Environment::getEncryptedVariables() * * @return EnvironmentProperty[] */ public function getProperties() { if (empty($this->_properties)) { /* @var $properties EnvironmentProperty[] */ $properties = EnvironmentProperty::findByEnvId($this->id); if (!empty($properties)) { foreach ($properties as $property) { $this->_properties[$property->name] = $property; } } } return $this->_properties; }
/** * {@inheritdoc} * @see \Scalr\Modules\PlatformModuleInterface::getLocations() */ public function getLocations(\Scalr_Environment $environment = null) { if ($environment === null) { return array(); } $locations = Entity\Account\EnvironmentProperty::find([['envId' => $environment->id], ['name' => Entity\Account\EnvironmentProperty::RACKSPACE_LOCATIONS]]); $retval = []; /* @var $location Entity\Account\EnvironmentProperty */ foreach ($locations as $location) { $retval[$location->group] = "Rackspace / {$location->group}"; } return $retval; }
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]); } } }
public function _costCenter($from, $to, $action) { switch ($action) { case static::ACT_CONVERT_TO_OBJECT: /* @var $from Environment */ $to->costCenter = $from->getProperty(EnvironmentProperty::SETTING_CC_ID); break; case static::ACT_CONVERT_TO_ENTITY: /* @var $to Environment */ $ccId = ApiController::getBareId($from, 'costCenter'); if (empty($ccId)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Missed property costCenter"); } $this->controller->getCostCenter($ccId); $to->setProperty(EnvironmentProperty::SETTING_CC_ID, $ccId); break; case static::ACT_GET_FILTER_CRITERIA: $env = new Environment(); $envProperty = new EnvironmentProperty(); return [AbstractEntity::STMT_FROM => "\n JOIN {$envProperty->table('cep')} ON {$env->columnId()} = {$envProperty->columnEnvId('cep')}\n AND {$envProperty->columnName('cep')} = " . $envProperty->qstr('name', EnvironmentProperty::SETTING_CC_ID) . "\n ", AbstractEntity::STMT_WHERE => "{$envProperty->columnValue('cep')} = " . $envProperty->qstr('value', $from->costCenter)]; } }
private function getCloudParams($platform) { $params = []; if (in_array($platform, $this->env->getEnabledPlatforms()) || $platform == SERVER_PLATFORMS::AZURE) { $cloudCredentials = $this->env->cloudCredentials($platform); $ccProps = $cloudCredentials->properties; switch ($platform) { case SERVER_PLATFORMS::EC2: $params[SERVER_PLATFORMS::EC2 . '.is_enabled'] = true; $params[Entity\CloudCredentialsProperty::AWS_ACCOUNT_ID] = $ccProps[Entity\CloudCredentialsProperty::AWS_ACCOUNT_ID]; $params[Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE] = $ccProps[Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE]; $params[Entity\CloudCredentialsProperty::AWS_ACCESS_KEY] = $ccProps[Entity\CloudCredentialsProperty::AWS_ACCESS_KEY]; $params[Entity\CloudCredentialsProperty::AWS_SECRET_KEY] = $ccProps[Entity\CloudCredentialsProperty::AWS_SECRET_KEY] != '' ? '******' : ''; $params[Entity\CloudCredentialsProperty::AWS_PRIVATE_KEY] = $ccProps[Entity\CloudCredentialsProperty::AWS_PRIVATE_KEY] != '' ? 'Uploaded' : ''; $params[Entity\CloudCredentialsProperty::AWS_CERTIFICATE] = $ccProps[Entity\CloudCredentialsProperty::AWS_CERTIFICATE] != '' ? 'Uploaded' : ''; $params[Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_BUCKET] = $ccProps[Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_BUCKET]; $params[Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_ENABLED] = $ccProps[Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_ENABLED]; $params[Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_PAYER_ACCOUNT] = $ccProps[Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_PAYER_ACCOUNT]; $params[Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_REGION] = $ccProps[Entity\CloudCredentialsProperty::AWS_DETAILED_BILLING_REGION]; try { if ($params[Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE] == Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_CN_CLOUD) { $params['arn'] = $this->env->aws('cn-north-1')->getUserArn(); } elseif ($params[Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE] == Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_GOV_CLOUD) { $params['arn'] = $this->env->aws('us-gov-west-1')->getUserArn(); } else { $params['arn'] = $this->env->aws('us-east-1')->getUserArn(); } //$params['username'] = $this->env->aws('us-east-1')->getUsername(); } catch (Exception $e) { } break; case SERVER_PLATFORMS::GCE: $params[SERVER_PLATFORMS::GCE . '.is_enabled'] = true; $params[Entity\CloudCredentialsProperty::GCE_PROJECT_ID] = $ccProps[Entity\CloudCredentialsProperty::GCE_PROJECT_ID]; $jsonKey = $ccProps[Entity\CloudCredentialsProperty::GCE_JSON_KEY]; if (!empty($jsonKey)) { $params[Entity\CloudCredentialsProperty::GCE_JSON_KEY] = 'Uploaded'; } else { $params[Entity\CloudCredentialsProperty::GCE_CLIENT_ID] = $ccProps[Entity\CloudCredentialsProperty::GCE_CLIENT_ID]; $params[Entity\CloudCredentialsProperty::GCE_SERVICE_ACCOUNT_NAME] = $ccProps[Entity\CloudCredentialsProperty::GCE_SERVICE_ACCOUNT_NAME]; $params[Entity\CloudCredentialsProperty::GCE_KEY] = $ccProps[Entity\CloudCredentialsProperty::GCE_KEY] != '' ? 'Uploaded' : ''; } break; case SERVER_PLATFORMS::CLOUDSTACK: case SERVER_PLATFORMS::IDCF: $params = $this->getCloudStackDetails($platform); break; case SERVER_PLATFORMS::OPENSTACK: case SERVER_PLATFORMS::RACKSPACENG_UK: case SERVER_PLATFORMS::RACKSPACENG_US: case SERVER_PLATFORMS::OCS: case SERVER_PLATFORMS::NEBULA: case SERVER_PLATFORMS::MIRANTIS: case SERVER_PLATFORMS::VIO: case SERVER_PLATFORMS::VERIZON: case SERVER_PLATFORMS::CISCO: case SERVER_PLATFORMS::HPCLOUD: $params = $this->getOpenStackDetails($platform); break; case SERVER_PLATFORMS::RACKSPACE: $params[SERVER_PLATFORMS::RACKSPACE . '.is_enabled'] = true; /* @var $locations Entity\Account\EnvironmentProperty[] */ $locations = Entity\Account\EnvironmentProperty::find([['envId' => $this->env->id], ['name' => Entity\Account\EnvironmentProperty::RACKSPACE_LOCATIONS]]); foreach ($locations as $location) { $ccProps = $this->env->cloudCredentials("{$location->group}.{$platform}")->properties; $params[$location->group] = [Entity\CloudCredentialsProperty::RACKSPACE_USERNAME => $ccProps[Entity\CloudCredentialsProperty::RACKSPACE_USERNAME], Entity\CloudCredentialsProperty::RACKSPACE_API_KEY => $ccProps[Entity\CloudCredentialsProperty::RACKSPACE_API_KEY], Entity\CloudCredentialsProperty::RACKSPACE_IS_MANAGED => $ccProps[Entity\CloudCredentialsProperty::RACKSPACE_IS_MANAGED]]; } break; case SERVER_PLATFORMS::AZURE: $params[SERVER_PLATFORMS::AZURE . '.is_enabled'] = $cloudCredentials->isEnabled(); $params[Entity\CloudCredentialsProperty::AZURE_TENANT_NAME] = $ccProps[Entity\CloudCredentialsProperty::AZURE_TENANT_NAME]; $params[Entity\CloudCredentialsProperty::AZURE_AUTH_STEP] = $ccProps[Entity\CloudCredentialsProperty::AZURE_AUTH_STEP] ?: 0; $params[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID] = $ccProps[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID]; $params['subscriptions'] = []; if ($params[Entity\CloudCredentialsProperty::AZURE_AUTH_STEP] > 1) { $subscriptionList = []; try { $subscriptions = $this->env->azure()->getSubscriptionsList(); foreach ($subscriptions as $subscription) { if ($subscription->state == 'Enabled') { $subscriptionList[] = ['displayName' => $subscription->displayName, 'subscriptionId' => $subscription->subscriptionId]; } } } catch (Exception $e) { if (strpos($e->getMessage(), 'Error validating credentials') !== false || strpos($e->getMessage(), 'Refresh token is expired or not exists') !== false) { $cloudCredentials->delete(); $cloudCredentials->release($this->env->getContainer()); $params[Entity\CloudCredentialsProperty::AZURE_AUTH_STEP] = 0; $params[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID] = null; } $params['errorMessage'] = $e->getMessage(); break; } if (empty($subscriptionList)) { $params['errorMessage'] = sprintf("There are no active subscriptions available for the '%s' tenant", $params[AzurePlatformModule::TENANT_NAME]); } $params['subscriptions'] = $subscriptionList; } break; } } if ($platform == SERVER_PLATFORMS::EC2) { $platformModule = PlatformFactory::NewPlatform($platform); /* @var $platformModule Ec2PlatformModule */ $params['cloudLocations'] = [Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_REGULAR => array_keys($platformModule->getLocationsByAccountType(Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_REGULAR)), Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_GOV_CLOUD => array_keys($platformModule->getLocationsByAccountType(Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_GOV_CLOUD)), Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_CN_CLOUD => array_keys($platformModule->getLocationsByAccountType(Entity\CloudCredentialsProperty::AWS_ACCOUNT_TYPE_CN_CLOUD))]; } return $params; }
/** * xMoveProjectsAction * * @param JsonData $projects Projects that should be moved * @throws AnalyticsException * @throws Exception * @throws \Scalr\Exception\ModelException */ public function xMoveProjectsAction(JsonData $projects = null) { $envChange = []; $accountChange = []; $projectChange = []; $ccEntityCache = []; $collisions = []; foreach ($projects as $project) { $projectEntity = ProjectEntity::findPk($project['projectId']); /* @var $projectEntity ProjectEntity */ if (empty($ccEntity)) { $ccEntity = $projectEntity->getCostCenter(); } if ($ccEntity->ccId == $project['ccId']) { continue; } if (empty($ccEntityCache[$project['ccId']])) { $newCcEntity = CostCentreEntity::findPk($project['ccId']); /* @var $newCcEntity CostCentreEntity */ if (!$newCcEntity) { throw new Exception(sprintf("Cost center with id %s has not been found.", $project['ccId']), 404); } $ccEntityCache[$project['ccId']] = $newCcEntity->ccId; } $farms[$projectEntity->projectId] = $projectEntity->getFarmsList(); foreach ($farms[$projectEntity->projectId] as $farmId => $farmName) { $farmEntity = Farm::findPk($farmId); /* @var $farmEntity Farm */ if (empty($accountChange[$farmEntity->accountId])) { $accountCss = AccountCostCenterEntity::findOne([['accountId' => $farmEntity->accountId], ['ccId' => $newCcEntity->ccId]]); if (!$accountCss) { $accountChange[$farmEntity->accountId] = $newCcEntity->ccId; } } if (empty($envChange[$farmEntity->envId])) { $project['name'] = $projectEntity->name; $envChange[$farmEntity->envId] = $project; } else { if ($envChange[$farmEntity->envId]['ccId'] != $project['ccId']) { if (!in_array($projectEntity->name, $collisions)) { $collisions[] = $projectEntity->name; } if (!in_array($envChange[$farmEntity->envId]['name'], $collisions)) { $collisions[] = $envChange[$farmEntity->envId]['name']; } continue; } } } $projectEntity->ccId = $project['ccId']; $projectChange[$projectEntity->projectId] = $projectEntity; } $remainningEnvs = []; $projectsCount = count($projectChange); if ($projectsCount) { if (isset($ccEntity)) { $envList = $ccEntity->getEnvironmentsList(); foreach ($envList as $envId => $name) { if (isset($envChange[$envId])) { $ccProjects = $this->getContainer()->analytics->projects->getUsedInEnvironment($envId); foreach ($ccProjects as $project) { /* @var $project ProjectEntity */ if (!isset($farms[$project->projectId])) { $farms[$project->projectId] = $project->getFarmsList(); } if (count($farms[$project->projectId]) > 0 && !isset($projectChange[$project->projectId])) { if (!in_array($envId, $remainningEnvs)) { $remainningEnvs[] = $envId; } } } } } } $this->db->BeginTrans(); try { foreach ($accountChange as $accountId => $ccId) { $accountCss = new AccountCostCenterEntity($accountId, $ccId); $accountCss->save(); } if (empty($remainningEnvs) && empty($collisions)) { foreach ($envChange as $envId => $data) { $envProp = EnvironmentProperty::findOne([['envId' => $envId], ['name' => EnvironmentProperty::SETTING_CC_ID]]); /* @var $envProp EnvironmentProperty */ $envProp->value = $data['ccId']; $envProp->save(); } } foreach ($projectChange as $project) { /* @var $project ProjectEntity */ $project->save(); } $this->db->CommitTrans(); } catch (Exception $e) { $this->db->RollbackTrans(); throw $e; } } if (count($collisions) > 0) { $this->response->warning(sprintf("%d Project%s %s been moved however collision occurred. Projects '%s' are used in the Farms from the same Environment however they have been moved to different Cost Centers.", $projectsCount, $projectsCount > 1 ? 's' : '', $projectsCount > 1 ? 'have' : 'has', implode("', '", $collisions))); } else { if (count($remainningEnvs) > 0) { $this->response->warning(sprintf("%d Project%s %s been moved however some Projects don't correspond to Cost Centers assigned to Environments '%s'.", $projectsCount, $projectsCount > 1 ? 's' : '', $projectsCount > 1 ? 'have' : 'has', implode("', '", $remainningEnvs))); } else { $this->response->success(sprintf("%d Project%s %s been moved to other Cost Center.", $projectsCount, $projectsCount > 1 ? 's' : '', $projectsCount > 1 ? 'have' : 'has')); } } }