Author: N.V.
Inheritance: extends Scalr\Model\AbstractEntity, implements Scalr\DataType\AccessPermissionsInterface
Exemplo n.º 1
0
 /**
  * Fill information about Farm/FarmRole for each object based on cloudServerId.
  * cloudServerId could be empty or didn't exist in our database.
  *
  * @param   array[]   $data   Array of arrays
  */
 private function applyFarmRoleInfo(&$data)
 {
     $cloudServerIds = [];
     foreach ($data as $row) {
         if ($row['cloudServerId']) {
             $cloudServerIds[] = $row['cloudServerId'];
         }
     }
     if (empty($cloudServerIds)) {
         return;
     }
     $server = new Entity\Server();
     $history = new Entity\Server\History();
     $farm = new Entity\Farm();
     $farmRole = new Entity\FarmRole();
     $cloudServerIds = join(",", array_map(function ($serverId) {
         return $this->db->qstr($serverId);
     }, $cloudServerIds));
     $sql = "\n            SELECT {$farm->columnId} AS farmId, {$farm->columnName} AS farmName, {$farmRole->columnId} AS farmRoleId,\n                {$farmRole->columnAlias} AS farmRoleName, {$server->columnServerId} AS serverId, {$server->columnIndex} AS serverIndex,\n                {$history->columnCloudServerId} AS cloudServerId FROM {$server->table()}\n            JOIN {$history->table()} ON {$server->columnServerId} = {$history->columnServerId}\n            JOIN {$farm->table()} ON {$server->columnFarmId} = {$farm->columnId}\n            JOIN {$farmRole->table()} ON {$server->columnFarmRoleId} = {$farmRole->columnId}\n            WHERE {$server->columnEnvId} = ? AND {$history->columnCloudServerId} IN ({$cloudServerIds})\n        ";
     $result = [];
     foreach ($this->db->Execute($sql, [$this->getEnvironmentId()]) as $row) {
         $result[$row['cloudServerId']] = $row;
     }
     foreach ($data as &$row) {
         if (!empty($row['cloudServerId']) && !empty($result[$row['cloudServerId']])) {
             $row = array_merge($row, $result[$row['cloudServerId']]);
         }
     }
 }
Exemplo n.º 2
0
Arquivo: Logs.php Projeto: scalr/scalr
 /**
  * Returns prepared orchestration log data for response
  *
  * @param EntityIterator $logs  List of Orchestration Log objects
  * @return array
  */
 private function prepareOrchestrationLogData($logs)
 {
     $farmIds = [];
     $serverIds = [];
     $taskIds = [];
     $eventIds = [];
     $ids = [];
     foreach ($logs as $row) {
         /* @var $row OrchestrationLog */
         $farmIds[] = $row->farmId;
         $serverIds[] = $row->serverId;
         if ($row->eventServerId) {
             $serverIds[] = $row->eventServerId;
         }
         if ($row->taskId) {
             $taskIds[] = $row->taskId;
         }
         if ($row->eventId) {
             $eventIds[] = $row->eventId;
         }
         if ($row->type == OrchestrationLog::TYPE_MANUAL) {
             $ids[] = $row->id;
         }
     }
     if (!empty($farmIds)) {
         $farms = Farm::find([['id' => ['$in' => array_unique($farmIds)]]]);
         foreach ($farms as $farm) {
             /* @var $farm Farm */
             $farmData[$farm->id] = $farm->name;
         }
     }
     if (!empty($serverIds)) {
         $servers = Server::find([['serverId' => ['$in' => array_unique($serverIds)]]]);
         $farmRoleIds = [];
         $serverFarmIds = [];
         foreach ($servers as $server) {
             /* @var $server Server */
             $serverData[$server->serverId]['serverIndex'] = $server->index;
             $farmRoleIds[$server->serverId] = $server->farmRoleId;
             $serverFarmIds[$server->serverId] = $server->farmId;
         }
         $farms = Farm::find([['id' => ['$in' => array_unique(array_values($serverFarmIds))]]]);
         foreach ($farms as $farm) {
             /* @var $farm Farm */
             foreach ($serverFarmIds as $serverId => $farmId) {
                 if ($farmId == $farm->id) {
                     $serverData[$serverId]['farmName'] = $farm->name;
                     $serverData[$serverId]['farmId'] = $farm->id;
                 }
             }
         }
         $farmRoles = FarmRole::find([['id' => ['$in' => array_unique(array_values($farmRoleIds))]]]);
         foreach ($farmRoles as $farmRole) {
             /* @var $farmRole FarmRole */
             foreach ($farmRoleIds as $serverId => $farmRoleId) {
                 if ($farmRoleId == $farmRole->id) {
                     $serverData[$serverId]['alias'] = $farmRole->alias;
                     $serverData[$serverId]['farmRoleId'] = $farmRole->id;
                 }
             }
         }
     }
     if (!empty($taskIds)) {
         $tasks = SchedulerTask::find([['id' => ['$in' => array_unique($taskIds)]]]);
         foreach ($tasks as $task) {
             /* @var $task SchedulerTask */
             $taskData[$task->id] = $task->name;
         }
     }
     if (!empty($eventIds)) {
         $events = Event::find([['eventId' => ['$in' => array_unique($eventIds)]]]);
         foreach ($events as $event) {
             /* @var $event Event */
             $eventData[$event->eventId] = $event->type;
         }
     }
     if (!empty($ids)) {
         $manualLogs = OrchestrationLogManualScript::find([['orchestrationLogId' => ['$in' => array_unique($ids)]]]);
         foreach ($manualLogs as $manualLog) {
             /* @var $manualLog OrchestrationLogManualScript */
             $scriptData[$manualLog->orchestrationLogId] = $manualLog->userEmail;
         }
     }
     $data = [];
     foreach ($logs as $row) {
         /* @var $row OrchestrationLog */
         $dataRow = get_object_vars($row);
         $dataRow['targetFarmName'] = isset($farmData[$row->farmId]) ? $farmData[$row->farmId] : null;
         $dataRow['targetFarmId'] = $row->farmId;
         $dataRow['targetServerId'] = $row->serverId;
         $dataRow['targetServerIndex'] = isset($serverData[$row->serverId]['serverIndex']) ? $serverData[$row->serverId]['serverIndex'] : null;
         $dataRow['targetFarmRoleId'] = isset($serverData[$row->serverId]['farmRoleId']) ? $serverData[$row->serverId]['farmRoleId'] : null;
         $dataRow['targetRoleName'] = isset($serverData[$row->serverId]['alias']) ? $serverData[$row->serverId]['alias'] : null;
         $dataRow['added'] = Scalr_Util_DateTime::convertTz($row->added);
         if (\Scalr::config('scalr.system.scripting.logs_storage') == 'scalr') {
             $dataRow['executionId'] = null;
         }
         if ($dataRow['message']) {
             $dataRow['message'] = nl2br(htmlspecialchars($dataRow['message']));
         }
         if ($row->eventServerId) {
             $dataRow['eventFarmName'] = isset($serverData[$row->eventServerId]['farmName']) ? $serverData[$row->eventServerId]['farmName'] : null;
             $dataRow['eventFarmId'] = isset($serverData[$row->eventServerId]['farmId']) ? $serverData[$row->eventServerId]['farmId'] : null;
             $dataRow['eventFarmRoleId'] = isset($serverData[$row->eventServerId]['farmRoleId']) ? $serverData[$row->eventServerId]['farmRoleId'] : null;
             $dataRow['eventRoleName'] = isset($serverData[$row->eventServerId]['alias']) ? $serverData[$row->eventServerId]['alias'] : null;
             $dataRow['eventServerIndex'] = isset($serverData[$row->eventServerId]['serverIndex']) ? $serverData[$row->eventServerId]['serverIndex'] : null;
         }
         $dataRow['event'] = null;
         if ($row->taskId) {
             $dataRow['event'] = isset($taskData[$row->taskId]) ? $taskData[$row->taskId] : null;
         }
         if ($row->eventId) {
             $dataRow['event'] = isset($eventData[$row->eventId]) ? $eventData[$row->eventId] : null;
         }
         if ($row->type == OrchestrationLog::TYPE_MANUAL) {
             $dataRow['event'] = isset($scriptData[$row->id]) ? $scriptData[$row->id] : null;
         }
         $data[] = $dataRow;
     }
     return $data;
 }
