Author: N.V.
Inheritance: extends Scalr\Model\AbstractEntity, implements Scalr\DataType\AccessPermissionsInterface
示例#1
0
文件: Disks.php 项目: scalr/scalr
 /**
  * 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']]);
         }
     }
 }
示例#2
0
 /**
  * {@inheritdoc}
  * @see Scalr\Tests\TestCase::tearDownAfterClass()
  */
 public static function tearDownAfterClass()
 {
     foreach (static::$testData as $rec) {
         if ($rec['class'] === Farm::class) {
             $entry = $rec['pk'];
             $farm = Farm::findPk(...$entry);
             /* @var $farm Farm */
             if (!empty($farm)) {
                 try {
                     \Scalr::FireEvent($farm->id, new FarmTerminatedEvent(false, false, false, false, true, static::$user->id));
                     foreach ($farm->servers as $server) {
                         try {
                             $dbServer = Server::findPk($server->serverId);
                             /* @var $dbServer Server */
                             $dbServer->terminate(Server::TERMINATE_REASON_FARM_TERMINATED, true, static::$user);
                         } catch (Exception $e) {
                             \Scalr::logException($e);
                         }
                         $server->delete();
                     }
                 } catch (Exception $e) {
                     \Scalr::logException($e);
                 }
             }
         }
     }
     parent::tearDownAfterClass();
 }
示例#3
0
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     if (self::isSkippedFunctionalTest(self::TEST_TYPE_UI)) {
         return;
     }
     $db = \Scalr::getDb();
     self::deleteTestVariables();
     $envId = \Scalr::config('scalr.phpunit.envid');
     if (!$envId) {
         return;
     }
     $env = \Scalr_Environment::init()->loadById($envId);
     self::$vars[ScopeInterface::SCOPE_SCALR] = new Scalr_Scripting_GlobalVariables();
     self::$vars[ScopeInterface::SCOPE_ACCOUNT] = new Scalr_Scripting_GlobalVariables($env->clientId, 0, ScopeInterface::SCOPE_ACCOUNT);
     self::$vars[ScopeInterface::SCOPE_ENVIRONMENT] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_ENVIRONMENT);
     self::$args[ScopeInterface::SCOPE_SCALR] = self::$args[ScopeInterface::SCOPE_ACCOUNT] = self::$args[ScopeInterface::SCOPE_ENVIRONMENT] = [0, 0, 0, ''];
     /* @var $farm Farm */
     $farm = Farm::findOne([['envId' => $env->id]]);
     if ($farm) {
         self::$vars[ScopeInterface::SCOPE_FARM] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_FARM);
         self::$args[ScopeInterface::SCOPE_FARM] = [0, $farm->id, 0, ''];
         /* @var $farmRole FarmRole */
         $farmRole = FarmRole::findOne([['farmId' => $farm->id]]);
         if ($farmRole) {
             self::$vars[ScopeInterface::SCOPE_ROLE] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_ROLE);
             self::$args[ScopeInterface::SCOPE_ROLE] = [$farmRole->roleId, 0, 0, ''];
             self::$vars[ScopeInterface::SCOPE_FARMROLE] = new Scalr_Scripting_GlobalVariables($env->clientId, $env->id, ScopeInterface::SCOPE_FARMROLE);
             self::$args[ScopeInterface::SCOPE_FARMROLE] = [$farmRole->roleId, $farm->id, $farmRole->id, ''];
         }
     }
 }
示例#4
0
文件: Farms.php 项目: mheydt/scalr
 /**
  * Gets specified Farm taking into account both scope and authentication token
  *
  * @param   string  $farmId              Numeric identifier of the Farm
  * @param   string  $permission optional Permission identifier
  *
  * @return  Farm    Returns the Farm Entity on success
  * @throws  ApiErrorException
  */
 public function getFarm($farmId, $permission = null)
 {
     /* @var $farm Farm */
     $farm = Farm::findPk($farmId);
     if (!$farm) {
         throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Requested Farm either does not exist or is not owned by your environment.");
     }
     $this->checkPermissions($farm, $permission);
     return $farm;
 }
示例#5
0
 /**
  * @return Entity\Farm
  * @throws Exception
  */
 public function __getNewFarmObject()
 {
     if (!$this->__newFarmObject) {
         if ($this->ID) {
             $this->__newFarmObject = Entity\Farm::findPk($this->ID);
         }
         if (!$this->__newFarmObject) {
             throw new Exception('Farm object is not found');
         }
     }
     return $this->__newFarmObject;
 }
示例#6
0
文件: Farms.php 项目: solderzzc/scalr
 /**
  * Gets specified Farm taking into account both scope and authentication token
  *
  * @param   string  $farmId              Numeric identifier of the Farm
  * @param   string  $permission optional Permission identifier
  *
  * @return  Farm    Returns the Farm Entity on success
  * @throws  ApiErrorException
  */
 public function getFarm($farmId, $permission = null)
 {
     /* @var $farm Farm */
     $farm = Farm::findPk($farmId);
     if (!$farm) {
         throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Requested Farm either does not exist or is not owned by your environment.");
     }
     if (!$this->hasPermissions($farm)) {
         //Checks entity level write access permissions
         throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
     }
     $this->checkPermissions($farm, $permission);
     return $farm;
 }
示例#7
0
文件: Servers.php 项目: scalr/scalr
 /**
  * Gets Default search criteria for the Environment scope
  *
  * @return array Returns array of the default search criteria for the Environment scope
  */
 private function getDefaultCriteria()
 {
     $server = new Server();
     $defaultCondition = sprintf("{$server->columnAccountId()} = %d AND {$server->columnEnvId()} = %d ", $this->getUser()->accountId, $this->getEnvironment()->id);
     $and = " AND {$server->columnFarmId()} IS NOT NULL ";
     if (!$this->hasPermissions(Acl::RESOURCE_FARMS)) {
         $where = [];
         $farm = new Farm();
         $farmTeam = new FarmTeam();
         $join[] = " LEFT JOIN {$farm->table('f')} ON {$farm->columnId('f')} = {$server->columnFarmId()}";
         if ($this->hasPermissions(Acl::RESOURCE_OWN_FARMS)) {
             $where[] = "{$farm->columnOwnerId('f')} = " . $farm->qstr('ownerId', $this->getUser()->id);
         }
         if ($this->hasPermissions(Acl::RESOURCE_TEAM_FARMS)) {
             $join[] = "\n                    LEFT JOIN {$farmTeam->table('ft')} ON {$farmTeam->columnFarmId('ft')} = {$farm->columnId('f')}\n                    LEFT JOIN `account_team_users` `atu` ON `atu`.`team_id` = {$farmTeam->columnTeamId('ft')}\n                    LEFT JOIN `account_team_envs` `ate` ON `ate`.`team_id` = {$farmTeam->columnTeamId('ft')} AND `ate`.`env_id` = {$farm->columnEnvId('f')}\n                ";
             $where[] = "`atu`.`user_id` = " . $farmTeam->db()->qstr($this->getUser()->id) . " AND `ate`.`team_id` IS NOT NULL";
         }
         if (!empty($where)) {
             $criteria[Farm::STMT_WHERE] = '(' . $defaultCondition . $and . ' AND ' . join(' OR ', $where) . ')';
         }
         $criteria[Farm::STMT_FROM] = $server->table() . implode(' ', $join);
     }
     // add Temporary and Importing Servers to response
     if ($this->hasPermissions(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) {
         $extraCondition = sprintf("({$defaultCondition} AND {$server->columnFarmId()} IS NULL AND {$server->columnStatus()} IN ('%s', '%s'))", Server::STATUS_IMPORTING, Server::STATUS_TEMPORARY);
         if (empty($criteria[Farm::STMT_WHERE]) && $this->hasPermissions(Acl::RESOURCE_FARMS)) {
             $criteria[Farm::STMT_WHERE] = "(({$defaultCondition}{$and}) OR {$extraCondition})";
         } else {
             if (empty($criteria[Farm::STMT_WHERE])) {
                 $criteria[Farm::STMT_WHERE] = "{$extraCondition}";
             } else {
                 $criteria[Farm::STMT_WHERE] = "({$criteria[Farm::STMT_WHERE]} OR {$extraCondition})";
             }
         }
     }
     if (empty($criteria[Farm::STMT_WHERE])) {
         $criteria[Farm::STMT_WHERE] = $defaultCondition;
     }
     return $criteria;
 }
示例#8
0
 /**
  * {@inheritdoc}
  * @see ApiEntityAdapter::validateEntity()
  */
 public function validateEntity($entity)
 {
     if (!$entity instanceof Farm) {
         throw new InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\Farm class"));
     }
     if ($entity->id !== null) {
         if (!Farm::findPk($entity->id)) {
             throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Farm with ID: %d", $entity->id));
         }
     } else {
         if (empty($entity->name)) {
             throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property name");
         }
         $criteria = $this->controller->getScopeCriteria();
         $criteria[] = ['name' => $entity->name];
         if (count(Farm::find($criteria))) {
             throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, "Farm with name '{$entity->name}' already exists");
         }
     }
     if (!empty($entity->settings[FarmSetting::EC2_VPC_REGION])) {
         $region = $entity->settings[FarmSetting::EC2_VPC_REGION];
         $vpcId = $entity->settings[FarmSetting::EC2_VPC_ID];
         if (!in_array($region, Aws::getCloudLocations())) {
             throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Unknown VPC region");
         }
         $gov = new Scalr_Governance($this->controller->getEnvironment()->id);
         $vpcGovernanceRegions = $gov->getValue(SERVER_PLATFORMS::EC2, Scalr_Governance::AWS_VPC, 'regions');
         if (isset($vpcGovernanceRegions)) {
             if (!array_key_exists($region, $vpcGovernanceRegions)) {
                 $regions = array_keys($vpcGovernanceRegions);
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Only %s %s allowed according to governance settings", ...count($regions) > 1 ? [implode(', ', $regions), 'regions are'] : [array_shift($regions), 'region is']));
             }
             $vpcGovernanceIds = $vpcGovernanceRegions[$region]['ids'];
             if (!empty($vpcGovernanceIds) && !in_array($vpcId, $vpcGovernanceIds)) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, sprintf("Only %s %s allowed according to governance settings", ...count($vpcGovernanceIds) > 1 ? [implode(', ', $vpcGovernanceIds), 'vpcs are'] : [array_shift($vpcGovernanceIds), 'vpc is']));
             }
         }
         $found = null;
         /* @var $vpc VpcData */
         //TODO rewrite aws service usage
         foreach ($this->controller->getContainer()->aws($region, $this->controller->getEnvironment())->ec2->vpc->describe() as $vpc) {
             if ($vpcId == $vpc->vpcId) {
                 $found = $vpc;
             }
         }
         if (empty($found)) {
             throw new ApiErrorException(400, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Could not find out the VPC with ID '{$vpcId}' in region '{$region}'");
         }
     } else {
         if (!empty($entity->settings[FarmSetting::EC2_VPC_ID])) {
             throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property vpc.region");
         }
     }
     if (\Scalr::config('scalr.analytics.enabled')) {
         if (isset($entity->settings[FarmSetting::PROJECT_ID])) {
             if (!$this->controller->getContainer()->analytics->projects->get($entity->settings[FarmSetting::PROJECT_ID])) {
                 throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "The project is not allowed for you");
             }
         } else {
             throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property project");
         }
     }
     if (!$this->controller->hasPermissions($entity, true)) {
         //Checks entity level write access permissions
         throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
     }
 }
示例#9
0
文件: FarmsTest.php 项目: scalr/scalr
 /**
  * Asserts post farm response
  *
  * @param ApiTestResponse $response
  * @param array           $data
  */
 public function assertPostFarms(ApiTestResponse $response, $data)
 {
     $this->assertEquals(201, $response->status, $this->printResponseError($response));
     $farmId = $response->getBody()->data->id;
     /* @var $farm Farm */
     $farm = Farm::findPk($farmId);
     $this->assertNotEmpty($farm);
     $this->farmToDelete($farmId);
     $this->assertObjectEqualsEntity($data, $farm);
 }
