def _build_os_profile(): os_profile = { 'computerName': name, 'adminUsername': admin_username } if admin_password: os_profile['adminPassword'] = "******" if custom_data: os_profile['customData'] = b64encode(custom_data) if ssh_key_value and ssh_key_path: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': authentication_type == 'ssh', 'ssh': { 'publicKeys': [ { 'keyData': ssh_key_value, 'path': ssh_key_path } ] } } if secrets: os_profile['secrets'] = secrets return os_profile
def _build_os_profile(): os_profile = {'computerName': name, 'adminUsername': admin_username} if admin_password: os_profile['adminPassword'] = "******" if custom_data: os_profile['customData'] = b64encode(custom_data) if ssh_key_value and ssh_key_path: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': True, 'ssh': { 'publicKeys': [{ 'keyData': ssh_key_value, 'path': ssh_key_path }] } } if secrets: os_profile['secrets'] = secrets return os_profile
def _build_os_profile(): special_chars = '`~!@#$%^&*()=+_[]{}\\|;:\'\",<>/?' os_profile = { # Use name as computer_name if it's not provided. Remove special characters from name. 'computerName': computer_name or ''.join(filter(lambda x: x not in special_chars, name)), 'adminUsername': admin_username } if admin_password: os_profile['adminPassword'] = "******" if custom_data: os_profile['customData'] = b64encode(custom_data) if ssh_key_values and ssh_key_path: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': authentication_type == 'ssh', 'ssh': { 'publicKeys': [{ 'keyData': ssh_key_value, 'path': ssh_key_path } for ssh_key_value in ssh_key_values] } } if enable_agent is not None: if custom_image_os_type.lower() == 'linux': if 'linuxConfiguration' not in os_profile: os_profile['linuxConfiguration'] = {} os_profile['linuxConfiguration'][ 'provisionVMAgent'] = enable_agent elif custom_image_os_type.lower() == 'windows': if 'windowsConfiguration' not in os_profile: os_profile['windowsConfiguration'] = {} os_profile['windowsConfiguration'][ 'provisionVMAgent'] = enable_agent if secrets: os_profile['secrets'] = secrets if enable_auto_update is not None and custom_image_os_type.lower( ) == 'windows': os_profile['windowsConfiguration'][ 'enableAutomaticUpdates'] = enable_auto_update if patch_mode is not None and custom_image_os_type.lower( ) == 'windows': os_profile['windowsConfiguration']['patchSettings'] = { 'patchMode': patch_mode, 'enableHotpatching': enable_hotpatching } return os_profile
def build_vmss_resource(cmd, name, naming_prefix, location, tags, overprovision, upgrade_policy_mode, vm_sku, instance_count, ip_config_name, nic_name, subnet_id, public_ip_per_vm, vm_domain_name, dns_servers, nsg, accelerated_networking, admin_username, authentication_type, storage_profile, os_disk_name, disk_info, storage_sku, os_type, image=None, admin_password=None, ssh_key_value=None, ssh_key_path=None, os_publisher=None, os_offer=None, os_sku=None, os_version=None, backend_address_pool_id=None, inbound_nat_pool_id=None, health_probe=None, single_placement_group=None, platform_fault_domain_count=None, custom_data=None, secrets=None, license_type=None, zones=None, priority=None, eviction_policy=None, application_security_groups=None): # Build IP configuration ip_configuration = { 'name': ip_config_name, 'properties': { 'subnet': { 'id': subnet_id } } } if public_ip_per_vm: ip_configuration['properties']['publicipaddressconfiguration'] = { 'name': 'instancepublicip', 'properties': { 'idleTimeoutInMinutes': 10, } } if vm_domain_name: ip_configuration['properties']['publicipaddressconfiguration'][ 'properties']['dnsSettings'] = { 'domainNameLabel': vm_domain_name } if backend_address_pool_id: key = 'loadBalancerBackendAddressPools' if 'loadBalancers' in backend_address_pool_id \ else 'ApplicationGatewayBackendAddressPools' ip_configuration['properties'][key] = [{'id': backend_address_pool_id}] if inbound_nat_pool_id: ip_configuration['properties']['loadBalancerInboundNatPools'] = [{ 'id': inbound_nat_pool_id }] if application_security_groups and cmd.supported_api_version( min_api='2018-06-01', operation_group='virtual_machine_scale_sets'): ip_configuration['properties']['applicationSecurityGroups'] = [{ 'id': x.id } for x in application_security_groups] # Build storage profile storage_properties = {} os_caching = disk_info['os'].get('caching') if storage_profile in [ StorageProfile.SACustomImage, StorageProfile.SAPirImage ]: storage_properties['osDisk'] = { 'name': os_disk_name, 'caching': os_caching, 'createOption': 'FromImage', } if storage_profile == StorageProfile.SACustomImage: storage_properties['osDisk'].update({ 'osType': os_type, 'image': { 'uri': image } }) else: storage_properties['osDisk'][ 'vhdContainers'] = "[variables('vhdContainers')]" elif storage_profile in [ StorageProfile.ManagedPirImage, StorageProfile.ManagedCustomImage ]: storage_properties['osDisk'] = { 'createOption': 'FromImage', 'caching': os_caching, 'managedDisk': { 'storageAccountType': storage_sku } } if storage_profile in [ StorageProfile.SAPirImage, StorageProfile.ManagedPirImage ]: storage_properties['imageReference'] = { 'publisher': os_publisher, 'offer': os_offer, 'sku': os_sku, 'version': os_version } if storage_profile == StorageProfile.ManagedCustomImage: storage_properties['imageReference'] = {'id': image} data_disks = [v for k, v in disk_info.items() if k != 'os'] if data_disks: storage_properties['dataDisks'] = data_disks # Build OS Profile os_profile = { 'computerNamePrefix': naming_prefix, 'adminUsername': admin_username } if authentication_type == 'password' and admin_password: os_profile['adminPassword'] = "******" else: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': True, 'ssh': { 'publicKeys': [{ 'path': ssh_key_path, 'keyData': ssh_key_value }] } } if custom_data: os_profile['customData'] = b64encode(custom_data) if secrets: os_profile['secrets'] = secrets # Build VMSS nic_config = { 'name': nic_name, 'properties': { 'primary': 'true', 'ipConfigurations': [ip_configuration] } } if cmd.supported_api_version(min_api='2017-03-30', operation_group='virtual_machine_scale_sets'): if dns_servers: nic_config['properties']['dnsSettings'] = { 'dnsServers': dns_servers } if accelerated_networking: nic_config['properties']['enableAcceleratedNetworking'] = True if nsg: nic_config['properties']['networkSecurityGroup'] = {'id': nsg} vmss_properties = { 'overprovision': overprovision, 'upgradePolicy': { 'mode': upgrade_policy_mode }, 'virtualMachineProfile': { 'storageProfile': storage_properties, 'osProfile': os_profile, 'networkProfile': { 'networkInterfaceConfigurations': [nic_config] } } } if license_type: vmss_properties['virtualMachineProfile']['licenseType'] = license_type if health_probe and cmd.supported_api_version( min_api='2017-03-30', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile']['networkProfile'][ 'healthProbe'] = { 'id': health_probe } if cmd.supported_api_version(min_api='2016-04-30-preview', operation_group='virtual_machine_scale_sets'): vmss_properties['singlePlacementGroup'] = single_placement_group if priority and cmd.supported_api_version( min_api='2017-12-01', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile']['priority'] = priority if eviction_policy and cmd.supported_api_version( min_api='2017-12-01', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile'][ 'evictionPolicy'] = eviction_policy if platform_fault_domain_count is not None and cmd.supported_api_version( min_api='2017-12-01', operation_group='virtual_machine_scale_sets'): vmss_properties[ 'platformFaultDomainCount'] = platform_fault_domain_count vmss = { 'type': 'Microsoft.Compute/virtualMachineScaleSets', 'name': name, 'location': location, 'tags': tags, 'apiVersion': cmd.get_api_version(ResourceType.MGMT_COMPUTE, operation_group='virtual_machine_scale_sets'), 'dependsOn': [], 'sku': { 'name': vm_sku, 'capacity': instance_count }, 'properties': vmss_properties } if zones: vmss['zones'] = zones return vmss
def build_vmss_resource(cmd, name, naming_prefix, location, tags, overprovision, upgrade_policy_mode, vm_sku, instance_count, ip_config_name, nic_name, subnet_id, public_ip_per_vm, vm_domain_name, dns_servers, nsg, accelerated_networking, admin_username, authentication_type, storage_profile, os_disk_name, os_caching, data_caching, storage_sku, data_disk_sizes_gb, image_data_disks, os_type, image=None, admin_password=None, ssh_key_value=None, ssh_key_path=None, os_publisher=None, os_offer=None, os_sku=None, os_version=None, backend_address_pool_id=None, inbound_nat_pool_id=None, health_probe=None, single_placement_group=None, custom_data=None, secrets=None, license_type=None, zones=None): # Build IP configuration ip_configuration = { 'name': ip_config_name, 'properties': { 'subnet': { 'id': subnet_id } } } if public_ip_per_vm: ip_configuration['properties']['publicipaddressconfiguration'] = { 'name': 'instancepublicip', 'properties': { 'idleTimeoutInMinutes': 10, } } if vm_domain_name: ip_configuration['properties']['publicipaddressconfiguration'][ 'properties']['dnsSettings'] = { 'domainNameLabel': vm_domain_name } if backend_address_pool_id: key = 'loadBalancerBackendAddressPools' if 'loadBalancers' in backend_address_pool_id \ else 'ApplicationGatewayBackendAddressPools' ip_configuration['properties'][key] = [{'id': backend_address_pool_id}] if inbound_nat_pool_id: ip_configuration['properties']['loadBalancerInboundNatPools'] = [{ 'id': inbound_nat_pool_id }] # Build storage profile storage_properties = {} if storage_profile in [ StorageProfile.SACustomImage, StorageProfile.SAPirImage ]: storage_properties['osDisk'] = { 'name': os_disk_name, 'caching': os_caching, 'createOption': 'FromImage', } if storage_profile == StorageProfile.SACustomImage: storage_properties['osDisk'].update({ 'osType': os_type, 'image': { 'uri': image } }) else: storage_properties['osDisk'][ 'vhdContainers'] = "[variables('vhdContainers')]" elif storage_profile in [ StorageProfile.ManagedPirImage, StorageProfile.ManagedCustomImage ]: storage_properties['osDisk'] = { 'createOption': 'FromImage', 'caching': os_caching, 'managedDisk': { 'storageAccountType': storage_sku } } if storage_profile in [ StorageProfile.SAPirImage, StorageProfile.ManagedPirImage ]: storage_properties['imageReference'] = { 'publisher': os_publisher, 'offer': os_offer, 'sku': os_sku, 'version': os_version } if storage_profile == StorageProfile.ManagedCustomImage: storage_properties['imageReference'] = {'id': image} storage_profile = _build_data_disks(storage_properties, data_disk_sizes_gb, image_data_disks, data_caching, storage_sku) # Build OS Profile os_profile = { 'computerNamePrefix': naming_prefix, 'adminUsername': admin_username } if authentication_type == 'password': os_profile['adminPassword'] = admin_password else: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': True, 'ssh': { 'publicKeys': [{ 'path': ssh_key_path, 'keyData': ssh_key_value }] } } if custom_data: os_profile['customData'] = b64encode(custom_data) if secrets: os_profile['secrets'] = secrets if single_placement_group is None: # this should never happen, but just in case raise ValueError('single_placement_group was not set by validators') # Build VMSS nic_config = { 'name': nic_name, 'properties': { 'primary': 'true', 'ipConfigurations': [ip_configuration] } } if cmd.supported_api_version( min_api='2017-03-30').virtual_machine_scale_sets: # pylint: disable=no-member if dns_servers: nic_config['properties']['dnsSettings'] = { 'dnsServers': dns_servers } if accelerated_networking: nic_config['properties']['enableAcceleratedNetworking'] = True if nsg: nic_config['properties']['networkSecurityGroup'] = {'id': nsg} vmss_properties = { 'overprovision': overprovision, 'upgradePolicy': { 'mode': upgrade_policy_mode }, 'virtualMachineProfile': { 'storageProfile': storage_properties, 'osProfile': os_profile, 'networkProfile': { 'networkInterfaceConfigurations': [nic_config] } } } if license_type: vmss_properties['virtualMachineProfile']['licenseType'] = license_type if health_probe and cmd.supported_api_version( min_api='2017-03-30').virtual_machine_scale_sets: # pylint: disable=no-member vmss_properties['virtualMachineProfile']['networkProfile'][ 'healthProbe'] = { 'id': health_probe } if cmd.supported_api_version( min_api='2016-04-30-preview').virtual_machine_scale_sets: # pylint: disable=no-member vmss_properties['singlePlacementGroup'] = single_placement_group vmss = { 'type': 'Microsoft.Compute/virtualMachineScaleSets', 'name': name, 'location': location, 'tags': tags, 'apiVersion': cmd.get_api_version( ResourceType.MGMT_COMPUTE).virtual_machine_scale_sets, # pylint: disable=no-member 'dependsOn': [], 'sku': { 'name': vm_sku, 'capacity': instance_count }, 'properties': vmss_properties } if zones: vmss['zones'] = zones return vmss
def build_vmss_resource(cmd, name, naming_prefix, location, tags, overprovision, upgrade_policy_mode, vm_sku, instance_count, ip_config_name, nic_name, subnet_id, public_ip_per_vm, vm_domain_name, dns_servers, nsg, accelerated_networking, admin_username, authentication_type, storage_profile, os_disk_name, disk_info, os_type, image=None, admin_password=None, ssh_key_values=None, ssh_key_path=None, os_publisher=None, os_offer=None, os_sku=None, os_version=None, backend_address_pool_id=None, inbound_nat_pool_id=None, health_probe=None, single_placement_group=None, platform_fault_domain_count=None, custom_data=None, secrets=None, license_type=None, zones=None, priority=None, eviction_policy=None, application_security_groups=None, ultra_ssd_enabled=None, proximity_placement_group=None, terminate_notification_time=None, max_price=None, scale_in_policy=None, os_disk_encryption_set=None, data_disk_encryption_sets=None, data_disk_iops=None, data_disk_mbps=None, automatic_repairs_grace_period=None, specialized=None, os_disk_size_gb=None, encryption_at_host=None, host_group=None): # Build IP configuration ip_configuration = { 'name': ip_config_name, 'properties': { 'subnet': {'id': subnet_id} } } if public_ip_per_vm: ip_configuration['properties']['publicipaddressconfiguration'] = { 'name': 'instancepublicip', 'properties': { 'idleTimeoutInMinutes': 10, } } if vm_domain_name: ip_configuration['properties']['publicipaddressconfiguration']['properties']['dnsSettings'] = { 'domainNameLabel': vm_domain_name } if backend_address_pool_id: key = 'loadBalancerBackendAddressPools' if 'loadBalancers' in backend_address_pool_id \ else 'ApplicationGatewayBackendAddressPools' ip_configuration['properties'][key] = [ {'id': backend_address_pool_id} ] if inbound_nat_pool_id: ip_configuration['properties']['loadBalancerInboundNatPools'] = [ {'id': inbound_nat_pool_id} ] if application_security_groups and cmd.supported_api_version(min_api='2018-06-01', operation_group='virtual_machine_scale_sets'): ip_configuration['properties']['applicationSecurityGroups'] = [{'id': x.id} for x in application_security_groups] # Build storage profile storage_properties = {} os_caching = disk_info['os'].get('caching') if storage_profile in [StorageProfile.SACustomImage, StorageProfile.SAPirImage]: storage_properties['osDisk'] = { 'name': os_disk_name, 'caching': os_caching, 'createOption': 'FromImage', } if storage_profile == StorageProfile.SACustomImage: storage_properties['osDisk'].update({ 'osType': os_type, 'image': { 'uri': image } }) else: storage_properties['osDisk']['vhdContainers'] = "[variables('vhdContainers')]" if os_disk_size_gb is not None: storage_properties['osDisk']['diskSizeGB'] = os_disk_size_gb elif storage_profile in [StorageProfile.ManagedPirImage, StorageProfile.ManagedCustomImage]: storage_properties['osDisk'] = { 'createOption': 'FromImage', 'caching': os_caching, 'managedDisk': {'storageAccountType': disk_info['os'].get('storageAccountType')} } if os_disk_encryption_set is not None: storage_properties['osDisk']['managedDisk']['diskEncryptionSet'] = { 'id': os_disk_encryption_set } if disk_info['os'].get('diffDiskSettings'): storage_properties['osDisk']['diffDiskSettings'] = disk_info['os']['diffDiskSettings'] if os_disk_size_gb is not None: storage_properties['osDisk']['diskSizeGB'] = os_disk_size_gb if storage_profile in [StorageProfile.SAPirImage, StorageProfile.ManagedPirImage]: storage_properties['imageReference'] = { 'publisher': os_publisher, 'offer': os_offer, 'sku': os_sku, 'version': os_version } if storage_profile == StorageProfile.ManagedCustomImage: storage_properties['imageReference'] = { 'id': image } data_disks = [v for k, v in disk_info.items() if k != 'os'] if data_disk_encryption_sets: if len(data_disk_encryption_sets) != len(data_disks): raise CLIError( 'usage error: Number of --data-disk-encryption-sets mismatches with number of data disks.') for i, data_disk in enumerate(data_disks): data_disk['managedDisk']['diskEncryptionSet'] = {'id': data_disk_encryption_sets[i]} if data_disk_iops: if len(data_disk_iops) != len(data_disks): raise CLIError('usage error: Number of --data-disk-iops mismatches with number of data disks.') for i, data_disk in enumerate(data_disks): data_disk['diskIOPSReadWrite'] = data_disk_iops[i] if data_disk_mbps: if len(data_disk_mbps) != len(data_disks): raise CLIError('usage error: Number of --data-disk-mbps mismatches with number of data disks.') for i, data_disk in enumerate(data_disks): data_disk['diskMBpsReadWrite'] = data_disk_mbps[i] if data_disks: storage_properties['dataDisks'] = data_disks # Build OS Profile os_profile = { 'computerNamePrefix': naming_prefix, 'adminUsername': admin_username } if admin_password: os_profile['adminPassword'] = "******" if ssh_key_values and ssh_key_path: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': authentication_type == 'ssh', 'ssh': { 'publicKeys': [ { 'path': ssh_key_path, 'keyData': ssh_key_value } for ssh_key_value in ssh_key_values ] } } if custom_data: os_profile['customData'] = b64encode(custom_data) if secrets: os_profile['secrets'] = secrets # Build VMSS nic_config = { 'name': nic_name, 'properties': { 'primary': 'true', 'ipConfigurations': [ip_configuration] } } if cmd.supported_api_version(min_api='2017-03-30', operation_group='virtual_machine_scale_sets'): if dns_servers: nic_config['properties']['dnsSettings'] = {'dnsServers': dns_servers} if accelerated_networking: nic_config['properties']['enableAcceleratedNetworking'] = True if nsg: nic_config['properties']['networkSecurityGroup'] = {'id': nsg} vmss_properties = { 'overprovision': overprovision, 'upgradePolicy': { 'mode': upgrade_policy_mode }, 'virtualMachineProfile': { 'storageProfile': storage_properties, 'networkProfile': { 'networkInterfaceConfigurations': [nic_config] } } } if not specialized: vmss_properties['virtualMachineProfile']['osProfile'] = os_profile if license_type: vmss_properties['virtualMachineProfile']['licenseType'] = license_type if health_probe and cmd.supported_api_version(min_api='2017-03-30', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile']['networkProfile']['healthProbe'] = {'id': health_probe} if cmd.supported_api_version(min_api='2016-04-30-preview', operation_group='virtual_machine_scale_sets'): vmss_properties['singlePlacementGroup'] = single_placement_group if priority and cmd.supported_api_version(min_api='2017-12-01', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile']['priority'] = priority if eviction_policy and cmd.supported_api_version(min_api='2017-12-01', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile']['evictionPolicy'] = eviction_policy if max_price is not None and cmd.supported_api_version( min_api='2019-03-01', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile']['billingProfile'] = {'maxPrice': max_price} if platform_fault_domain_count is not None and cmd.supported_api_version( min_api='2017-12-01', operation_group='virtual_machine_scale_sets'): vmss_properties['platformFaultDomainCount'] = platform_fault_domain_count if ultra_ssd_enabled is not None: if cmd.supported_api_version(min_api='2019-03-01', operation_group='virtual_machine_scale_sets'): vmss_properties['additionalCapabilities'] = {'ultraSSDEnabled': ultra_ssd_enabled} else: vmss_properties['virtualMachineProfile']['additionalCapabilities'] = {'ultraSSDEnabled': ultra_ssd_enabled} if proximity_placement_group: vmss_properties['proximityPlacementGroup'] = {'id': proximity_placement_group} if terminate_notification_time is not None: scheduled_events_profile = { 'terminateNotificationProfile': { 'notBeforeTimeout': terminate_notification_time, 'enable': 'true' } } vmss_properties['virtualMachineProfile']['scheduledEventsProfile'] = scheduled_events_profile if automatic_repairs_grace_period is not None: automatic_repairs_policy = { 'enabled': 'true', 'gracePeriod': automatic_repairs_grace_period } vmss_properties['automaticRepairsPolicy'] = automatic_repairs_policy if scale_in_policy: vmss_properties['scaleInPolicy'] = {'rules': scale_in_policy} if encryption_at_host: vmss_properties['virtualMachineProfile']['securityProfile'] = {'encryptionAtHost': encryption_at_host} if host_group: vmss_properties['hostGroup'] = {'id': host_group} vmss = { 'type': 'Microsoft.Compute/virtualMachineScaleSets', 'name': name, 'location': location, 'tags': tags, 'apiVersion': cmd.get_api_version(ResourceType.MGMT_COMPUTE, operation_group='virtual_machine_scale_sets'), 'dependsOn': [], 'sku': { 'name': vm_sku, 'capacity': instance_count }, 'properties': vmss_properties } if zones: vmss['zones'] = zones return vmss
def build_vmss_resource(name, naming_prefix, location, tags, overprovision, upgrade_policy_mode, vm_sku, instance_count, ip_config_name, nic_name, subnet_id, admin_username, authentication_type, storage_profile, os_disk_name, os_caching, data_caching, storage_sku, data_disk_sizes_gb, image_data_disks, os_type, image=None, admin_password=None, ssh_key_value=None, ssh_key_path=None, os_publisher=None, os_offer=None, os_sku=None, os_version=None, backend_address_pool_id=None, inbound_nat_pool_id=None, single_placement_group=None, custom_data=None, secrets=None): # Build IP configuration ip_configuration = { 'name': ip_config_name, 'properties': { 'subnet': {'id': subnet_id} } } if backend_address_pool_id: key = 'loadBalancerBackendAddressPools' if 'loadBalancers' in backend_address_pool_id \ else 'ApplicationGatewayBackendAddressPools' ip_configuration['properties'][key] = [ {'id': backend_address_pool_id} ] if inbound_nat_pool_id: ip_configuration['properties']['loadBalancerInboundNatPools'] = [ {'id': inbound_nat_pool_id} ] # Build storage profile storage_properties = {} if storage_profile in [StorageProfile.SACustomImage, StorageProfile.SAPirImage]: storage_properties['osDisk'] = { 'name': os_disk_name, 'caching': os_caching, 'createOption': 'FromImage', } if storage_profile == StorageProfile.SACustomImage: storage_properties['osDisk'].update({ 'osType': os_type, 'image': { 'uri': image } }) else: storage_properties['osDisk']['vhdContainers'] = "[variables('vhdContainers')]" elif storage_profile in [StorageProfile.ManagedPirImage, StorageProfile.ManagedCustomImage]: storage_properties['osDisk'] = { 'createOption': 'FromImage', 'caching': os_caching, 'managedDisk': {'storageAccountType': storage_sku} } if storage_profile in [StorageProfile.SAPirImage, StorageProfile.ManagedPirImage]: storage_properties['imageReference'] = { 'publisher': os_publisher, 'offer': os_offer, 'sku': os_sku, 'version': os_version } if storage_profile == StorageProfile.ManagedCustomImage: storage_properties['imageReference'] = { 'id': image } storage_profile = _build_data_disks(storage_properties, data_disk_sizes_gb, image_data_disks, data_caching, storage_sku) # Build OS Profile os_profile = { 'computerNamePrefix': naming_prefix, 'adminUsername': admin_username } if authentication_type == 'password': os_profile['adminPassword'] = admin_password else: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': True, 'ssh': { 'publicKeys': [ { 'path': ssh_key_path, 'keyData': ssh_key_value } ] } } if custom_data: os_profile['customData'] = b64encode(custom_data) if secrets: os_profile['secrets'] = secrets if single_placement_group is None: # this should never happen, but just in case raise ValueError('single_placement_group was not set by validators') # Build VMSS vmss_properties = { 'overprovision': overprovision, 'upgradePolicy': { 'mode': upgrade_policy_mode }, 'virtualMachineProfile': { 'storageProfile': storage_properties, 'osProfile': os_profile, 'networkProfile': { 'networkInterfaceConfigurations': [{ 'name': nic_name, 'properties': { 'primary': 'true', 'ipConfigurations': [ip_configuration] } }] } } } if supported_api_version(ResourceType.MGMT_COMPUTE, min_api='2016-04-30-preview'): vmss_properties['singlePlacementGroup'] = single_placement_group vmss_api_version = get_api_version(ResourceType.MGMT_COMPUTE) vmss = { 'type': 'Microsoft.Compute/virtualMachineScaleSets', 'name': name, 'location': location, 'tags': tags, 'apiVersion': vmss_api_version, 'dependsOn': [], 'sku': { 'name': vm_sku, 'tier': 'Standard', 'capacity': instance_count }, 'properties': vmss_properties } return vmss
def build_vmss_resource(cmd, name, naming_prefix, location, tags, overprovision, upgrade_policy_mode, vm_sku, instance_count, ip_config_name, nic_name, subnet_id, public_ip_per_vm, vm_domain_name, dns_servers, nsg, accelerated_networking, admin_username, authentication_type, storage_profile, os_disk_name, disk_info, os_type, image=None, admin_password=None, ssh_key_value=None, ssh_key_path=None, os_publisher=None, os_offer=None, os_sku=None, os_version=None, backend_address_pool_id=None, inbound_nat_pool_id=None, health_probe=None, single_placement_group=None, platform_fault_domain_count=None, custom_data=None, secrets=None, license_type=None, zones=None, priority=None, eviction_policy=None, application_security_groups=None, ultra_ssd_enabled=None): # Build IP configuration ip_configuration = { 'name': ip_config_name, 'properties': { 'subnet': {'id': subnet_id} } } if public_ip_per_vm: ip_configuration['properties']['publicipaddressconfiguration'] = { 'name': 'instancepublicip', 'properties': { 'idleTimeoutInMinutes': 10, } } if vm_domain_name: ip_configuration['properties']['publicipaddressconfiguration']['properties']['dnsSettings'] = { 'domainNameLabel': vm_domain_name } if backend_address_pool_id: key = 'loadBalancerBackendAddressPools' if 'loadBalancers' in backend_address_pool_id \ else 'ApplicationGatewayBackendAddressPools' ip_configuration['properties'][key] = [ {'id': backend_address_pool_id} ] if inbound_nat_pool_id: ip_configuration['properties']['loadBalancerInboundNatPools'] = [ {'id': inbound_nat_pool_id} ] if application_security_groups and cmd.supported_api_version(min_api='2018-06-01', operation_group='virtual_machine_scale_sets'): ip_configuration['properties']['applicationSecurityGroups'] = [{'id': x.id} for x in application_security_groups] # Build storage profile storage_properties = {} os_caching = disk_info['os'].get('caching') if storage_profile in [StorageProfile.SACustomImage, StorageProfile.SAPirImage]: storage_properties['osDisk'] = { 'name': os_disk_name, 'caching': os_caching, 'createOption': 'FromImage', } if storage_profile == StorageProfile.SACustomImage: storage_properties['osDisk'].update({ 'osType': os_type, 'image': { 'uri': image } }) else: storage_properties['osDisk']['vhdContainers'] = "[variables('vhdContainers')]" elif storage_profile in [StorageProfile.ManagedPirImage, StorageProfile.ManagedCustomImage]: storage_properties['osDisk'] = { 'createOption': 'FromImage', 'caching': os_caching, 'managedDisk': {'storageAccountType': disk_info['os'].get('storageAccountType')} } if disk_info['os'].get('diffDiskSettings'): storage_properties['osDisk']['diffDiskSettings'] = disk_info['os']['diffDiskSettings'] if storage_profile in [StorageProfile.SAPirImage, StorageProfile.ManagedPirImage]: storage_properties['imageReference'] = { 'publisher': os_publisher, 'offer': os_offer, 'sku': os_sku, 'version': os_version } if storage_profile == StorageProfile.ManagedCustomImage: storage_properties['imageReference'] = { 'id': image } data_disks = [v for k, v in disk_info.items() if k != 'os'] if data_disks: storage_properties['dataDisks'] = data_disks # Build OS Profile os_profile = { 'computerNamePrefix': naming_prefix, 'adminUsername': admin_username } if admin_password: os_profile['adminPassword'] = "******" if ssh_key_value and ssh_key_path: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': authentication_type == 'ssh', 'ssh': { 'publicKeys': [ { 'path': ssh_key_path, 'keyData': ssh_key_value } ] } } if custom_data: os_profile['customData'] = b64encode(custom_data) if secrets: os_profile['secrets'] = secrets # Build VMSS nic_config = { 'name': nic_name, 'properties': { 'primary': 'true', 'ipConfigurations': [ip_configuration] } } if cmd.supported_api_version(min_api='2017-03-30', operation_group='virtual_machine_scale_sets'): if dns_servers: nic_config['properties']['dnsSettings'] = {'dnsServers': dns_servers} if accelerated_networking: nic_config['properties']['enableAcceleratedNetworking'] = True if nsg: nic_config['properties']['networkSecurityGroup'] = {'id': nsg} vmss_properties = { 'overprovision': overprovision, 'upgradePolicy': { 'mode': upgrade_policy_mode }, 'virtualMachineProfile': { 'storageProfile': storage_properties, 'osProfile': os_profile, 'networkProfile': { 'networkInterfaceConfigurations': [nic_config] } } } if license_type: vmss_properties['virtualMachineProfile']['licenseType'] = license_type if health_probe and cmd.supported_api_version(min_api='2017-03-30', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile']['networkProfile']['healthProbe'] = {'id': health_probe} if cmd.supported_api_version(min_api='2016-04-30-preview', operation_group='virtual_machine_scale_sets'): vmss_properties['singlePlacementGroup'] = single_placement_group if priority and cmd.supported_api_version(min_api='2017-12-01', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile']['priority'] = priority if eviction_policy and cmd.supported_api_version(min_api='2017-12-01', operation_group='virtual_machine_scale_sets'): vmss_properties['virtualMachineProfile']['evictionPolicy'] = eviction_policy if platform_fault_domain_count is not None and cmd.supported_api_version( min_api='2017-12-01', operation_group='virtual_machine_scale_sets'): vmss_properties['platformFaultDomainCount'] = platform_fault_domain_count if ultra_ssd_enabled is not None: vmss_properties['virtualMachineProfile']['additionalCapabilities'] = {'ultraSSDEnabled': ultra_ssd_enabled} vmss = { 'type': 'Microsoft.Compute/virtualMachineScaleSets', 'name': name, 'location': location, 'tags': tags, 'apiVersion': cmd.get_api_version(ResourceType.MGMT_COMPUTE, operation_group='virtual_machine_scale_sets'), 'dependsOn': [], 'sku': { 'name': vm_sku, 'capacity': instance_count }, 'properties': vmss_properties } if zones: vmss['zones'] = zones return vmss
def _build_os_profile(): special_chars = '`~!@#$%^&*()=+_[]{}\\|;:\'\",<>/?' # _computer_name is used to avoid shadow names _computer_name = computer_name or ''.join( filter(lambda x: x not in special_chars, name)) os_profile = { # Use name as computer_name if it's not provided. Remove special characters from name. 'computerName': _computer_name, 'adminUsername': admin_username } if count: os_profile['computerName'] = "[concat('{}', copyIndex())]".format( _computer_name) if admin_password: os_profile['adminPassword'] = "******" if custom_data: os_profile['customData'] = b64encode(custom_data) if ssh_key_values and ssh_key_path: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': authentication_type == 'ssh', 'ssh': { 'publicKeys': [{ 'keyData': ssh_key_value, 'path': ssh_key_path } for ssh_key_value in ssh_key_values] } } if enable_agent is not None: if custom_image_os_type.lower() == 'linux': if 'linuxConfiguration' not in os_profile: os_profile['linuxConfiguration'] = {} os_profile['linuxConfiguration'][ 'provisionVMAgent'] = enable_agent elif custom_image_os_type.lower() == 'windows': if 'windowsConfiguration' not in os_profile: os_profile['windowsConfiguration'] = {} os_profile['windowsConfiguration'][ 'provisionVMAgent'] = enable_agent if secrets: os_profile['secrets'] = secrets if enable_auto_update is not None and custom_image_os_type.lower( ) == 'windows': os_profile['windowsConfiguration'][ 'enableAutomaticUpdates'] = enable_auto_update # Windows patch settings if patch_mode is not None and custom_image_os_type.lower( ) == 'windows': if patch_mode.lower() not in [ 'automaticbyos', 'automaticbyplatform', 'manual' ]: raise ValidationError( 'Invalid value of --patch-mode for Windows VM. Valid values are AutomaticByOS, ' 'AutomaticByPlatform, Manual.') os_profile['windowsConfiguration']['patchSettings'] = { 'patchMode': patch_mode, 'enableHotpatching': enable_hotpatching } # Linux patch settings if patch_mode is not None and custom_image_os_type.lower() == 'linux': if patch_mode.lower() not in [ 'automaticbyplatform', 'imagedefault' ]: raise ValidationError( 'Invalid value of --patch-mode for Linux VM. Valid values are AutomaticByPlatform, ImageDefault.' ) os_profile['linuxConfiguration']['patchSettings'] = { 'patchMode': patch_mode } return os_profile
def build_vmss_resource(name, naming_prefix, location, tags, overprovision, upgrade_policy_mode, vm_sku, instance_count, ip_config_name, nic_name, subnet_id, admin_username, authentication_type, storage_profile, os_disk_name, os_caching, data_caching, storage_sku, data_disk_sizes_gb, image_data_disks, os_type, image=None, admin_password=None, ssh_key_value=None, ssh_key_path=None, os_publisher=None, os_offer=None, os_sku=None, os_version=None, backend_address_pool_id=None, inbound_nat_pool_id=None, single_placement_group=None, custom_data=None, secrets=None): # Build IP configuration ip_configuration = { 'name': ip_config_name, 'properties': { 'subnet': {'id': subnet_id} } } if backend_address_pool_id: key = 'loadBalancerBackendAddressPools' if 'loadBalancers' in backend_address_pool_id \ else 'ApplicationGatewayBackendAddressPools' ip_configuration['properties'][key] = [ {'id': backend_address_pool_id} ] if inbound_nat_pool_id: ip_configuration['properties']['loadBalancerInboundNatPools'] = [ {'id': inbound_nat_pool_id} ] # Build storage profile storage_properties = {} if storage_profile in [StorageProfile.SACustomImage, StorageProfile.SAPirImage]: storage_properties['osDisk'] = { 'name': os_disk_name, 'caching': os_caching, 'createOption': 'FromImage', } if storage_profile == StorageProfile.SACustomImage: storage_properties['osDisk'].update({ 'osType': os_type, 'image': { 'uri': image } }) else: storage_properties['osDisk']['vhdContainers'] = "[variables('vhdContainers')]" elif storage_profile in [StorageProfile.ManagedPirImage, StorageProfile.ManagedCustomImage]: storage_properties['osDisk'] = { 'createOption': 'FromImage', 'caching': os_caching, 'managedDisk': {'storageAccountType': storage_sku} } if storage_profile in [StorageProfile.SAPirImage, StorageProfile.ManagedPirImage]: storage_properties['imageReference'] = { 'publisher': os_publisher, 'offer': os_offer, 'sku': os_sku, 'version': os_version } if storage_profile == StorageProfile.ManagedCustomImage: storage_properties['imageReference'] = { 'id': image } storage_profile = _build_data_disks(storage_properties, data_disk_sizes_gb, image_data_disks, data_caching, storage_sku) # Build OS Profile os_profile = { 'computerNamePrefix': naming_prefix, 'adminUsername': admin_username } if authentication_type == 'password': os_profile['adminPassword'] = admin_password else: os_profile['linuxConfiguration'] = { 'disablePasswordAuthentication': True, 'ssh': { 'publicKeys': [ { 'path': ssh_key_path, 'keyData': ssh_key_value } ] } } if custom_data: os_profile['customData'] = b64encode(custom_data) if secrets: os_profile['secrets'] = secrets if single_placement_group is None: # this should never happen, but just in case raise ValueError('single_placement_group was not set by validators') # Build VMSS vmss_properties = { 'overprovision': overprovision, 'upgradePolicy': { 'mode': upgrade_policy_mode }, 'virtualMachineProfile': { 'storageProfile': storage_properties, 'osProfile': os_profile, 'networkProfile': { 'networkInterfaceConfigurations': [{ 'name': nic_name, 'properties': { 'primary': 'true', 'ipConfigurations': [ip_configuration] } }] } } } if supported_api_version(ResourceType.MGMT_COMPUTE, min_api='2016-04-30-preview'): vmss_properties['singlePlacementGroup'] = single_placement_group vmss_api_version = get_api_version(ResourceType.MGMT_COMPUTE) vmss = { 'type': 'Microsoft.Compute/virtualMachineScaleSets', 'name': name, 'location': location, 'tags': tags, 'apiVersion': vmss_api_version, 'dependsOn': [], 'sku': { 'name': vm_sku, 'tier': 'Standard', 'capacity': instance_count }, 'properties': vmss_properties } return vmss