Exemplo n.º 3
0
 /**
  * Detach configured cloud configuration from specified environment
  *
  * @param   int     $envId  Environment ID
  * @param   string  $cloud  Cloud platform name
  *
  * @return  ResultEnvelope
  *
  * @throws  ApiErrorException
  * @throws  ModelException
  */
 public function detachCredentialsAction($envId, $cloud)
 {
     if (!$this->getUser()->canManageAcl()) {
         $this->checkPermissions(Acl::RESOURCE_ENV_CLOUDS_ENVIRONMENT);
     }
     $env = $this->getEnv($envId);
     $cloudCredentials = $env->keychain($cloud);
     if (empty($cloudCredentials->id)) {
         throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Cloud '{$cloud}' not configured for this environment");
     }
     if (in_array($cloudCredentials->cloud, [SERVER_PLATFORMS::EC2, SERVER_PLATFORMS::GCE]) && (count(Entity\Server::find([['envId' => $envId], ['platform' => $cloudCredentials->cloud]])) || count(Entity\Image::find([['envId' => $envId], ['platform' => $cloudCredentials->cloud]])))) {
         throw new ApiErrorException(409, ErrorMessage::ERR_OBJECT_IN_USE, "Cloud Credentials are used");
     }
     $cloudCredentials->environments[$envId]->delete();
     return $this->result(null);
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  * @see AbstractEntity::delete()
  */
 public function delete()
 {
     if ($this->status != FARM_STATUS::TERMINATED) {
         throw new FarmInUseException("Cannot delete a running farm, please terminate a farm before deleting it");
     }
     $servers = Server::find([['farmId' => $this->id], ['status' => ['$ne' => Server::STATUS_TERMINATED]]]);
     if (count($servers)) {
         throw new FarmInUseException(sprintf("Cannot delete a running farm, %s server%s still running on this farm", count($servers), count($servers) > 1 ? 's are' : ' is'));
     }
     $db = $this->db();
     try {
         $db->BeginTrans();
         foreach ($this->farmRoles as $farmRole) {
             $farmRole->delete();
         }
         $this->deleteScheduled();
         $db->Execute("DELETE FROM `logentries` WHERE `farmid` = ?", [$this->id]);
         $db->Execute("DELETE FROM `elastic_ips` WHERE `farmid` = ?", [$this->id]);
         $db->Execute("DELETE FROM `events` WHERE `farmid` = ?", [$this->id]);
         $db->Execute("DELETE FROM `ec2_ebs` WHERE `farm_id` = ?", [$this->id]);
         $db->Execute("DELETE FROM `farm_lease_requests` WHERE `farm_id` = ?", [$this->id]);
         foreach ($this->servers as $server) {
             $server->delete();
         }
         $db->Execute("UPDATE `dns_zones` SET `farm_id` = '0', `farm_roleid` ='0' WHERE `farm_id` = ?", [$this->id]);
         $db->Execute("UPDATE `apache_vhosts` SET `farm_id` = '0', `farm_roleid` ='0' WHERE `farm_id` = ?", [$this->id]);
         parent::delete();
         $db->CommitTrans();
     } catch (Exception $e) {
         $db->RollbackTrans();
         throw $e;
     }
     $db->Execute("DELETE FROM `scripting_log` WHERE `farmid` = ?", [$this->id]);
 }
Exemplo n.º 5
0
 /**
  * @test
  * @functional
  */
 public function testComplex()
 {
     /* @var $farm Farm */
     $farm = $this->createTestFarm('server', ['base-ubuntu1404']);
     /* @var $farmRole FarmRole */
     $farmRole = $farm->farmRoles->current();
     $server = null;
     static::toDelete(Farm::class, [$farm->id]);
     $uri = self::getUserApiUrl("/farms/{$farm->id}/actions/launch");
     $this->request($uri, Request::METHOD_POST);
     for ($time = time(), $sleep = 50; time() - $time < 400 && (empty($server) || $server->status != Server::STATUS_RUNNING); $sleep += 50) {
         sleep($sleep);
         $testServers = $this->listServers(['farmId' => $farm->id]);
         if (count($testServers) > 0) {
             $server = reset($testServers);
             /* @var $server Server */
             continue;
         }
     }
     $this->assertNotEmpty($server);
     $this->assertEquals($server->status, 'running');
     $testDescribe = [['farmId' => $farm->id], ['farmRoleId' => $farmRole->id], ['serverId' => 'all']];
     // testing describe and fetch action
     foreach ($testDescribe as $value) {
         $servers = $this->listServers($value);
         $serverAdapter = $this->getAdapter('server');
         $filterable = $serverAdapter->getRules()[ApiEntityAdapter::RULE_TYPE_FILTERABLE];
         foreach ($servers as $server) {
             /* @var $server Server */
             foreach ($filterable as $property) {
                 $filterValue = $server->{$property};
                 $listResult = $this->listServers($value, [$property => $filterValue]);
                 if (!static::isRecursivelyEmpty($filterValue)) {
                     foreach ($listResult as $filtered) {
                         $this->assertEquals($filterValue, $filtered->{$property}, "Property '{$property}' mismatch");
                     }
                 }
             }
             $response = $this->getServer($server->id);
             $this->assertEquals(200, $response->status, $this->printResponseError($response));
             $dbServer = Server::findPk($server->id);
             $this->assertObjectEqualsEntity($response->getBody()->data, $dbServer, $serverAdapter);
         }
     }
     $entity = Server::findPk($server->id);
     /* @var $entity Server */
     $DBServerObject = $entity->__getDBServer();
     $oldModel = DBServer::LoadByID($server->id);
     $this->assertEquals($DBServerObject, $oldModel);
     $serverEntity = (new Server())->__getEntityFromDBServer($oldModel);
     $this->assertEquals($entity, $serverEntity);
     // testing reboot action
     $response = $this->rebootServer($server->id, true);
     $this->assertEquals(200, $response->status, $this->printResponseError($response));
     $data = $response->getBody()->data;
     $this->assertEquals($server->id, $data->id);
     $propertyReboot = ServerProperty::findOne([['serverId' => $server->id], ['name' => SERVER_PROPERTIES::REBOOTING]]);
     /* @var $propertyReboot ServerProperty */
     for ($time = time(), $sleep = 50; time() - $time < 300 && (!$propertyReboot || $propertyReboot->value); $sleep += 50) {
         $propertyReboot = ServerProperty::findOne([['serverId' => $server->id], ['name' => SERVER_PROPERTIES::REBOOTING]]);
     }
     $server = $this->waitForChanges($server->id, Server::STATUS_RUNNING);
     // testing suspend action
     $response = $this->suspendServer($server->id);
     $this->assertEquals(200, $response->status, $this->printResponseError($response));
     $data = $response->getBody()->data;
     $this->assertEquals($server->id, $data->id);
     $this->assertEquals($data->status, 'pending_suspend');
     $server = $this->waitForChanges($server->id, Server::STATUS_SUSPENDED, 600);
     // testing resume action
     $sleep = 0;
     do {
         try {
             $response = $this->resumeServer($server->id);
             if ($response->status == 200) {
                 break;
             }
         } catch (Exception $e) {
             if (strpos($e->getMessage(), "is not in a state from which it can be started") !== false) {
                 $sleep += 60;
                 sleep(60);
             } else {
                 throw $e;
             }
         }
     } while ($sleep < 300);
     $this->assertEquals(200, $response->status, $this->printResponseError($response));
     $data = $response->getBody()->data;
     $this->assertEquals($server->id, $data->id);
     $this->assertEquals($data->status, 'resuming');
     $server = $this->waitForChanges($server->id, Server::STATUS_RUNNING);
 }
Exemplo n.º 6
0
 /**
  * Launches server
  *
  * @param   \ServerCreateInfo       $ServerCreateInfo optional The server create info
  * @param   \DBServer               $DBServer         optional The DBServer object
  * @param   bool                    $delayed          optional
  * @param   integer|array            $reason           optional
  * @param   \Scalr_Account_User|int $user             optional The Scalr_Account_User object or its unique identifier
  * @return  DBServer|null           Returns the DBServer object on cussess or null otherwise
  */
 public static function LaunchServer(ServerCreateInfo $ServerCreateInfo = null, DBServer $DBServer = null, $delayed = false, $reason = 0, $user = null)
 {
     $db = self::getDb();
     $farm = null;
     //Ensures handling identifier of the user instead of the object
     if ($user !== null && !$user instanceof \Scalr_Account_User) {
         try {
             $user = Scalr_Account_User::init()->loadById(intval($user));
         } catch (\Exception $e) {
         }
     }
     if (!$DBServer && $ServerCreateInfo) {
         $ServerCreateInfo->SetProperties(array(SERVER_PROPERTIES::SZR_KEY => self::GenerateRandomKey(40), SERVER_PROPERTIES::SZR_KEY_TYPE => SZR_KEY_TYPE::ONE_TIME));
         $DBServer = DBServer::Create($ServerCreateInfo, false, true);
     } elseif (!$DBServer && !$ServerCreateInfo) {
         // incorrect arguments
         self::getContainer()->logger(LOG_CATEGORY::FARM)->error(sprintf("Cannot create server"));
         return null;
     } else {
         if ($DBServer && empty($DBServer->cloudLocation)) {
             trigger_error('Cloud location is missing in DBServer', E_USER_WARNING);
         }
     }
     $propsToSet = array();
     if ($user instanceof \Scalr_Account_User) {
         $propsToSet[SERVER_PROPERTIES::LAUNCHED_BY_ID] = $user->id;
         $propsToSet[SERVER_PROPERTIES::LAUNCHED_BY_EMAIL] = $user->getEmail();
     }
     //We should keep role_id and farm_role_id in server properties to use in cost analytics
     if (!empty($DBServer->farmRoleId)) {
         $propsToSet[SERVER_PROPERTIES::FARM_ROLE_ID] = $DBServer->farmRoleId;
         $propsToSet[SERVER_PROPERTIES::ROLE_ID] = $DBServer->farmRoleId ? $DBServer->GetFarmRoleObject()->RoleID : 0;
     }
     try {
         // Ensures the farm object will be fetched as correctly as possible
         $farm = $DBServer->farmId ? $DBServer->GetFarmObject() : null;
         $farmRole = $DBServer->farmRoleId ? $DBServer->GetFarmRoleObject() : null;
         if (!$farmRole instanceof DBFarmRole) {
             $farmRole = null;
         } else {
             if (!$farm instanceof DBFarm) {
                 // Gets farm through FarmRole object in this case
                 $farm = $farmRole->GetFarmObject();
             }
         }
         if ($farm instanceof DBFarm) {
             $propsToSet[SERVER_PROPERTIES::FARM_CREATED_BY_ID] = $farm->ownerId ?: $farm->GetSetting(Entity\FarmSetting::CREATED_BY_ID);
             $propsToSet[SERVER_PROPERTIES::FARM_CREATED_BY_EMAIL] = $farm->ownerId ? Entity\Account\User::findPk($farm->ownerId)->email : $farm->GetSetting(Entity\FarmSetting::CREATED_BY_EMAIL);
             $projectId = $farm->GetSetting(Entity\FarmSetting::PROJECT_ID);
             if (!empty($projectId)) {
                 try {
                     $projectEntity = ProjectEntity::findPk($projectId);
                     if ($projectEntity instanceof ProjectEntity) {
                         /* @var $projectEntity ProjectEntity */
                         $ccId = $projectEntity->ccId;
                     } else {
                         $projectId = null;
                     }
                 } catch (Exception $e) {
                     $projectId = null;
                 }
             }
             $propsToSet[SERVER_PROPERTIES::FARM_PROJECT_ID] = $projectId;
         }
         if ($farmRole instanceof DBFarmRole) {
             $role = $farmRole->GetRoleObject();
             $DBServer->isScalarized = $role->isScalarized;
             if (!$DBServer->isScalarized) {
                 $propsToSet[SERVER_PROPERTIES::SZR_VESION] = '';
             }
             $DBServer->Save();
         }
         if (!empty($ccId)) {
             $propsToSet[SERVER_PROPERTIES::ENV_CC_ID] = $ccId;
         } elseif ($DBServer->envId && ($environment = $DBServer->GetEnvironmentObject()) instanceof Scalr_Environment) {
             $propsToSet[SERVER_PROPERTIES::ENV_CC_ID] = $environment->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID);
         }
     } catch (Exception $e) {
         self::getContainer()->logger(LOG_CATEGORY::FARM)->error(sprintf("Could not load related object for recently created server %s. It says: %s", $DBServer->serverId, $e->getMessage()));
     }
     if (!empty($propsToSet)) {
         $DBServer->SetProperties($propsToSet);
     }
     $fnGetReason = function ($reasonId) {
         $args = func_get_args();
         $args[0] = DBServer::getLaunchReason($reasonId);
         return [call_user_func_array('sprintf', $args), $reasonId];
     };
     if ($reason) {
         list($reasonMsg, $reasonId) = is_array($reason) ? call_user_func_array($fnGetReason, $reason) : $fnGetReason($reason);
         $DBServer->SetProperties([SERVER_PROPERTIES::LAUNCH_REASON => $reasonMsg, SERVER_PROPERTIES::LAUNCH_REASON_ID => $reasonId]);
     } else {
         $reasonMsg = $DBServer->GetProperty(SERVER_PROPERTIES::LAUNCH_REASON);
         $reasonId = $DBServer->GetProperty(SERVER_PROPERTIES::LAUNCH_REASON_ID);
     }
     if ($delayed) {
         $DBServer->updateStatus(SERVER_STATUS::PENDING_LAUNCH);
         return $DBServer;
     }
     if ($ServerCreateInfo && $ServerCreateInfo->roleId) {
         $dbRole = DBRole::loadById($ServerCreateInfo->roleId);
         if ($dbRole->generation == 1) {
             $DBServer->updateStatus(SERVER_STATUS::PENDING_LAUNCH);
             $DBServer->SetProperties([SERVER_PROPERTIES::LAUNCH_ERROR => "ami-scripts servers no longer supported", SERVER_PROPERTIES::LAUNCH_ATTEMPT => $DBServer->GetProperty(SERVER_PROPERTIES::LAUNCH_ATTEMPT) + 1, SERVER_PROPERTIES::LAUNCH_LAST_TRY => (new DateTime())->format('Y-m-d H:i:s')]);
             return $DBServer;
         }
     }
     // Limit amount of pending servers
     if ($DBServer->isOpenstack()) {
         $config = self::getContainer()->config;
         if ($config->defined("scalr.{$DBServer->platform}.pending_servers_limit")) {
             $pendingServersLimit = $config->get("scalr.{$DBServer->platform}.pending_servers_limit");
             $pendingServers = $db->GetOne("SELECT COUNT(*) FROM servers WHERE platform=? AND status=? AND server_id != ?", array($DBServer->platform, SERVER_STATUS::PENDING, $DBServer->serverId));
             if ($pendingServers >= $pendingServersLimit) {
                 self::getContainer()->logger("SERVER_LAUNCH")->warn("{$pendingServers} servers in PENDING state on {$DBServer->platform}. Limit is: {$pendingServersLimit}. Waiting.");
                 $DBServer->updateStatus(SERVER_STATUS::PENDING_LAUNCH);
                 $DBServer->SetProperties([SERVER_PROPERTIES::LAUNCH_ATTEMPT => $DBServer->GetProperty(SERVER_PROPERTIES::LAUNCH_ATTEMPT) + 1, SERVER_PROPERTIES::LAUNCH_LAST_TRY => (new DateTime())->format('Y-m-d H:i:s')]);
                 return $DBServer;
             } else {
                 self::getContainer()->logger("SERVER_LAUNCH")->warn("{$pendingServers} servers in PENDING state on {$DBServer->platform}. Limit is: {$pendingServersLimit}. Launching server.");
             }
         }
     }
     try {
         $account = Scalr_Account::init()->loadById($DBServer->clientId);
         $account->validateLimit(Scalr_Limits::ACCOUNT_SERVERS, 1);
         PlatformFactory::NewPlatform($DBServer->platform)->LaunchServer($DBServer);
         $DBServer->status = SERVER_STATUS::PENDING;
         $DBServer->Save();
         try {
             $DBServer->getServerHistory()->markAsLaunched($reasonMsg, $reasonId);
             $DBServer->updateTimelog('ts_launched');
             if ($DBServer->imageId) {
                 //Update Image last used date
                 /* @var $server Entity\Server */
                 $server = Entity\Server::findPk($DBServer->serverId);
                 $image = $server->getImage();
                 if ($image) {
                     $image->update(['dtLastUsed' => new DateTime()]);
                 }
                 if ($server->farmRoleId && empty($server->getFarmRole())) {
                     trigger_error(sprintf("Call to a member function getRole() on null. Server: %s, FarmRole: %d", $server->serverId, $server->farmRoleId), E_USER_WARNING);
                 }
                 if ($server->farmRoleId && !empty($server->getFarmRole()->getRole())) {
                     //Update Role last used date
                     $server->getFarmRole()->getRole()->update(['lastUsed' => new DateTime()]);
                 }
             }
         } catch (Exception $e) {
             self::getContainer()->logger('SERVER_HISTORY')->error(sprintf("Cannot update servers history: {$e->getMessage()}"));
         }
     } catch (Exception $e) {
         self::getContainer()->logger(LOG_CATEGORY::FARM)->error(new FarmLogMessage($DBServer, sprintf("Cannot launch server on '%s' platform: %s", !empty($DBServer->platform) ? $DBServer->platform : null, $e->getMessage())));
         $existingLaunchError = $DBServer->GetProperty(SERVER_PROPERTIES::LAUNCH_ERROR);
         $DBServer->SetProperties([SERVER_PROPERTIES::LAUNCH_ERROR => $e->getMessage(), SERVER_PROPERTIES::LAUNCH_ATTEMPT => $DBServer->GetProperty(SERVER_PROPERTIES::LAUNCH_ATTEMPT) + 1, SERVER_PROPERTIES::LAUNCH_LAST_TRY => (new DateTime())->format('Y-m-d H:i:s')]);
         $DBServer->updateStatus(SERVER_STATUS::PENDING_LAUNCH);
         if ($DBServer->farmId && !$existingLaunchError) {
             self::FireEvent($DBServer->farmId, new InstanceLaunchFailedEvent($DBServer, $e->getMessage()));
         }
     }
     if ($DBServer->status == SERVER_STATUS::PENDING) {
         self::FireEvent($DBServer->farmId, new BeforeInstanceLaunchEvent($DBServer));
         $DBServer->SetProperty(SERVER_PROPERTIES::LAUNCH_ERROR, "");
     }
     return $DBServer;
 }
