/**
* 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;
}