/** * Migrates an Image to another Cloud Location * * @param string $cloudLocation The cloud location * @param \Scalr_Account_User|\Scalr\Model\Entity\Account\User $user The user object * @return Image * @throws Exception * @throws NotEnabledPlatformException * @throws DomainException */ public function migrateEc2Location($cloudLocation, $user) { if (!$this->getEnvironment()->isPlatformEnabled(SERVER_PLATFORMS::EC2)) { throw new NotEnabledPlatformException("You can migrate image between regions only on EC2 cloud"); } if ($this->cloudLocation == $cloudLocation) { throw new DomainException('Destination region is the same as source one'); } $snap = $this->getEnvironment()->aws($this->cloudLocation)->ec2->image->describe($this->id); if ($snap->count() == 0) { throw new Exception("Image haven't been found on cloud."); } if ($snap->get(0)->toArray()['imageState'] != 'available') { throw new Exception('Image is not in "available" status on cloud and cannot be copied.'); } $this->checkImage(); // re-check properties $aws = $this->getEnvironment()->aws($cloudLocation); $newImageId = $aws->ec2->image->copy($this->cloudLocation, $this->id, $this->name, "Image was copied by Scalr from image: {$this->name}, cloudLocation: {$this->cloudLocation}, id: {$this->id}", null, $cloudLocation); $newImage = new Image(); $newImage->platform = $this->platform; $newImage->cloudLocation = $cloudLocation; $newImage->id = $newImageId; $newImage->name = $this->name; $newImage->architecture = $this->architecture; $newImage->size = $this->size; $newImage->accountId = $this->accountId; $newImage->envId = $this->envId; $newImage->osId = $this->osId; $newImage->source = Image::SOURCE_MANUAL; $newImage->type = $this->type; $newImage->agentVersion = $this->agentVersion; $newImage->createdById = $user->getId(); $newImage->createdByEmail = $user->getEmail(); $newImage->status = Image::STATUS_ACTIVE; $newImage->isScalarized = $this->isScalarized; $newImage->hasCloudInit = $this->hasCloudInit; $newImage->save(); $newImage->setSoftware($this->getSoftware()); return $newImage; }
/** * Marks server as to be terminated. * * @param int|array $reason The reason possibly with the format parameters. * @param bool $forcefully optional Method: forcefully (true) | gracefully (false) * @param User $user optional The user entity * @return bool */ public function terminate($reason, $forcefully = null, $user = null) { if (in_array($this->status, [Server::STATUS_PENDING_TERMINATE, Server::STATUS_TERMINATED])) { return false; } $forcefully = $forcefully === null ? true : (bool) $forcefully; $fnGetReason = function ($reasonId) { $args = func_get_args(); $args[0] = Server::getTerminateReason($reasonId); return [call_user_func_array('sprintf', $args), $reasonId]; }; list($reason, $reasonId) = is_array($reason) ? call_user_func_array($fnGetReason, $reason) : $fnGetReason($reason); $properties = $this->properties; if ($user instanceof User) { $properties[self::TERMINATED_BY_ID] = $user->getId(); $properties[self::TERMINATED_BY_EMAIL] = $user->getEmail(); } $properties[self::REBOOTING] = 0; $properties->save(); $this->update(['status' => Server::STATUS_PENDING_TERMINATE, 'shutdownScheduled' => new DateTime($forcefully ? 'now' : Scalr::config('scalr.system.server_terminate_timeout'))]); $this->getHistory()->markAsTerminated($reason, $reasonId); if (isset($this->farmId)) { $DBServer = $this->__getDBServer(); Scalr::FireEvent($this->farmId, new BeforeHostTerminateEvent($DBServer, false)); // If instance was terminated outside scalr, we need manually fire HostDown if ($reasonId == self::TERMINATE_REASON_CRASHED) { Scalr::FireEvent($this->farmId, new HostDownEvent($DBServer, false)); } } return true; }
/** * Checks whether the user is allowed to remove specified user * * @param User $user The user to remove * @return boolean Returns true if the user is allowed to remove specified user */ public function canRemoveUser(User $user) { return !$this->isTeamUser() && $user->accountId == $this->accountId && !$user->isAccountOwner() && $this->getId() != $user->getId() && ($this->isAccountOwner() || $this->isAccountSuperAdmin() || !$user->isAccountSuperAdmin()); }
/** * Migrates an Image to another Cloud Location * * @param string $cloudLocation The cloud location * @param \Scalr_Account_User|\Scalr\Model\Entity\Account\User $user The user object * @return Image * @throws NotEnabledPlatformException * @throws DomainException */ public function migrateEc2Location($cloudLocation, $user) { if (!$this->getEnvironment()->isPlatformEnabled(SERVER_PLATFORMS::EC2)) { throw new NotEnabledPlatformException("You can migrate image between regions only on EC2 cloud"); } if ($this->cloudLocation == $cloudLocation) { throw new DomainException('Destination region is the same as source one'); } $this->checkImage(); // re-check properties $aws = $this->getEnvironment()->aws($cloudLocation); $newImageId = $aws->ec2->image->copy($this->cloudLocation, $this->id, $this->name, "Image was copied by Scalr from image: {$this->name}, cloudLocation: {$this->cloudLocation}, id: {$this->id}", null, $cloudLocation); $newImage = new Image(); $newImage->platform = $this->platform; $newImage->cloudLocation = $cloudLocation; $newImage->id = $newImageId; $newImage->name = $this->name; $newImage->architecture = $this->architecture; $newImage->size = $this->size; $newImage->envId = $this->envId; $newImage->osId = $this->osId; $newImage->source = Image::SOURCE_MANUAL; $newImage->type = $this->type; $newImage->agentVersion = $this->agentVersion; $newImage->createdById = $user->getId(); $newImage->createdByEmail = $user->getEmail(); $newImage->status = Image::STATUS_ACTIVE; $newImage->save(); $newImage->setSoftware($this->getSoftware()); return $newImage; }