Exemplo n.º 7
0
 /**
  * Terminates servers used this farm role
  */
 public function terminateServers()
 {
     /* @var $server Server */
     foreach (Server::findByFarmRoleId($this->id) as $server) {
         $DBServer = \DBServer::LoadByID($server->serverId);
         $DBServer->terminate(DBServer::TERMINATE_REASON_ROLE_REMOVED);
         $event = new HostDownEvent($DBServer);
         Scalr::FireEvent($DBServer->farmId, $event);
     }
 }
Exemplo n.º 8
0
 /**
  * Check if given server exists, is allowed for current environment and has farm, farmRole, role
  *
  * @param   string  $serverId
  * @return  Server
  * @throws  Exception
  */
 public function getServerEntity($serverId)
 {
     /* @var $server Server */
     if (!$serverId || !($server = Server::findPk($serverId))) {
         throw new Exception('Server not found');
     }
     if (empty($server->getFarm())) {
         throw new Exception("Farm was not found for this Server");
     }
     if (empty($server->getFarmRole())) {
         throw new Exception("FarmRole was not found for this Server");
     }
     if (empty($server->getFarmRole()->getRole())) {
         throw new Exception("Role was not found for this Server");
     }
     return $server;
 }
Exemplo n.º 9
0
 /**
  * Gets specified Server
  *
  * @param   string      $serverId           UUID of the server
  *
  * @return  Server    Returns the Server Entity on success
  * @throws  ApiErrorException
  */
 public function getServer($serverId)
 {
     $criteria = [['serverId' => $serverId]];
     $server = Server::findOne(array_merge($this->getDefaultCriteria(), $criteria));
     /* @var $server Server */
     if (!$server) {
         throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Requested Server either does not exist or is not owned by your environment.");
     }
     if (!$this->hasPermissions($server)) {
         //Checks entity level write access permissions
         throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
     }
     return $server;
 }