示例#10
0
文件: Roles.php 项目: scalr/scalr
 /**
  * Get information about role
  *
  * @param   int     $roleId      Identifier of role
  * @param   bool    $extended    Get extended information about role
  * @param   array   $canAddImage Array of platform, cloudLocation to check if role has image in that location
  * @return  array
  * @throws  Exception
  * @throws  Scalr_Exception_Core
  * @throws  Scalr_Exception_InsufficientPermissions
  */
 private function getInfo($roleId, $extended = false, $canAddImage = null)
 {
     /* @var $role Role */
     $role = Role::findPk($roleId);
     if (!$role) {
         throw new Scalr_Exception_Core(sprintf(_("Role ID#%s not found in database"), $roleId));
     }
     $this->request->checkPermissions($role);
     $usedBy = $role->getFarmsCount($this->user->getAccountId(), $this->getEnvironmentId(true));
     $platforms = array_keys($role->fetchImagesArray());
     $allPlatforms = array_flip(array_keys(SERVER_PLATFORMS::GetList()));
     usort($platforms, function ($a, $b) use($allPlatforms) {
         return $allPlatforms[$a] > $allPlatforms[$b] ? 1 : -1;
     });
     $result = array('name' => $role->name, 'behaviors' => $role->getBehaviors(), 'id' => $role->id, 'accountId' => $role->accountId, 'envId' => $role->envId, 'catId' => $role->catId, 'status' => $usedBy > 0 ? 'In use' : 'Not used', 'scope' => $role->getScope(), 'os' => $role->getOs()->name, 'osId' => $role->osId, 'osFamily' => $role->getOs()->family, 'dtAdded' => $role->added ? Scalr_Util_DateTime::convertTz($role->added) : NULL, 'dtLastUsed' => $role->lastUsed ? Scalr_Util_DateTime::convertTz($role->lastUsed) : NULL, 'isQuickStart' => $role->isQuickStart, 'isDeprecated' => $role->isDeprecated, 'isScalarized' => $role->isScalarized, 'platforms' => $platforms, 'environments' => !empty($envs = $role->getAllowedEnvironments()) ? $this->db->GetCol("SELECT name FROM client_environments WHERE id IN(" . join(',', $envs) . ")") : []);
     if ($canAddImage) {
         try {
             $role->getImage($canAddImage['platform'], $canAddImage['cloudLocation']);
             $result['canAddImage'] = false;
         } catch (Exception $e) {
             $result['canAddImage'] = true;
         }
     }
     if ($extended) {
         $result['description'] = $role->description;
         $result['images'] = [];
         foreach (RoleImage::find([['roleId' => $role->id]]) as $image) {
             /* @var $image RoleImage */
             $im = $image->getImage();
             $ext = [];
             if ($im) {
                 $ext = get_object_vars($im);
                 $ext['software'] = $im->getSoftwareAsString();
             }
             $result['images'][] = ['imageId' => $image->imageId, 'platform' => $image->platform, 'cloudLocation' => $image->cloudLocation, 'extended' => $ext];
         }
         if ($result['status'] == 'In use' && $this->getEnvironmentId(true)) {
             $farms = [];
             $f = [];
             foreach (FarmRole::find([['roleId' => $role->id]]) as $farmRole) {
                 /* @var $farmRole FarmRole */
                 $f[] = $farmRole->farmId;
             }
             $f = array_unique($f);
             if (count($f)) {
                 foreach (Farm::find([['id' => ['$in' => $f]], ['envId' => $this->getEnvironmentId()]]) as $fm) {
                     /* @var $fm Farm */
                     $farms[] = ['id' => $fm->id, 'name' => $fm->name];
                 }
             }
             $result['usedBy'] = ['farms' => $farms, 'cnt' => count($farms)];
         }
     }
     return $result;
 }
示例#11
0
文件: Servers.php 项目: scalr/scalr
 /**
  * Get list of servers
  *
  * @param string  $cloudServerId       optional Cloud server ID
  * @param string  $cloudServerLocation optional Cloud server location
  * @param string  $hostname            optional Hostname
  * @param int     $farmId              optional Farm ID
  * @param int     $farmRoleId          optional Farm role ID
  * @param int     $roleId              optional Role ID
  * @param string  $serverId            optional Server ID
  * @param string  $imageId             optional Image ID
  * @param boolean $showTerminated      optional Whether to show terminated servers as well
  * @param string  $uptime              optional Uptime
  * @throws \Scalr_Exception_InsufficientPermissions
  */
 public function xListServersAction($cloudServerId = null, $cloudServerLocation = null, $hostname = null, $farmId = null, $farmRoleId = null, $roleId = null, $serverId = null, $imageId = null, $showTerminated = null, $uptime = null)
 {
     if (!$this->request->isAllowed([Acl::RESOURCE_FARMS, Acl::RESOURCE_TEAM_FARMS, Acl::RESOURCE_OWN_FARMS]) && !$this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) {
         throw new \Scalr_Exception_InsufficientPermissions();
     }
     $sortParamsLoad = '';
     foreach ($this->getSortOrder() as $param) {
         if ($param['property'] == 'remote_ip_int' || $param['property'] == 'local_ip_int') {
             $sortParamsLoad .= ", INET_ATON(servers." . substr($param['property'], 0, -4) . ") AS " . $param['property'];
         }
     }
     $stmFrom = "SELECT servers.*,\n                       f.name AS farm_name,\n                       roles.name AS role_name,\n                       roles.behaviors AS behaviors,\n                       farm_roles.alias AS role_alias,\n                       f.created_by_id AS farmOwnerId,\n                       (SELECT " . Entity\Farm::getUserTeamOwnershipSql($this->getUser()->id) . ") AS farmTeamIdPerm,\n                       servers.dtinitialized AS uptime,\n                       ste.last_error AS termination_error{$sortParamsLoad}\n                FROM servers\n                LEFT JOIN farms f ON servers.farm_id = f.id\n                LEFT JOIN farm_roles ON farm_roles.id = servers.farm_roleid\n                LEFT JOIN roles ON roles.id = farm_roles.role_id\n                LEFT JOIN server_termination_errors ste ON servers.server_id = ste.server_id";
     $stmWhere = ["servers.env_id = ?"];
     $args = [$this->getEnvironmentId()];
     $sqlFlt = [];
     if (!empty($cloudServerId)) {
         $cloudServerProps = [CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID, AZURE_SERVER_PROPERTIES::SERVER_NAME, EC2_SERVER_PROPERTIES::INSTANCE_ID, GCE_SERVER_PROPERTIES::SERVER_NAME, OPENSTACK_SERVER_PROPERTIES::SERVER_ID];
         $sqlFlt["sp1"] = "sp1.`name` IN (" . implode(", ", array_fill(0, count($cloudServerProps), "?")) . ") AND sp1.`value` = ?";
         foreach ($cloudServerProps as $spn) {
             $args[] = $spn;
         }
         $args[] = $cloudServerId;
     }
     if (!empty($cloudServerLocation)) {
         $cloudServerLocationProps = [CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION, AZURE_SERVER_PROPERTIES::CLOUD_LOCATION, EC2_SERVER_PROPERTIES::REGION, GCE_SERVER_PROPERTIES::CLOUD_LOCATION, OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION];
         $sqlFlt["sp2"] = "sp2.`name` IN (" . implode(", ", array_fill(0, count($cloudServerLocationProps), "?")) . ") AND sp2.`value` = ?";
         foreach ($cloudServerLocationProps as $spn) {
             $args[] = $spn;
         }
         $args[] = $cloudServerLocation;
     }
     if (!empty($hostname)) {
         $sqlFlt["sp3"] = "sp3.`name` = ? AND sp3.`value` LIKE ?";
         $args[] = Scalr_Role_Behavior::SERVER_BASE_HOSTNAME;
         $args[] = "%" . $hostname . "%";
     }
     if (!empty($sqlFlt)) {
         foreach ($sqlFlt as $alias => $where) {
             $stmFrom .= " INNER JOIN server_properties AS " . $alias . " ON servers.server_id = " . $alias . ".server_id";
             $stmWhere[] = $where;
         }
     }
     if (!empty($farmId)) {
         $stmWhere[] = "farm_id = ?";
         $args[] = $farmId;
     }
     $where = ["farm_id IS NOT NULL AND " . $this->request->getFarmSqlQuery()];
     if ($this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) {
         $where[] = "farm_id IS NULL AND servers.status IN (?, ?)";
         $args[] = Entity\Server::STATUS_IMPORTING;
         $args[] = Entity\Server::STATUS_TEMPORARY;
     }
     $stmWhere[] = '(' . join(" OR ", $where) . ')';
     if (!empty($farmRoleId)) {
         $stmWhere[] = "farm_roleid = ?";
         $args[] = $farmRoleId;
     }
     if (!empty($roleId)) {
         $stmWhere[] = "farm_roles.role_id = ?";
         $args[] = $roleId;
     }
     if (!empty($serverId)) {
         $stmWhere[] = "servers.server_id = ?";
         $args[] = $serverId;
     }
     if (!empty($imageId)) {
         $stmWhere[] = "image_id = ?";
         $args[] = $imageId;
     }
     if (empty($showTerminated)) {
         $stmWhere[] = "servers.status != ?";
         $args[] = Entity\Server::STATUS_TERMINATED;
     }
     if (!empty($uptime) && preg_match('/^(m|l)([0-9]+)(d|h)$/', $uptime, $matches)) {
         if ($matches[1] == 'm') {
             $stmWhere[] = "servers.dtinitialized < ?";
         } else {
             $stmWhere[] = "servers.dtinitialized > ?";
         }
         $args[] = date("Y-m-d H:i:s", strtotime("-" . $matches[2] . ($matches[3] == 'd' ? 'day' : 'hour')));
     }
     $stmWhere[] = ":FILTER:";
     $response = $this->buildResponseFromSql2($stmFrom . " WHERE " . implode(" AND ", $stmWhere), ["platform", "farm_name", "role_name", "role_alias", "index", "servers.server_id", "remote_ip_int", "local_ip_int", "uptime", "status"], ["servers.server_id", "farm_id", "f.name", "remote_ip", "local_ip", "servers.status", "farm_roles.alias"], $args);
     $this->listServersResponseHelper($response);
     $this->response->data($response);
 }
示例#12
0
文件: Request.php 项目: scalr/scalr
 /**
  * Generate conditions for sql query to limit access by only allowable farms.
  * Table `farms` should have alias `f`.
  *
  * @param   string  $permissionId   optional
  * @return  string
  */
 public function getFarmSqlQuery($permissionId = null)
 {
     if (!$this->isAllowed(Acl::RESOURCE_FARMS, $permissionId)) {
         $q = [];
         if ($this->isAllowed(Acl::RESOURCE_TEAM_FARMS, $permissionId)) {
             $q[] = Farm::getUserTeamOwnershipSql($this->user->id);
         }
         if ($this->isAllowed(Acl::RESOURCE_OWN_FARMS, $permissionId)) {
             $q[] = "f.created_by_id = '{$this->user->getId()}'";
         }
         if (count($q)) {
             $sql = '(' . join(' OR ', $q) . ')';
         } else {
             $sql = '0';
             // no permissions
         }
     } else {
         $sql = '1';
         // all farms in env
     }
     return $sql;
 }
示例#13
0
文件: Sshkeys.php 项目: mheydt/scalr
 /**
  * Covert object to array (without public/private keys)
  *
  * @param SshKey $key
  * @return array
  */
 public function getSshKeyObject($key)
 {
     /* @var $farm Farm */
     return ['id' => $key->id, 'type' => $key->type, 'cloudKeyName' => $key->cloudKeyName, 'platform' => $key->platform, 'cloudLocation' => $key->cloudLocation, 'farmId' => $key->farmId, 'farmName' => $key->farmId && ($farm = Farm::findPk($key->farmId)) ? $farm->name : '', 'status' => $key->isUsed() ? 'In use' : 'Not used'];
 }
