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']);
             }
         }
     }
 }
Exemple #2
0
 /**
  * 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]);
 }