Exemplo n.º 10
0
 /**
  * @return Image
  */
 public function createImageEntity()
 {
     $snapshot = $this->getSnapshotDetails();
     $envId = $this->envId;
     /* @var Entity\Server $server */
     $server = Entity\Server::findOneByServerId($this->serverId);
     if (!empty($server->farmRoleId)) {
         /* @var Entity\FarmRole $farmRole */
         $farmRole = Entity\FarmRole::findPk($server->farmRoleId);
         if (!empty($farmRole->roleId)) {
             /* @var Entity\Role $role */
             $role = Entity\Role::findPk($farmRole->roleId);
             $envId = $role->getScope() == ScopeInterface::SCOPE_ACCOUNT ? NULL : $envId;
         }
     }
     $image = new Image();
     $image->id = $this->snapshotId;
     $image->accountId = $this->clientId;
     $image->envId = $envId;
     $image->bundleTaskId = $this->id;
     $image->platform = $this->platform;
     $image->cloudLocation = $this->cloudLocation;
     $image->createdById = $this->createdById;
     $image->createdByEmail = $this->createdByEmail;
     $image->architecture = is_null($snapshot['os']->arch) ? 'x86_64' : $snapshot['os']->arch;
     $image->source = Image::SOURCE_BUNDLE_TASK;
     $image->status = Image::STATUS_ACTIVE;
     $image->agentVersion = $snapshot['szr_version'];
     $image->checkImage();
     if (!$image->name) {
         $image->name = $this->roleName . '-' . date('YmdHi');
     }
     $image->osId = $this->osId;
     $image->save();
     if ($snapshot['software']) {
         $software = [];
         foreach ((array) $snapshot['software'] as $soft) {
             $software[$soft->name] = $soft->version;
         }
         $image->setSoftware($software);
     }
     return $image;
 }
