Пример #1
0
        internal static VirtualMachineScaleSetListWithLinkResult DeserializeVirtualMachineScaleSetListWithLinkResult(JsonElement element)
        {
            IReadOnlyList <VirtualMachineScaleSetData> value = default;
            Optional <string> nextLink = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("value"))
                {
                    List <VirtualMachineScaleSetData> array = new List <VirtualMachineScaleSetData>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(VirtualMachineScaleSetData.DeserializeVirtualMachineScaleSetData(item));
                    }
                    value = array;
                    continue;
                }
                if (property.NameEquals("nextLink"))
                {
                    nextLink = property.Value.GetString();
                    continue;
                }
            }
            return(new VirtualMachineScaleSetListWithLinkResult(value, nextLink.Value));
        }
        VirtualMachineScaleSet IOperationSource <VirtualMachineScaleSet> .CreateResult(Response response, CancellationToken cancellationToken)
        {
            using var document = JsonDocument.Parse(response.ContentStream);
            var data = VirtualMachineScaleSetData.DeserializeVirtualMachineScaleSetData(document.RootElement);

            return(new VirtualMachineScaleSet(_operationBase, data));
        }
Пример #3
0
    static OneFuzzResult <Scaleset> TrySetIdentity(Scaleset scaleset, VirtualMachineScaleSetData vmss)
    {
        if (scaleset.ClientObjectId is null)
        {
            return(OneFuzzResult.Ok(scaleset));
        }

        if (vmss.Identity is not null && vmss.Identity.UserAssignedIdentities is not null)
        {
            if (vmss.Identity.UserAssignedIdentities.Count != 1)
            {
                return(OneFuzzResult <Scaleset> .Error(ErrorCode.VM_CREATE_FAILED, "The scaleset is expected to have exactly 1 user assigned identity"));
            }
            var principalId = vmss.Identity.UserAssignedIdentities.First().Value.PrincipalId;
            if (principalId is null)
            {
                return(OneFuzzResult <Scaleset> .Error(ErrorCode.VM_CREATE_FAILED, "The scaleset principal ID is null"));
            }
            return(OneFuzzResult <Scaleset> .Ok(scaleset with {
                ClientObjectId = principalId
            }));
        }
Пример #4
0
 public static void AssertVirtualMachineScaleSet(VirtualMachineScaleSetData vmss1, VirtualMachineScaleSetData vmss2)
 {
     Assert.AreEqual(vmss1.Id, vmss2.Id);
     Assert.AreEqual(vmss1.Name, vmss2.Name);
 }
Пример #5
0
    public async Async.Task <OneFuzzResultVoid> CreateVmss(
        string location,
        Guid name,
        string vmSku,
        long vmCount,
        string image,
        string networkId,
        bool?spotInstance,
        bool ephemeralOsDisks,
        IList <VirtualMachineScaleSetExtensionData>?extensions,
        string password,
        string sshPublicKey,
        IDictionary <string, string> tags)
    {
        var vmss = await GetVmss(name);

        if (vmss is not null)
        {
            return(OneFuzzResultVoid.Ok);
        }
        _log.Info($"creating VM name: {name}, vm_sku: {vmSku}, vm_count: {vmCount}, image: {image}, subnet: {networkId}, spot_instance: {spotInstance}");
        var getOsResult = await _imageOps.GetOs(location, image);

        if (!getOsResult.IsOk)
        {
            return(getOsResult.ErrorV);
        }

        var vmssData = new VirtualMachineScaleSetData(location)
        {
            DoNotRunExtensionsOnOverprovisionedVms = false,
            Sku = new ComputeSku()
            {
                Name = vmSku
            },
            Overprovision        = false,
            SinglePlacementGroup = false,
            UpgradePolicy        = new UpgradePolicy()
            {
                Mode = UpgradeMode.Manual
            },
            Identity = new ManagedServiceIdentity(managedServiceIdentityType: ManagedServiceIdentityType.UserAssigned),
        };

        vmssData.Identity.UserAssignedIdentities.Add(_creds.GetScalesetIdentityResourcePath(), new UserAssignedIdentity());
        vmssData.VirtualMachineProfile = new VirtualMachineScaleSetVmProfile()
        {
            Priority = VirtualMachinePriorityTypes.Regular
        };
        var imageRef = new ImageReference();

        if (image.StartsWith('/'))
        {
            imageRef.Id = image;
        }
        else
        {
            var info = IImageOperations.GetImageInfo(image);
            imageRef.Publisher = info.Publisher;
            imageRef.Offer     = info.Offer;
            imageRef.Sku       = info.Sku;
            imageRef.Version   = info.Version;
        }
        vmssData.VirtualMachineProfile.StorageProfile = new VirtualMachineScaleSetStorageProfile()
        {
            ImageReference = imageRef
        };
        vmssData.VirtualMachineProfile.OSProfile = new VirtualMachineScaleSetOSProfile()
        {
            ComputerNamePrefix = "node", AdminUsername = "******"
        };

        var networkConfiguration = new VirtualMachineScaleSetNetworkConfiguration("onefuzz-nic")
        {
            Primary = true
        };
        var ipConfig = new VirtualMachineScaleSetIPConfiguration("onefuzz-ip-config");

        ipConfig.SubnetId = new ResourceIdentifier(networkId);
        networkConfiguration.IPConfigurations.Add(ipConfig);

        vmssData.VirtualMachineProfile.NetworkProfile = new VirtualMachineScaleSetNetworkProfile();
        vmssData.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations.Add(networkConfiguration);

        if (extensions is not null)
        {
            vmssData.VirtualMachineProfile.ExtensionProfile = new VirtualMachineScaleSetExtensionProfile();
            foreach (var e in extensions)
            {
                vmssData.VirtualMachineProfile.ExtensionProfile.Extensions.Add(e);
            }
        }

        switch (getOsResult.OkV)
        {
        case Os.Windows:
            vmssData.VirtualMachineProfile.OSProfile.AdminPassword = password;
            break;

        case Os.Linux:
            vmssData.VirtualMachineProfile.OSProfile.LinuxConfiguration = new LinuxConfiguration();
            vmssData.VirtualMachineProfile.OSProfile.LinuxConfiguration.DisablePasswordAuthentication = true;
            var i = new SshPublicKeyInfo()
            {
                KeyData = sshPublicKey, Path = "/home/onefuzz/.ssh/authorized_keys"
            };
            vmssData.VirtualMachineProfile.OSProfile.LinuxConfiguration.SshPublicKeys.Add(i);
            break;

        default:
            return(OneFuzzResultVoid.Error(ErrorCode.INVALID_CONFIGURATION, $"unhandled OS: {getOsResult.OkV} in image: {image}"));
        }

        if (ephemeralOsDisks)
        {
            vmssData.VirtualMachineProfile.StorageProfile.OSDisk = new VirtualMachineScaleSetOSDisk(DiskCreateOptionTypes.FromImage);
            vmssData.VirtualMachineProfile.StorageProfile.OSDisk.DiffDiskSettings        = new DiffDiskSettings();
            vmssData.VirtualMachineProfile.StorageProfile.OSDisk.DiffDiskSettings.Option = DiffDiskOptions.Local;
            vmssData.VirtualMachineProfile.StorageProfile.OSDisk.Caching = CachingTypes.ReadOnly;
        }

        if (spotInstance.HasValue && spotInstance.Value)
        {
            // Setting max price to -1 means it won't be evicted because of
            // price.
            //
            // https://docs.microsoft.com/en-us/azure/
            //   virtual-machine-scale-sets/use-spot#resource-manager-templates
            vmssData.VirtualMachineProfile.EvictionPolicy  = VirtualMachineEvictionPolicyTypes.Deallocate;
            vmssData.VirtualMachineProfile.Priority        = VirtualMachinePriorityTypes.Spot;
            vmssData.VirtualMachineProfile.BillingMaxPrice = 1.0;
        }

        foreach (var tag in tags)
        {
            vmssData.Tags.Add(tag);
        }

        if (_serviceConfig.OneFuzzOwner is not null)
        {
            vmssData.Tags.Add("OWNER", _serviceConfig.OneFuzzOwner);
        }

        try {
            var rg           = _creds.GetResourceGroupResource();
            var createUpdate = await rg.GetVirtualMachineScaleSets().CreateOrUpdateAsync(WaitUntil.Started, name.ToString(), vmssData);

            if (createUpdate.GetRawResponse().IsError)
            {
                var msg = $"Failed to create new scaleset due to {createUpdate.GetRawResponse().ReasonPhrase}";
                _log.Error(msg);
                return(OneFuzzResultVoid.Error(ErrorCode.VM_CREATE_FAILED, new[] { msg }));
            }
            else
            {
                return(OneFuzzResultVoid.Ok);
            }
        } catch (Exception ex) {
            _log.Exception(ex);
            return(OneFuzzResultVoid.Error(ErrorCode.VM_CREATE_FAILED, new[] { ex.Message }));
        }
    }