Example #1
0
 /**
  * Create or update a virtual machine in a given subscription.
  * In the update scenario, these APIs will be specifically used for
  * detaching a data disk from a VM.
  *
  * @param string $subscriptionId    Subscription Id
  * @param string $resourceGroup     Name of Resource Group
  * @param array|CreateVirtualMachine|VirtualMachineData $requestData       Request data
  * @param bool   $validating        optional Validating
  * @return VirtualMachineData
  */
 public function create($subscriptionId, $resourceGroup, $requestData, $validating = false)
 {
     $result = null;
     if (!$requestData instanceof CreateVirtualMachine && !$requestData instanceof VirtualMachineData) {
         $requestData = CreateVirtualMachine::initArray($requestData);
     }
     $path = '/subscriptions/' . $subscriptionId . '/resourceGroups/' . $resourceGroup . ComputeService::ENDPOINT_MICROSOFT_COMPUTE . '/virtualMachines/' . $requestData->name;
     if (empty($requestData->id)) {
         $requestData->id = $path;
     }
     $request = $this->getClient()->prepareRequest($path, 'PUT', $this->getApiVersion(), $this->getServiceUrl(), ['validating' => $validating], $requestData->toArray());
     $response = $this->getClient()->call($request);
     if (!$response->hasError()) {
         $result = VirtualMachineData::initArray($response->getResult());
     }
     return $result;
 }
Example #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;
 }