Since: 5.0 (08.05.2014)
Author: Igor Vodiasov (invar@scalr.com)
Example #1
0
 /**
  * @param string $query
  * @param string $platform
  * @param string $cloudLocation
  * @param JsonData $sort
  * @param int $start
  * @param int $limit
  * @throws Exception
  */
 public function xListAction($query = null, $platform = null, $cloudLocation = null, JsonData $sort, $start = 0, $limit = 20)
 {
     $this->request->restrictAccess(Acl::RESOURCE_FARMS_ROLES);
     $criteria = [];
     $criteria[] = ['envId' => $this->getEnvironmentId()];
     if ($query) {
         $querySql = '%' . $query . '%';
         $criteria[] = ['$or' => [['id' => ['$like' => $querySql]]]];
     }
     if ($platform) {
         $criteria[] = ['platform' => $platform];
     }
     if ($cloudLocation) {
         $criteria[] = ['cloudLocation' => $cloudLocation];
     }
     $result = Image::find($criteria, \Scalr\UI\Utils::convertOrder($sort, ['id' => 'ASC'], ['id', 'platform', 'cloudLocation', 'os', 'dtAdded', 'architecture', 'source']), $limit, $start, true);
     $data = [];
     foreach ($result as $image) {
         /* @var Image $image */
         $s = get_object_vars($image);
         $s['dtAdded'] = Scalr_Util_DateTime::convertTz($image->dtAdded);
         $s['status'] = $image->isUsed() ? 'In use' : 'Not used';
         $data[] = $s;
     }
     $this->response->data(['total' => $result->totalNumber, 'data' => $data]);
 }
Example #2
0
 /**
  * @param   string      $query
  * @param   string      $sshKeyId
  * @param   int         $farmId
  * @param   string      $platform
  * @param   string      $cloudLocation
  * @param   JsonData    $sort
  * @param   int         $start
  * @param   int         $limit
  */
 public function xListAction($query = null, $sshKeyId = null, $farmId = null, $platform = null, $cloudLocation = null, JsonData $sort, $start = 0, $limit = 20)
 {
     $criteria = [['envId' => $this->getEnvironmentId()]];
     if ($this->request->isAllowed(Acl::RESOURCE_FARMS, Acl::PERM_FARMS_SERVERS)) {
         if (!$this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) {
             $criteria[] = ['farmId' => ['$ne' => NULL]];
         }
     } else {
         $farmSql = "SELECT id FROM farms WHERE env_id = ?";
         $farmArgs = [$this->getEnvironmentId()];
         list($farmSql, $farmArgs) = $this->request->prepareFarmSqlQuery($farmSql, $farmArgs, '', Acl::PERM_FARMS_SERVERS);
         $farms = $this->db->GetCol($farmSql, $farmArgs);
         if ($this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) {
             $criteria[] = ['$or' => [['farmId' => NULL], ['farmId' => ['$in' => $farms]]]];
         } else {
             if (count($farms)) {
                 $criteria[] = ['farmId' => ['$in' => $farms]];
             } else {
                 // user doesn't have access to any farm. try to find better solution
                 $criteria[] = ['farmId' => -1];
             }
         }
     }
     if ($sshKeyId) {
         $criteria[] = ['id' => $sshKeyId];
     }
     if ($farmId) {
         $criteria[] = ['farmId' => $farmId];
     }
     if ($query) {
         $querySql = '%' . $query . '%';
         $criteria[] = ['$or' => [['cloudKeyName' => ['$like' => $querySql]]]];
     }
     if ($platform) {
         $criteria[] = ['platform' => $platform];
         if ($cloudLocation) {
             $criteria[] = ['cloudLocation' => $cloudLocation];
         }
     }
     $result = SshKey::find($criteria, null, \Scalr\UI\Utils::convertOrder($sort, ['id' => true], ['id', 'cloudKeyName', 'platform', 'cloudLocation']), $limit, $start, true);
     $data = [];
     foreach ($result as $key) {
         /* @var SshKey $key */
         $data[] = $this->getSshKeyObject($key);
     }
     $this->response->data(['total' => $result->totalNumber, 'data' => $data]);
 }
