/** * @param string $query * @param string $platform * @param string $cloudLocation * @param string $scope * @param string $osFamily * @param string $osId * @param string $id * @param string $hash * @param JsonData $sort * @param int $start * @param int $limit * @param JsonData $hideLocation * @param bool $hideNotActive * @throws Exception */ public function xListAction($query = null, $platform = null, $cloudLocation = null, $scope = null, $osFamily = null, $osId = null, $id = null, $hash = null, JsonData $sort, $start = 0, $limit = 20, JsonData $hideLocation, $hideNotActive = false) { $this->restrictAccess('IMAGES'); $osIds = $criteria = []; $accountId = $this->user->getAccountId() ?: NULL; $envId = $this->getEnvironmentId(true); if ($this->request->getScope() == ScopeInterface::SCOPE_SCALR) { $criteria[] = ['accountId' => NULL]; } else { if ($this->request->getScope() == ScopeInterface::SCOPE_ACCOUNT) { $criteria[] = ['$or' => [['accountId' => $accountId], ['accountId' => NULL]]]; $criteria[] = ['envId' => NULL]; } else { $enabledPlatforms = $this->getEnvironment()->getEnabledPlatforms(); $criteria[] = ['$or' => [['$and' => [['accountId' => NULL], ['platform' => empty($enabledPlatforms) ? NULL : ['$in' => $enabledPlatforms]]]], ['$and' => [['accountId' => $accountId], ['envId' => NULL]]], ['envId' => $envId]]]; } } if ($hash) { $criteria[] = ['hash' => $hash]; } else { if ($query) { $querySql = '%' . $query . '%'; $criteria[] = ['name' => ['$like' => $querySql]]; } if ($platform) { $criteria[] = ['platform' => $platform]; } if ($cloudLocation) { $criteria[] = ['cloudLocation' => $cloudLocation]; } if ($scope == ScopeInterface::SCOPE_SCALR) { $criteria[] = ['accountId' => NULL]; } else { if ($scope == ScopeInterface::SCOPE_ACCOUNT) { $criteria[] = ['accountId' => $accountId]; $criteria[] = ['envId' => NULL]; } else { if ($scope == ScopeInterface::SCOPE_ENVIRONMENT) { $criteria[] = ['envId' => $envId]; } } } if ($osFamily) { $osIds = Os::findIdsBy($osFamily); } if ($osId) { $os = Os::find([['id' => $osId]]); $osIds = []; foreach ($os as $i) { /* @var $i Os */ array_push($osIds, $i->id); } } if (!empty($osIds)) { $criteria[] = ['osId' => ['$in' => $osIds]]; } if ($id) { $criteria[] = ['id' => ['$like' => $id . '%']]; } if ($hideLocation) { foreach ($hideLocation as $platform => $locations) { foreach ($locations as $loc) { if ($loc) { $criteria[] = ['$or' => [['platform' => ['$ne' => $platform]], ['cloudLocation' => ['$ne' => $loc]]]]; } else { $criteria[] = ['platform' => ['$ne' => $platform]]; } } } } if ($hideNotActive) { $criteria[] = ['status' => Image::STATUS_ACTIVE]; } } $image = new Image(); $os = new Os(); $order = \Scalr\UI\Utils::convertOrder($sort, ['dtAdded' => false], ['id', 'platform', 'cloudLocation', 'name', 'osId', 'dtAdded', 'architecture', 'createdByEmail', 'source', 'type']); if (!empty($order)) { $sOrder = ''; foreach ($order as $k => $v) { if ($k == 'osId') { $sOrder .= ', IF(os.family IS NOT NULL, os.family, images.os_id)' . ($v ? '' : ' DESC') . ", CAST(os.version AS DECIMAL(10,2))" . ($v ? '' : ' DESC'); } else { $field = $image->getIterator()->getField($k); if (!$field) { throw new InvalidArgumentException(sprintf("Property %s does not exist in %s", $k, get_class($image))); } $sOrder .= ', ' . $field->getColumnName() . ($v ? '' : ' DESC'); } } $sOrder = $sOrder != '' ? 'ORDER BY ' . substr($sOrder, 2) : ''; } $result = $this->db->Execute("\n SELECT " . (isset($limit) ? 'SQL_CALC_FOUND_ROWS ' : '') . $image->fields('images') . "\n FROM " . $image->table('images') . "\n LEFT JOIN " . $os->table('os') . " ON {$os->columnId} = {$image->columnOsId}\n WHERE " . $image->_buildQuery($criteria, 'AND', 'images')['where'] . "\n " . (!empty($sOrder) ? $sOrder : "") . "\n " . (isset($limit) ? "LIMIT " . ($start ? intval($start) . ',' : '') . intval($limit) : "") . "\n "); if (isset($limit)) { $totalNumber = $this->db->getOne('SELECT FOUND_ROWS()'); } else { $totalNumber = $result->RowCount(); } $data = []; while ($rec = $result->FetchRow()) { $image = new Image(); $image->load($rec); $s = get_object_vars($image); $dtAdded = $image->getDtAdded(); $s['dtAdded'] = $dtAdded ? Scalr_Util_DateTime::convertTz($dtAdded) : ''; $s['dtLastUsed'] = $image->dtLastUsed ? Scalr_Util_DateTime::convertTz($image->dtLastUsed) : ''; $s['used'] = $image->getUsed($accountId, $envId); $s['software'] = $image->getSoftwareAsString(); $s['osFamily'] = $image->getOs()->family; $s['osGeneration'] = $image->getOs()->generation; $s['osVersion'] = $image->getOs()->version; $s['os'] = $image->getOs()->name; $s['scope'] = $image->getScope(); $data[] = $s; } $this->response->data(['total' => $totalNumber, 'data' => $data]); }
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']); } } } }
/** * Gets the Image Entity * * @return Image|null Returns the Image that corresponds to the Server */ public function getImage() { if (empty($this->_image) && !empty($this->roleId) && !empty($this->platform)) { $i = new Image(); $ri = new RoleImage(); $rec = $this->db()->GetRow("\n SELECT {$i->fields()}\n FROM {$i->table()}\n LEFT JOIN {$ri->table()} ON {$i->columnPlatform} = {$ri->columnPlatform}\n AND {$i->columnCloudLocation} = {$ri->columnCloudLocation}\n AND {$i->columnId} = {$ri->columnImageId}\n WHERE {$ri->columnRoleId} = ?\n AND {$ri->columnPlatform} = ?\n AND {$ri->columnCloudLocation} = ?\n AND ({$i->columnAccountId} IS NULL OR {$i->columnAccountId} = ?\n AND ({$i->columnEnvId} IS NULL OR {$i->columnEnvId} = ?)\n )\n ", [$this->roleId, $this->platform, in_array($this->platform, [SERVER_PLATFORMS::GCE, SERVER_PLATFORMS::AZURE]) ? '' : $this->cloudLocation, $this->getFarm()->accountId, $this->getFarm()->envId]); if ($rec) { $this->_image = $i; $this->_image->load($rec); } } return $this->_image; }