示例#14
0
 /**
  * @test
  * @functional
  */
 public function testFarmRoleGlobalVariables()
 {
     $db = \Scalr::getDb();
     $testName = str_replace('-', '', $this->getTestName());
     $farm = Farm::findOne([['envId' => static::$testEnvId]]);
     /* @var $farm Farm */
     $farmRole = FarmRole::findOne([['farmId' => $farm->id]]);
     /* @var $farmRole FarmRole */
     $roleId = $farmRole->id;
     $uri = static::getUserApiUrl("farm-roles/{$roleId}/global-variables");
     $variables = null;
     $declaredNotInRole = null;
     do {
         $query = [];
         if (isset($variables->pagination->next)) {
             $parts = parse_url($variables->pagination->next);
             parse_str($parts['query'], $query);
         }
         $query[ApiController::QUERY_PARAM_MAX_RESULTS] = 2;
         $describe = $this->request($uri, Request::METHOD_GET, $query);
         $this->assertDescribeResponseNotEmpty($describe);
         $this->assertNotEmpty($describe->getBody());
         $variables = $describe->getBody();
         $this->assertLessThanOrEqual(2, count($variables->data));
         foreach ($variables->data as $variable) {
             $this->assertVariableObjectNotEmpty($variable);
             if (empty($declaredNotInRole) && $variable->declaredIn !== ScopeInterface::SCOPE_FARMROLE && !$variable->hidden) {
                 $declaredNotInRole = $variable->name;
             }
             if (strpos($variable->name, $testName) !== false) {
                 $delete = $this->request($uri . '/' . $variable->name, Request::METHOD_DELETE);
                 $this->assertEquals(200, $delete->response->getStatus());
             }
         }
     } while (!empty($variables->pagination->next));
     $this->assertNotNull($declaredNotInRole);
     $notFoundRoleId = 10 + $db->GetOne("SELECT MAX(f.id) FROM farm_roles f");
     $describe = $this->request(static::getUserApiUrl("/farm-roles/{$notFoundRoleId}/global-variables"), Request::METHOD_GET);
     $this->assertErrorMessageContains($describe, 404, ErrorMessage::ERR_OBJECT_NOT_FOUND);
     $create = $this->request($uri, Request::METHOD_POST, [], ['invalid' => 'value']);
     $this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'You are trying to set');
     $create = $this->request($uri, Request::METHOD_POST, [], ['name' => 'invalid val--ue']);
     $this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_VALUE, 'Name should contain only letters, numbers and underscores, start with letter and be from 2 to 128 chars long');
     //test invalid category name
     $create = $this->request($uri, Request::METHOD_POST, [], ['name' => 'TestName', 'category' => 'invalid category']);
     $this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_VALUE);
     $create = $this->request($uri, Request::METHOD_POST);
     $this->assertErrorMessageContains($create, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'Invalid body');
     $create = $this->request($uri, Request::METHOD_POST, [], ['name' => $testName, 'value' => $testName, 'description' => $testName]);
     $this->assertEquals(201, $create->response->getStatus());
     $this->assertFetchResponseNotEmpty($create);
     $createBody = $create->getBody();
     $this->assertNotEmpty($createBody);
     $this->assertVariableObjectNotEmpty($createBody->data);
     $this->assertEquals($testName, $createBody->data->name);
     $this->assertEquals($testName, $createBody->data->value);
     $this->assertEquals($testName, $createBody->data->description);
     $create = $this->request($uri, Request::METHOD_POST, [], ['name' => $testName]);
     $this->assertErrorMessageContains($create, 409, ErrorMessage::ERR_UNICITY_VIOLATION, 'Variable with name');
     $fetch = $this->request($uri . '/' . $testName, Request::METHOD_GET);
     $this->assertEquals(200, $fetch->response->getStatus());
     $this->assertFetchResponseNotEmpty($fetch);
     $fetchBody = $fetch->getBody();
     $this->assertNotEmpty($fetchBody);
     $this->assertVariableObjectNotEmpty($fetchBody->data);
     $this->assertEquals($testName, $fetchBody->data->name);
     $this->assertEquals($testName, $fetchBody->data->value);
     $modify = $this->request($uri . '/' . $testName, Request::METHOD_PATCH, [], ['value' => '']);
     $this->assertEquals(200, $modify->response->getStatus());
     $this->assertFetchResponseNotEmpty($modify);
     $modifyBody = $modify->getBody();
     $this->assertNotEmpty($modifyBody);
     $this->assertVariableObjectNotEmpty($modifyBody->data);
     $this->assertEquals($testName, $modifyBody->data->name);
     $this->assertEquals('', $modifyBody->data->value);
     $modify = $this->request($uri . '/' . $testName . 'notFound', Request::METHOD_PATCH, [], ['value' => '']);
     $this->assertEquals(404, $modify->response->getStatus());
     $this->assertErrorMessageErrorEquals(ErrorMessage::ERR_OBJECT_NOT_FOUND, $modify);
     $modify = $this->request($uri . '/' . $testName, Request::METHOD_PATCH, [], ['name' => '']);
     $this->assertErrorMessageContains($modify, 400, ErrorMessage::ERR_INVALID_STRUCTURE, 'You are trying to set');
     $modify = $this->request($uri . '/' . $declaredNotInRole, Request::METHOD_PATCH, [], ['hidden' => 1]);
     $this->assertEquals(403, $modify->response->getStatus());
     $this->assertErrorMessageErrorEquals(ErrorMessage::ERR_SCOPE_VIOLATION, $modify);
     $delete = $this->request($uri . '/' . $declaredNotInRole, Request::METHOD_DELETE);
     $this->assertEquals(403, $delete->response->getStatus());
     $this->assertErrorMessageErrorEquals(ErrorMessage::ERR_SCOPE_VIOLATION, $delete);
     $delete = $this->request($uri . '/' . $testName . 'notfound', Request::METHOD_DELETE);
     $this->assertEquals(404, $delete->response->getStatus());
     $this->assertErrorMessageErrorEquals(ErrorMessage::ERR_OBJECT_NOT_FOUND, $delete);
     $delete = $this->request($uri . '/' . $testName, Request::METHOD_DELETE);
     $this->assertEquals(200, $delete->response->getStatus());
 }
示例#15
0
文件: Farms.php 项目: scalr/scalr
 /**
  * Creates clone for the farm
  *
  * @param   int     $farmId Unique farm identifier
  *
  * @return  ResultEnvelope
  *
  * @throws  ApiErrorException
  */
 public function cloneAction($farmId)
 {
     $farm = $this->getFarm($farmId, Acl::PERM_FARMS_CLONE);
     if (!$this->getUser()->getAccount()->checkLimit(Limit::ACCOUNT_FARMS, 1)) {
         throw new ApiErrorException(400, ErrorMessage::ERR_LIMIT_EXCEEDED, "Farms limit for your account exceeded");
     }
     $object = $this->request->getJsonBody();
     if (empty($object->name)) {
         throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Missed property name");
     }
     $name = FarmAdapter::convertInputValue('string', $object->name, 'name');
     $criteria = $this->getScopeCriteria();
     $criteria[] = ['name' => $name];
     if (count(Farm::find($criteria))) {
         throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, "Farm with name '{$name}' already exists");
     }
     $clone = $farm->cloneFarm($name, $this->getUser());
     return $this->result($this->adapter('farm')->toData($clone));
 }
示例#16
0
文件: FarmRole.php 项目: mheydt/scalr
 /**
  * {@inheritdoc}
  * @see AccessPermissionsInterface::hasAccessPermissions()
  */
 public function hasAccessPermissions($user, $environment = null, $modify = null)
 {
     /* @var $farm Farm */
     $farm = Farm::findPk($this->farmId);
     return $environment ? $farm->envId == $environment->id : $user->hasAccessToEnvironment($farm->envId);
 }
示例#17
0
 /**
  * xMoveProjectsAction
  *
  * @param JsonData $projects Projects that should be moved
  * @throws AnalyticsException
  * @throws Exception
  * @throws \Scalr\Exception\ModelException
  */
 public function xMoveProjectsAction(JsonData $projects = null)
 {
     $envChange = [];
     $accountChange = [];
     $projectChange = [];
     $ccEntityCache = [];
     $collisions = [];
     foreach ($projects as $project) {
         $projectEntity = ProjectEntity::findPk($project['projectId']);
         /* @var $projectEntity ProjectEntity */
         if (empty($ccEntity)) {
             $ccEntity = $projectEntity->getCostCenter();
         }
         if ($ccEntity->ccId == $project['ccId']) {
             continue;
         }
         if (empty($ccEntityCache[$project['ccId']])) {
             $newCcEntity = CostCentreEntity::findPk($project['ccId']);
             /* @var $newCcEntity CostCentreEntity */
             if (!$newCcEntity) {
                 throw new Exception(sprintf("Cost center with id %s has not been found.", $project['ccId']), 404);
             }
             $ccEntityCache[$project['ccId']] = $newCcEntity->ccId;
         }
         $farms[$projectEntity->projectId] = $projectEntity->getFarmsList();
         foreach ($farms[$projectEntity->projectId] as $farmId => $farmName) {
             $farmEntity = Farm::findPk($farmId);
             /* @var $farmEntity Farm */
             if (empty($accountChange[$farmEntity->accountId])) {
                 $accountCss = AccountCostCenterEntity::findOne([['accountId' => $farmEntity->accountId], ['ccId' => $newCcEntity->ccId]]);
                 if (!$accountCss) {
                     $accountChange[$farmEntity->accountId] = $newCcEntity->ccId;
                 }
             }
             if (empty($envChange[$farmEntity->envId])) {
                 $project['name'] = $projectEntity->name;
                 $envChange[$farmEntity->envId] = $project;
             } else {
                 if ($envChange[$farmEntity->envId]['ccId'] != $project['ccId']) {
                     if (!in_array($projectEntity->name, $collisions)) {
                         $collisions[] = $projectEntity->name;
                     }
                     if (!in_array($envChange[$farmEntity->envId]['name'], $collisions)) {
                         $collisions[] = $envChange[$farmEntity->envId]['name'];
                     }
                     continue;
                 }
             }
         }
         $projectEntity->ccId = $project['ccId'];
         $projectChange[$projectEntity->projectId] = $projectEntity;
     }
     $remainningEnvs = [];
     $projectsCount = count($projectChange);
     if ($projectsCount) {
         if (isset($ccEntity)) {
             $envList = $ccEntity->getEnvironmentsList();
             foreach ($envList as $envId => $name) {
                 if (isset($envChange[$envId])) {
                     $ccProjects = $this->getContainer()->analytics->projects->getUsedInEnvironment($envId);
                     foreach ($ccProjects as $project) {
                         /* @var $project ProjectEntity */
                         if (!isset($farms[$project->projectId])) {
                             $farms[$project->projectId] = $project->getFarmsList();
                         }
                         if (count($farms[$project->projectId]) > 0 && !isset($projectChange[$project->projectId])) {
                             if (!in_array($envId, $remainningEnvs)) {
                                 $remainningEnvs[] = $envId;
                             }
                         }
                     }
                 }
             }
         }
         $this->db->BeginTrans();
         try {
             foreach ($accountChange as $accountId => $ccId) {
                 $accountCss = new AccountCostCenterEntity($accountId, $ccId);
                 $accountCss->save();
             }
             if (empty($remainningEnvs) && empty($collisions)) {
                 foreach ($envChange as $envId => $data) {
                     $envProp = EnvironmentProperty::findOne([['envId' => $envId], ['name' => EnvironmentProperty::SETTING_CC_ID]]);
                     /* @var $envProp EnvironmentProperty */
                     $envProp->value = $data['ccId'];
                     $envProp->save();
                 }
             }
             foreach ($projectChange as $project) {
                 /* @var $project ProjectEntity */
                 $project->save();
             }
             $this->db->CommitTrans();
         } catch (Exception $e) {
             $this->db->RollbackTrans();
             throw $e;
         }
     }
     if (count($collisions) > 0) {
         $this->response->warning(sprintf("%d Project%s %s been moved however collision occurred. Projects '%s' are used in the Farms from the same Environment however they have been moved to different Cost Centers.", $projectsCount, $projectsCount > 1 ? 's' : '', $projectsCount > 1 ? 'have' : 'has', implode("', '", $collisions)));
     } else {
         if (count($remainningEnvs) > 0) {
             $this->response->warning(sprintf("%d Project%s %s been moved however some Projects don't correspond to Cost Centers assigned to Environments '%s'.", $projectsCount, $projectsCount > 1 ? 's' : '', $projectsCount > 1 ? 'have' : 'has', implode("', '", $remainningEnvs)));
         } else {
             $this->response->success(sprintf("%d Project%s %s been moved to other Cost Center.", $projectsCount, $projectsCount > 1 ? 's' : '', $projectsCount > 1 ? 'have' : 'has'));
         }
     }
 }
