protected function run8($stage) { $knownOses = []; //Retrieves the list of all known OSes foreach (Entity\Os::all() as $os) { /* @var $os Entity\Os */ $knownOses[$os->id] = $os; } $role = new Entity\Role(); //Trying to clarify the operating system of the Roles using Images which are associated with them. //If all Images have the same operating system it will be considered as acceptable for the Role at latter will be updated. $rs = $this->db->Execute("\n SELECT " . $role->fields('r', true) . ", GROUP_CONCAT(t.os_id) `osids`\n FROM roles r JOIN (\n SELECT DISTINCT ri.role_id, i.os_id\n FROM images i\n JOIN role_images ri ON i.id = ri.image_id\n AND i.platform = ri.platform\n AND i.cloud_location = ri.cloud_location\n ) t ON t.role_id = r.id\n WHERE r.os_id = ?\n GROUP BY r.id\n HAVING osids != r.os_id\n ", ['unknown-os']); if ($rs->RecordCount()) { $this->console->out("Found %d Roles the OS value of which can be filled from the Images. Updating...", $rs->RecordCount()); } while ($row = $rs->FetchRow()) { $role = new Entity\Role(); $role->load($row, 'r'); if (!empty($row['osids'])) { if (isset($knownOses[$row['osids']])) { //Updating OS value of the Role $role->osId = $row['osids']; $role->save(); } else { $this->console->warning("Role %s (%d) is associated with the Images with either different or unknown OS: %s", $role->name, $role->id, $row['osids']); } } } $image = new Entity\Image(); //Trying to clarify the operating sytem of the Images using Roles which are associated with them. $rs = $this->db->Execute("\n SELECT " . $image->fields('i', true) . ", GROUP_CONCAT(t.os_id) `osids`\n FROM images i JOIN (\n SELECT DISTINCT ri.image_id, ri.platform, ri.cloud_location, r.os_id\n FROM roles r\n JOIN role_images ri ON ri.role_id = r.id\n ) t ON t.image_id = i.id AND t.platform = i.platform AND t.cloud_location = i.cloud_location\n WHERE i.os_id = ?\n GROUP BY i.hash\n HAVING osids != i.os_id\n ", ['unknown-os']); if ($rs->RecordCount()) { $this->console->out("Found %d Images the OS value of which can be filled from the Roles. Updating...", $rs->RecordCount()); } while ($row = $rs->FetchRow()) { $image = new Entity\Image(); $image->load($row, 'i'); if (!empty($row['osids'])) { if (isset($knownOses[$row['osids']])) { //Updating OS value of the Image $image->osId = $row['osids']; $image->save(); } else { $this->console->warning("Image (%s) imageId: %s, platform: %s, cloudLocation: %s is associated with the Roles with either different or unknown OS: %s", $image->hash, $image->id, $image->platform, $image->cloudLocation, $row['osids']); } } } }
/** * @param int $roleId * @param string $name * @param string $description * @param string $osId * @param int $catId * @param bool $isQuickStart * @param bool $isDeprecated * @param bool $isScalarized * @param JsonData $behaviors * @param JsonData $images * @param JsonData $scripts * @param JsonData $variables * @param JsonData $chef * @param JsonData $environments * @throws Exception * @throws Scalr_Exception_Core * @throws Scalr_Exception_InsufficientPermissions */ public function xSaveAction($roleId = 0, $name, $description, $osId, $catId, $isQuickStart = false, $isDeprecated = false, $isScalarized = true, JsonData $behaviors, JsonData $images, JsonData $scripts, JsonData $variables, JsonData $chef, JsonData $environments) { $this->request->restrictAccess('ROLES', 'MANAGE'); $accountId = $this->user->getAccountId() ?: NULL; if (!Role::isValidName($name)) { throw new Exception(_("Role name is incorrect")); } if (Role::isNameUsed($name, $accountId, $this->getEnvironmentId(true), $roleId)) { throw new Exception('Selected role name is already used. Please select another one.'); } if (!$catId) { throw new Exception('Role category is required'); } if ($roleId == 0) { if (!Os::findPk($osId)) { throw new Exception(sprintf('%s is not valid osId', $osId)); } $role = new Role(); $role->generation = 2; $role->origin = $this->user->isScalrAdmin() ? ROLE_TYPE::SHARED : ROLE_TYPE::CUSTOM; $role->accountId = $accountId; $role->envId = $this->getEnvironmentId(true); $role->name = $name; $role->catId = $catId; $role->osId = $osId; $role->isScalarized = $isScalarized ? 1 : 0; $role->addedByUserId = $this->user->getId(); $role->addedByEmail = $this->user->getEmail(); $role->setBehaviors((array) $behaviors); $role->save(); } else { $role = Role::findPk($roleId); if (!$role) { throw new Scalr_Exception_Core(sprintf(_("Role ID#%s not found in database"), $roleId)); } $this->request->checkPermissions($role, true); $role->name = $name; $role->catId = $catId; } $globalVariables = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(true), ScopeInterface::SCOPE_ROLE); $globalVariables->setValues($variables, $role->id); foreach (RoleProperty::find([['roleId' => $role->id], ['name' => ['$like' => ['chef.%']]]]) as $prop) { $prop->delete(); } foreach ($chef as $name => $value) { $prop = new RoleProperty(); $prop->roleId = $role->id; $prop->name = $name; $prop->value = $value; $prop->save(); } $role->description = $description; $role->isQuickStart = $isQuickStart; $role->isDeprecated = $isDeprecated; foreach ($images as $i) { if (isset($i['platform']) && isset($i['cloudLocation']) && isset($i['imageId'])) { $role->setImage($i['platform'], $i['cloudLocation'], $i['imageId'], $this->user->getId(), $this->user->getEmail()); } } $role->setScripts((array) $scripts); $role->save(); if ($this->request->getScope() == ScopeInterface::SCOPE_ACCOUNT) { foreach (RoleEnvironment::find([['roleId' => $roleId]]) as $re) { $re->delete(); } $accountEnvironments = []; $allowedEnvironments = []; foreach (Environment::find([['accountId' => $this->user->getAccountId()]]) as $env) { $accountEnvironments[] = $env->id; } foreach ($environments as $e) { if ($e['enabled'] == 1 && in_array($e['id'], $accountEnvironments)) { $allowedEnvironments[] = $e['id']; } } if (count($allowedEnvironments) < count($accountEnvironments)) { foreach ($allowedEnvironments as $id) { $re = new RoleEnvironment(); $re->roleId = $role->id; $re->envId = $id; $re->save(); } } } $this->response->data(['role' => $this->getInfo($role->id, true), 'categories' => $this->listRoleCategories(true)]); $this->response->success('Role saved'); }