Example #3
0
 /**
  * @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]);
 }
Example #4
0
 /**
  * @param string $scriptId
  * @param string $query
  * @param string $scope
  * @param JsonData $sort
  * @param int $start
  * @param int $limit
  */
 public function xListAction($scriptId = null, $query = null, $scope = null, JsonData $sort, $start = 0, $limit = 20)
 {
     $this->request->restrictAccess('SCRIPTS');
     $criteria = [];
     if ($this->user->isScalrAdmin()) {
         $criteria[] = ['accountId' => NULL];
     } else {
         if ($scope == ScopeInterface::SCOPE_SCALR) {
             $criteria[] = ['accountId' => NULL];
         } else {
             if ($scope == ScopeInterface::SCOPE_ACCOUNT) {
                 $criteria[] = ['accountId' => $this->user->getAccountId()];
                 $criteria[] = ['envId' => NULL];
             } else {
                 if ($scope == ScopeInterface::SCOPE_ENVIRONMENT) {
                     $criteria[] = ['accountId' => $this->user->getAccountId()];
                     $criteria[] = ['envId' => $this->getEnvironmentId(true)];
                 } else {
                     $criteria[] = ['$or' => [['accountId' => $this->user->getAccountId()], ['accountId' => NULL]]];
                     if ($this->request->getScope() == ScopeInterface::SCOPE_ENVIRONMENT) {
                         $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId(true)], ['envId' => NULL]]];
                     } else {
                         $criteria[] = ['envId' => $this->getEnvironmentId(true)];
                     }
                 }
             }
         }
     }
     if ($query) {
         $querySql = '%' . $query . '%';
         $criteria[] = ['$or' => [['id' => ['$like' => $query]], ['name' => ['$like' => $querySql]], ['description' => ['$like' => $querySql]]]];
     }
     if ($scriptId) {
         $criteria[] = ['id' => $scriptId];
     }
     $result = Script::find($criteria, null, Utils::convertOrder($sort, ['name' => true], ['id', 'name', 'description', 'isSync', 'dtCreated', 'dtChanged']), $limit, $start, true);
     $data = [];
     foreach ($result as $script) {
         /* @var $script Script */
         $data[] = $this->getScript($script);
     }
     $this->response->data(['total' => $result->totalNumber, 'data' => $data]);
 }
Example #5
0
 /**
  * @param  JsonData  $sort   Sorting order, defaults to added desc
  * @param  string    $query  optional If set search in fields `id` (exact match), `title`, `msg` (partial match)
  * @param  int       $start  optional Offset
  * @param  int       $limit  optional Limit
  * @throws Exception
  * @throws Scalr_Exception_InsufficientPermissions
  */
 public function xListAction(JsonData $sort, $query = null, $start = 0, $limit = 25)
 {
     $this->request->restrictAccess(Acl::RESOURCE_ANNOUNCEMENTS);
     $criteria = [];
     $scope = $this->request->getScope();
     switch ($scope) {
         case ScopeInterface::SCOPE_SCALR:
             //scalr-scoped messages only
             $criteria[] = ['accountId' => null];
             break;
         case ScopeInterface::SCOPE_ACCOUNT:
             //account-related messages
             $criteria[] = ['$or' => [['accountId' => null], ['accountId' => $this->getUser()->accountId ?: null]]];
             break;
         default:
             throw new Scalr_Exception_InsufficientPermissions();
     }
     if ($query) {
         $queryLike = '%' . $query . '%';
         $criteria[] = ['$or' => [['id' => ['$like' => $query]], ['title' => ['$like' => $queryLike]], ['msg' => ['$like' => $queryLike]]]];
     }
     $result = Announcement::find($criteria, null, Utils::convertOrder($sort, ['added' => false], ['id', 'title', 'msg', 'added']), $limit, $start, true);
     $data = [];
     /* @var $announcement Scalr\Model\Entity\Announcement */
     foreach ($result as $announcement) {
         $data[] = $this->prepareDataForList($announcement);
     }
     $this->response->data(['total' => $result->totalNumber, 'data' => $data]);
 }
