/** * @param string $platform * @param string $cloudLocation * @param string $cloudServerId * @param string $name * @param bool $createImage * @throws Exception */ public function xInitiateImportAction($platform, $cloudLocation, $cloudServerId, $name, $createImage = false) { if (!Role::validateName($name)) { throw new Exception(_("Name is incorrect")); } if (!$createImage && $this->db->GetOne("SELECT id FROM roles WHERE name=? AND (env_id IS NULL OR env_id = ?) LIMIT 1", array($name, $this->getEnvironmentId()))) { throw new Exception('Selected role name is already used. Please select another one.'); } $cryptoKey = Scalr::GenerateRandomKey(40); $creInfo = new ServerCreateInfo($platform, null, 0, 0); $creInfo->clientId = $this->user->getAccountId(); $creInfo->envId = $this->getEnvironmentId(); $creInfo->farmId = 0; $creInfo->SetProperties(array(SERVER_PROPERTIES::SZR_IMPORTING_ROLE_NAME => $name, SERVER_PROPERTIES::SZR_IMPORTING_OBJECT => $createImage ? BundleTask::BUNDLETASK_OBJECT_IMAGE : BundleTask::BUNDLETASK_OBJECT_ROLE, SERVER_PROPERTIES::SZR_KEY => $cryptoKey, SERVER_PROPERTIES::SZR_KEY_TYPE => SZR_KEY_TYPE::PERMANENT, SERVER_PROPERTIES::SZR_VESION => "0.14.0", SERVER_PROPERTIES::SZR_IMPORTING_VERSION => 2, SERVER_PROPERTIES::SZR_IMPORTING_STEP => 1, SERVER_PROPERTIES::LAUNCHED_BY_ID => $this->user->id, SERVER_PROPERTIES::LAUNCHED_BY_EMAIL => $this->user->getEmail())); $platformObj = PlatformFactory::NewPlatform($platform); if ($platform == SERVER_PLATFORMS::EC2) { $client = $this->environment->aws($cloudLocation)->ec2; $r = $client->instance->describe($cloudServerId); $instance = $r->get(0)->instancesSet->get(0); $creInfo->SetProperties(array(EC2_SERVER_PROPERTIES::REGION => $cloudLocation, EC2_SERVER_PROPERTIES::INSTANCE_ID => $cloudServerId, EC2_SERVER_PROPERTIES::AMIID => $instance->imageId, EC2_SERVER_PROPERTIES::AVAIL_ZONE => $instance->placement->availabilityZone)); } else { if ($platform == SERVER_PLATFORMS::EUCALYPTUS) { $client = $this->environment->eucalyptus($cloudLocation)->ec2; $r = $client->instance->describe($cloudServerId); $instance = $r->get(0)->instancesSet->get(0); $creInfo->SetProperties(array(EUCA_SERVER_PROPERTIES::REGION => $cloudLocation, EUCA_SERVER_PROPERTIES::INSTANCE_ID => $cloudServerId, EUCA_SERVER_PROPERTIES::EMIID => $instance->imageId, EUCA_SERVER_PROPERTIES::AVAIL_ZONE => $instance->placement->availabilityZone)); } else { if ($platform == SERVER_PLATFORMS::GCE) { $gce = $platformObj->getClient($this->environment, $cloudLocation); $result = $gce->instances->get($this->environment->getPlatformConfigValue(GoogleCEPlatformModule::PROJECT_ID), $cloudLocation, $cloudServerId); $creInfo->SetProperties(array(GCE_SERVER_PROPERTIES::SERVER_NAME => $cloudServerId, GCE_SERVER_PROPERTIES::CLOUD_LOCATION => $cloudLocation)); } else { if (PlatformFactory::isOpenstack($platform)) { $creInfo->SetProperties(array(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $cloudLocation, OPENSTACK_SERVER_PROPERTIES::SERVER_ID => $cloudServerId)); } else { if (PlatformFactory::isCloudstack($platform)) { $creInfo->SetProperties(array(CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $cloudLocation, CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID => $cloudServerId)); } } } } } $dbServer = DBServer::Create($creInfo, true); $ips = $platformObj->GetServerIPAddresses($dbServer); $dbServer->localIp = $ips['localIp']; $dbServer->remoteIp = $ips['remoteIp']; $dbServer->Save(); $this->response->data(array('command' => $this->getSzrCmd($dbServer), 'serverId' => $dbServer->serverId)); }
/** * {@inheritdoc} * @see \Scalr\Api\DataType\ApiEntityAdapter::validateEntity() */ public function validateEntity($entity) { if (!$entity instanceof Entity\Image) { throw new \InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\Image class")); } if ($entity->hash !== null) { //Checks if the image does exist if (!Entity\Image::findPk($entity->hash)) { throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the Image with ID: %d", $entity->hash)); } } else { $image = Entity\Image::findOne([['id' => $entity->id], ['$or' => [['envId' => $entity->envId], ['envId' => null]]], ['platform' => $entity->platform], ['cloudLocation' => $entity->cloudLocation]]); if ($image) { throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, "This Image has already been registered in Scalr"); } } //Is this a new Image if (!$entity->hash) { $entity->createdByEmail = $this->controller->getUser()->email; $entity->createdById = $this->controller->getUser()->id; } if (!Entity\Role::validateName($entity->name)) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid name of the Image"); } $entity->architecture = $entity->architecture ?: 'x86_64'; if (!in_array($entity->architecture, ['i386', 'x86_64'])) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Invalid architecture of the Image."); } if (!$this->controller->hasPermissions($entity, true)) { //Checks entity level write access permissions throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions"); } //We only allow to either create or modify Environment Scope Roles if ($entity->getScope() !== ScopeInterface::SCOPE_ENVIRONMENT) { throw new ApiErrorException(403, ErrorMessage::ERR_SCOPE_VIOLATION, sprintf("Only %s scope is allowed.", ScopeInterface::SCOPE_ENVIRONMENT)); } //Validates OS if (!empty($entity->osId)) { //Tries to find out the specified OS $os = Entity\Os::findPk($entity->osId); if (!$os instanceof Entity\Os) { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Specified OS does not exist"); } } else { throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "OS must be provided with the request."); } }
/** * @param $id * @param $platform * @param $cloudLocation * @param $name */ public function xUpdateNameAction($id, $platform, $cloudLocation, $name) { $this->request->restrictAccess(Acl::RESOURCE_FARMS_IMAGES, Acl::PERM_FARMS_IMAGES_MANAGE); if (!\Scalr\Model\Entity\Role::validateName($name)) { $this->response->failure('Invalid name for image'); return; } /* @var $image Image */ $image = Image::findOne([['id' => $id], ['envId' => $this->getEnvironmentId(true)], ['platform' => $platform], ['cloudLocation' => $cloudLocation]]); if (!$image) { $this->response->failure('Image not found'); return; } $image->name = $name; $image->save(); $this->response->data(['name' => $name]); $this->response->success('Image\'s name was updated'); }
/** * @param string $platform * @param string $architecture * @param JsonData $behaviors * @param string $name * @param bool $createImage * @param string $imageId * @param string $cloudLocation * @param string $osId * @param integer $hvm * @param JsonData $advanced * @param JsonData $chef * @throws Exception */ public function xBuildAction($platform, $architecture, JsonData $behaviors, $name = '', $createImage = false, $imageId, $cloudLocation, $osId, $hvm = 0, JsonData $advanced, JsonData $chef) { $this->request->restrictAccess(Acl::RESOURCE_FARMS_ROLES, Acl::PERM_FARMS_ROLES_CREATE); if (!\Scalr\Model\Entity\Role::validateName($name)) { throw new Exception(_("Name is incorrect")); } if (!$createImage && $this->db->GetOne("SELECT id FROM roles WHERE name=? AND (env_id IS NULL OR env_id = ?) LIMIT 1", array($name, $this->getEnvironmentId()))) { throw new Exception('Selected role name is already used. Please select another one.'); } $behaviours = implode(",", array_values($behaviors->getArrayCopy())); $os = Os::findPk($osId); if (!$os) { throw new Exception('Operating system not found.'); } // Create server $creInfo = new ServerCreateInfo($platform, null, 0, 0); $creInfo->clientId = $this->user->getAccountId(); $creInfo->envId = $this->getEnvironmentId(); $creInfo->farmId = 0; $creInfo->SetProperties(array(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR => $behaviours, SERVER_PROPERTIES::SZR_IMPORTING_IMAGE_ID => $imageId, SERVER_PROPERTIES::SZR_KEY => Scalr::GenerateRandomKey(40), SERVER_PROPERTIES::SZR_KEY_TYPE => SZR_KEY_TYPE::PERMANENT, SERVER_PROPERTIES::SZR_VESION => "0.13.0", SERVER_PROPERTIES::SZR_IMPORTING_MYSQL_SERVER_TYPE => "mysql", SERVER_PROPERTIES::SZR_DEV_SCALARIZR_BRANCH => $advanced['scalrbranch'], SERVER_PROPERTIES::ARCHITECTURE => $architecture, SERVER_PROPERTIES::SZR_IMPORTING_LEAVE_ON_FAIL => $advanced['dontterminatefailed'] == 'on' ? 1 : 0, SERVER_PROPERTIES::SZR_IMPORTING_CHEF_SERVER_ID => $chef['chef.server'], SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ENVIRONMENT => $chef['chef.environment'], SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ROLE_NAME => $chef['chef.role'])); $dbServer = DBServer::Create($creInfo, true); $dbServer->status = SERVER_STATUS::TEMPORARY; $dbServer->imageId = $imageId; $dbServer->save(); //Launch server $launchOptions = new Scalr_Server_LaunchOptions(); $launchOptions->imageId = $imageId; $launchOptions->cloudLocation = $cloudLocation; $launchOptions->architecture = $architecture; $platformObj = PlatformFactory::NewPlatform($platform); switch ($platform) { case SERVER_PLATFORMS::ECS: $launchOptions->serverType = 10; if ($cloudLocation == 'all') { $locations = array_keys($platformObj->getLocations($this->environment)); $launchOptions->cloudLocation = $locations[0]; } //Network here: $osClient = $platformObj->getOsClient($this->environment, $launchOptions->cloudLocation); $networks = $osClient->network->listNetworks(); $tenantId = $osClient->getConfig()->getAuthToken()->getTenantId(); foreach ($networks as $network) { if ($network->status == 'ACTIVE') { if ($network->{"router:external"} != true) { if ($tenantId == $network->tenant_id) { $launchOptions->networks = array($network->id); break; } } } } break; case SERVER_PLATFORMS::IDCF: $launchOptions->serverType = 24; break; case SERVER_PLATFORMS::RACKSPACE: if ($os->family == 'ubuntu') { $launchOptions->serverType = 1; } else { $launchOptions->serverType = 3; } break; case SERVER_PLATFORMS::RACKSPACENG_US: $launchOptions->serverType = 3; break; case SERVER_PLATFORMS::RACKSPACENG_UK: $launchOptions->serverType = 3; break; case SERVER_PLATFORMS::EC2: if ($hvm == 1) { $launchOptions->serverType = 'm3.xlarge'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } else { if ($os->family == 'oel') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } elseif ($os->family == 'rhel') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } elseif ($os->family == 'scientific') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } elseif ($os->family == 'debian' && $os->generation == '8') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } elseif ($os->family == 'centos' && $os->generation == '7') { $launchOptions->serverType = 'm3.large'; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM; } else { $launchOptions->serverType = 'm3.large'; } } $launchOptions->userData = "#cloud-config\ndisable_root: false"; break; case SERVER_PLATFORMS::GCE: $launchOptions->serverType = 'n1-standard-1'; $location = null; $locations = array_keys($platformObj->getLocations($this->environment)); while (count($locations) != 0) { $location = array_shift($locations); if (strstr($location, "us-")) { break; } } $launchOptions->cloudLocation = $locations[0]; $bundleType = SERVER_SNAPSHOT_CREATION_TYPE::GCE_STORAGE; break; } if ($advanced['servertype']) { $launchOptions->serverType = $advanced['servertype']; } if ($advanced['availzone']) { $launchOptions->availZone = $advanced['availzone']; } if ($advanced['region']) { $launchOptions->cloudLocation = $advanced['region']; } //Add Bundle task $creInfo = new ServerSnapshotCreateInfo($dbServer, $name, SERVER_REPLACEMENT_TYPE::NO_REPLACE); $bundleTask = BundleTask::Create($creInfo, true); if ($bundleType) { $bundleTask->bundleType = $bundleType; } $bundleTask->createdById = $this->user->id; $bundleTask->createdByEmail = $this->user->getEmail(); $bundleTask->osFamily = $os->family; $bundleTask->object = $createImage ? BundleTask::BUNDLETASK_OBJECT_IMAGE : BundleTask::BUNDLETASK_OBJECT_ROLE; $bundleTask->cloudLocation = $launchOptions->cloudLocation; $bundleTask->save(); $bundleTask->Log(sprintf("Launching temporary server (%s)", serialize($launchOptions))); $dbServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BUNDLE_TASK_ID, $bundleTask->id); try { $platformObj->LaunchServer($dbServer, $launchOptions); $bundleTask->Log(_("Temporary server launched. Waiting for running state...")); } catch (Exception $e) { $bundleTask->SnapshotCreationFailed(sprintf(_("Unable to launch temporary server: %s"), $e->getMessage())); } $this->response->data(array('serverId' => $dbServer->serverId, 'bundleTaskId' => $bundleTask->id)); }