save() публичный Метод

public save ( )
Пример #1
0
 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);
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 public function run1($stage)
 {
     if ($this->hasTable('images')) {
         $this->db->Execute('DROP TABLE images');
         // drop old table if existed
     }
     $this->db->Execute("CREATE TABLE `images` (\n              `hash` binary(16) NOT NULL,\n              `id` varchar(128) NOT NULL DEFAULT '',\n              `env_id` int(11) NULL DEFAULT NULL,\n              `bundle_task_id` int(11) NULL DEFAULT NULL,\n              `platform` varchar(25) NOT NULL DEFAULT '',\n              `cloud_location` varchar(255) NOT NULL DEFAULT '',\n              `os_family` varchar(25) NULL DEFAULT NULL,\n              `os_version` varchar(10) NULL DEFAULT NULL,\n              `os_name` varchar(255) NULL DEFAULT NULL,\n              `created_by_id` int(11) NULL DEFAULT NULL,\n              `created_by_email` varchar(100) NULL DEFAULT NULL,\n              `architecture` enum('i386','x86_64') NOT NULL DEFAULT 'x86_64',\n              `is_deprecated` tinyint(1) NOT NULL DEFAULT '0',\n              `source` enum('BundleTask','Manual') NOT NULL DEFAULT 'Manual',\n              `type` varchar(20) NULL DEFAULT NULL,\n              `status` varchar(20) NOT NULL,\n              `status_error` varchar(255) NULL DEFAULT NULL,\n              `agent_version` varchar(20) NULL DEFAULT NULL,\n              PRIMARY KEY (`hash`),\n              UNIQUE KEY `idx_id` (`env_id`, `id`, `platform`, `cloud_location`),\n              CONSTRAINT `fk_images_client_environmnets_id` FOREIGN KEY (`env_id`) REFERENCES `client_environments` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION\n            ) ENGINE=InnoDB DEFAULT CHARSET=latin1;\n        ");
     $allRecords = 0;
     $excludedCL = 0;
     $excludedMissing = 0;
     // convert
     $tasks = [];
     foreach ($this->db->GetAll('SELECT id as bundle_task_id, client_id as account_id, env_id, platform, snapshot_id as id, cloud_location, os_family, os_name,
         os_version, created_by_id, created_by_email, bundle_type as type FROM bundle_tasks WHERE status = ?', [\SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS]) as $t) {
         if (!is_array($tasks[$t['env_id']])) {
             $tasks[$t['env_id']] = [];
         }
         $allRecords++;
         $tasks[$t['env_id']][] = $t;
     }
     foreach ($this->db->GetAll('SELECT r.client_id as account_id, r.env_id, ri.platform, ri.image_id as id, ri.cloud_location, ri.os_family, ri.os_name,
         ri.os_version, r.added_by_userid as created_by_id, r.added_by_email as created_by_email, ri.agent_version FROM role_images ri JOIN roles r ON r.id = ri.role_id') as $t) {
         if (!is_array($tasks[$t['env_id']])) {
             $tasks[$t['env_id']] = [];
         }
         $allRecords++;
         $tasks[$t['env_id']][] = $t;
     }
     foreach ($tasks as $id => $e) {
         if ($id == 0) {
             continue;
         }
         try {
             $env = (new \Scalr_Environment())->loadById($id);
         } catch (\Exception $e) {
             $this->console->warning('Invalid environment %d: %s', $id, $e->getMessage());
             continue;
         }
         foreach ($e as $t) {
             // check if snapshot exists
             $add = false;
             if ($this->db->GetOne('SELECT id FROM images WHERE id = ? AND env_id = ? AND platform = ? AND cloud_location = ? LIMIT 1', [$t['id'], $t['env_id'], $t['platform'], $t['cloud_location']])) {
                 continue;
             }
             if ($t['platform'] != \SERVER_PLATFORMS::GCE && !$t['cloud_location']) {
                 $excludedCL++;
                 continue;
             }
             try {
                 switch ($t['platform']) {
                     case \SERVER_PLATFORMS::EC2:
                         $snap = $env->aws($t['cloud_location'])->ec2->image->describe($t['id']);
                         if (count($snap)) {
                             $add = true;
                             $t['architecture'] = $snap->toArray()[0]['architecture'];
                         }
                         break;
                     case \SERVER_PLATFORMS::RACKSPACE:
                         $platform = PlatformFactory::NewPlatform(\SERVER_PLATFORMS::RACKSPACE);
                         /* @var $platform RackspacePlatformModule */
                         $client = \Scalr_Service_Cloud_Rackspace::newRackspaceCS($env->getPlatformConfigValue(RackspacePlatformModule::USERNAME, true, $t['cloud_location']), $env->getPlatformConfigValue(RackspacePlatformModule::API_KEY, true, $t['cloud_location']), $t['cloud_location']);
                         $snap = $client->getImageDetails($t['id']);
                         if ($snap) {
                             $add = true;
                         } else {
                             $excludedMissing++;
                         }
                         break;
                     case \SERVER_PLATFORMS::GCE:
                         $platform = PlatformFactory::NewPlatform(\SERVER_PLATFORMS::GCE);
                         /* @var $platform GoogleCEPlatformModule */
                         $client = $platform->getClient($env);
                         /* @var $client \Google_Service_Compute */
                         $projectId = $env->getPlatformConfigValue(GoogleCEPlatformModule::PROJECT_ID);
                         $snap = $client->images->get($projectId, str_replace($projectId . '/images/', '', $t['id']));
                         if ($snap) {
                             $add = true;
                             $t['architecture'] = 'x86_64';
                         } else {
                             $excludedMissing++;
                         }
                         break;
                     case \SERVER_PLATFORMS::EUCALYPTUS:
                         $snap = $env->eucalyptus($t['cloud_location'])->ec2->image->describe($t['id']);
                         if (count($snap)) {
                             $add = true;
                             $t['architecture'] = $snap->toArray()[0]['architecture'];
                         }
                         break;
                     default:
                         if (PlatformFactory::isOpenstack($t['platform'])) {
                             $snap = $env->openstack($t['platform'], $t['cloud_location'])->servers->getImage($t['id']);
                             if ($snap) {
                                 $add = true;
                                 $t['architecture'] = $snap->metadata->arch == 'x84-64' ? 'x84_64' : 'i386';
                             } else {
                                 $excludedMissing++;
                             }
                         } else {
                             if (PlatformFactory::isCloudstack($t['platform'])) {
                                 $snap = $env->cloudstack($t['platform'])->template->describe(['templatefilter' => 'executable', 'id' => $t['id'], 'zoneid' => $t['cloud_location']]);
                                 if ($snap) {
                                     if (isset($snap[0])) {
                                         $add = true;
                                     }
                                 } else {
                                     $excludedMissing++;
                                 }
                             } else {
                                 $this->console->warning('Unknown platform: %s', $t['platform']);
                             }
                         }
                 }
                 if ($add) {
                     $image = new Image();
                     $image->id = $t['id'];
                     $image->envId = $t['env_id'];
                     $image->bundleTaskId = $t['bundle_task_id'];
                     $image->platform = $t['platform'];
                     $image->cloudLocation = $t['cloud_location'];
                     $image->createdById = $t['created_by_id'];
                     $image->createdByEmail = $t['created_by_email'];
                     $image->architecture = $t['architecture'] ? $t['architecture'] : 'x86_64';
                     $image->isDeprecated = 0;
                     $image->source = $t['bundle_task_id'] ? 'BundleTask' : 'Manual';
                     $image->type = $t['type'];
                     $image->status = Image::STATUS_ACTIVE;
                     $image->agentVersion = $t['agent_version'];
                     $image->save();
                 } else {
                     $excludedMissing++;
                 }
             } catch (\Exception $e) {
                 if (strpos($e->getMessage(), 'The resource could not be found') !== FALSE) {
                     $excludedMissing++;
                 } else {
                     if (strpos($e->getMessage(), 'The requested URL / was not found on this server.') !== FALSE) {
                         $excludedMissing++;
                     } else {
                         if (strpos($e->getMessage(), 'Not Found') !== FALSE) {
                             $excludedMissing++;
                         } else {
                             if (strpos($e->getMessage(), 'was not found') !== FALSE) {
                                 $excludedMissing++;
                             } else {
                                 if (strpos($e->getMessage(), 'Bad username or password') !== FALSE) {
                                     $excludedMissing++;
                                 } else {
                                     if (strpos($e->getMessage(), 'unable to verify user credentials and/or request signature') !== FALSE) {
                                         $excludedMissing++;
                                     } else {
                                         if (strpos($e->getMessage(), 'OpenStack error. Image not found.') !== FALSE) {
                                             $excludedMissing++;
                                         } else {
                                             if (strpos($e->getMessage(), 'Neither api key nor password was provided for the OpenStack config.') !== FALSE) {
                                                 $excludedMissing++;
                                             } else {
                                                 $this->console->warning('SnapshotId: %s, envId: %d, error: %s', $t['id'], $t['env_id'], $e->getMessage());
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $this->console->notice('Found %d records', $allRecords);
     $this->console->notice('Excluded %d images because of null cloud_location', $excludedCL);
     $this->console->notice('Excluded %d missed images', $excludedMissing);
 }
Пример #4
0
 protected function run8($stage)
 {
     $knownOses = [];
     //Retrieves the list of all known OSes
     foreach (Entity\Os::all() as $os) {
         /* @var $os Entity\Os */
         $knownOses[$os->id] = $os;
     }
     $role = new Entity\Role();
     //Trying to clarify the operating system of the Roles using Images which are associated with them.
     //If all Images have the same operating system it will be considered as acceptable for the Role at latter will be updated.
     $rs = $this->db->Execute("\n            SELECT " . $role->fields('r', true) . ", GROUP_CONCAT(t.os_id) `osids`\n            FROM roles r JOIN (\n                SELECT DISTINCT ri.role_id, i.os_id\n                FROM images i\n                JOIN role_images ri ON i.id = ri.image_id\n                    AND i.platform = ri.platform\n                    AND i.cloud_location = ri.cloud_location\n            ) t ON t.role_id = r.id\n            WHERE r.os_id = ?\n            GROUP BY r.id\n            HAVING osids != r.os_id\n        ", ['unknown-os']);
     if ($rs->RecordCount()) {
         $this->console->out("Found %d Roles the OS value of which can be filled from the Images. Updating...", $rs->RecordCount());
     }
     while ($row = $rs->FetchRow()) {
         $role = new Entity\Role();
         $role->load($row, 'r');
         if (!empty($row['osids'])) {
             if (isset($knownOses[$row['osids']])) {
                 //Updating OS value of the Role
                 $role->osId = $row['osids'];
                 $role->save();
             } else {
                 $this->console->warning("Role %s (%d) is associated with the Images with either different or unknown OS: %s", $role->name, $role->id, $row['osids']);
             }
         }
     }
     $image = new Entity\Image();
     //Trying to clarify the operating sytem of the Images using Roles which are associated with them.
     $rs = $this->db->Execute("\n            SELECT " . $image->fields('i', true) . ", GROUP_CONCAT(t.os_id) `osids`\n            FROM images i JOIN (\n                SELECT DISTINCT ri.image_id, ri.platform, ri.cloud_location, r.os_id\n                FROM roles r\n                JOIN role_images ri ON ri.role_id = r.id\n            ) t ON t.image_id = i.id AND t.platform = i.platform AND t.cloud_location = i.cloud_location\n            WHERE i.os_id = ?\n            GROUP BY i.hash\n            HAVING osids != i.os_id\n        ", ['unknown-os']);
     if ($rs->RecordCount()) {
         $this->console->out("Found %d Images the OS value of which can be filled from the Roles. Updating...", $rs->RecordCount());
     }
     while ($row = $rs->FetchRow()) {
         $image = new Entity\Image();
         $image->load($row, 'i');
         if (!empty($row['osids'])) {
             if (isset($knownOses[$row['osids']])) {
                 //Updating OS value of the Image
                 $image->osId = $row['osids'];
                 $image->save();
             } else {
                 $this->console->warning("Image (%s) imageId: %s, platform: %s, cloudLocation: %s is associated with the Roles with either different or unknown OS: %s", $image->hash, $image->id, $image->platform, $image->cloudLocation, $row['osids']);
             }
         }
     }
 }
Пример #5
0
 /**
  * @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');
 }
Пример #6
0
 /**
  * @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;
 }
Пример #7
0
 /**
  * @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;
 }
Пример #8
0
 /**
  * @return Image
  */
 public function createImageEntity()
 {
     $os = $this->getOsDetails();
     $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->osFamily = $os->family;
     $image->osVersion = $os->version;
     $image->osName = $os->name;
     $image->createdById = $this->createdById;
     $image->createdByEmail = $this->createdByEmail;
     $image->architecture = is_null($snapshot['os']->arch) ? 'x86_64' : $snapshot['os']->arch;
     $image->isDeprecated = 0;
     $image->source = Image::SOURCE_BUNDLE_TASK;
     $image->type = '';
     $image->status = Image::STATUS_ACTIVE;
     $image->agentVersion = $snapshot['szr_version'];
     $image->save();
     return $image;
 }
Пример #9
0
 /**
  * @return Image
  */
 public function createImageEntity()
 {
     $snapshot = $this->getSnapshotDetails();
     $envId = $this->envId;
     /* @var Entity\Server $server */
     $server = Entity\Server::findOneByServerId($this->serverId);
     if (!empty($server->farmRoleId)) {
         /* @var Entity\FarmRole $farmRole */
         $farmRole = Entity\FarmRole::findPk($server->farmRoleId);
         if (!empty($farmRole->roleId)) {
             /* @var Entity\Role $role */
             $role = Entity\Role::findPk($farmRole->roleId);
             $envId = $role->getScope() == ScopeInterface::SCOPE_ACCOUNT ? NULL : $envId;
         }
     }
     $image = new Image();
     $image->id = $this->snapshotId;
     $image->accountId = $this->clientId;
     $image->envId = $envId;
     $image->bundleTaskId = $this->id;
     $image->platform = $this->platform;
     $image->cloudLocation = $this->cloudLocation;
     $image->createdById = $this->createdById;
     $image->createdByEmail = $this->createdByEmail;
     $image->architecture = is_null($snapshot['os']->arch) ? 'x86_64' : $snapshot['os']->arch;
     $image->source = Image::SOURCE_BUNDLE_TASK;
     $image->status = Image::STATUS_ACTIVE;
     $image->agentVersion = $snapshot['szr_version'];
     $image->checkImage();
     if (!$image->name) {
         $image->name = $this->roleName . '-' . date('YmdHi');
     }
     $image->osId = $this->osId;
     $image->save();
     if ($snapshot['software']) {
         $software = [];
         foreach ((array) $snapshot['software'] as $soft) {
             $software[$soft->name] = $soft->version;
         }
         $image->setSoftware($software);
     }
     return $image;
 }
Пример #10
0
 /**
  * @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');
 }
Пример #11
0
 /**
  * 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;
 }