Example #6
0
 /**
  * @param int $farmId optional
  * @param int $roleId optional
  * @param string $scalrPageHash optional
  * @param string $scalrPageUiHash optional
  * @throws Scalr_Exception_InsufficientPermissions
  */
 public function xGetFarmAction($farmId = null, $roleId = null, $scalrPageHash = null, $scalrPageUiHash = null)
 {
     if ($scalrPageHash && $scalrPageHash != $this->calcFarmDesignerHash()) {
         $this->response->data(['scalrPageHashMismatch' => true]);
         return;
     }
     if ($scalrPageUiHash && $scalrPageUiHash != $this->response->pageUiHash()) {
         $this->response->data(['scalrPageUiHashMismatch' => true]);
         return;
     }
     $moduleParams = array('farmId' => $farmId, 'roleId' => $roleId, 'behaviors' => ROLE_BEHAVIORS::GetName(null, true));
     unset($moduleParams['behaviors'][ROLE_BEHAVIORS::CASSANDRA]);
     unset($moduleParams['behaviors'][ROLE_BEHAVIORS::CUSTOM]);
     unset($moduleParams['behaviors'][ROLE_BEHAVIORS::HAPROXY]);
     //platforms list
     $platforms = self::loadController('Platforms')->getEnabledPlatforms();
     if (empty($platforms)) {
         throw new Exception('Before building new farm you need to configure environment and setup cloud credentials');
     }
     $moduleParams['categories'] = self::loadController('Roles')->listRoleCategories(true, true);
     $moduleParams['farmVpcEc2Enabled'] = $this->getEnvironment()->isPlatformEnabled(SERVER_PLATFORMS::EC2);
     if ($moduleParams['farmVpcEc2Enabled']) {
         $moduleParams['farmVpcEc2Locations'] = self::loadController('Platforms')->getCloudLocations(SERVER_PLATFORMS::EC2, false);
     }
     if ($farmId) {
         $this->request->checkPermissions(DBFarm::LoadByID($farmId)->__getNewFarmObject(), Acl::PERM_FARMS_UPDATE);
         $c = self::loadController('Builder', 'Scalr_UI_Controller_Farms');
         $moduleParams['farm'] = $c->getFarm2($farmId);
     } else {
         $this->request->restrictAccess(Acl::RESOURCE_OWN_FARMS, Acl::PERM_FARMS_CREATE);
         // TODO: remove hack, do better
         $vars = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), ScopeInterface::SCOPE_FARM);
         $moduleParams['farmVariables'] = $vars->getValues();
     }
     $moduleParams['tabs'] = array('vpcrouter', 'dbmsr', 'mongodb', 'mysql', 'scaling', 'network', 'cloudfoundry', 'rabbitmq', 'haproxy', 'proxy', 'scripting', 'ec2', 'openstack', 'gce', 'azure', 'security', 'devel', 'storage', 'variables', 'advanced', 'chef');
     //deprecated tabs
     if (\Scalr::config('scalr.ui.show_deprecated_features')) {
         $moduleParams['tabs'][] = 'ebs';
     }
     $conf = $this->getContainer()->config->get('scalr.load_statistics.connections.plotter');
     $moduleParams['tabParams'] = array('farmId' => $farmId, 'farmHash' => $moduleParams['farm'] ? $moduleParams['farm']['farm']['hash'] : '', 'accountId' => $this->environment->keychain(SERVER_PLATFORMS::EC2)->properties[Entity\CloudCredentialsProperty::AWS_ACCOUNT_ID], 'remoteAddress' => $this->request->getRemoteAddr(), 'monitoringHostUrl' => "{$conf['scheme']}://{$conf['host']}:{$conf['port']}", 'nginx' => array('server_section' => file_get_contents(APPPATH . '/templates/services/nginx/server_section.tpl'), 'server_section_ssl' => file_get_contents(APPPATH . '/templates/services/nginx/server_section_ssl.tpl')));
     $moduleParams['tabParams']['scalr.instances_connection_policy'] = \Scalr::config('scalr.instances_connection_policy');
     $moduleParams['tabParams']['scalr.scalarizr_update.default_repo'] = \Scalr::config('scalr.scalarizr_update.default_repo');
     if (Scalr::isHostedScalr()) {
         $moduleParams['tabParams']['scalr.scalarizr_update.repos'] = ['latest' => Utils::getScalarizrUpdateRepoTitle('latest')];
         if ($this->user->getAccount()->priority == 100) {
             $moduleParams['tabParams']['scalr.scalarizr_update.repos']['stable'] = Utils::getScalarizrUpdateRepoTitle('stable');
         }
     } else {
         $repos = array_keys(\Scalr::config('scalr.scalarizr_update.repos'));
         $moduleParams['tabParams']['scalr.scalarizr_update.repos'] = array_combine($repos, $repos);
     }
     $moduleParams['tabParams']['scalr.scalarizr_update.devel_repos'] = is_array(\Scalr::config('scalr.scalarizr_update.devel_repos')) ? array_keys(\Scalr::config('scalr.scalarizr_update.devel_repos')) : [];
     $moduleParams['tabParams']['scalr.aws.ec2.limits.security_groups_per_instance'] = \Scalr::config('scalr.aws.ec2.limits.security_groups_per_instance');
     $moduleParams['metrics'] = Entity\ScalingMetric::getList($this->getEnvironmentId());
     $moduleParams['timezones_list'] = Scalr_Util_DateTime::getTimezones();
     $moduleParams['timezone_default'] = $this->user->getSetting(Scalr_Account_User::SETTING_UI_TIMEZONE);
     if ($moduleParams['farm']['farm']['ownerEditable']) {
         $moduleParams['usersList'] = [];
         foreach (Entity\Account\User::findByAccountId($this->user->getAccountId()) as $user) {
             $moduleParams['usersList'][] = ['id' => $user->id, 'email' => $user->email];
         }
     }
     $defaultFarmRoleSecurityGroups = array('default');
     if (\Scalr::config('scalr.aws.security_group_name')) {
         $defaultFarmRoleSecurityGroups[] = \Scalr::config('scalr.aws.security_group_name');
     }
     $moduleParams['roleDefaultSettings'] = array('base.keep_scripting_logs_time' => \Scalr::config('scalr.system.scripting.default_instance_log_rotation_period'), 'security_groups.list' => json_encode($defaultFarmRoleSecurityGroups), 'base.abort_init_on_script_fail' => \Scalr::config('scalr.system.scripting.default_abort_init_on_script_fail') ? 1 : 0, 'base.disable_firewall_management' => \Scalr::config('scalr.system.default_disable_firewall_management') ? 1 : 0);
     //cost analytics
     if ($this->getContainer()->analytics->enabled && $this->getEnvironment()->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID)) {
         $farmCostData = $this->getFarmCostData($farmId);
         $moduleParams['analytics'] = $farmCostData['analytics'];
         if ($farmId) {
             $moduleParams['farm']['farm']['projectId'] = $farmCostData['projectId'];
             $moduleParams['analytics']['farmCostMetering'] = $farmCostData['farmCostMetering'];
         }
     }
     $moduleParams['farmLaunchPermission'] = $farmId ? $moduleParams['farm']['farm']['launchPermission'] : $this->request->isAllowed([Acl::RESOURCE_FARMS, Acl::RESOURCE_TEAM_FARMS, Acl::RESOURCE_OWN_FARMS], Acl::PERM_FARMS_LAUNCH_TERMINATE);
     $this->response->data($moduleParams);
 }