Exemplo n.º 11
0
 /**
  * Helper for the server lister
  *
  * @param array $response Reference to a response array
  */
 private function listServersResponseHelper(&$response)
 {
     if (empty($response["data"])) {
         return;
     }
     $serverIds = [];
     $farmRoles = [];
     $userBelongsToTeam = [];
     foreach ($response["data"] as $idx => $row) {
         $serverIds[$row["server_id"]][$idx] = [];
         $farmRoles[$row["farm_roleid"]][$idx] = [];
     }
     $neededServerProperties = [\GCE_SERVER_PROPERTIES::SERVER_NAME => "cloud_server_id", \OPENSTACK_SERVER_PROPERTIES::SERVER_ID => "cloud_server_id", \CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID => "cloud_server_id", \EC2_SERVER_PROPERTIES::INSTANCE_ID => "cloud_server_id", \RACKSPACE_SERVER_PROPERTIES::SERVER_ID => "cloud_server_id", \AZURE_SERVER_PROPERTIES::SERVER_NAME => "cloud_server_id", \Scalr_Role_Behavior::SERVER_BASE_HOSTNAME => "hostname", Entity\Server::DB_MYSQL_MASTER => "cluster_role", \Scalr_Db_Msr::REPLICATION_MASTER => "cluster_role", \EC2_SERVER_PROPERTIES::AVAIL_ZONE => "avail_zone", \EC2_SERVER_PROPERTIES::REGION => "cloud_location", \OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => "cloud_location", \CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => "cloud_location", \GCE_SERVER_PROPERTIES::CLOUD_LOCATION => "cloud_location", \RACKSPACE_SERVER_PROPERTIES::DATACENTER => "cloud_location", \AZURE_SERVER_PROPERTIES::CLOUD_LOCATION => "cloud_location", \Scalr_Role_Behavior_MongoDB::SERVER_SHARD_INDEX => "cluster_position", \Scalr_Role_Behavior_MongoDB::SERVER_REPLICA_SET_INDEX => "cluster_position", Entity\Server::REBOOTING => "status", Entity\Server::MISSING => "status", \EC2_SERVER_PROPERTIES::IS_LOCKED => "is_locked", Entity\Server::SZR_VESION => "szr_version", Entity\Server::SZR_IS_INIT_FAILED => "isInitFailed", Entity\Server::LAUNCH_ERROR => "launch_error", Entity\Server::EXCLUDE_FROM_DNS => "excluded_from_dns"];
     $neededFarmRoleSettings = [\Scalr_Db_Msr::SLAVE_TO_MASTER, Entity\FarmRoleSetting::MYSQL_SLAVE_TO_MASTER, Entity\FarmRoleSetting::EXCLUDE_FROM_DNS, Entity\FarmRoleSetting::SCALING_ENABLED];
     // get necessary properties
     foreach (Entity\Server\Property::fetch(array_keys($serverIds), array_keys($neededServerProperties)) as $prop) {
         foreach ($serverIds[$prop->serverId] as $idx => &$upd) {
             $upd[$prop->name] = $prop->value;
         }
     }
     // get farm role settings
     foreach (Entity\FarmRoleSetting::fetch(array_keys($farmRoles), $neededFarmRoleSettings) as $prop) {
         foreach ($farmRoles[$prop->farmRoleId] as $idx => &$upd) {
             $upd[$prop->name] = $prop->value;
         }
     }
     // check elastic IP existence
     foreach (Entity\Server\ElasticIp::checkPresenceOfPublicIP(array_keys($serverIds)) as $resRow) {
         foreach ($serverIds[$resRow["server_id"]] as $idx => &$upd) {
             $upd["extIp"] = $resRow["ipc"];
         }
     }
     // check alerts
     foreach (Entity\Server\Alert::checkPresenceOfAlerts(array_keys($serverIds)) as $resRow) {
         foreach ($serverIds[$resRow["server_id"]] as $idx => &$upd) {
             $upd["alerts"] = $resRow["alerts"];
         }
     }
     foreach ($response["data"] as $idx => &$row) {
         $status = $row["status"];
         $behaviors = explode(",", $row["behaviors"]);
         $row["hostname"] = $row['cluster_role'] = $row["alerts"] = "";
         $cloudServerIds = [];
         foreach ($this->propertyFilter($neededServerProperties, "cloud_server_id") as $prop => $key) {
             if (array_key_exists($prop, $serverIds[$row["server_id"]][$idx])) {
                 $cloudServerIds[] = $serverIds[$row["server_id"]][$idx][$prop];
             }
         }
         $row['cloud_server_id'] = empty($cloudServerIds) ? "" : $cloudServerIds[0];
         if (array_key_exists(\Scalr_Role_Behavior::SERVER_BASE_HOSTNAME, $serverIds[$row["server_id"]][$idx])) {
             $row["hostname"] = $serverIds[$row["server_id"]][$idx][\Scalr_Role_Behavior::SERVER_BASE_HOSTNAME];
         }
         $row['flavor'] = $row['type'];
         if (in_array($status, [Entity\Server::STATUS_RUNNING, Entity\Server::STATUS_INIT])) {
             $hasDbBehavior = array_intersect([\ROLE_BEHAVIORS::REDIS, \ROLE_BEHAVIORS::POSTGRESQL, \ROLE_BEHAVIORS::MYSQL, \ROLE_BEHAVIORS::MYSQL2, \ROLE_BEHAVIORS::PERCONA, \ROLE_BEHAVIORS::MARIADB], $behaviors);
             if (!empty($hasDbBehavior)) {
                 $isMaster = false;
                 foreach ($this->propertyFilter($neededServerProperties, "cluster_role") as $prop => $key) {
                     if (array_key_exists($prop, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][$prop] != 0) {
                         $isMaster = true;
                         break;
                     }
                 }
                 $row['cluster_role'] = $isMaster ? 'Master' : 'Slave';
                 if ($isMaster && (array_key_exists(\Scalr_Db_Msr::SLAVE_TO_MASTER, $farmRoles[$row["farm_roleid"]][$idx]) && $farmRoles[$row["farm_roleid"]][$idx][\Scalr_Db_Msr::SLAVE_TO_MASTER] == 1) || array_key_exists(Entity\FarmRoleSetting::MYSQL_SLAVE_TO_MASTER, $farmRoles[$row["farm_roleid"]][$idx]) && $farmRoles[$row["farm_roleid"]][$idx][Entity\FarmRoleSetting::MYSQL_SLAVE_TO_MASTER] == 1) {
                     $row['cluster_role'] = 'Promoting';
                 }
             }
             $row['suspendHidden'] = $this->hasDatabaseBehavior($behaviors);
             /* @var $image Image */
             $row['suspendEc2Locked'] = $row['platform'] == SERVER_PLATFORMS::EC2 && ($image = Image::findOne([['platform' => $row['platform']], ['cloudLocation' => $row['cloud_location']], ['id' => $row['image_id']], ['$or' => [['accountId' => null], ['accountId' => $row['client_id']]]], ['$or' => [['envId' => null], ['envId' => $row['env_id']]]]])) && $image->isEc2InstanceStoreImage();
         }
         $cloudLocations = [];
         foreach ($this->propertyFilter($neededServerProperties, "cloud_location") as $prop => $key) {
             if (array_key_exists($prop, $serverIds[$row["server_id"]][$idx])) {
                 $cloudLocations[] = $serverIds[$row["server_id"]][$idx][$prop];
             }
         }
         $row['cloud_location'] = empty($cloudLocations) ? null : $cloudLocations[0];
         if ($row["platform"] === \SERVER_PLATFORMS::EC2 && array_key_exists(\EC2_SERVER_PROPERTIES::AVAIL_ZONE, $serverIds[$row["server_id"]][$idx])) {
             $loc = $serverIds[$row["server_id"]][$idx][\EC2_SERVER_PROPERTIES::AVAIL_ZONE];
             if ($loc && $loc != 'x-scalr-diff') {
                 $row['cloud_location'] .= "/" . substr($loc, -1, 1);
             }
             $row['has_eip'] = array_key_exists("extIp", $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx]["extIp"] > 0;
         }
         if (in_array(\ROLE_BEHAVIORS::MONGODB, $behaviors)) {
             $shardIndex = $serverIds[$row["server_id"]][$idx][\Scalr_Role_Behavior_MongoDB::SERVER_SHARD_INDEX];
             $replicaSetIndex = $serverIds[$row["server_id"]][$idx][\Scalr_Role_Behavior_MongoDB::SERVER_REPLICA_SET_INDEX];
             $row['cluster_position'] = $shardIndex . "-" . $replicaSetIndex;
         }
         if (in_array($status, [Entity\Server::STATUS_RUNNING, Entity\Server::STATUS_SUSPENDED])) {
             if (array_key_exists(Entity\Server::REBOOTING, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][Entity\Server::REBOOTING] != 0) {
                 $row["status"] = "Rebooting";
             }
             if (array_key_exists(Entity\Server::MISSING, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][Entity\Server::MISSING] != 0) {
                 $row["status"] = "Missing";
             }
         }
         $row['agent_version'] = $serverIds[$row["server_id"]][$idx][Entity\Server::SZR_VESION];
         $agentVersion = Entity\Server::versionInfo($row['agent_version']);
         $row['is_locked'] = array_key_exists(\EC2_SERVER_PROPERTIES::IS_LOCKED, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][\EC2_SERVER_PROPERTIES::IS_LOCKED] != 0 ? 1 : 0;
         $row['is_szr'] = $agentVersion >= Entity\Server::versionInfo("0.5");
         $row['initDetailsSupported'] = $agentVersion >= Entity\Server::versionInfo("0.7.181");
         if (array_key_exists(Entity\Server::SZR_IS_INIT_FAILED, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][Entity\Server::SZR_IS_INIT_FAILED] == 1 && in_array($status, [Entity\Server::STATUS_INIT, Entity\Server::STATUS_PENDING])) {
             $row['isInitFailed'] = 1;
         }
         if (array_key_exists(Entity\Server::LAUNCH_ERROR, $serverIds[$row["server_id"]][$idx]) && strlen($serverIds[$row["server_id"]][$idx][Entity\Server::LAUNCH_ERROR]) > 0) {
             $row['launch_error'] = "1";
         }
         $row['isScalarized'] = $row["is_scalarized"];
         $row['agent_update_needed'] = $agentVersion >= Entity\Server::versionInfo("0.7") && $agentVersion < Entity\Server::versionInfo("0.7.189");
         $row['agent_update_manual'] = $agentVersion < Entity\Server::versionInfo("0.5");
         $row['os_family'] = $row["os_type"];
         $flavors = [];
         foreach ($this->propertyFilter($neededServerProperties, "flavor") as $prop => $key) {
             if (array_key_exists($prop, $serverIds[$row["server_id"]][$idx])) {
                 $flavors[] = $serverIds[$row["server_id"]][$idx][$prop];
             }
         }
         $row["flavor"] = empty($flavors) ? "" : $flavors[0];
         if (array_key_exists("alerts", $serverIds[$row["server_id"]][$idx])) {
             $row["alerts"] = $serverIds[$row["server_id"]][$idx]["alerts"];
         }
         if ($status === Entity\Server::STATUS_RUNNING) {
             $row['uptime'] = \Scalr_Util_DateTime::getHumanReadableTimeout(time() - strtotime($row['uptime']), false);
         } else {
             $row['uptime'] = '';
         }
         $row['excluded_from_dns'] = !(!(array_key_exists(Entity\Server::EXCLUDE_FROM_DNS, $serverIds[$row["server_id"]][$idx]) && $serverIds[$row["server_id"]][$idx][Entity\Server::EXCLUDE_FROM_DNS] == 1) && !(array_key_exists(Entity\FarmRoleSetting::EXCLUDE_FROM_DNS, $farmRoles[$row["farm_roleid"]][$idx]) && $farmRoles[$row["farm_roleid"]][$idx][Entity\FarmRoleSetting::EXCLUDE_FROM_DNS] == 1));
         $row['scalingEnabled'] = array_key_exists(Entity\FarmRoleSetting::SCALING_ENABLED, $farmRoles[$row["farm_roleid"]][$idx]) && $farmRoles[$row["farm_roleid"]][$idx][Entity\FarmRoleSetting::SCALING_ENABLED] == 1;
         if ($row['farmTeamId']) {
             if (!isset($userBelongsToTeam[$row['farmTeamId']])) {
                 $userBelongsToTeam[$row['farmTeamId']] = $this->user->isInTeam($row['farmTeamId']);
             }
             $row['farmTeamIdPerm'] = $userBelongsToTeam[$row['farmTeamId']];
         }
         $row['farmOwnerIdPerm'] = $row['farmOwnerId'] && $this->user->getId() == $row['farmOwnerId'];
     }
 }