示例#18
0
文件: Farms.php 项目: scalr/scalr
 /**
  * @param int $farmId
  * @throws Scalr_Exception_InsufficientPermissions
  */
 public function xGetOwnerHistoryAction($farmId)
 {
     $dbFarm = DBFarm::LoadByID($farmId);
     $this->user->getPermissions()->validate($dbFarm);
     $this->request->checkPermissions($dbFarm->__getNewFarmObject(), Acl::PERM_FARMS_UPDATE);
     if ($dbFarm->ownerId == $this->user->getId() || $this->request->hasPermissions($dbFarm->__getNewFarmObject(), Acl::PERM_FARMS_CHANGE_OWNERSHIP)) {
         $data = array_map(function ($item) {
             $item['dtTm'] = Scalr_Util_DateTime::convertTz($item['dt']);
             return $item;
         }, Entity\FarmSetting::getOwnerHistory(Entity\Farm::findPk($farmId)));
         $this->response->data(['history' => $data]);
     } else {
         throw new Scalr_Exception_InsufficientPermissions();
     }
 }
示例#19
0
 /**
  * {@inheritdoc}
  * @see \Scalr\System\Zmq\Cron\TaskInterface::worker()
  */
 public function worker($request)
 {
     $db = \Scalr::getDb();
     //Warming up static DI cache
     \Scalr::getContainer()->warmup();
     // Reconfigure observers
     \Scalr::ReconfigureObservers();
     $bundleTask = BundleTask::LoadById($request->bundleTaskId);
     if (!$bundleTask instanceof BundleTask) {
         $this->getLogger()->fatal("Could not load bundle task id: %s", $request->bundleTaskId);
         return false;
     } else {
         $this->bundleTask = $bundleTask;
         $this->getLogger()->info("Processing bundle task id: %d status: %s serverid: %s", $bundleTask->id, $bundleTask->status, $bundleTask->serverId);
     }
     try {
         $dbServer = DBServer::LoadByID($bundleTask->serverId);
     } catch (\Scalr\Exception\ServerNotFoundException $e) {
         if (!$bundleTask->snapshotId && $bundleTask->bundleType != \SERVER_SNAPSHOT_CREATION_TYPE::GCE_WINDOWS) {
             $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::FAILED;
             $bundleTask->setDate('finished');
             $bundleTask->failureReason = sprintf(_("Server '%s' was terminated during snapshot creation process"), $bundleTask->serverId);
             $bundleTask->Save();
             return;
         }
         $this->getLogger()->warn("Could not load server: %s. %s says: %s", $bundleTask->serverId, get_class($e), $e->getMessage());
     } catch (Exception $e) {
         $this->getLogger()->warn("Could not load server: %s. %s says: %s", $bundleTask->serverId, get_class($e), $e->getMessage());
     }
     switch ($bundleTask->status) {
         case SERVER_SNAPSHOT_CREATION_STATUS::ESTABLISHING_COMMUNICATION:
             $conn = @fsockopen($dbServer->getSzrHost(), $dbServer->getPort(DBServer::PORT_CTRL), $errno, $errstr, 10);
             if ($conn) {
                 $dbServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_OUT_CONNECTION, 1);
                 $this->bundleTaskLog("Outbound connection successfully established. Awaiting user action: prebuild automation selection");
                 $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::AWAITING_USER_ACTION;
                 $this->bundleTaskLog(sprintf(_("Bundle task status: %s"), $bundleTask->status));
                 $bundleTask->Save();
             } else {
                 $errstr = sprintf("Unable to establish outbound (Scalr -> Scalarizr) communication (%s:%s): %s.", $dbServer->getSzrHost(), $dbServer->getPort(DBServer::PORT_CTRL), $errstr);
                 $errMsg = $dbServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_OUT_CONNECTION_ERROR);
                 if (!$errMsg || $errstr != $errMsg) {
                     $dbServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_OUT_CONNECTION_ERROR, $errstr);
                     $this->bundleTaskLog("{$errstr} Will try again in a few minutes.");
                 }
             }
             return false;
         case SERVER_SNAPSHOT_CREATION_STATUS::AWAITING_USER_ACTION:
             //nothing to do;
             return false;
         case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
             $bundleTask->setDate('started');
         case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
         case SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE:
             if (!PlatformFactory::NewPlatform($dbServer->platform)->GetServerID($dbServer)) {
                 $this->bundleTaskLog(sprintf(_("Waiting for temporary server")));
                 return false;
             }
             $status = PlatformFactory::NewPlatform($dbServer->platform)->GetServerRealStatus($dbServer);
             if ($status->isPending()) {
                 //Server is in pensing state
                 $this->bundleTaskLog(sprintf(_("Server status: %s"), $status->getName()));
                 $this->bundleTaskLog(sprintf(_("Waiting for running state."), $status->getName()));
                 return false;
             } elseif ($status->isTerminated()) {
                 $this->bundleTaskLog(sprintf(_("Server status: %s"), $status->getName()));
                 $dbServer->status = SERVER_STATUS::TERMINATED;
                 $dbServer->save();
                 $bundleTask->SnapshotCreationFailed("Server was terminated and no longer available in cloud.");
                 return false;
             }
             break;
     }
     $this->getLogger()->info("Continue bundle task id:%d status:%s", $bundleTask->id, $bundleTask->status);
     switch ($bundleTask->status) {
         case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
             $ips = PlatformFactory::NewPlatform($dbServer->platform)->GetServerIPAddresses($dbServer);
             $dbServer->remoteIp = $ips['remoteIp'];
             $dbServer->localIp = $ips['localIp'];
             $dbServer->save();
             $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV;
             $bundleTask->save();
             $this->bundleTaskLog(sprintf(_("Bundle task status: %s"), $bundleTask->status));
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
             $this->bundleTaskLog(sprintf(_("Initializing SSH2 session to the server")));
             if ($dbServer->platform == SERVER_PLATFORMS::IDCF && !$dbServer->remoteIp) {
                 try {
                     $this->bundleTaskLog("Creating port forwarding rules to be able to connect to the server by SSH");
                     $environment = $dbServer->GetEnvironmentObject();
                     $cloudLocation = $dbServer->GetCloudLocation();
                     $platform = PlatformFactory::NewPlatform($dbServer->platform);
                     $ccProps = $environment->keychain($dbServer->platform)->properties;
                     $sharedIpId = $ccProps[CloudCredentialsProperty::CLOUDSTACK_SHARED_IP_ID . ".{$cloudLocation}"];
                     $sharedIp = $ccProps[CloudCredentialsProperty::CLOUDSTACK_SHARED_IP . ".{$cloudLocation}"];
                     $this->bundleTaskLog("Shared IP: {$sharedIp}");
                     $cs = $environment->cloudstack($dbServer->platform);
                     // Create port forwarding rules for scalarizr
                     $port = $ccProps[CloudCredentialsProperty::CLOUDSTACK_SZR_PORT_COUNTER . ".{$cloudLocation}.{$sharedIpId}"];
                     if (!$port) {
                         $port1 = 30000;
                         $port2 = 30001;
                         $port3 = 30002;
                         $port4 = 30003;
                     } else {
                         $port1 = $port + 1;
                         $port2 = $port1 + 1;
                         $port3 = $port2 + 1;
                         $port4 = $port3 + 1;
                     }
                     $virtualmachineid = $dbServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID);
                     $result2 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 8014, 'protocol' => "udp", 'publicport' => $port1, 'virtualmachineid' => $virtualmachineid));
                     $result1 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 8013, 'protocol' => "tcp", 'publicport' => $port1, 'virtualmachineid' => $virtualmachineid));
                     $result3 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 8010, 'protocol' => "tcp", 'publicport' => $port3, 'virtualmachineid' => $virtualmachineid));
                     $result4 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 8008, 'protocol' => "tcp", 'publicport' => $port2, 'virtualmachineid' => $virtualmachineid));
                     $result5 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 22, 'protocol' => "tcp", 'publicport' => $port4, 'virtualmachineid' => $virtualmachineid));
                     $dbServer->SetProperties(array(SERVER_PROPERTIES::SZR_CTRL_PORT => $port1, SERVER_PROPERTIES::SZR_SNMP_PORT => $port1, SERVER_PROPERTIES::SZR_API_PORT => $port3, SERVER_PROPERTIES::SZR_UPDC_PORT => $port2, SERVER_PROPERTIES::CUSTOM_SSH_PORT => $port4));
                     $dbServer->remoteIp = $sharedIp;
                     $dbServer->Save();
                     $ccProps->saveSettings([CloudCredentialsProperty::CLOUDSTACK_SZR_PORT_COUNTER . ".{$cloudLocation}.{$sharedIpId}" => $port4]);
                 } catch (Exception $e) {
                     $this->bundleTaskLog("Unable to create port-forwarding rules: {$e->getMessage()}");
                 }
                 return false;
             }
             try {
                 $ssh2Client = $dbServer->GetSsh2Client();
                 $ssh2Client->connect($dbServer->remoteIp, $dbServer->getPort(DBServer::PORT_SSH));
             } catch (Exception $e) {
                 $this->bundleTaskLog(sprintf(_("Scalr unable to establish SSH connection with server on %:%. Error: %s"), $dbServer->remoteIp, $dbServer->getPort(DBServer::PORT_SSH), $e->getMessage()));
                 //TODO: Set status of bundle log to failed
                 return false;
             }
             $this->bundleTaskLog(sprintf(_("Created SSH session. Username: %s"), $ssh2Client->getLogin()));
             //Prepare script
             $this->bundleTaskLog(sprintf(_("Uploading builder scripts...")));
             $behaviors = $dbServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR);
             try {
                 if ($dbServer->isOpenstack()) {
                     $platform = SERVER_PLATFORMS::OPENSTACK;
                 } else {
                     $platform = $dbServer->platform;
                 }
                 $baseUrl = rtrim(\Scalr::config('scalr.endpoint.scheme') . "://" . \Scalr::config('scalr.endpoint.host'), '/');
                 $options = array('server-id' => $dbServer->serverId, 'role-name' => $bundleTask->roleName, 'crypto-key' => $dbServer->GetProperty(SERVER_PROPERTIES::SZR_KEY), 'platform' => $platform, 'queryenv-url' => $baseUrl . "/query-env", 'messaging-p2p.producer-url' => $baseUrl . "/messaging", 'behaviour' => trim(trim(str_replace("base", "", $behaviors), ",")), 'env-id' => $dbServer->envId, 'region' => $dbServer->GetCloudLocation(), 'scalr-id' => SCALR_ID);
                 $command = 'scalarizr --import -y';
                 foreach ($options as $k => $v) {
                     $command .= sprintf(' -o %s=%s', $k, $v);
                 }
                 if ($dbServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_MYSQL_SERVER_TYPE) == 'percona') {
                     $recipes = 'mysql=percona';
                 } else {
                     $recipes = '';
                 }
                 $scalarizrBranch = $dbServer->GetProperty(SERVER_PROPERTIES::SZR_DEV_SCALARIZR_BRANCH);
                 $scriptContents = @file_get_contents(APPPATH . "/templates/services/role_builder/chef_import.tpl");
                 /*
                 %CHEF_SERVER_URL%
                 %CHEF_VALIDATOR_NAME%
                 %CHEF_VALIDATOR_KEY%
                 %CHEF_ENVIRONMENT%
                 %CHEF_ROLE_NAME%
                 */
                 $chefServerId = $dbServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_CHEF_SERVER_ID);
                 if ($chefServerId) {
                     $chefServerInfo = $db->GetRow("SELECT * FROM services_chef_servers WHERE id=?", array($chefServerId));
                     $chefServerInfo['v_auth_key'] = \Scalr::getContainer()->crypto->decrypt($chefServerInfo['v_auth_key']);
                 }
                 $scriptContents = str_replace(array("%PLATFORM%", "%BEHAVIOURS%", "%SZR_IMPORT_STRING%", "%DEV%", "%SCALARIZR_BRANCH%", "%RECIPES%", "%BUILD_ONLY%", "%CHEF_SERVER_URL%", "%CHEF_VALIDATOR_NAME%", "%CHEF_VALIDATOR_KEY%", "%CHEF_ENVIRONMENT%", "%CHEF_ROLE%", "%CHEF_ROLE_NAME%", "%CHEF_NODE_NAME%", "\r\n"), array($platform, trim(str_replace("base", "", str_replace(",", " ", $behaviors))), $command, $scalarizrBranch ? '1' : '0', $scalarizrBranch, $recipes, '0', $chefServerInfo['url'], $chefServerInfo['v_username'], $chefServerInfo['v_auth_key'], $dbServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ENVIRONMENT), $dbServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ROLE_NAME), $dbServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ROLE_NAME), '', "\n"), $scriptContents);
                 if (!$ssh2Client->sendFile('/tmp/scalr-builder.sh', $scriptContents, "w+", false)) {
                     throw new Exception("Cannot upload script");
                 }
                 /*
                 $this->bundleTaskLog(sprintf(_("Uploading chef recipes...")));
                 if (!$ssh2Client->sendFile('/tmp/recipes.tar.gz', APPPATH . '/www/storage/chef/recipes.tar.gz')) {
                     throw new Exception("Cannot upload chef recipes");
                 }
                 */
             } catch (Exception $e) {
                 $this->bundleTaskLog(sprintf(_("Scripts upload failed: %s"), $e->getMessage()));
                 //TODO: Set status of bundle log to failed
                 return false;
             }
             $this->bundleTaskLog("Launching role builder routines on server");
             $ssh2Client->exec("chmod 0777 /tmp/scalr-builder.sh");
             // For CGE we need to use sudo
             if ($bundleTask->platform == SERVER_PLATFORMS::GCE || $bundleTask->osFamily == 'amazon') {
                 $shell = $ssh2Client->getShell();
                 @stream_set_blocking($shell, true);
                 @stream_set_timeout($shell, 5);
                 @fwrite($shell, "sudo touch /var/log/role-builder-output.log 2>&1" . PHP_EOL);
                 $output = @fgets($shell, 4096);
                 $this->bundleTaskLog("Verbose 1: {$output}");
                 @fwrite($shell, "sudo chmod 0666 /var/log/role-builder-output.log 2>&1" . PHP_EOL);
                 $output2 = @fgets($shell, 4096);
                 $this->bundleTaskLog("Verbose 2: {$output2}");
                 @fwrite($shell, "sudo setsid /tmp/scalr-builder.sh > /var/log/role-builder-output.log 2>&1 &" . PHP_EOL);
                 $output3 = @fgets($shell, 4096);
                 $this->bundleTaskLog("Verbose 3: {$output3}");
                 sleep(5);
                 $meta = stream_get_meta_data($shell);
                 $this->bundleTaskLog(sprintf("Verbose (Meta): %s", json_encode($meta)));
                 $i = 4;
                 if ($meta['eof'] == false && $meta['unread_bytes'] != 0) {
                     $output4 = @fread($shell, $meta['unread_bytes']);
                     $this->bundleTaskLog("Verbose {$i}: {$output4}");
                     $meta = stream_get_meta_data($shell);
                     $this->bundleTaskLog(sprintf("Verbose (Meta): %s", json_encode($meta)));
                 }
                 @fclose($shell);
                 /*
                 $r1 = $ssh2Client->exec("sudo touch /var/log/role-builder-output.log");
                 $this->bundleTaskLog("1: {$r1} ({$ssh2Client->stdErr})");
                 $r2 = $ssh2Client->exec("sudo chmod 0666 /var/log/role-builder-output.log");
                 $this->bundleTaskLog("2: {$r2} ({$ssh2Client->stdErr})");
                 $r3 = $ssh2Client->exec("sudo setsid /tmp/scalr-builder.sh > /var/log/role-builder-output.log 2>&1 &");
                 $this->bundleTaskLog("3: {$r3} ({$ssh2Client->stdErr})");
                 */
             } else {
                 $ssh2Client->exec("setsid /tmp/scalr-builder.sh > /var/log/role-builder-output.log 2>&1 &");
             }
             $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE;
             $bundleTask->save();
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE:
             try {
                 $ssh2Client = $dbServer->GetSsh2Client();
                 $ssh2Client->connect($dbServer->remoteIp, $dbServer->getPort(DBServer::PORT_SSH));
             } catch (Exception $e) {
                 $this->bundleTaskLog(sprintf(_("Scalr unable to establish SSH connection with server on %:%. Error: %s"), $dbServer->remoteIp, $dbServer->getPort(DBServer::PORT_SSH), $e->getMessage()));
                 //TODO: Set status of bundle log to failed
                 return false;
             }
             $log = $ssh2Client->getFile('/var/log/role-builder-output.log');
             $log_lines = explode("\r\n", $log);
             $last_msg = $dbServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_LAST_LOG_MESSAGE);
             while ($msg = trim(array_shift($log_lines))) {
                 if (substr($msg, -1, 1) != ']') {
                     continue;
                 }
                 if ($last_msg) {
                     if ($msg != $last_msg) {
                         continue;
                     } elseif ($msg == $last_msg) {
                         $last_msg = null;
                         continue;
                     }
                 }
                 if (stristr($msg, '[ Failed ]')) {
                     $stepLog = $ssh2Client->getFile('/var/log/role-builder-step.log');
                     $this->bundleTaskLog(sprintf("role-builder-step.log: %s", $stepLog));
                     $bundleTask->SnapshotCreationFailed($msg);
                 } else {
                     $this->bundleTaskLog($msg);
                     $dbServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_LAST_LOG_MESSAGE, $msg);
                 }
             }
             //Read /var/log/role-builder-output.log
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::PENDING:
             try {
                 $platformModule = PlatformFactory::NewPlatform($bundleTask->platform);
                 $platformModule->CreateServerSnapshot($bundleTask);
             } catch (Exception $e) {
                 $this->getLogger()->error($e->getMessage());
                 $bundleTask->SnapshotCreationFailed($e->getMessage());
             }
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING:
             $addedTime = strtotime($bundleTask->dateAdded);
             if ($addedTime + 3600 < time()) {
                 $bundleTask->SnapshotCreationFailed("Server didn't send PrepareBundleResult message in time.");
             }
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS:
             PlatformFactory::NewPlatform($bundleTask->platform)->CheckServerSnapshotStatus($bundleTask);
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::CREATING_ROLE:
             try {
                 if ($bundleTask->object == BundleTask::BUNDLETASK_OBJECT_IMAGE) {
                     if ($bundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_ALL) {
                         $dbRole = $dbServer->GetFarmRoleObject()->GetRoleObject();
                         $dbRole->__getNewRoleObject()->setImage($bundleTask->platform, $bundleTask->cloudLocation, $bundleTask->snapshotId, $bundleTask->createdById, $bundleTask->createdByEmail);
                         $this->bundleTaskLog(sprintf(_("Image replacement completed.")));
                     }
                     $this->bundleTaskLog(sprintf(_("Bundle task completed.")));
                     $bundleTask->setDate('finished');
                     $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                     $bundleTask->Save();
                 } elseif ($bundleTask->object == BundleTask::BUNDLETASK_OBJECT_ROLE) {
                     if ($bundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_ALL) {
                         $saveOldRole = false;
                         try {
                             $dbRole = $dbServer->GetFarmRoleObject()->GetRoleObject();
                             if ($dbRole->name == $bundleTask->roleName && $dbRole->envId == $bundleTask->envId) {
                                 $saveOldRole = true;
                             }
                         } catch (Exception $e) {
                             //NO OLD ROLE
                         }
                         if ($dbRole && $saveOldRole) {
                             if ($dbServer) {
                                 $new_role_name = BundleTask::GenerateRoleName($dbServer->GetFarmRoleObject(), $dbServer);
                             } else {
                                 $new_role_name = $bundleTask->roleName . "-" . rand(1000, 9999);
                             }
                             $dbRole->name = $new_role_name;
                             $this->bundleTaskLog(sprintf(_("Old role '%s' (ID: %s) renamed to '%s'"), $bundleTask->roleName, $dbRole->id, $new_role_name));
                             $dbRole->save();
                         }
                     }
                     try {
                         $DBRole = DBRole::createFromBundleTask($bundleTask);
                     } catch (Exception $e) {
                         $bundleTask->SnapshotCreationFailed("Role creation failed due to internal error ({$e->getMessage()}). Please try again.");
                         return;
                     }
                     if ($bundleTask->replaceType == SERVER_REPLACEMENT_TYPE::NO_REPLACE) {
                         $bundleTask->setDate('finished');
                         $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                         $this->bundleTaskLog(sprintf(_("Replacement type: %s. Bundle task status: %s"), SERVER_REPLACEMENT_TYPE::NO_REPLACE, SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS));
                     } else {
                         try {
                             $this->bundleTaskLog(sprintf(_("Replacement type: %s"), $bundleTask->replaceType));
                             $r_farm_roles = array();
                             try {
                                 $DBFarm = DBFarm::LoadByID($bundleTask->farmId);
                             } catch (Exception $e) {
                                 if (stristr($e->getMessage(), "not found in database")) {
                                     $bundleTask->SnapshotCreationFailed("Farm was removed before task was finished");
                                 }
                                 return;
                             }
                             if ($bundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_FARM) {
                                 try {
                                     $r_farm_roles[] = $DBFarm->GetFarmRoleByRoleID($bundleTask->prototypeRoleId);
                                 } catch (Exception $e) {
                                 }
                             } elseif ($bundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_ALL) {
                                 /* @var $user Entity\Account\User */
                                 $user = Entity\Account\User::findPk($bundleTask->createdById);
                                 /* @var $env Entity\Account\Environment */
                                 $env = Entity\Account\Environment::findPk($bundleTask->envId);
                                 /* @var $acl Acl */
                                 $acl = Scalr::getContainer()->acl;
                                 $sql = "SELECT fr.id FROM farm_roles fr JOIN farms f ON f.id = fr.farmid WHERE fr.role_id=? AND f.env_id = ?";
                                 $args = [$bundleTask->prototypeRoleId, $bundleTask->envId];
                                 if (!$acl->isUserAllowedByEnvironment($user, $env, Acl::RESOURCE_FARMS, Acl::PERM_FARMS_UPDATE)) {
                                     $q = [];
                                     if ($acl->isUserAllowedByEnvironment($user, $env, Acl::RESOURCE_TEAM_FARMS, Acl::PERM_FARMS_UPDATE)) {
                                         $q[] = Entity\Farm::getUserTeamOwnershipSql($user->id);
                                     }
                                     if ($acl->isUserAllowedByEnvironment($user, $env, Acl::RESOURCE_OWN_FARMS, Acl::PERM_FARMS_UPDATE)) {
                                         $q[] = "f.created_by_id = ?";
                                         $args[] = $user->getId();
                                     }
                                     if (count($q)) {
                                         $sql .= ' AND (' . join(' OR ', $q) . ')';
                                     } else {
                                         $sql .= ' AND 0';
                                         // no permissions
                                     }
                                 }
                                 $farm_roles = $db->GetAll($sql, $args);
                                 foreach ($farm_roles as $farm_role) {
                                     try {
                                         $r_farm_roles[] = DBFarmRole::LoadByID($farm_role['id']);
                                     } catch (Exception $e) {
                                     }
                                 }
                             }
                             foreach ($r_farm_roles as $DBFarmRole) {
                                 if ($DBFarmRole->CloudLocation != $bundleTask->cloudLocation) {
                                     $this->bundleTaskLog(sprintf("Role '%s' (ID: %s), farm '%s' (ID: %s) using the same role " . "but in abother cloud location. Skiping it.", $DBFarmRole->GetRoleObject()->name, $DBFarmRole->ID, $DBFarmRole->GetFarmObject()->Name, $DBFarmRole->FarmID));
                                 } else {
                                     $DBFarmRole->RoleID = $bundleTask->roleId;
                                     $DBFarmRole->Save();
                                 }
                             }
                             $this->bundleTaskLog(sprintf(_("Replacement completed. Bundle task completed.")));
                             try {
                                 if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                                     $dbServer->Remove();
                                 } elseif ($dbServer->status == SERVER_STATUS::TEMPORARY) {
                                     $this->bundleTaskLog("Terminating temporary server");
                                     $dbServer->terminate(DBServer::TERMINATE_REASON_TEMPORARY_SERVER_ROLE_BUILDER);
                                     $this->bundleTaskLog("Termination request has been sent");
                                 }
                             } catch (Exception $e) {
                                 $this->bundleTaskLog("Warning: {$e->getMessage()}");
                             }
                             $bundleTask->setDate('finished');
                             $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                             $bundleTask->Save();
                         } catch (Exception $e) {
                             $this->getLogger()->error($e->getMessage());
                             $this->bundleTaskLog(sprintf(_("Server replacement failed: %s"), $e->getMessage()));
                             $bundleTask->setDate('finished');
                             $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                         }
                     }
                 }
                 if ($bundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS) {
                     try {
                         if ($dbServer->status == SERVER_STATUS::IMPORTING) {
                             $dbServer->Remove();
                         } elseif ($dbServer->status == SERVER_STATUS::TEMPORARY) {
                             $this->bundleTaskLog("Terminating temporary server");
                             $dbServer->terminate(DBServer::TERMINATE_REASON_TEMPORARY_SERVER_ROLE_BUILDER);
                             $this->bundleTaskLog("Termination request has been sent");
                         }
                     } catch (Exception $e) {
                         $this->bundleTaskLog("Warning: {$e->getMessage()}");
                     }
                 }
                 $bundleTask->Save();
             } catch (Exception $e) {
                 $this->getLogger()->error($e->getMessage());
             }
             break;
     }
     return $request;
 }