Example #7
0
 /**
  * @param DBServer $dbServer
  * @param bool $cached check only cached information
  * @param int $timeout
  * @return array|null
  */
 public function getServerStatus(DBServer $dbServer, $cached = true, $timeout = 0)
 {
     if ($dbServer->status == SERVER_STATUS::RUNNING && ($dbServer->IsSupported('0.8') && $dbServer->osType == 'linux' || $dbServer->IsSupported('0.19') && $dbServer->osType == 'windows')) {
         if ($cached && !$dbServer->IsSupported('2.7.7')) {
             return ['status' => 'statusNoCache', 'error' => "<span style='color:gray;'>Scalarizr is checking actual status</span>"];
         }
         try {
             $scalarizr = $dbServer->scalarizrUpdateClient->getStatus($cached);
             try {
                 if ($dbServer->farmRoleId != 0) {
                     $scheduledOn = $dbServer->GetFarmRoleObject()->GetSetting('scheduled_on');
                 }
             } catch (Exception $e) {
             }
             $nextUpdate = null;
             if ($scalarizr->candidate && $scalarizr->installed != $scalarizr->candidate) {
                 $nextUpdate = ['candidate' => htmlspecialchars($scalarizr->candidate), 'scheduledOn' => $scheduledOn ? Scalr_Util_DateTime::convertTzFromUTC($scheduledOn) : null];
             }
             return ['status' => htmlspecialchars($scalarizr->service_status), 'version' => htmlspecialchars($scalarizr->installed), 'candidate' => htmlspecialchars($scalarizr->candidate), 'repository' => Scalr::isHostedScalr() ? Utils::getScalarizrUpdateRepoTitle($scalarizr->repository) : ucfirst(htmlspecialchars($scalarizr->repository)), 'lastUpdate' => ['date' => $scalarizr->executed_at ? Scalr_Util_DateTime::convertTzFromUTC($scalarizr->executed_at) : "", 'error' => nl2br(htmlspecialchars($scalarizr->error))], 'nextUpdate' => $nextUpdate, 'fullInfo' => $scalarizr];
         } catch (Exception $e) {
             if (stristr($e->getMessage(), "Method not found")) {
                 return ['status' => 'statusNotAvailable', 'error' => "<span style='color:red;'>Scalarizr status is not available, because scalr-upd-client installed on this server is too old.</span>"];
             } else {
                 return ['status' => 'statusNotAvailable', 'error' => "<span style='color:red;'>Scalarizr status is not available: {$e->getMessage()}</span>"];
             }
         }
     }
 }