Exemplo n.º 12
0
 /**
  * Marks server as to be terminated.
  *
  * @param   int|array       $reason      The reason possibly with the format parameters.
  * @param   bool            $forcefully  optional Method: forcefully (true) | gracefully (false)
  * @param   User            $user        optional The user entity
  * @return  bool
  */
 public function terminate($reason, $forcefully = null, $user = null)
 {
     if (in_array($this->status, [Server::STATUS_PENDING_TERMINATE, Server::STATUS_TERMINATED])) {
         return false;
     }
     $forcefully = $forcefully === null ? true : (bool) $forcefully;
     $fnGetReason = function ($reasonId) {
         $args = func_get_args();
         $args[0] = Server::getTerminateReason($reasonId);
         return [call_user_func_array('sprintf', $args), $reasonId];
     };
     list($reason, $reasonId) = is_array($reason) ? call_user_func_array($fnGetReason, $reason) : $fnGetReason($reason);
     $properties = $this->properties;
     if ($user instanceof User) {
         $properties[self::TERMINATED_BY_ID] = $user->getId();
         $properties[self::TERMINATED_BY_EMAIL] = $user->getEmail();
     }
     $properties[self::REBOOTING] = 0;
     $properties->save();
     $this->update(['status' => Server::STATUS_PENDING_TERMINATE, 'shutdownScheduled' => new DateTime($forcefully ? 'now' : Scalr::config('scalr.system.server_terminate_timeout'))]);
     $this->getHistory()->markAsTerminated($reason, $reasonId);
     if (isset($this->farmId)) {
         $DBServer = $this->__getDBServer();
         Scalr::FireEvent($this->farmId, new BeforeHostTerminateEvent($DBServer, false));
         // If instance was terminated outside scalr, we need manually fire HostDown
         if ($reasonId == self::TERMINATE_REASON_CRASHED) {
             Scalr::FireEvent($this->farmId, new HostDownEvent($DBServer, false));
         }
     }
     return true;
 }