示例#20
0
文件: Server.php 项目: scalr/scalr
 /**
  * Get Farm entity
  *
  * @return Farm|null    Returns the Farm entity which is associated with the Server or null otherwise
  */
 public function getFarm()
 {
     if (empty($this->_farm) && !empty($this->farmId)) {
         $this->_farm = Farm::findPk($this->farmId);
     }
     return $this->_farm;
 }
示例#21
0
文件: Users.php 项目: scalr/scalr
 /**
  * @param   JsonData    $ids
  * @param   string      $action
  * @param   int         $ownerId
  */
 public function xGroupActionHandlerAction(JsonData $ids, $action, $ownerId = null)
 {
     $processed = array();
     $errors = array();
     $needUpdateFarmOwner = false;
     $actionMsg = '';
     $ids = (array) $ids;
     if ($ownerId && !User::findOne([['id' => $ownerId], ['accountId' => $this->user->getAccountId()]])) {
         $ownerId = null;
     }
     foreach ($ids as $userId) {
         try {
             $user = Scalr_Account_User::init();
             $user->loadById($userId);
             switch ($action) {
                 case 'delete':
                     $actionMsg = 'removed';
                     if ($this->user->canRemoveUser($user)) {
                         $ownedFarms = Farm::findByOwnerId($user->id);
                         if ($ownedFarms->count() > 0) {
                             if ($ownerId) {
                                 /* @var $newOwner User */
                                 $newOwner = User::findPk($ownerId);
                                 /* @var $u User */
                                 $u = User::findPk($this->user->getId());
                                 foreach ($ownedFarms as $farm) {
                                     /* @var $farm Farm */
                                     FarmSetting::addOwnerHistory($farm, $newOwner, $u);
                                     $farm->ownerId = $ownerId;
                                     $farm->save();
                                 }
                             } else {
                                 $needUpdateFarmOwner = true;
                                 throw new Exception("You can't delete owner of the Farm");
                             }
                         }
                         $user->delete();
                         $processed[] = $user->getId();
                     } else {
                         throw new Exception('Insufficient permissions to remove user');
                     }
                     break;
                 case 'activate':
                     $actionMsg = 'activated';
                     if ($this->user->getId() !== $user->getId() && $this->user->canEditUser($user)) {
                         if ($user->status == Scalr_Account_User::STATUS_ACTIVE) {
                             throw new Scalr_Exception_Core('User(s) has already been activated');
                         }
                         $user->status = Scalr_Account_User::STATUS_ACTIVE;
                         $user->save();
                         $processed[] = $user->getId();
                     } else {
                         throw new Scalr_Exception_Core('Insufficient permissions to activate user');
                     }
                     break;
                 case 'deactivate':
                     $actionMsg = 'deactivated';
                     if ($this->user->getId() !== $user->getId() && $this->user->canEditUser($user)) {
                         if ($user->status == Scalr_Account_User::STATUS_INACTIVE) {
                             throw new Scalr_Exception_Core('User(s) has already been suspended');
                         }
                         $user->status = Scalr_Account_User::STATUS_INACTIVE;
                         $user->save();
                         $processed[] = $user->getId();
                     } else {
                         throw new Scalr_Exception_Core('Insufficient permissions to deactivate user');
                     }
                     break;
             }
         } catch (Exception $e) {
             $errors[] = $e->getMessage();
         }
     }
     $num = count($ids);
     if (count($processed) == $num) {
         $this->response->success("Selected user(s) successfully {$actionMsg}");
     } else {
         array_walk($errors, function (&$item) {
             $item = '- ' . $item;
         });
         $this->response->warning(sprintf("Successfully {$actionMsg} only %d from %d users. \nFollowing errors occurred:\n%s", count($processed), $num, join(array_unique($errors), "\n")));
     }
     $this->response->data(['processed' => $processed]);
     if ($needUpdateFarmOwner) {
         $users = [];
         foreach (User::findByAccountId($this->user->getAccountId()) as $user) {
             /* @var $user User */
             if (in_array($user->id, $ids)) {
                 continue;
             }
             $users[] = ['id' => $user->id, 'email' => $user->email];
         }
         $this->response->data(['needUpdateFarmOwner' => $needUpdateFarmOwner, 'usersList' => $users]);
     }
 }