Example #8
0
 /**
  * @param string $query
  * @param string $origin
  * @param JsonData $sort
  * @param int $start
  * @param int $limit
  */
 public function xListAction($query = null, $origin = null, JsonData $sort, $start = 0, $limit = 20)
 {
     $this->request->restrictAccess(Acl::RESOURCE_ADMINISTRATION_SCRIPTS);
     $criteria = [];
     if ($this->user->isScalrAdmin()) {
         $criteria[] = ['accountId' => NULL];
     } else {
         if ($origin == 'Shared') {
             $criteria[] = ['accountId' => NULL];
         } else {
             if ($origin == 'Custom') {
                 $criteria[] = ['accountId' => $this->user->getAccountId()];
                 $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId()], ['envId' => NULL]]];
             } else {
                 $criteria[] = ['$or' => [['accountId' => $this->user->getAccountId()], ['accountId' => NULL]]];
                 $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId()], ['envId' => NULL]]];
             }
         }
     }
     if ($query) {
         $querySql = '%' . $query . '%';
         $criteria[] = ['$or' => [['id' => ['$like' => $query]], ['name' => ['$like' => $querySql]], ['description' => ['$like' => $querySql]]]];
     }
     $result = Script::find($criteria, \Scalr\UI\Utils::convertOrder($sort, ['name' => 'ASC'], ['id', 'name', 'description', 'isSync', 'dtCreated', 'dtChanged']), $limit, $start, true);
     $data = [];
     foreach ($result as $script) {
         /* @var Script $script */
         $s = get_object_vars($script);
         $s['dtCreated'] = Scalr_Util_DateTime::convertTz($script->dtCreated);
         $s['dtChanged'] = Scalr_Util_DateTime::convertTz($script->dtChanged);
         $s['version'] = $script->getLatestVersion()->version;
         $data[] = $s;
     }
     $this->response->data(['total' => $result->totalNumber, 'data' => $data]);
 }
Example #9
0
 /**
  * @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 $os
  * @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->request->restrictAccess(Acl::RESOURCE_FARMS_IMAGES);
     $criteria = [];
     if ($hash) {
         $criteria[] = ['hash' => $hash];
         if ($this->getEnvironment()) {
             $enabledPlatforms = $this->getEnvironment()->getEnabledPlatforms();
             $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId(true)], ['$and' => [['envId' => NULL], ['platform' => empty($enabledPlatforms) ? NULL : ['$in' => $enabledPlatforms]]]]]];
         } else {
             $criteria[] = ['envId' => NULL];
         }
     } else {
         if ($query) {
             $querySql = '%' . $query . '%';
             $criteria[] = ['name' => ['$like' => $querySql]];
         }
         if ($platform) {
             $criteria[] = ['platform' => $platform];
         }
         if ($cloudLocation) {
             $criteria[] = ['cloudLocation' => $cloudLocation];
         }
         if ($this->getEnvironment()) {
             $enabledPlatforms = $this->getEnvironment()->getEnabledPlatforms();
             if ($scope) {
                 if ($scope == 'env') {
                     $criteria[] = ['envId' => $this->getEnvironmentId(true)];
                 } else {
                     // hide shared images, which platforms are not configured
                     if (empty($enabledPlatforms)) {
                         $criteria[] = ['platform' => NULL];
                     } else {
                         $criteria[] = ['envId' => NULL];
                         $criteria[] = ['platform' => ['$in' => $enabledPlatforms]];
                     }
                 }
             } else {
                 $criteria[] = ['$or' => [['envId' => $this->getEnvironmentId(true)], ['$and' => [['envId' => NULL], ['platform' => empty($enabledPlatforms) ? NULL : ['$in' => $enabledPlatforms]]]]]];
             }
         } else {
             $criteria[] = ['envId' => NULL];
         }
         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 (count($osIds) > 0) {
             $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];
         }
     }
     $result = Image::find($criteria, \Scalr\UI\Utils::convertOrder($sort, ['id' => 'ASC'], ['id', 'platform', 'cloudLocation', 'name', 'osId', 'dtAdded', 'architecture', 'createdByEmail', 'source']), $limit, $start, true);
     $data = [];
     foreach ($result as $image) {
         /* @var $image Image */
         $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($this->getEnvironmentId(true));
         $s['software'] = $image->getSoftwareAsString();
         $s['osFamily'] = $image->getOs()->family;
         $s['osGeneration'] = $image->getOs()->generation;
         $s['osVersion'] = $image->getOs()->version;
         $s['os'] = $image->getOs()->name;
         $data[] = $s;
     }
     $this->response->data(['total' => $result->totalNumber, 'data' => $data, 'debug' => $criteria]);
 }