Exemplo n.º 13
0
 /**
  * @param   string  $query
  * @throws  Scalr_Exception_Core
  */
 public function xSearchResourcesAction($query)
 {
     if (trim($query) == '') {
         $this->response->data(['data' => []]);
         return;
     }
     $environments = $this->request->getScope() == ScopeInterface::SCOPE_ACCOUNT ? array_map(function ($r) {
         return $r['id'];
     }, $this->user->getEnvironments()) : [$this->getEnvironmentId()];
     $f = new Entity\Farm();
     $s = new Entity\Server();
     $fr = new Entity\FarmRole();
     $e = new Entity\Account\Environment();
     $at = new Entity\Account\Team();
     $sp = new Entity\Server\Property();
     $farmSql = [];
     $serverSql = [];
     $queryEnc = "%{$query}%";
     foreach ($environments as $envId) {
         $acl = $this->user->getAclRolesByEnvironment($envId);
         $isTermporaryServerPerm = $acl->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_BUILD) || $acl->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_IMPORT);
         if ($acl->isAllowed(Acl::RESOURCE_FARMS)) {
             $farmSql[] = "{$f->columnEnvId} = {$envId}";
             if ($isTermporaryServerPerm) {
                 $serverSql[] = "{$s->columnEnvId} = {$envId}";
             } else {
                 $serverSql[] = "{$s->columnEnvId} = {$envId} AND {$s->columnFarmId} IS NOT NULL";
             }
         } else {
             $q = [];
             if ($acl->isAllowed(Acl::RESOURCE_TEAM_FARMS)) {
                 $t = array_map(function ($t) {
                     return $t['id'];
                 }, $this->user->getTeams());
                 if (count($t)) {
                     $q[] = "{$f->columnTeamId} IN (" . join(',', $t) . ")";
                 }
             }
             if ($acl->isAllowed(Acl::RESOURCE_OWN_FARMS)) {
                 $q[] = "{$f->columnCreatedById} = {$this->user->getId()}";
             }
             if (count($q)) {
                 $farmSql[] = "{$f->columnEnvId} = {$envId} AND (" . join(" OR ", $q) . ")";
             }
             if ($isTermporaryServerPerm) {
                 $q[] = "{$s->columnStatus} IN ('" . Entity\Server::STATUS_IMPORTING . "', '" . Entity\Server::STATUS_TEMPORARY . "') AND {$s->columnFarmId} IS NULL";
             }
             if (count($q)) {
                 $serverSql[] = "{$s->columnEnvId} = {$envId} AND (" . join(" OR ", $q) . ")";
             }
         }
     }
     $farms = [];
     if (count($farmSql)) {
         $farmStmt = $this->db->Execute("\n                SELECT {$f->columnId} AS id, {$f->columnName} AS name, {$f->columnEnvId} AS envId, {$f->columnStatus} AS status,\n                {$f->columnAdded} AS added, {$f->columnCreatedByEmail} AS createdByEmail, {$at->columnName} AS teamName, {$e->columnName} AS `envName`\n                FROM {$f->table()}\n                LEFT JOIN {$at->table()} ON {$at->columnId} = {$f->columnTeamId}\n                LEFT JOIN {$e->table()} ON {$f->columnEnvId} = {$e->columnId}\n                WHERE ({$f->columnName} LIKE ?) AND (" . join(" OR ", $farmSql) . ")", [$queryEnc]);
         while ($farm = $farmStmt->FetchRow()) {
             $farm['status'] = Entity\Farm::getStatusName($farm['status']);
             $farm['added'] = Scalr_Util_DateTime::convertTz($farm['added'], 'M j, Y H:i');
             $farms[] = ['entityName' => 'farm', 'envId' => $farm['envId'], 'envName' => $farm['envName'], 'matchField' => 'Name', 'matchValue' => $farm['name'], 'data' => $farm];
         }
     }
     $servers = [];
     if (count($serverSql)) {
         $serverStmt = $this->db->Execute("\n                SELECT {$s->columnServerId} AS serverId, {$s->columnFarmId} AS farmId, {$s->columnFarmRoleId} AS farmRoleId,\n                {$s->columnEnvId} AS envId, {$s->columnPlatform} AS platform, {$s->columnInstanceTypeName} AS instanceTypeName,\n                {$s->columnStatus} AS status, {$s->columnCloudLocation} AS cloudLocation, {$s->columnRemoteIp} AS publicIp,\n                {$s->columnLocalIp} AS privateIp, {$s->columnAdded} AS added, {$f->columnName} AS farmName,\n                {$fr->columnAlias} AS farmRoleName, {$e->columnName} AS `envName`, {$fr->columnRoleId} AS roleId,\n                {$sp->columnValue('sp1', 'hostname')}\n                FROM {$s->table()}\n                LEFT JOIN {$f->table()} ON {$f->columnId} = {$s->columnFarmId}\n                LEFT JOIN {$fr->table()} ON {$fr->columnId} = {$s->columnFarmRoleId}\n                LEFT JOIN {$e->table()} ON {$e->columnId} = {$s->columnEnvId}\n                LEFT JOIN {$sp->table('sp1')} ON {$sp->columnServerId('sp1')} = {$s->columnServerId} AND {$sp->columnName('sp1')} = ?\n                WHERE ({$s->columnRemoteIp} LIKE ? OR {$s->columnLocalIp} LIKE ? OR {$sp->columnValue('sp1')} LIKE ?) AND (" . join(" OR ", $serverSql) . ")\n                GROUP BY {$s->columnServerId}", [Scalr_Role_Behavior::SERVER_BASE_HOSTNAME, $queryEnc, $queryEnc, $queryEnc]);
         $names = ['publicIp' => 'Public IP', 'privateIp' => 'Private IP', 'hostname' => 'Hostname'];
         while ($server = $serverStmt->FetchRow()) {
             $server['added'] = Scalr_Util_DateTime::convertTz($server['added'], 'M j, Y H:i');
             if (strstr($server['publicIp'], $query)) {
                 $m = 'publicIp';
             } else {
                 if (strstr($server['privateIp'], $query)) {
                     $m = 'privateIp';
                 } else {
                     $m = 'hostname';
                 }
             }
             $servers[] = ['entityName' => 'server', 'envId' => $server['envId'], 'envName' => $server['envName'], 'matchField' => $names[$m], 'matchValue' => $server[$m], 'data' => $server];
         }
     }
     $this->response->data(['data' => array_merge($farms, $servers)]);
 }
Exemplo n.º 14
0
 protected function _launchedBy($from, $to, $action)
 {
     switch ($action) {
         case static::ACT_CONVERT_TO_OBJECT:
             /* @var $from Server */
             $to->launchedBy = ['id' => $from->properties[Server::LAUNCHED_BY_ID]];
             break;
         case static::ACT_CONVERT_TO_ENTITY:
             /* @var $to Server */
             $launchedBy = ApiController::getBareId($from, 'launchedBy');
             if (!isset($launchedBy)) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed launchedBy.id property");
             }
             $to->properties[Server::LAUNCHED_BY_ID] = $launchedBy;
             break;
         case static::ACT_GET_FILTER_CRITERIA:
             $entity = new Server();
             return $entity->getSettingCriteria(Server::LAUNCHED_BY_ID, ApiController::getBareId($from, 'launchedBy'));
     }
 }