示例#22
0
文件: Logs.php 项目: 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;
 }
示例#23
0
文件: Import.php 项目: scalr/scalr
 /**
  * @param   string  $platform       Name of platform
  * @param   string  $cloudLocation  Name of location
  * @param   array   $ids            The list of the identifiers of the cloud instances
  * @param   int     $roleId         optional    Identifier of Role
  * @return  array
  * @throws  Exception
  */
 public function checkStatus($platform, $cloudLocation, $ids, $roleId = null)
 {
     if (!in_array($platform, $this->allowedPlatforms)) {
         throw new Exception(sprintf("Platform '%s' is not supported", $platform));
     }
     if (!$this->environment->isPlatformEnabled($platform)) {
         throw new Exception(sprintf("Platform '%s' is not enabled", $platform));
     }
     if (empty($ids) || empty($ids[0])) {
         throw new Exception("You should provide at least one instanceId");
     }
     $instances = PlatformFactory::NewPlatform($platform)->getOrphanedServers($this->getEnvironmentEntity(), $cloudLocation, $ids);
     $status = ['instances' => $instances];
     $imageIds = array_unique(array_map(function ($item) {
         return $item->imageId;
     }, $instances));
     if (count($imageIds) != 1) {
         $status['compatibility'] = ['success' => false];
         return $status;
     }
     /* @var $instance OrphanedServer */
     $instance = $instances[0];
     $status['compatibility'] = ['success' => true];
     // Check vpc compatibility
     if ($instance->vpcId && $instance->subnetId) {
         $gov = new Scalr_Governance($this->getEnvironmentId());
         $vpcGovernanceRegions = $gov->getValue(SERVER_PLATFORMS::EC2, Scalr_Governance::AWS_VPC, 'regions');
         if (isset($vpcGovernanceRegions)) {
             if (!array_key_exists($cloudLocation, $vpcGovernanceRegions)) {
                 $status['compatibility']['success'] = false;
                 $status['compatibility']['message'] = sprintf('Region <b>%s</b> is not allowed by the Governance.', $cloudLocation);
             } else {
                 $vpcGovernanceIds = $vpcGovernanceRegions[$cloudLocation]['ids'];
                 if (!empty($vpcGovernanceIds) && !in_array($instance->vpcId, $vpcGovernanceIds)) {
                     $status['compatibility']['success'] = false;
                     $status['compatibility']['message'] = sprintf('VPC <b>%s</b> is not allowed by the Governance.', $instance->vpcId);
                 } else {
                     $vpcGovernanceIds = $gov->getValue(SERVER_PLATFORMS::EC2, Scalr_Governance::AWS_VPC, 'ids');
                     /* @var $platformObject Ec2PlatformModule */
                     $platformObject = PlatformFactory::NewPlatform(SERVER_PLATFORMS::EC2);
                     $subnet = $platformObject->listSubnets($this->getEnvironment(), $cloudLocation, $instance->vpcId, true, $instance->subnetId);
                     if (isset($vpcGovernanceIds[$instance->vpcId])) {
                         if (!empty($vpcGovernanceIds[$instance->vpcId]) && is_array($vpcGovernanceIds[$instance->vpcId]) && !in_array($instance->subnetId, $vpcGovernanceIds[$instance->vpcId])) {
                             $status['compatibility']['success'] = false;
                             $status['compatibility']['message'] = sprintf('Subnet <b>%s</b> is prohibited by the Governance.', $instance->subnetId);
                         } else {
                             if ($vpcGovernanceIds[$instance->vpcId] == "outbound-only" && $subnet['type'] != 'private') {
                                 $status['compatibility']['success'] = false;
                                 $status['compatibility']['message'] = 'Only private subnets are allowed by the Governance.';
                             } else {
                                 if ($vpcGovernanceIds[$instance->vpcId] == "full" && $subnet['type'] != 'public') {
                                     $status['compatibility']['success'] = false;
                                     $status['compatibility']['message'] = 'Only public subnets are allowed by the Governance.';
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if (!$status['compatibility']['success']) {
         return $status;
     }
     $scopeCriteria = ['$or' => [['accountId' => null], ['$and' => [['accountId' => $this->getUser()->accountId], ['$or' => [['envId' => null], ['envId' => $this->getEnvironment()->id]]]]]]];
     /* @var $image Entity\Image */
     $image = Entity\Image::findOne([['platform' => $platform], ['cloudLocation' => $cloudLocation], ['id' => $instance->imageId], $scopeCriteria]);
     $status['image'] = ['success' => !!$image, 'data' => ['id' => $instance->imageId]];
     if ($image) {
         if ($image->isScalarized) {
             $status['image']['success'] = false;
             $status['image']['isScalarized'] = true;
             return $status;
         }
         $status['image']['data'] = ['hash' => $image->hash, 'name' => $image->name, 'id' => $image->id, 'scope' => $image->getScope()];
         $criteria = [['platform' => $platform], ['imageId' => $image->id]];
         if (!($platform == SERVER_PLATFORMS::GCE || $platform == SERVER_PLATFORMS::AZURE)) {
             $criteria[] = ['cloudLocation' => $cloudLocation];
         }
         $roleIds = [];
         foreach (Entity\RoleImage::find($criteria) as $ri) {
             $roleIds[] = $ri->roleId;
         }
         if (count($roleIds)) {
             $roles = Entity\Role::find([['id' => ['$in' => $roleIds]], ['isScalarized' => false], $scopeCriteria]);
             $status['role'] = ['availableRoles' => [], 'image' => Scalr_UI_Controller_Images::controller()->convertEntityToArray($image)];
             $selectedRole = null;
             if (count($roles) == 1) {
                 $selectedRole = $roles->current();
             } else {
                 if ($roleId && in_array($roleId, $roleIds)) {
                     foreach ($roles as $role) {
                         /* @var $role Entity\Role */
                         if ($role->id == $roleId) {
                             $selectedRole = $role;
                             break;
                         }
                     }
                 }
             }
             foreach ($roles as $role) {
                 /* @var $role Entity\Role */
                 $status['role']['availableRoles'][] = ['id' => $role->id, 'name' => $role->name, 'scope' => $role->getScope()];
             }
             if ($selectedRole) {
                 $status['role']['success'] = true;
                 $status['role']['data'] = ['id' => $selectedRole->id, 'name' => $selectedRole->name, 'scope' => $selectedRole->getScope()];
                 $farms = [];
                 $status['farmrole'] = ['instance' => ['instanceType' => $instance->instanceType, 'vpcId' => $instance->vpcId, 'subnetId' => $instance->subnetId, 'roleName' => $selectedRole->name]];
                 foreach (Entity\Farm::find([['envId' => $this->getEnvironment()->id], ['status' => FARM_STATUS::RUNNING]]) as $farm) {
                     /* @var $farm Entity\Farm */
                     if ($this->request->hasPermissions($farm, Acl::PERM_FARMS_UPDATE) && $this->request->hasPermissions($farm, Acl::PERM_FARMS_SERVERS)) {
                         // cloud specific (EC2)
                         if ($farm->settings[Entity\FarmSetting::EC2_VPC_ID] == $instance->vpcId) {
                             $farms[$farm->id] = ['id' => $farm->id, 'name' => $farm->name, 'farmroles' => []];
                         }
                     }
                 }
                 foreach (Entity\FarmRole::find([['farmId' => ['$in' => array_keys($farms)]], ['roleId' => $selectedRole->id]]) as $farmRole) {
                     /* @var $farmRole Entity\FarmRole */
                     if (isset($farms[$farmRole->farmId])) {
                         if (!$instance->subnetId || $instance->subnetId && in_array($instance->subnetId, json_decode($farmRole->settings[Entity\FarmRoleSetting::AWS_VPC_SUBNET_ID]))) {
                             $farms[$farmRole->farmId]['farmroles'][] = ['id' => $farmRole->id, 'name' => $farmRole->alias, 'tags' => $farmRole->getCloudTags(true)];
                         }
                     }
                 }
                 $status['farmrole']['data'] = array_values($farms);
                 $status['farmrole']['success'] = false;
             } else {
                 if (count($roles) > 1) {
                     $status['role']['success'] = false;
                 } else {
                     $status['role']['success'] = false;
                 }
             }
         } else {
             $status['role']['success'] = false;
             $status['role']['image'] = Scalr_UI_Controller_Images::controller()->convertEntityToArray($image);
         }
     }
     return $status;
 }
示例#24
0
文件: Core.php 项目: mheydt/scalr
 /**
  * @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)]);
 }
示例#25
0
文件: Scripts.php 项目: scalr/scalr
 /**
  * @param int $farmId
  * @param int $farmRoleId optional
  * @param string $serverId optional
  * @param int $scriptId optional
  * @param string $scriptPath optional
  * @param int $scriptIsSync
  * @param int $scriptTimeout
  * @param int $scriptVersion
  * @param array $scriptParams optional
  * @param int $shortcutId optional
  * @param int $editShortcut optional
  * @throws Exception
  */
 public function xExecuteAction($farmId, $farmRoleId = 0, $serverId = '', $scriptId = 0, $scriptPath = '', $scriptIsSync, $scriptTimeout, $scriptVersion, array $scriptParams = [], $shortcutId = null, $editShortcut = null)
 {
     $this->request->restrictAccess(Acl::RESOURCE_SCRIPTS_ENVIRONMENT, Acl::PERM_SCRIPTS_ENVIRONMENT_EXECUTE);
     if ($serverId) {
         $dbServer = DBServer::LoadByID($serverId);
         $this->user->getPermissions()->validate($dbServer);
         $target = Script::TARGET_INSTANCE;
         $serverId = $dbServer->serverId;
         $farmRoleId = $dbServer->farmRoleId;
         $farmId = $dbServer->farmId;
     } else {
         if ($farmRoleId) {
             $dbFarmRole = DBFarmRole::LoadByID($farmRoleId);
             $this->user->getPermissions()->validate($dbFarmRole);
             $target = Script::TARGET_ROLE;
             $farmRoleId = $dbFarmRole->ID;
             $farmId = $dbFarmRole->FarmID;
         } else {
             if (!$farmId) {
                 $target = Script::TARGET_ALL;
             } else {
                 $dbFarm = DBFarm::LoadByID($farmId);
                 $this->user->getPermissions()->validate($dbFarm);
                 $target = Script::TARGET_FARM;
                 $farmId = $dbFarm->ID;
             }
         }
     }
     if ($farmId) {
         $this->request->checkPermissions(Entity\Farm::findPk($farmId), Acl::PERM_FARMS_SERVERS);
     }
     if ($scriptId) {
         $script = Script::findPk($scriptId);
         /* @var $script Script */
         if (!$script) {
             throw new Scalr_UI_Exception_NotFound();
         }
         $script->checkPermission($this->user, $this->getEnvironmentId());
     } elseif (!$scriptPath) {
         throw new Scalr_Exception_Core('scriptId or scriptPath should be set');
     }
     if (!$scriptTimeout) {
         $scriptTimeout = $scriptIsSync == 1 ? Scalr::config('scalr.script.timeout.sync') : Scalr::config('scalr.script.timeout.async');
     }
     $executeScript = true;
     if ($shortcutId && ($target != Script::TARGET_INSTANCE || $target != Script::TARGET_ALL)) {
         if ($shortcutId == -1) {
             $shortcut = new ScriptShortcut();
             $shortcut->farmId = $farmId;
         } else {
             $shortcut = ScriptShortcut::findPk($shortcutId);
             /* @var $shortcut ScriptShortcut */
             if (!$shortcut) {
                 throw new Scalr_UI_Exception_NotFound();
             }
             if ($editShortcut == 1) {
                 $executeScript = false;
             }
         }
         $shortcut->farmRoleId = $farmRoleId == 0 ? NULL : $farmRoleId;
         if ($scriptId) {
             $shortcut->scriptId = $scriptId;
             $shortcut->scriptPath = '';
         } else {
             $shortcut->scriptPath = $scriptPath;
             $shortcut->scriptId = NULL;
         }
         $shortcut->isSync = $scriptIsSync;
         $shortcut->version = $scriptVersion;
         $shortcut->timeout = $scriptTimeout;
         $shortcut->params = $scriptParams;
         $shortcut->save();
     }
     if ($executeScript) {
         switch ($target) {
             case Script::TARGET_FARM:
                 $servers = $this->db->GetAll("\n                        SELECT server_id\n                        FROM servers\n                        WHERE is_scalarized = 1 AND status IN (?,?) AND farm_id=?", [SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, $farmId]);
                 break;
             case Script::TARGET_ROLE:
                 $servers = $this->db->GetAll("\n                        SELECT server_id\n                        FROM servers\n                        WHERE is_scalarized = 1 AND status IN (?,?) AND farm_roleid=?", [SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, $farmRoleId]);
                 break;
             case Script::TARGET_INSTANCE:
                 $servers = $this->db->GetAll("\n                        SELECT server_id\n                        FROM servers\n                        WHERE is_scalarized = 1 AND status IN (?,?) AND server_id=?", [SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, $serverId]);
                 break;
             case Script::TARGET_ALL:
                 $sql = "\n                        SELECT s.server_id\n                        FROM servers s\n                        JOIN farms f ON f.id = s.farm_id\n                        WHERE s.is_scalarized = 1\n                        AND s.status IN (?,?)\n                        AND s.env_id = ?\n                        AND " . $this->request->getFarmSqlQuery(Acl::PERM_FARMS_SERVERS);
                 $args = [SERVER_STATUS::INIT, SERVER_STATUS::RUNNING, $this->getEnvironmentId()];
                 $servers = $this->db->GetAll($sql, $args);
                 break;
         }
         $scriptSettings = array('version' => $scriptVersion, 'timeout' => $scriptTimeout, 'issync' => $scriptIsSync, 'params' => serialize($scriptParams));
         if ($scriptId) {
             $scriptSettings['scriptid'] = $scriptId;
             $scriptSettings['type'] = Scalr_Scripting_Manager::ORCHESTRATION_SCRIPT_TYPE_SCALR;
         } else {
             $scriptSettings['script_path'] = $scriptPath;
             $scriptSettings['type'] = Scalr_Scripting_Manager::ORCHESTRATION_SCRIPT_TYPE_LOCAL;
         }
         $serializer = Scalr_Messaging_JsonSerializer::getInstance();
         $cryptoTool = \Scalr::getContainer()->srzcrypto;
         // send message to start executing task (starts script)
         if (count($servers) > 0) {
             foreach ($servers as $server) {
                 $DBServer = DBServer::LoadByID($server['server_id']);
                 $msg = new Scalr_Messaging_Msg_ExecScript("Manual");
                 $msg->setServerMetaData($DBServer);
                 $script = Scalr_Scripting_Manager::prepareScript($scriptSettings, $DBServer);
                 if ($script) {
                     $DBServer->executeScript($script, $msg);
                     $this->auditLog("script.execute", $script, $DBServer);
                     $manualLog = new OrchestrationLogManualScript($script['execution_id'], $msg->serverId);
                     $manualLog->userId = $this->getUser()->getId();
                     $manualLog->userEmail = $this->getUser()->getEmail();
                     $manualLog->added = new DateTime('now', new DateTimeZone('UTC'));
                     $manualLog->save();
                 }
             }
         }
         $this->response->success('Script execution has been queued and will occur on the selected instance(s) within a couple of minutes.');
     } else {
         $this->response->success('Script shortcut successfully saved');
     }
 }
示例#26
0
文件: User.php 项目: mheydt/scalr
 /**
  * Checks whether Farm can be accessed by user
  *
  * @param       int         $farmId   ID of Farm
  * @param       int         $envId    ID of Environment
  * @param       string      $perm     optional Name of permission
  * @return      boolean     Returns true if access is granted
  */
 public function hasAccessFarm($farmId, $envId, $perm = null)
 {
     /* @var Farm $farm */
     $farm = Farm::findPk($farmId);
     if (!$farm) {
         return false;
     }
     if ($farm->envId != $envId) {
         return false;
     }
     $superposition = $this->getAclRolesByEnvironment($envId);
     $result = $superposition->isAllowed(Acl::RESOURCE_FARMS, $perm);
     if (!$result && $farm->teamId && $this->inTeam($farm->teamId)) {
         $result = $superposition->isAllowed(Acl::RESOURCE_TEAM_FARMS, $perm);
     }
     if (!$result && $farm->createdById && $this->id == $farm->createdById) {
         $result = $superposition->isAllowed(Acl::RESOURCE_OWN_FARMS, $perm);
     }
     return $result;
 }
示例#27
0
文件: Farm.php 项目: scalr/scalr
 /**
  * Searches farms by criteria and selecting and initiating their Teams
  *
  * @param    array        $criteria     optional The search criteria.
  * @param    array        $group        optional The group by looks like [property1, ...], by default groups by `id`
  * @param    array        $order        optional The results order looks like [property1 => true|false, ... ]
  * @param    int          $limit        optional The records limit
  * @param    int          $offset       optional The offset
  * @param    bool         $countRecords optional True to calculate total number of the records without limit
  *
  * @return   ArrayCollection|Farm[] Returns collection of the entities.
  *
  * @throws \Scalr\Exception\ModelException
  */
 public static function findWithTeams(array $criteria = null, array $group = null, array $order = null, $limit = null, $offset = null, $countRecords = null)
 {
     $farm = new Farm();
     /* @var $farms Farm[] */
     $farms = [];
     if (!isset($group)) {
         $group = ['id'];
     }
     $collection = $farm->result(AbstractEntity::RESULT_ENTITY_COLLECTION)->find($criteria, $group, $order, $limit, $offset, $countRecords);
     /* @var $farm Farm */
     foreach ($collection as $farm) {
         $farms[$farm->id] = $farm;
     }
     $team = new Team();
     $farmTeam = new FarmTeam();
     $stmt = "\n            SELECT {$team->fields()}, {$farmTeam->fields('ft', true)}\n            FROM {$team->table()}\n            JOIN {$farmTeam->table('ft')} ON {$farmTeam->columnTeamId('ft')} = {$team->columnId()}\n              AND {$farmTeam->columnFarmId('ft')} IN ('" . implode("', '", array_keys($farms)) . "')\n        ";
     foreach ($team->db()->Execute($stmt) as $row) {
         $team = new Team();
         $team->load($row);
         $farmTeam->load($row, 'ft');
         $farms[$farmTeam->farmId]->_teams[$team->id] = $team;
     }
     return $collection;
 }
示例#28
0
文件: SshKey.php 项目: scalr/scalr
 /**
  * {@inheritdoc}
  * @see \Scalr\DataType\AccessPermissionsInterface::hasAccessPermissions()
  */
 public function hasAccessPermissions($user, $environment = null, $modify = null)
 {
     if (empty($environment)) {
         return false;
     }
     if ($environment->id != $this->envId) {
         return false;
     }
     return $this->farmId ? Farm::findPk($this->farmId)->hasAccessPermissions($user, $environment) : true;
 }
示例#29
0
文件: Builder.php 项目: scalr/scalr
 public function getFarm2($farmId)
 {
     $dbFarm = DBFarm::LoadByID($farmId);
     $this->user->getPermissions()->validate($dbFarm);
     $farmRoles = array();
     $variables = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), ScopeInterface::SCOPE_FARM);
     $farmRoleVariables = new Scalr_Scripting_GlobalVariables($this->user->getAccountId(), $this->getEnvironmentId(), ScopeInterface::SCOPE_FARMROLE);
     foreach ($dbFarm->GetFarmRoles() as $dbFarmRole) {
         $scripts = $this->db->GetAll("\n                SELECT farm_role_scripts.*, scripts.name, scripts.os\n                FROM farm_role_scripts\n                LEFT JOIN scripts ON scripts.id = farm_role_scripts.scriptid\n                WHERE farm_roleid=? AND issystem='1'\n            ", array($dbFarmRole->ID));
         $scriptsObject = array();
         foreach ($scripts as $script) {
             if (!empty($script['scriptid']) && $script['script_type'] == Scalr_Scripting_Manager::ORCHESTRATION_SCRIPT_TYPE_SCALR || !empty($script['script_path']) && $script['script_type'] == Scalr_Scripting_Manager::ORCHESTRATION_SCRIPT_TYPE_LOCAL || !empty($script['params']) && $script['script_type'] == Scalr_Scripting_Manager::ORCHESTRATION_SCRIPT_TYPE_CHEF) {
                 $s = array('script_type' => $script['script_type'], 'script_id' => (int) $script['scriptid'], 'script' => $script['name'], 'os' => $script['os'], 'params' => unserialize($script['params']), 'target' => $script['target'], 'version' => (int) $script['version'], 'timeout' => $script['timeout'], 'isSync' => (int) $script['issync'], 'order_index' => $script['order_index'], 'event' => $script['event_name'], 'script_path' => $script['script_path'], 'run_as' => $script['run_as']);
                 if ($script['target'] == Script::TARGET_BEHAVIORS || $script['target'] == Script::TARGET_ROLES || $script['target'] == Script::TARGET_FARMROLES) {
                     switch ($script['target']) {
                         case $script['target'] == Script::TARGET_ROLES:
                             $varName = 'target_roles';
                             break;
                         case $script['target'] == Script::TARGET_FARMROLES:
                             $varName = 'target_farmroles';
                             break;
                         case $script['target'] == Script::TARGET_BEHAVIORS:
                             $varName = 'target_behaviors';
                             break;
                     }
                     $s[$varName] = array();
                     $r = $this->db->GetAll("SELECT `target` FROM farm_role_scripting_targets WHERE farm_role_script_id = ?", array($script['id']));
                     foreach ($r as $v) {
                         array_push($s[$varName], $v['target']);
                     }
                 }
                 $scriptsObject[] = $s;
             }
         }
         //Scripting params
         $scriptingParams = $this->db->Execute("\n                SELECT * FROM farm_role_scripting_params\n                WHERE farm_role_id = ? AND farm_role_script_id = '0'\n            ", array($dbFarmRole->ID));
         $sParams = array();
         while ($p = $scriptingParams->FetchRow()) {
             $sParams[] = array('hash' => $p['hash'], 'role_script_id' => $p['role_script_id'], 'params' => unserialize($p['params']));
         }
         $scalingManager = new Scalr_Scaling_Manager($dbFarmRole);
         $scaling = array();
         foreach ($scalingManager->getFarmRoleMetrics() as $farmRoleMetric) {
             $scaling[$farmRoleMetric->metricId] = $farmRoleMetric->getSettings();
         }
         $roleName = $dbFarmRole->GetRoleObject()->name;
         $storages = array('configs' => $dbFarmRole->getStorage()->getConfigs());
         foreach ($dbFarmRole->getStorage()->getVolumes() as $configKey => $config) {
             $storages['devices'][$configKey] = array();
             foreach ($config as $device) {
                 $info = array('farmRoleId' => $device->farmRoleId, 'placement' => $device->placement, 'serverIndex' => $device->serverIndex, 'storageId' => $device->storageId, 'storageConfigId' => $device->storageConfigId, 'status' => $device->status);
                 try {
                     $server = DBServer::LoadByFarmRoleIDAndIndex($device->farmRoleId, $device->serverIndex);
                     if ($server->status != SERVER_STATUS::TERMINATED) {
                         $info['serverId'] = $server->serverId;
                         $info['serverInstanceId'] = $server->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID);
                     }
                 } catch (Exception $e) {
                     $this->response->debugException($e);
                 }
                 $storages['devices'][$configKey][] = $info;
             }
         }
         $image = $dbFarmRole->GetRoleObject()->__getNewRoleObject()->getImage($dbFarmRole->Platform, $dbFarmRole->CloudLocation);
         $securityGroups = $this->getInitialSecurityGroupsList($dbFarmRole);
         $farmRoles[] = array('farm_role_id' => $dbFarmRole->ID, 'alias' => $dbFarmRole->Alias ? $dbFarmRole->Alias : $dbFarmRole->GetRoleObject()->name, 'role_id' => $dbFarmRole->RoleID, 'platform' => $dbFarmRole->Platform, 'os' => $dbFarmRole->GetRoleObject()->getOs()->name, 'os_family' => $dbFarmRole->GetRoleObject()->getOs()->family, 'os_generation' => $dbFarmRole->GetRoleObject()->getOs()->generation, 'os_version' => $dbFarmRole->GetRoleObject()->getOs()->version, 'osId' => $dbFarmRole->GetRoleObject()->getOs()->id, 'generation' => $dbFarmRole->GetRoleObject()->generation, 'group' => $dbFarmRole->GetRoleObject()->getCategoryName(), 'cat_id' => $dbFarmRole->GetRoleObject()->catId, 'isScalarized' => $dbFarmRole->GetRoleObject()->isScalarized, 'name' => $roleName, 'behaviors' => implode(",", $dbFarmRole->GetRoleObject()->getBehaviors()), 'scripting' => $scriptsObject, 'scripting_params' => $sParams, 'settings' => $dbFarmRole->GetAllSettings(), 'cloud_location' => $dbFarmRole->CloudLocation, 'launch_index' => (int) $dbFarmRole->LaunchIndex, 'scaling' => $scaling, 'image' => $image->getImage(), 'storages' => $storages, 'variables' => $farmRoleVariables->getValues($dbFarmRole->GetRoleID(), $dbFarm->ID, $dbFarmRole->ID), 'running_servers' => $dbFarmRole->GetRunningInstancesCount(), 'suspended_servers' => $dbFarmRole->GetSuspendedInstancesCount(), 'security_groups' => $securityGroups, 'hourly_rate' => $this->getInstanceTypeHourlyRate($dbFarmRole->Platform, $dbFarmRole->CloudLocation, $dbFarmRole->getInstanceType(), $dbFarmRole->GetRoleObject()->getOs()->family));
     }
     $vpc = array();
     if ($dbFarm->GetSetting(Entity\FarmSetting::EC2_VPC_ID)) {
         $vpc = array('id' => $dbFarm->GetSetting(Entity\FarmSetting::EC2_VPC_ID), 'region' => $dbFarm->GetSetting(Entity\FarmSetting::EC2_VPC_REGION));
     }
     /* @var $farm Entity\Farm */
     $farm = Entity\Farm::findPk($dbFarm->ID);
     // or implement AccessPermissionsInterface in DBFarm
     $farmOwnerEditable = $this->request->hasPermissions($farm, Acl::PERM_FARMS_CHANGE_OWNERSHIP) || $dbFarm->ownerId == $this->user->getId();
     $projectEditable = $this->user->isAccountOwner() || $this->request->hasPermissions($farm, Acl::PERM_FARMS_PROJECTS);
     $farmTeams = $farm->getTeams()->map(function ($team) {
         /* @var $ft Entity\Account\Team */
         return $team->name;
     });
     return array('farm' => array('name' => $dbFarm->Name, 'description' => $dbFarm->Comments, 'rolesLaunchOrder' => $dbFarm->RolesLaunchOrder, 'timezone' => $dbFarm->GetSetting(Entity\FarmSetting::TIMEZONE), 'variables' => $variables->getValues(0, $dbFarm->ID), 'vpc' => $vpc, 'status' => $dbFarm->Status, 'hash' => $dbFarm->Hash, 'owner' => $farmOwnerEditable ? $dbFarm->ownerId : ($dbFarm->ownerId ? Entity\Account\User::findPk($dbFarm->ownerId)->email : ''), 'ownerEditable' => $farmOwnerEditable, 'teamOwner' => $farmTeams, 'teamOwnerEditable' => $farmOwnerEditable, 'launchPermission' => $this->request->hasPermissions($farm, Acl::PERM_FARMS_LAUNCH_TERMINATE), 'projectEditable' => $projectEditable, Entity\FarmSetting::SZR_UPD_REPOSITORY => $dbFarm->GetSetting(Entity\FarmSetting::SZR_UPD_REPOSITORY), Entity\FarmSetting::SZR_UPD_SCHEDULE => $dbFarm->GetSetting(Entity\FarmSetting::SZR_UPD_SCHEDULE)), 'roles' => $farmRoles, 'lock' => $dbFarm->isLocked(false), 'changed' => $dbFarm->changedTime);
 }