function _testRunInstances() { $RunInstancesType = new RunInstancesType(); $RunInstancesType->imageId = $this->imageId; $RunInstancesType->minCount = 1; $RunInstancesType->maxCount = 1; $RunInstancesType->AddSecurityGroup("default"); $RunInstancesType->additionalInfo = "http://webta.net"; $RunInstancesType->SetUserData("test123"); $RunInstancesType->instanceType = "m1.small"; $result = $this->AmazonEC2->RunInstances($RunInstancesType); $this->assertTrue($result->instancesSet->item->instanceId, "RunInstances return instanceId"); $this->instanceId = $result->instancesSet->item->instanceId; }
public function LaunchServer(DBServer $DBServer, Scalr_Server_LaunchOptions $launchOptions = null) { $RunInstancesType = new RunInstancesType(); $RunInstancesType->ConfigureRootPartition(); if (!$launchOptions) { $launchOptions = new Scalr_Server_LaunchOptions(); $DBRole = DBRole::loadById($DBServer->roleId); // Set Cloudwatch monitoring $RunInstancesType->SetCloudWatchMonitoring($DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_AWS_ENABLE_CW_MONITORING)); $launchOptions->architecture = $DBRole->architecture; $launchOptions->imageId = $DBRole->getImageId(SERVER_PLATFORMS::EC2, $DBServer->GetFarmRoleObject()->CloudLocation); $launchOptions->cloudLocation = $DBServer->GetFarmRoleObject()->CloudLocation; $akiId = $DBServer->GetProperty(EC2_SERVER_PROPERTIES::AKIID); if (!$akiId) { $akiId = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_AWS_AKI_ID); } if ($akiId) { $RunInstancesType->kernelId = $akiId; } $ariId = $DBServer->GetProperty(EC2_SERVER_PROPERTIES::ARIID); if (!$ariId) { $ariId = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_AWS_ARI_ID); } if ($ariId) { $RunInstancesType->ramdiskId = $ariId; } $i_type = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_AWS_INSTANCE_TYPE); if (!$i_type) { $DBRole = DBRole::loadById($DBServer->roleId); $i_type = $DBRole->getProperty(EC2_SERVER_PROPERTIES::INSTANCE_TYPE); } $launchOptions->serverType = $i_type; foreach ($DBServer->GetCloudUserData() as $k => $v) { $u_data .= "{$k}={$v};"; } $RunInstancesType->SetUserData(trim($u_data, ";")); $vpcPrivateIp = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_AWS_VPC_PRIVATE_IP); $vpcSubnetId = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_AWS_VPC_SUBNET_ID); if ($vpcSubnetId) { $RunInstancesType->subnetId = $vpcSubnetId; if ($vpcPrivateIp) { $RunInstancesType->privateIpAddress = $vpcPrivateIp; } } } else { $RunInstancesType->SetUserData(trim($launchOptions->userData)); } $DBServer->SetProperty(SERVER_PROPERTIES::ARCHITECTURE, $launchOptions->architecture); $EC2Client = Scalr_Service_Cloud_Aws::newEc2($launchOptions->cloudLocation, $DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::PRIVATE_KEY), $DBServer->GetEnvironmentObject()->getPlatformConfigValue(self::CERTIFICATE)); // Set AMI, AKI and ARI ids $RunInstancesType->imageId = $launchOptions->imageId; if (!$RunInstancesType->subnetId) { // Set Security groups foreach ($this->GetServerSecurityGroupsList($DBServer, $EC2Client) as $sgroup) { $RunInstancesType->AddSecurityGroup($sgroup); } } $RunInstancesType->minCount = 1; $RunInstancesType->maxCount = 1; // Set availability zone if (!$launchOptions->availZone) { $avail_zone = $this->GetServerAvailZone($DBServer, $EC2Client, $launchOptions); if ($avail_zone) { $RunInstancesType->SetAvailabilityZone($avail_zone); } } else { $RunInstancesType->SetAvailabilityZone($launchOptions->availZone); } // Set instance type $RunInstancesType->instanceType = $launchOptions->serverType; if (in_array($RunInstancesType->instanceType, array('cc1.4xlarge', 'cg1.4xlarge', 'cc2.8xlarge'))) { $placementGroup = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_AWS_CLUSTER_PG); if (!$placementGroup) { $placementGroup = "scalr-role-{$DBServer->farmRoleId}"; if (!$EC2Client->CreatePlacementGroup($placementGroup)) { throw new Exception(sprintf(_("Cannot launch new instance. Unable to create placement group: %s"), $result->faultstring)); } $DBServer->GetFarmRoleObject()->SetSetting(DBFarmRole::SETTING_AWS_CLUSTER_PG, $placementGroup); } $RunInstancesType->SetPlacementGroup($placementGroup); } // Set additional info $RunInstancesType->additionalInfo = ""; ///// if ($DBServer->status == SERVER_STATUS::TEMPORARY) { $keyName = "SCALR-ROLESBUILDER"; $sshKey = Scalr_Model::init(Scalr_Model::SSH_KEY); if (!$sshKey->loadGlobalByName($keyName, $launchOptions->cloudLocation, $DBServer->envId)) { $result = $EC2Client->CreateKeyPair($keyName); if ($result->keyMaterial) { $sshKey->farmId = 0; $sshKey->clientId = $DBServer->clientId; $sshKey->envId = $DBServer->envId; $sshKey->type = Scalr_SshKey::TYPE_GLOBAL; $sshKey->cloudLocation = $launchOptions->cloudLocation; $sshKey->cloudKeyName = $keyName; $sshKey->platform = SERVER_PLATFORMS::EC2; $sshKey->setPrivate($result->keyMaterial); $sshKey->setPublic($sshKey->generatePublicKey()); $sshKey->save(); } } } else { $sshKey = Scalr_Model::init(Scalr_Model::SSH_KEY)->loadGlobalByFarmId($DBServer->farmId, $DBServer->GetProperty(EC2_SERVER_PROPERTIES::REGION)); $keyName = $sshKey->cloudKeyName; } ///// $RunInstancesType->keyName = $keyName; try { $result = $EC2Client->RunInstances($RunInstancesType); } catch (Exception $e) { if (stristr($e->getMessage(), "key pair") && stristr($e->getMessage(), "does not exist")) { $result = $EC2Client->CreateKeyPair($keyName); if ($result->keyMaterial) { $sshKey->setPrivate($result->keyMaterial); $sshKey->setPublic($sshKey->generatePublicKey()); $sshKey->save(); } //Your requested instance type (m2.2xlarge) is not supported in your requested Availability Zone (us-east-1a). Please retry your request by not specifying an Availability Zone or choosing us-east-1c, us-east-1b, us-east-1 } else { if (stristr($e->getMessage(), "The requested Availability Zone is no longer supported") || stristr($e->getMessage(), "is not supported in your requested Availability Zone")) { $availZone = $RunInstancesType->placement->availabilityZone; $DBServer->GetEnvironmentObject()->setPlatformConfig(array("aws.{$launchOptions->cloudLocation}.{$availZone}.unavailable" => time()), false); throw $e; } else { throw $e; } } } if ($result->instancesSet) { $DBServer->SetProperty(EC2_SERVER_PROPERTIES::AVAIL_ZONE, (string) $result->instancesSet->item->placement->availabilityZone); $DBServer->SetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID, (string) $result->instancesSet->item->instanceId); $DBServer->SetProperty(EC2_SERVER_PROPERTIES::INSTANCE_TYPE, $RunInstancesType->instanceType); $DBServer->SetProperty(EC2_SERVER_PROPERTIES::AMIID, $RunInstancesType->imageId); $DBServer->SetProperty(EC2_SERVER_PROPERTIES::REGION, $launchOptions->cloudLocation); try { if ($DBServer->farmId != 0) { $CreateTagsType = new CreateTagsType(array((string) $result->instancesSet->item->instanceId), array("scalr-farm-id" => $DBServer->farmId, "scalr-farm-name" => $DBServer->GetFarmObject()->Name, "scalr-farm-role-id" => $DBServer->farmRoleId, "scalr-role-name" => $DBServer->GetFarmRoleObject()->GetRoleObject()->name, "scalr-server-id" => $DBServer->serverId)); $EC2Client->CreateTags($CreateTagsType); } } catch (Exception $e) { Logger::getLogger('EC2')->warn("Cannot add tags to server: {$e->getMessage()}"); } return $DBServer; } else { throw new Exception(sprintf(_("Cannot launch new instance. %s"), serialize($result))); } }