public function xInitiateImportAction()
{
$this->request->defineParams(array('platform', 'cloudLocation', 'cloudServerId', 'roleName'));
$validator = new Scalr_Validator();
if ($validator->validateNotEmpty($this->getParam('roleName')) !== true) {
$err['roleName'] = 'Role name cannot be empty';
}
if (strlen($this->getParam('roleName')) < 3) {
$err['roleName'] = _("Role name should be greater than 3 chars");
}
if (!preg_match("/^[A-Za-z0-9-]+\$/si", $this->getParam('roleName'))) {
$err['roleName'] = _("Role name is incorrect");
}
if ($this->db->GetOne("SELECT id FROM roles WHERE name=? AND (env_id = '0' OR env_id = ?) LIMIT 1", array($this->getParam('roleName'), $this->getEnvironmentId()))) {
$err['roleName'] = 'Selected role name is already used. Please select another one.';
}
$cryptoKey = Scalr::GenerateRandomKey(40);
$creInfo = new ServerCreateInfo($this->getParam('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 => $this->getParam('roleName'), 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()));
$platform = PlatformFactory::NewPlatform($this->getParam('platform'));
if ($this->getParam('platform') == SERVER_PLATFORMS::EC2) {
$client = $this->environment->aws($this->getParam('cloudLocation'))->ec2;
$r = $client->instance->describe($this->getParam('cloudServerId'));
$instance = $r->get(0)->instancesSet->get(0);
$creInfo->SetProperties(array(EC2_SERVER_PROPERTIES::REGION => $this->getParam('cloudLocation'), EC2_SERVER_PROPERTIES::INSTANCE_ID => $this->getParam('cloudServerId'), EC2_SERVER_PROPERTIES::AMIID => $instance->imageId, EC2_SERVER_PROPERTIES::AVAIL_ZONE => $instance->placement->availabilityZone));
} else {
if ($this->getParam('platform') == SERVER_PLATFORMS::GCE) {
$gce = $platform->getClient($this->environment, $this->getParam('cloudLocation'));
$result = $gce->instances->get($this->environment->getPlatformConfigValue(Modules_Platforms_GoogleCE::PROJECT_ID), $this->getParam('cloudLocation'), $this->getParam('cloudServerId'));
$creInfo->SetProperties(array(GCE_SERVER_PROPERTIES::SERVER_ID => $result->id, GCE_SERVER_PROPERTIES::SERVER_NAME => $this->getParam('cloudServerId'), GCE_SERVER_PROPERTIES::CLOUD_LOCATION => $this->getParam('cloudLocation')));
} else {
if (PlatformFactory::isOpenstack($this->getParam('platform'))) {
$creInfo->SetProperties(array(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $this->getParam('cloudLocation'), OPENSTACK_SERVER_PROPERTIES::SERVER_ID => $this->getParam('cloudServerId')));
} else {
if (PlatformFactory::isCloudstack($this->getParam('platform'))) {
$creInfo->SetProperties(array(CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $this->getParam('cloudLocation'), CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID => $this->getParam('cloudServerId')));
}
}
}
}
$dbServer = DBServer::Create($creInfo, true);
$platform = PlatformFactory::NewPlatform($this->getParam('platform'));
$ips = $platform->GetServerIPAddresses($dbServer);
$dbServer->localIp = $ips['localIp'];
$dbServer->remoteIp = $ips['remoteIp'];
$dbServer->Save();
$this->response->data(array('command' => $this->getSzrCmd($dbServer), 'serverId' => $dbServer->serverId));
}