예제 #1
0
파일: AzureTest.php 프로젝트: mheydt/scalr
 /**
  * @test
  * @functional
  */
 public function testAzureFunctional()
 {
     $region = 'westus';
     $containerName = 'vhds';
     $osDiskName = 'osdisk1';
     $providers = $this->azure->getProvidersList($this->subscriptionId);
     $this->assertNotEmpty($providers);
     foreach ($providers as $provider) {
         /* @var $provider ProviderData */
         $this->assertNotEmpty($provider->namespace);
         $this->assertNotEmpty($provider->registrationState);
     }
     $provider = $this->azure->getLocationsList(ProviderData::RESOURCE_PROVIDER_COMPUTE);
     $this->assertNotEmpty($provider->namespace);
     $this->deleteAllTestObjects();
     $createResGroup = $this->azure->resourceManager->resourceGroup->create($this->subscriptionId, $this->resourceGroupName, $region);
     $this->assertNotEmpty($createResGroup);
     $request = new CreateAvailabilitySet($this->availabilitySetName, $region);
     $request->tags = ['key' => 'value'];
     $request->setProperties(['platformUpdateDomainCount' => 5, 'platformFaultDomainCount' => 3]);
     $createAs = $this->azure->compute->availabilitySet->create($this->subscriptionId, $this->resourceGroupName, $request);
     $this->assertNotEmpty($createAs);
     $virtualNetworkProperties = new VirtualNetworkProperties([['name' => 'mysubnet1', 'properties' => ["addressPrefix" => "10.1.0.0/24"]]]);
     $virtualNetworkProperties->addressSpace = ["addressPrefixes" => ["10.1.0.0/16", "10.2.0.0/16"]];
     $createVirtualNetwork = new CreateVirtualNetwork($this->vnName, $region, $virtualNetworkProperties);
     $vnResponse = $this->azure->network->virtualNetwork->create($this->subscriptionId, $this->resourceGroupName, $createVirtualNetwork);
     $this->assertNotEmpty($vnResponse);
     $createPublicIpAddress = new CreatePublicIpAddress($region, ["publicIPAllocationMethod" => 'Dynamic', 'settings' => ['domainNameLabel' => 'scalrtestlabel67']]);
     $nicIpAddressResponse = $this->azure->network->publicIPAddress->create($this->subscriptionId, $this->resourceGroupName, $this->publicIpName, $createPublicIpAddress);
     $this->assertNotEmpty($nicIpAddressResponse);
     $ipConfigProperties = new IpConfigurationProperties(["id" => "/subscriptions/" . $this->subscriptionId . "/resourceGroups/" . $this->resourceGroupName . "/providers/Microsoft.Network/virtualNetworks/" . $this->vnName . "/subnets/mysubnet1"], "Dynamic");
     $ipConfigProperties->publicIPAddress = ["id" => "/subscriptions/" . $this->subscriptionId . "/resourceGroups/" . $this->resourceGroupName . "/providers/Microsoft.Network/publicIPAddresses/" . $this->publicIpName];
     $nicProperties = new InterfaceProperties([new InterfaceIpConfigurationsData('ipconfig1', $ipConfigProperties)]);
     $createSecurityRule = new CreateSecurityRule('Tcp', '23-45', '46-56', '*', '*', 'Allow', 123, 'Inbound');
     $ruleData = new SecurityRuleData();
     $ruleData->name = 'rule_name';
     $ruleData->setProperties($createSecurityRule);
     $securityGroupProperties = new SecurityGroupProperties();
     $securityGroupProperties->setSecurityRules([$ruleData]);
     $createSecurityGroup = new CreateSecurityGroup($region);
     $createSecurityGroup->setProperties($securityGroupProperties);
     $sgResponse = $this->azure->network->securityGroup->create($this->subscriptionId, $this->resourceGroupName, $this->sgName, $createSecurityGroup);
     $this->assertNotEmpty($sgResponse);
     $secGroupId = "/subscriptions/" . $this->subscriptionId . "/resourceGroups/" . $this->resourceGroupName . "/providers/Microsoft.Network/networkSecurityGroups/" . $this->sgName;
     $nicProperties->setNetworkSecurityGroup(['id' => $secGroupId]);
     $createNic = new CreateInterface($region, $nicProperties);
     $nicResponse = $this->azure->network->interface->create($this->subscriptionId, $this->resourceGroupName, $this->nicName, $createNic);
     $this->assertNotEmpty($nicResponse);
     $nicInfo = $this->azure->network->interface->getInfo($this->subscriptionId, $this->resourceGroupName, $this->nicName);
     $this->assertNotEmpty($nicInfo);
     $disassociate = $this->azure->network->publicIPAddress->disassociate($this->subscriptionId, $this->resourceGroupName, $this->nicName, $this->publicIpName);
     $this->assertTrue($disassociate);
     $associate = $this->azure->network->publicIPAddress->associate($this->subscriptionId, $this->resourceGroupName, $this->nicName, $this->publicIpName);
     $this->assertTrue($associate);
     $createStorage = new AccountData($region, ['accountType' => 'Standard_LRS']);
     $storageResponse = $this->azure->storage->account->create($this->subscriptionId, $this->resourceGroupName, $this->storageName, $createStorage);
     $this->assertNotEmpty($storageResponse);
     $networkProfile = ["networkInterfaces" => [["id" => "/subscriptions/" . $this->subscriptionId . "/resourceGroups/" . $this->resourceGroupName . "/providers/Microsoft.Network/networkInterfaces/" . $this->nicName]]];
     $osProfile = new OsProfile('vladtest34', 'Tex6-HBU*7');
     $osProfile->computerName = 'testscalr';
     $vhd = ['uri' => 'https://' . $this->storageName . '.blob.core.windows.net/' . $containerName . '/' . $osDiskName . '.vhd'];
     $storageProfile = new StorageProfile(new OsDisk($osDiskName, $vhd, 'FromImage'));
     $publishers = $this->azure->compute->location->getPublishersList($this->subscriptionId, $region);
     $this->assertNotEmpty($publishers);
     $offers = null;
     foreach ($publishers as $publisher) {
         $this->assertObjectHasAttribute('name', $publisher);
         $offers = $this->azure->compute->location->getOffersList($this->subscriptionId, $region, $publisher->name);
         if (!empty($offers)) {
             break;
         }
     }
     $this->assertNotNull($offers);
     $offer = reset($offers);
     $this->assertObjectHasAttribute('name', $offer);
     $skus = $this->azure->compute->location->getSkusList($this->subscriptionId, $region, $publisher->name, $offer->name);
     $this->assertNotEmpty($skus);
     $sku = reset($skus);
     $this->assertObjectHasAttribute('name', $sku);
     $versions = $this->azure->compute->location->getVersionsList($this->subscriptionId, $region, $publisher->name, $offer->name, $sku->name);
     $this->assertNotEmpty($versions);
     $version = reset($versions);
     $this->assertObjectHasAttribute('name', $version);
     $storageProfile->setImageReference(['publisher' => 'Canonical', 'offer' => 'UbuntuServer', 'sku' => '14.04.2-LTS', 'version' => 'latest']);
     $vmProperties = new VirtualMachineProperties(["vmSize" => "Standard_A0"], $networkProfile, $storageProfile, $osProfile);
     //$this->azure->getClient()->setDebug();
     $vmRequest = new CreateVirtualMachine($this->vmName, $region, $vmProperties);
     $createVm = $this->azure->compute->virtualMachine->create($this->subscriptionId, $this->resourceGroupName, $vmRequest);
     $this->assertNotEmpty($createVm);
     $modelInfo = $this->azure->compute->virtualMachine->getModelViewInfo($this->subscriptionId, $this->resourceGroupName, $this->vmName, true);
     $this->assertNotEmpty($modelInfo);
     $extensionProperties = new ResourceExtensionProperties('Microsoft.OSTCExtensions', 'CustomScriptForLinux', '1.2');
     // for Windows use "new ResourceExtensionProperties('Microsoft.Compute', 'CustomScriptExtension', '1.0')"
     $extensionProperties->setSettings(['fileUris' => ["http://tru4.scalr.com/public/installScalarizr?osType=linux&repo=latest&platform=azure"], 'commandToExecute' => 'bash -c "cat installScalarizr | bash && service scalr-upd-client start"']);
     $createExtension = new CreateResourceExtension('scalarizr', $region, $extensionProperties);
     $this->azure->compute->resourceExtension->create($this->subscriptionId, $this->resourceGroupName, $this->vmName, $createExtension);
     $this->deleteAllTestObjects();
 }
