Beispiel #1
0
 /**
  * @param   int         $farmId
  * @param   int         $expirePeriod
  * @param   bool        $manageable
  * @param   string      $owner
  * @param   int         $chefServerId
  * @param   string      $projectId
  * @param   int         $status
  * @throws  Exception
  * @throws  Scalr_Exception_Core
  * @throws  Scalr_Exception_InsufficientPermissions
  */
 public function xListFarmsAction($farmId = null, $expirePeriod = null, $manageable = false, $owner = null, $chefServerId = null, $projectId = null, $status = null)
 {
     $this->request->restrictAccess([Acl::RESOURCE_FARMS, Acl::RESOURCE_TEAM_FARMS, Acl::RESOURCE_OWN_FARMS]);
     $governance = new Scalr_Governance($this->getEnvironmentId());
     $leaseStatus = $governance->isEnabled(Scalr_Governance::CATEGORY_GENERAL, Scalr_Governance::GENERAL_LEASE);
     $f = new Entity\Farm();
     $sql = "\n            SELECT {$f->fields('f')}, au.email AS ownerEmail,\n                (SELECT " . Entity\Farm::getUserTeamOwnershipSql($this->getUser()->id) . ") AS farmTeamIdPerm,\n                GROUP_CONCAT(DISTINCT at.name SEPARATOR ', ') AS farmTeams,\n                GROUP_CONCAT(DISTINCT behavior) AS behaviors,\n                (SELECT COUNT(*) FROM farm_roles WHERE farmid = f.id) AS rolesCnt,\n                (SELECT COUNT(*) FROM dns_zones WHERE farm_id = f.id) AS zonesCnt,\n                (SELECT COUNT(*) FROM server_alerts WHERE farm_id= f.id AND status = ?) AS alertsCnt,\n                (SELECT COUNT(*) FROM servers WHERE farm_id = f.id AND status IN (?,?,?,?,?)) AS runningServers,\n                (SELECT COUNT(*) FROM servers WHERE farm_id = f.id AND status IN (?,?)) AS suspendedServers,\n                (SELECT COUNT(*) FROM servers WHERE farm_id = f.id AND status IN (?,?)) AS nonRunningServers\n            FROM {$f->table('f')}\n            LEFT JOIN account_users au ON au.id = f.created_by_id\n            LEFT JOIN farm_teams ft ON ft.farm_id = {$f->columnId('f')}\n            LEFT JOIN account_teams at ON at.id = ft.team_id\n            LEFT JOIN farm_roles fr ON fr.farmid = {$f->columnId('f')}\n            LEFT JOIN role_behaviors rb ON rb.role_id = fr.role_id\n            WHERE {$f->columnEnvId('f')} = ?\n            AND :FILTER:\n            AND " . $this->request->getFarmSqlQuery($manageable ? Acl::PERM_FARMS_UPDATE : null);
     $args = [Entity\Server\Alert::STATUS_FAILED, Entity\Server::STATUS_PENDING, Entity\Server::STATUS_INIT, Entity\Server::STATUS_RUNNING, Entity\Server::STATUS_TEMPORARY, Entity\Server::STATUS_RESUMING, Entity\Server::STATUS_SUSPENDED, Entity\Server::STATUS_PENDING_SUSPEND, Entity\Server::STATUS_TERMINATED, Entity\Server::STATUS_PENDING_TERMINATE, $this->getEnvironmentId()];
     if ($leaseStatus && $expirePeriod) {
         $dt = new DateTime();
         $dt->add(new DateInterval('P' . $expirePeriod . 'D'));
         $sql .= " AND EXISTS (\n                        SELECT 1 FROM farm_settings\n                        WHERE farm_settings.farmid = f.id\n                        AND farm_settings.name = ?\n                        AND farm_settings.value != ''\n                        AND farm_settings.value < ?\n                    ) AND f.status = ?";
         $args[] = Entity\FarmSetting::LEASE_TERMINATE_DATE;
         $args[] = $dt->format('Y-m-d H:i:s');
         $args[] = Entity\Farm::STATUS_RUNNING;
     }
     if ($farmId) {
         $sql .= ' AND f.id = ?';
         $args[] = $farmId;
     }
     if ($owner) {
         if ($owner == 'me') {
             $sql .= " AND {$f->columnOwnerId('f')} = ?";
             $args[] = $this->getUser()->id;
         } else {
             if ($owner == 'team') {
                 $sql .= " AND " . Entity\Farm::getUserTeamOwnershipSql($this->getUser()->id);
             }
         }
     }
     if ($chefServerId) {
         $sql .= " AND f.id IN (\n                SELECT fr.farmid\n                FROM farm_roles fr\n                INNER JOIN farm_role_settings frs1 ON fr.id = frs1.farm_roleid AND frs1.name = ? AND frs1.value = ?\n                INNER JOIN farm_role_settings frs2 ON fr.id = frs2.farm_roleid AND frs2.name = ? AND frs2.value = ?\n            )";
         $args[] = \Scalr_Role_Behavior_Chef::ROLE_CHEF_SERVER_ID;
         $args[] = $chefServerId;
         $args[] = \Scalr_Role_Behavior_Chef::ROLE_CHEF_BOOTSTRAP;
         $args[] = 1;
     }
     if ($this->getContainer()->analytics->enabled && $projectId) {
         $sql .= " AND EXISTS (\n                SELECT 1 FROM farm_settings\n                WHERE farm_settings.farmid = f.id\n                AND farm_settings.name = " . $this->db->qstr(Entity\FarmSetting::PROJECT_ID) . "\n                AND farm_settings.value = ?) ";
         $args[] = $projectId;
     }
     $sql .= " GROUP BY {$f->columnId('f')}";
     $response = $this->buildResponseFromSql2($sql, array('id', 'name', 'dtadded', 'ownerEmail', 'status'), array('f.name', 'f.id', 'f.comments'), $args);
     foreach ($response["data"] as &$r) {
         $farm = new Entity\Farm();
         $farm->load($r);
         $row = get_object_vars($farm);
         $row['ownerEmail'] = $r['ownerEmail'];
         $row['farmTeams'] = $r['farmTeams'];
         $row['behaviors'] = $r['behaviors'];
         $row['rolesCnt'] = $r['rolesCnt'];
         $row['zonesCnt'] = $r['zonesCnt'];
         $row['alertsCnt'] = $r['alertsCnt'];
         $row['runningServers'] = $r['runningServers'];
         $row['suspendedServers'] = $r['suspendedServers'];
         $row['nonRunningServers'] = $r['nonRunningServers'];
         $row['added'] = Scalr_Util_DateTime::convertTz($farm->added);
         $row['lock'] = $farm->settings[Entity\FarmSetting::LOCK];
         if ($row['lock']) {
             try {
                 $farm->checkLocked();
             } catch (\Scalr\Exception\LockedException $e) {
                 $row['lockComment'] = $e->getMessage();
             }
         }
         if ($leaseStatus && $farm->settings[Entity\FarmSetting::LEASE_STATUS]) {
             $row['lease'] = $farm->settings[Entity\FarmSetting::LEASE_NOTIFICATION_SEND] ? 'Expire' : $farm->settings[Entity\FarmSetting::LEASE_STATUS];
             if ($row['lease'] == 'Expire') {
                 $dt = new DateTime();
                 $td = new DateTime($farm->settings[Entity\FarmSetting::LEASE_TERMINATE_DATE]);
                 $days = 0;
                 $hours = 1;
                 $interval = $dt->diff($td);
                 if ($interval) {
                     $days = $interval->days;
                     $hours = $interval->h ? $interval->h : 1;
                 }
                 $row['leaseMessage'] = sprintf('Your farm lease is about to expire in %d %s, after which this farm will be terminated', $days ? $days : $hours, $days ? $days > 1 ? 'days' : 'day' : ($hours > 1 ? 'hours' : 'hour'));
             }
         }
         $behaviors = explode(',', $row['behaviors']);
         $row["havemysqlrole"] = in_array(ROLE_BEHAVIORS::MYSQL, $behaviors);
         $row["havemysql2role"] = in_array(ROLE_BEHAVIORS::MYSQL2, $behaviors);
         $row["havepgrole"] = in_array(ROLE_BEHAVIORS::POSTGRESQL, $behaviors);
         $row["haveredisrole"] = in_array(ROLE_BEHAVIORS::REDIS, $behaviors);
         $row["haverabbitmqrole"] = in_array(ROLE_BEHAVIORS::RABBITMQ, $behaviors);
         $row["havemongodbrole"] = in_array(ROLE_BEHAVIORS::MONGODB, $behaviors);
         $row["haveperconarole"] = in_array(ROLE_BEHAVIORS::PERCONA, $behaviors);
         $row["havemariadbrole"] = in_array(ROLE_BEHAVIORS::MARIADB, $behaviors);
         $row['statusTxt'] = Entity\Farm::getStatusName($farm->status);
         if ($row['status'] == Entity\Farm::STATUS_RUNNING) {
             $row['shortcuts'] = [];
             foreach (\Scalr\Model\Entity\ScriptShortcut::find([['farmId' => $farm->id], ['farmRoleId' => null]]) as $shortcut) {
                 /* @var $shortcut \Scalr\Model\Entity\ScriptShortcut */
                 $row['shortcuts'][] = array('id' => $shortcut->id, 'name' => $shortcut->getScriptName());
             }
         }
         $row['farmOwnerIdPerm'] = $farm->ownerId && $this->getUser()->id == $farm->ownerId;
         $row['farmTeamIdPerm'] = !!$r['farmTeamIdPerm'];
         $r = $row;
     }
     $this->response->data($response);
 }