/** * Get list of roles for listView * * @param int $roleId optional * @param string $platform optional * @param string $cloudLocation optional * @param string $imageId optional * @param string $scope optional * @param int $chefServerId optional * @param int $catId optional * @param string $osFamily optional * @param bool $isQuickStart optional * @param bool $isDeprecated optional * @param string $status optional * @param JsonData $addImage optional */ public function xListRolesAction($roleId = null, $platform = null, $cloudLocation = null, $imageId = null, $scope = null, $chefServerId = null, $catId = null, $osFamily = null, $isQuickStart = false, $isDeprecated = false, $status = null, JsonData $addImage = null) { $this->request->restrictAccess('ROLES'); $args = []; $inUseJoin = ''; $envId = $this->getEnvironmentId(true); $accountId = $this->user->getAccountId() ?: NULL; if ($accountId) { $inUseJoin = " JOIN farms ON farm_roles.farmid = farms.id AND farms.clientid = ? "; $args[] = $accountId; if ($envId) { $inUseJoin .= " AND farms.env_id = ?"; $args[] = $envId; } } $role = new Role(); $sql = "\n SELECT DISTINCT " . $role->fields('r') . ", os.name as osName, os.family as osFamily,\n (SELECT EXISTS(SELECT 1 FROM farm_roles " . $inUseJoin . "\n WHERE farm_roles.role_id = r.id)) AS inUse,\n (SELECT GROUP_CONCAT(name SEPARATOR ',') FROM `client_environments` ce LEFT JOIN role_environments re ON ce.id = re.env_id\n WHERE re.role_id = r.id) AS environments\n FROM " . $role->table('r') . "\n LEFT JOIN role_images ON r.id = role_images.role_id\n LEFT JOIN os ON r.os_id = os.id\n LEFT JOIN role_environments re ON re.role_id = r.id\n WHERE\n "; if ($this->request->getScope() == ScopeInterface::SCOPE_SCALR) { $sql .= " r.client_id IS NULL"; } else { if ($this->request->getScope() == ScopeInterface::SCOPE_ACCOUNT) { $sql .= " (r.client_id IS NULL AND role_images.role_id IS NOT NULL OR r.client_id = ? AND r.env_id IS NULL) AND r.generation = ?"; $args = array_merge($args, [$accountId, 2]); } else { $sql .= " (r.client_id IS NULL AND role_images.role_id IS NOT NULL OR r.client_id = ? AND r.env_id IS NULL AND (re.env_id IS NULL OR re.env_id = ?) OR r.env_id = ?) AND r.generation = ?"; $args = array_merge($args, [$accountId, $envId, $envId, 2]); } } if ($roleId) { $sql .= " AND r.id = ?"; $args[] = $roleId; } else { $sql .= " AND :FILTER: "; if ($scope == ScopeInterface::SCOPE_SCALR) { $sql .= " AND r.client_id IS NULL"; } else { if ($scope == ScopeInterface::SCOPE_ACCOUNT) { $sql .= " AND r.client_id = ? AND r.env_id IS NULL"; $args[] = $accountId; } else { if ($scope == ScopeInterface::SCOPE_ENVIRONMENT) { $sql .= " AND r.env_id = ?"; $args[] = $envId; } } } if ($platform) { $sql .= " AND role_images.platform = ?"; $args[] = $platform; } if ($cloudLocation) { $sql .= " AND role_images.cloud_location = ?"; $args[] = $cloudLocation; } if ($imageId) { $sql .= " AND role_images.image_id = ?"; $args[] = $imageId; } if ($catId) { $sql .= " AND r.cat_id = ?"; $args[] = $catId; } if ($osFamily) { $sql .= " AND os.family = ?"; $args[] = $osFamily; } if ($scope) { $sql .= " AND r.origin = ?"; $args[] = $scope == 'scalr' ? 'Shared' : 'Custom'; } if ($status) { $sql .= " AND ("; $used = $status == 'inUse' ? true : false; if ($this->user->getAccountId() != 0) { $sql .= "r.id " . ($used ? '' : "NOT") . " IN (SELECT role_id FROM farm_roles fr " . "JOIN farms f ON f.id = fr.farmid WHERE f." . ($envId ? "env_id" : "clientid") . " = ?)"; $args[] = $envId ?: $this->user->getAccountId(); } else { $sql .= "r.id " . ($used ? '' : "NOT") . " IN (SELECT role_id FROM farm_roles)"; } $sql .= ')'; } if ($chefServerId) { $sql .= " AND r.id IN (SELECT role_id FROM role_properties WHERE name = ? AND value = ?)"; $sql .= " AND r.id IN (SELECT role_id FROM role_properties WHERE name = ? AND value = ?)"; $args[] = \Scalr_Role_Behavior_Chef::ROLE_CHEF_SERVER_ID; $args[] = $chefServerId; $args[] = \Scalr_Role_Behavior_Chef::ROLE_CHEF_BOOTSTRAP; $args[] = 1; } if ($addImage) { if (isset($addImage['osId'])) { $sql .= " AND r.os_id = ?"; $args[] = $addImage['osId']; } if (isset($addImage['isScalarized']) && isset($addImage['hasCloudInit']) && $addImage['isScalarized'] == 0 && $addImage['hasCloudInit'] == 0) { $sql .= " AND r.is_scalarized = 0"; } } if ($isQuickStart) { $sql .= " AND r.is_quick_start = 1"; } if ($isDeprecated) { $sql .= " AND r.is_deprecated = 1"; } } $response = $this->buildResponseFromSql2($sql, ['id', 'name', 'os_id'], ['r.name'], $args); $data = []; $allPlatforms = array_flip(array_keys(SERVER_PLATFORMS::GetList())); foreach ($response['data'] as $r) { $role = new Role(); $role->load($r); $row = new stdClass(); $row->name = $role->name; $row->behaviors = $role->getBehaviors(); $row->id = $role->id; $row->accountId = $role->accountId; $row->envId = $role->envId; $row->status = $r['inUse'] ? 'In use' : 'Not used'; $row->scope = $role->getScope(); $row->os = $r['osName']; $row->osId = $role->osId; $row->osFamily = $r['osFamily']; $row->dtAdded = $role->added ? Scalr_Util_DateTime::convertTz($role->added) : null; $row->dtLastUsed = $role->lastUsed ? Scalr_Util_DateTime::convertTz($role->lastUsed) : null; $row->isQuickStart = $role->isQuickStart ? "1" : "0"; $row->isDeprecated = $role->isDeprecated ? "1" : "0"; $row->isScalarized = $role->isScalarized ? 1 : 0; $row->client_name = $role->accountId == 0 ? 'Scalr' : 'Private'; $row->environments = $r['environments'] ? explode(',', $r['environments']) : []; $platforms = array_keys($role->fetchImagesArray()); usort($platforms, function ($a, $b) use($allPlatforms) { return $allPlatforms[$a] > $allPlatforms[$b] ? 1 : -1; }); $row->platforms = $platforms; if ($addImage->count()) { try { $role->getImage($addImage['platform'], $addImage['cloudLocation']); $row->canAddImage = false; } catch (Exception $e) { $row->canAddImage = true; } } $data[] = $row; } $this->response->data(['total' => $response['total'], 'data' => $data]); }
/** * @param int $userId * @param int $envId * @param JsonData $uiStorage * @param JsonData $updateDashboard */ public function xPerpetuumMobileAction($userId, $envId, JsonData $uiStorage, JsonData $updateDashboard) { $result = array(); if ($this->user) { if ($updateDashboard) { $result['updateDashboard'] = Scalr_UI_Controller::loadController('dashboard')->checkLifeCycle($updateDashboard); } if (!Scalr_Session::getInstance()->isVirtual() && $uiStorage->count()) { $this->user->setSetting(Scalr_Account_User::SETTING_UI_STORAGE_TIME, $uiStorage['time']); $this->user->setVar(Scalr_Account_User::VAR_UI_STORAGE, $uiStorage['dump']); } } $equal = $this->user && $this->user->getId() == $userId && ($this->getEnvironment() ? $this->getEnvironmentId() : 0) == $envId; $result['equal'] = $equal; $result['isAuthenticated'] = $this->user ? true : false; $this->response->data($result); }
/** * @param string $platform * @param string $architecture * @param JsonData $behaviors * @param string $name * @param bool $createImage * @param string $imageId * @param string $cloudLocation * @param string $osId * @param integer $hvm * @param JsonData $advanced * @param JsonData $chef * @throws Exception */ public function xBuildAction($platform, $architecture, JsonData $behaviors, $name = '', $createImage = false, $imageId, $cloudLocation, $osId, $hvm = 0, JsonData $advanced, JsonData $chef) { $this->request->restrictAccess(Acl::RESOURCE_FARMS_ROLES, Acl::PERM_FARMS_ROLES_CREATE); if (!\Scalr\Model\Entity\Role::validateName($name)) { throw new Exception(_("Name is incorrect")); } if (!$createImage && $this->db->GetOne("SELECT id FROM roles WHERE name=? AND (env_id IS NULL OR env_id = ?) LIMIT 1", array($name, $this->getEnvironmentId()))) { throw new Exception('Selected role name is already used. Please select another one.'); } $behaviours = implode(",", array_values($behaviors->getArrayCopy())); $os = Os::findPk($osId); if (!$os) { throw new Exception('Operating system not found.'); } // Create server $creInfo = new ServerCreateInfo($platform, null, 0, 0); $creInfo->clientId = $this->user->getAccountId(); $creInfo->envId = $this->getEnvironmentId(); $creInfo->farmId = 0; $creInfo->SetProperties(array(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR => $behaviours, SERVER_PROPERTIES::SZR_IMPORTING_IMAGE_ID => $imageId, SERVER_PROPERTIES::SZR_KEY => Scalr::GenerateRandomKey(40), SERVER_PROPERTIES::SZR_KEY_TYPE => SZR_KEY_TYPE::PERMANENT, SERVER_PROPERTIES::SZR_VESION => "0.13.0", SERVER_PROPERTIES::SZR_IMPORTING_MYSQL_SERVER_TYPE => "mysql", SERVER_PROPERTIES::SZR_DEV_SCALARIZR_BRANCH => $advanced['scalrbranch'], SERVER_PROPERTIES::ARCHITECTURE => $architecture, SERVER_PROPERTIES::SZR_IMPORTING_LEAVE_ON_FAIL => $advanced['dontterminatefailed'] == 'on' ? 1 : 0, SERVER_PROPERTIES::SZR_IMPORTING_CHEF_SERVER_ID => $chef['chef.server'], SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ENVIRONMENT => $chef['chef.environment'], SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ROLE_NAME => $chef['chef.role'])); $dbServer = DBServer::Create($creInfo, true); $dbServer->status = SERVER_STATUS::TEMPORARY; $dbServer->imageId = $imageId; $dbServer->save(); //Launch server $launchOptions = new Scalr_Server_LaunchOptions(); $launchOptions->imageId = $imageId; $launchOptions->cloudLocation = $cloudLocation; $launchOptions->architecture = $architecture; $platformObj = PlatformFactory::NewPlatform($platform); switch ($platform) { case SERVER_PLATFORMS::ECS: $launchOptions->serverType = 10; if ($cloudLocation == 'all') { $locations = array_keys($platformObj->getLocations($this->environment)); $launchOptions->cloudLocation = $locations[0]; } //Network here: $osClient = $platformObj->getOsClient($this->environment, $launchOptions->cloudLocation); $networks = $osClient->network->listNetworks(); $tenantId = $osClient->getConfig()->getAuthToken()->getTenantId(); foreach ($networks as $network) { if ($network->status == 'ACTIVE') { if ($network->{"router:external"} != true) { if ($tenantId == $network->tenant_id) { $launchOptions->networks = array($network->id); break; } } } } break; case SERVER_PLATFORMS::IDCF: $launchOptions->serverType = 24; break; case SERVER_PLATFORMS::RACKSPACE: if ($os->family == 'ubuntu') { $launchOptions->serverType = 1; } else { $launchOptions->serverType = 3; } break; case SERVER_PLATFORMS::RACKSPACENG_US: $launchOptions->serverType = 3; break; case SERVER_PLATFORMS::RACKSPACENG_UK: $launchOptions->serverType = 3; break; case SERVER_PLATFORMS::EC2: if ($hvm == 1) { $launchOptions->serverType = 'm3.xlarge'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } else { if ($os->family == 'oel') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } elseif ($os->family == 'rhel') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } elseif ($os->family == 'scientific') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } elseif ($os->family == 'debian' && $os->generation == '8') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } elseif ($os->family == 'centos' && $os->generation == '7') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } else { $launchOptions->serverType = 'm3.large'; } } $launchOptions->userData = "#cloud-config\ndisable_root: false"; break; case SERVER_PLATFORMS::GCE: $launchOptions->serverType = 'n1-standard-1'; $location = null; $locations = array_keys($platformObj->getLocations($this->environment)); while (count($locations) != 0) { $location = array_shift($locations); if (strstr($location, "us-")) { break; } } $launchOptions->cloudLocation = $locations[0]; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::GCE_STORAGE; break; } if ($advanced['servertype']) { $launchOptions->serverType = $advanced['servertype']; } if ($advanced['availzone']) { $launchOptions->availZone = $advanced['availzone']; } if ($advanced['region']) { $launchOptions->cloudLocation = $advanced['region']; } //Add Bundle task $creInfo = new ServerSnapshotCreateInfo($dbServer, $name, SERVER_REPLACEMENT_TYPE::NO_REPLACE); $bundleTask = BundleTask::Create($creInfo, true); if ($bundleType) { $bundleTask->bundleType = $bundleType; } $bundleTask->createdById = $this->user->id; $bundleTask->createdByEmail = $this->user->getEmail(); $bundleTask->osFamily = $os->family; $bundleTask->object = $createImage ? BundleTask::BUNDLETASK_OBJECT_IMAGE : BundleTask::BUNDLETASK_OBJECT_ROLE; $bundleTask->cloudLocation = $launchOptions->cloudLocation; $bundleTask->save(); $bundleTask->Log(sprintf("Launching temporary server (%s)", serialize($launchOptions))); $dbServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BUNDLE_TASK_ID, $bundleTask->id); try { $platformObj->LaunchServer($dbServer, $launchOptions); $bundleTask->Log(_("Temporary server launched. Waiting for running state...")); } catch (Exception $e) { $bundleTask->SnapshotCreationFailed(sprintf(_("Unable to launch temporary server: %s"), $e->getMessage())); } $this->response->data(array('serverId' => $dbServer->serverId, 'bundleTaskId' => $bundleTask->id)); }