예제 #2
0
 /**
  * {@inheritdoc}
  * @see \Scalr\Modules\PlatformModuleInterface::LaunchServer()
  */
 public function LaunchServer(DBServer $DBServer, Scalr_Server_LaunchOptions $launchOptions = null)
 {
     $environment = $DBServer->GetEnvironmentObject();
     $governance = new \Scalr_Governance($DBServer->envId);
     $azure = $environment->azure();
     $subscriptionId = $environment->keychain(SERVER_PLATFORMS::AZURE)->properties[CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID];
     if (!$launchOptions) {
         $dbFarmRole = $DBServer->GetFarmRoleObject();
         $DBRole = $dbFarmRole->GetRoleObject();
         $launchOptions = new \Scalr_Server_LaunchOptions();
         $launchOptions->cloudLocation = $dbFarmRole->CloudLocation;
         $launchOptions->serverType = $dbFarmRole->GetSetting(FarmRoleSetting::INSTANCE_TYPE);
         $launchOptions->availZone = $dbFarmRole->GetSetting(FarmRoleSetting::SETTING_AZURE_AVAIL_SET);
         $launchOptions->imageId = $DBRole->__getNewRoleObject()->getImage(\SERVER_PLATFORMS::AZURE, "")->imageId;
         $isWindows = $DBRole->getOs()->family == 'windows';
         // Set User Data
         $u_data = "";
         foreach ($DBServer->GetCloudUserData() as $k => $v) {
             $u_data .= "{$k}={$v};";
         }
         $launchOptions->userData = trim($u_data, ";");
         $launchOptions->azureResourceGroup = $dbFarmRole->GetSetting(FarmRoleSetting::SETTING_AZURE_RESOURCE_GROUP);
         $launchOptions->azureStorageAccount = $dbFarmRole->GetSetting(FarmRoleSetting::SETTING_AZURE_STORAGE_ACCOUNT);
         //Create NIC
         try {
             $ipConfigProperties = new IpConfigurationProperties(["id" => sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s/subnets/%s", $subscriptionId, $launchOptions->azureResourceGroup, $dbFarmRole->GetSetting(FarmRoleSetting::SETTING_AZURE_VIRTUAL_NETWORK), $dbFarmRole->GetSetting(FarmRoleSetting::SETTING_AZURE_SUBNET))], "Dynamic");
             $publicIpName = null;
             if ($governance->isEnabled(\SERVER_PLATFORMS::AZURE, \Scalr_Governance::AZURE_NETWORK)) {
                 $usePublicIp = $governance->getValue(\SERVER_PLATFORMS::AZURE, \Scalr_Governance::AZURE_NETWORK, 'use_public_ips');
             }
             if (!isset($usePublicIp)) {
                 $usePublicIp = $dbFarmRole->GetSetting(FarmRoleSetting::SETTING_AZURE_USE_PUBLIC_IPS);
             }
             if ($usePublicIp) {
                 //Create Public IP object
                 $publicIpName = "scalr-{$DBServer->serverId}";
                 $createPublicIpAddressRequest = new CreatePublicIpAddress($launchOptions->cloudLocation, new PublicIpAddressProperties('Dynamic'));
                 $ipCreateResult = $azure->network->publicIPAddress->create($subscriptionId, $launchOptions->azureResourceGroup, $publicIpName, $createPublicIpAddressRequest);
             }
             if ($publicIpName) {
                 $ipConfigProperties->publicIPAddress = ["id" => sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/publicIPAddresses/%s", $subscriptionId, $launchOptions->azureResourceGroup, $publicIpName)];
             }
             $nicProperties = new InterfaceProperties([new InterfaceIpConfigurationsData('public1', $ipConfigProperties)]);
             //Security group
             $sg = $dbFarmRole->GetSetting(FarmRoleSetting::SETTING_AZURE_SECURITY_GROUPS_LIST);
             if ($sg) {
                 $sgName = json_decode($sg);
                 if ($sgName) {
                     $sgroup = new SecurityGroupData();
                     $sgroup->id = sprintf('/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/%s', $subscriptionId, $launchOptions->azureResourceGroup, $sgName[0]);
                     $nicProperties->setNetworkSecurityGroup($sgroup);
                 }
             }
             $createNicData = new CreateInterface($launchOptions->cloudLocation, $nicProperties);
             $nicResponse = $azure->network->interface->create($subscriptionId, $launchOptions->azureResourceGroup, "scalr-{$DBServer->serverId}", $createNicData);
         } catch (\Exception $e) {
             throw new \Exception("Scalr is unable to create NetworkInterface: {$e->getMessage()}");
         }
         $launchOptions->azureNicName = "scalr-{$DBServer->serverId}";
         $launchOptions->azurePublicIpName = $publicIpName;
     }
     // Configure OS Profile
     // Make sure that password always have 1 special character.
     $adminPassword = \Scalr::GenerateSecurePassword(16, ['D' => '.']);
     $osProfile = new OsProfile('scalr', $adminPassword);
     $osProfile->computerName = \Scalr::GenerateUID(true);
     $osProfile->customData = base64_encode(trim($launchOptions->userData));
     // Configure Network Profile
     $networkProfile = ["networkInterfaces" => [["id" => sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkInterfaces/%s", $subscriptionId, $launchOptions->azureResourceGroup, $launchOptions->azureNicName)]]];
     // Configure Storage Profile
     $osDiskName = "scalr-{$DBServer->serverId}";
     $vhd = ['uri' => sprintf("https://%s.blob.core.windows.net/vhds/%s.vhd", $launchOptions->azureStorageAccount, $osDiskName)];
     $storageProfile = new StorageProfile(new OsDisk($osDiskName, $vhd, 'FromImage'));
     if (preg_match("/^([^\\/]+)\\/([^\\/]+)\\/([^\\/]+)\\/([^\\/]+)(\\/(1))?\$/", rtrim($launchOptions->imageId, '/'), $imageChunks)) {
         $publisher = $imageChunks[1];
         $offer = $imageChunks[2];
         $sku = $imageChunks[3];
         $version = $imageChunks[4];
         $isMarketPlaceImage = isset($imageChunks[5]) ? true : false;
         if ($isMarketPlaceImage) {
             $plan = new PlanProperties($sku, $publisher, $offer);
         }
         $storageProfile->setImageReference(['publisher' => $publisher, 'offer' => $offer, 'sku' => $sku, 'version' => $version]);
     } else {
         throw new \Exception("Image definition '{$launchOptions->imageId}' is not supported");
     }
     $vmProps = new VirtualMachineProperties(["vmSize" => $launchOptions->serverType], $networkProfile, $storageProfile, $osProfile);
     // Set availability set if configured.
     if ($launchOptions->availZone) {
         $vmProps->availabilitySet = ['id' => sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/availabilitySets/%s", $subscriptionId, $launchOptions->azureResourceGroup, $launchOptions->availZone)];
     }
     $vmData = new CreateVirtualMachine($DBServer->serverId, $launchOptions->cloudLocation, $vmProps);
     $vmData->tags = $DBServer->getAzureTags();
     if (isset($plan)) {
         $vmData->setPlan($plan);
     }
     $azure->compute->virtualMachine->create($subscriptionId, $launchOptions->azureResourceGroup, $vmData);
     $DBServer->setOsType($isWindows ? 'windows' : 'linux');
     $instanceTypeInfo = $this->getInstanceType($launchOptions->serverType, $environment, $launchOptions->cloudLocation);
     /* @var $instanceTypeInfo CloudInstanceType */
     $DBServer->SetProperties([\AZURE_SERVER_PROPERTIES::SERVER_NAME => $DBServer->serverId, \AZURE_SERVER_PROPERTIES::ADMIN_PASSWORD => $adminPassword, \AZURE_SERVER_PROPERTIES::RESOURCE_GROUP => $launchOptions->azureResourceGroup, \AZURE_SERVER_PROPERTIES::CLOUD_LOCATION => $launchOptions->cloudLocation, \AZURE_SERVER_PROPERTIES::AVAIL_SET => $launchOptions->availZone, \AZURE_SERVER_PROPERTIES::NETWORK_INTERFACE => $launchOptions->azureNicName, \AZURE_SERVER_PROPERTIES::PUBLIC_IP_NAME => $launchOptions->azurePublicIpName, \SERVER_PROPERTIES::INFO_INSTANCE_VCPUS => $instanceTypeInfo ? $instanceTypeInfo->vcpus : null]);
     $params = ['type' => $launchOptions->serverType];
     if ($instanceTypeInfo) {
         $params['instanceTypeName'] = $instanceTypeInfo->name;
     }
     $DBServer->imageId = $launchOptions->imageId;
     $DBServer->update($params);
     $DBServer->cloudLocation = $launchOptions->cloudLocation;
     $DBServer->cloudLocationZone = $launchOptions->availZone;
     // we set server history here
     $DBServer->getServerHistory()->update(['cloudServerId' => $DBServer->serverId]);
     return $DBServer;
 }
예제 #3
0
 /**
  * Sets StorageProfile
  *
  * @param   array|StorageProfile $storageProfile
  * @return  VirtualMachineProperties
  */
 public function setStorageProfile($storageProfile = null)
 {
     if (!$storageProfile instanceof StorageProfile) {
         $storageProfile = StorageProfile::initArray($storageProfile);
     }
     return $this->__call(__FUNCTION__, [$storageProfile]);
 }