Check if image exists and return more info if special data exists
public checkImage ( ) : boolean | array | ||
return | boolean | array | Returns array of data if exists when update is required |
protected function run2() { $cnt = 0; $images = $this->db->GetAll('SELECT ri.*, r.env_id FROM role_images ri LEFT JOIN roles r ON r.id = ri.role_id'); foreach ($images as $i) { /* @var Image $imObj */ $i['env_id'] = $i['env_id'] == 0 ? NULL : $i['env_id']; $imObj = Image::findOne([['id' => $i['image_id']], ['$or' => [['envId' => $i['env_id']], ['envId' => null]]], ['platform' => $i['platform']], ['cloudLocation' => $i['cloud_location']]]); if (!$imObj) { $imObj = new Image(); $imObj->id = $i['image_id']; $imObj->envId = $i['env_id']; $imObj->platform = $i['platform']; $imObj->cloudLocation = $i['cloud_location']; $imObj->architecture = $i['architecture'] ? $i['architecture'] : 'x84_64'; $imObj->osId = $i['os_id']; $imObj->isDeprecated = 0; $imObj->dtAdded = NULL; $imObj->source = Image::SOURCE_MANUAL; if ($imObj->envId) { $imObj->checkImage(); } else { $imObj->status = Image::STATUS_ACTIVE; } if (is_null($imObj->status)) { $imObj->status = Image::STATUS_ACTIVE; } if (is_null($imObj->cloudLocation)) { $imObj->cloudLocation = ''; } $imObj->save(); $cnt++; } } $this->console->notice('Added %s images', $cnt); }
/** * @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'); }
/** * @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'); }