/** * @param string $imageId * @param string $platform * @param string $osId * @param string $name * @param string $cloudLocation * @param string $architecture * @param int $size * @param string $ec2Type * @param bool $ec2Hvm * @param JsonData $software * @throws Scalr_Exception_Core */ public function xSaveAction($imageId, $platform, $osId, $name, $cloudLocation = '', $architecture = '', $size = null, $ec2Type = null, $ec2Hvm = null, JsonData $software = null) { $this->restrictAccess('IMAGES', 'MANAGE'); if ($platform == SERVER_PLATFORMS::GCE || $platform == SERVER_PLATFORMS::AZURE) { $cloudLocation = ''; } if ($accountId = $this->user->getAccountId()) { if ($envId = $this->getEnvironmentId(true)) { if (Image::findOne([['id' => $imageId], ['envId' => $envId], ['platform' => $platform], ['cloudLocation' => $cloudLocation]])) { throw new Scalr_Exception_Core('This Image has already been registered in the Environment Scope.'); } } if (Image::findOne([['id' => $imageId], ['accountId' => $this->user->getAccountId()], ['envId' => null], ['platform' => $platform], ['cloudLocation' => $cloudLocation]])) { throw new Scalr_Exception_Core('This Image has already been registered in the Account Scope.'); } } if (Image::findOne([['id' => $imageId], ['accountId' => null], ['platform' => $platform], ['cloudLocation' => $cloudLocation]])) { $this->response->failure('This Image has already been registered in the Scalr Scope.'); return; } if (!Role::isValidName($name)) { $this->response->failure('Name should start and end with letter or number and contain only letters, numbers and dashes.'); return; } $image = new Image(); $image->accountId = $this->user->getAccountId() ?: null; $image->envId = $this->getEnvironmentId(true); $image->id = $imageId; $image->platform = $platform; $image->cloudLocation = $cloudLocation; $image->architecture = 'x86_64'; if ($this->request->getScope() == ScopeInterface::SCOPE_ENVIRONMENT) { if ($image->checkImage() === false) { $this->response->failure("This Image does not exist, or isn't usable by your account"); return; } } else { $image->architecture = $architecture; $image->size = $size; if ($platform == SERVER_PLATFORMS::EC2) { if ($ec2Type == 'ebs' || $ec2Type == 'instance-store') { $image->type = $ec2Type; if ($ec2Hvm) { $image->type = $image->type . '-hvm'; } } } } $image->name = $name; $image->source = Image::SOURCE_MANUAL; $image->osId = $osId; $image->createdById = $this->user->getId(); $image->createdByEmail = $this->user->getEmail(); $image->status = Image::STATUS_ACTIVE; $image->save(); $props = []; foreach ($software as $value) { $props[$value] = null; } $image->setSoftware($props); $this->response->data(['hash' => $image->hash]); $this->response->success('Image has been added'); }
/** * 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; }
/** * @return Image */ public function createImageEntity() { $snapshot = $this->getSnapshotDetails(); $image = new Image(); $image->id = $this->snapshotId; $image->accountId = $this->clientId; $image->envId = $this->envId; $image->bundleTaskId = $this->id; $image->platform = $this->platform; $image->cloudLocation = $this->cloudLocation; $image->createdById = $this->createdById; $image->createdByEmail = $this->createdByEmail; $image->architecture = is_null($snapshot['os']->arch) ? 'x86_64' : $snapshot['os']->arch; $image->source = Image::SOURCE_BUNDLE_TASK; $image->status = Image::STATUS_ACTIVE; $image->agentVersion = $snapshot['szr_version']; $image->isScalarized = 1; $image->hasCloudInit = 0; $image->checkImage(); if (!$image->name) { $image->name = $this->roleName . '-' . date('YmdHi'); } // before checkImage we should set current envId, so that request to cloud could fill required fields, after that set correct envId if ($this->objectScope == ScopeInterface::SCOPE_ACCOUNT) { $image->envId = null; } $image->osId = $this->osId; $image->save(); if ($snapshot['software']) { $software = []; foreach ((array) $snapshot['software'] as $soft) { $software[$soft->name] = $soft->version; } $image->setSoftware($software); } return $image; }
/** * @return Image */ public function createImageEntity() { $snapshot = $this->getSnapshotDetails(); $image = new Image(); $image->id = $this->snapshotId; $image->envId = $this->envId; $image->bundleTaskId = $this->id; $image->platform = $this->platform; $image->cloudLocation = $this->cloudLocation; $image->createdById = $this->createdById; $image->createdByEmail = $this->createdByEmail; $image->architecture = is_null($snapshot['os']->arch) ? 'x86_64' : $snapshot['os']->arch; $image->source = Image::SOURCE_BUNDLE_TASK; $image->status = Image::STATUS_ACTIVE; $image->agentVersion = $snapshot['szr_version']; $image->checkImage(); if (!$image->name) { $image->name = $this->roleName . '-' . date('YmdHi'); } $image->osId = $this->osId; $image->save(); if ($snapshot['software']) { $software = []; foreach ((array) $snapshot['software'] as $soft) { $software[$soft->name] = $soft->version; } $image->setSoftware($software); } return $image; }
/** * @return Image */ public function createImageEntity() { $snapshot = $this->getSnapshotDetails(); $envId = $this->envId; /* @var Entity\Server $server */ $server = Entity\Server::findOneByServerId($this->serverId); if (!empty($server->farmRoleId)) { /* @var Entity\FarmRole $farmRole */ $farmRole = Entity\FarmRole::findPk($server->farmRoleId); if (!empty($farmRole->roleId)) { /* @var Entity\Role $role */ $role = Entity\Role::findPk($farmRole->roleId); $envId = $role->getScope() == ScopeInterface::SCOPE_ACCOUNT ? NULL : $envId; } } $image = new Image(); $image->id = $this->snapshotId; $image->accountId = $this->clientId; $image->envId = $envId; $image->bundleTaskId = $this->id; $image->platform = $this->platform; $image->cloudLocation = $this->cloudLocation; $image->createdById = $this->createdById; $image->createdByEmail = $this->createdByEmail; $image->architecture = is_null($snapshot['os']->arch) ? 'x86_64' : $snapshot['os']->arch; $image->source = Image::SOURCE_BUNDLE_TASK; $image->status = Image::STATUS_ACTIVE; $image->agentVersion = $snapshot['szr_version']; $image->checkImage(); if (!$image->name) { $image->name = $this->roleName . '-' . date('YmdHi'); } $image->osId = $this->osId; $image->save(); if ($snapshot['software']) { $software = []; foreach ((array) $snapshot['software'] as $soft) { $software[$soft->name] = $soft->version; } $image->setSoftware($software); } return $image; }
/** * @param string $imageId * @param string $platform * @param string $cloudLocation * @param string $name * @param string $architecture * @param int $size * @param string $osId, * @param string $ec2Type * @param bool $ec2Hvm * @param array $software */ public function xSaveAction($imageId, $platform, $cloudLocation = '', $name, $architecture = '', $osId, $size = NULL, $ec2Type = NULL, $ec2Hvm = NULL, $software = []) { $this->request->restrictAccess(Acl::RESOURCE_FARMS_IMAGES, Acl::PERM_FARMS_IMAGES_CREATE); $image = Image::findOne([['id' => $imageId], ['envId' => $this->getEnvironmentId(true)], ['platform' => $platform], ['cloudLocation' => $cloudLocation]]); if ($image) { $this->response->failure('This Image has already been registered in Scalr.'); return; } if (Image::findOne([['id' => $imageId], ['envId' => NULL], ['platform' => $platform], ['cloudLocation' => $cloudLocation]])) { $this->response->failure('This Image has already been registered in the Scalr Scope.'); return; } $image = new Image(); $image->envId = $this->getEnvironmentId(true); $image->id = $imageId; $image->platform = $platform; $image->cloudLocation = $cloudLocation; $image->architecture = 'x86_64'; if ($this->user->isScalrAdmin()) { $image->architecture = $architecture; $image->size = $size; if ($platform == SERVER_PLATFORMS::EC2) { if ($ec2Type == 'ebs' || $ec2Type == 'instance-store') { $image->type = $ec2Type; if ($ec2Hvm) { $image->type = $image->type . '-hvm'; } } } } else { if (!$image->checkImage(true)) { $this->response->failure("This Image does not exist, or isn't usable by your account"); return; } } $image->name = $name; $image->source = Image::SOURCE_MANUAL; $image->osId = $osId; $image->createdById = $this->user->getId(); $image->createdByEmail = $this->user->getEmail(); $image->status = Image::STATUS_ACTIVE; $image->save(); if (count($software)) { $props = []; foreach ($software as $value) { $props[$value] = null; } $image->setSoftware($props); } $this->response->data(['hash' => $image->hash]); $this->response->success('Image has been added'); }
/** * 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; }