def update_vmss_image(vmScaleSetName,resourceGroupName,location,image_id): image_reference = ImageReference(id=image_id) storage_profile = VirtualMachineScaleSetStorageProfile(image_reference=image_reference) virtual_machine_profile = VirtualMachineScaleSetVMProfile(storage_profile=storage_profile) parameters = VirtualMachineScaleSet(location=location,virtual_machine_profile=virtual_machine_profile) vmScaleSetData = compute_client.virtual_machine_scale_sets.create_or_update(resource_group_name=resourceGroupName, vm_scale_set_name=vmScaleSetName,parameters=parameters)
def create_vm_from_marketplace(self, compute_management_client, image_offer, image_publisher, image_sku, image_version, disk_type, vm_credentials, computer_name, group_name, nic_id, region, vm_name, tags, vm_size, purchase_plan, cancellation_context): """ :param vm_size: (str) Azure instance type :param compute_management_client: azure.mgmt.compute.ComputeManagementClient instance :param image_offer: (str) image offer :param image_publisher: (str) image publisher :param image_sku: (str) image SKU :param image_version: (str) image version :param str disk_type: Disk type (HDD/SDD) :param vm_credentials: cloudshell.cp.azure.models.vm_credentials.VMCredentials instance :param computer_name: computer name :param group_name: Azure resource group name (reservation id) :param nic_id: Azure network id :param region: Azure region :param vm_name: name for VM :param tags: Azure tags :param purchase_plan: PurchasePlan :param cancellation_context cloudshell.shell.core.driver_context.CancellationContext instance :rtype: azure.mgmt.compute.models.VirtualMachine """ os_profile = self._prepare_os_profile(vm_credentials=vm_credentials, computer_name=computer_name) hardware_profile = HardwareProfile(vm_size=vm_size) network_profile = NetworkProfile( network_interfaces=[NetworkInterfaceReference(id=nic_id)]) storage_profile = StorageProfile( os_disk=self._prepare_os_disk(disk_type), image_reference=ImageReference(publisher=image_publisher, offer=image_offer, sku=image_sku, version=image_version)) vm_plan = None if purchase_plan is not None: vm_plan = Plan(name=purchase_plan.name, publisher=purchase_plan.publisher, product=purchase_plan.product) return self._create_vm( compute_management_client=compute_management_client, region=region, group_name=group_name, vm_name=vm_name, hardware_profile=hardware_profile, network_profile=network_profile, os_profile=os_profile, storage_profile=storage_profile, cancellation_context=cancellation_context, tags=tags, vm_plan=vm_plan)
def test_encryption_distro_check(self): image = ImageReference(None, 'canonical', 'ubuntuserver', '16.04.0-LTS') result, msg = _check_encrypt_is_supported(image, 'data') self.assertTrue(result) self.assertEqual(None, msg) image = ImageReference(None, 'OpenLogic', 'CentOS', '7.2n') result, msg = _check_encrypt_is_supported(image, 'data') self.assertTrue(result) self.assertEqual(None, msg) image = ImageReference(None, 'OpenLogic', 'CentOS', '7.2') result, msg = _check_encrypt_is_supported(image, 'all') self.assertFalse(result) self.assertEqual(msg, "Encryption might fail as current VM uses a distro not in the known list, which are '['RHEL 7.2', 'RHEL 7.3', 'CentOS 7.2n', 'Ubuntu 14.04', 'Ubuntu 16.04']'") image = ImageReference(None, 'OpenLogic', 'CentOS', '7.2') result, msg = _check_encrypt_is_supported(image, 'data') self.assertTrue(result)
def create_vm_from_custom_image(self, compute_management_client, image_name, image_resource_group, disk_type, vm_credentials, computer_name, group_name, nic_id, region, vm_name, tags, vm_size, cancellation_context): """Create VM from custom image URN :param cancellation_context: :param str vm_size: Azure instance type :param azure.mgmt.compute.ComputeManagementClient compute_management_client: instance :param str image_name: Azure custom image name :param str image_resource_group: Azure resource group :param str disk_type: Disk type (HDD/SDD) :param cloudshell.cp.azure.models.vm_credentials.VMCredentials vm_credentials: :param str computer_name: computer name :param str group_name: Azure resource group name (reservation id) :param str nic_id: Azure network id :param str region: Azure region :param str vm_name: name for VM :param tags: Azure tags :return: :rtype: azure.mgmt.compute.models.VirtualMachine """ os_profile = self._prepare_os_profile(vm_credentials=vm_credentials, computer_name=computer_name) hardware_profile = HardwareProfile(vm_size=vm_size) network_profile = NetworkProfile( network_interfaces=[NetworkInterfaceReference(id=nic_id)]) image = compute_management_client.images.get( resource_group_name=image_resource_group, image_name=image_name) storage_profile = StorageProfile( os_disk=self._prepare_os_disk(disk_type), image_reference=ImageReference(id=image.id)) return self._create_vm( compute_management_client=compute_management_client, region=region, group_name=group_name, vm_name=vm_name, hardware_profile=hardware_profile, network_profile=network_profile, os_profile=os_profile, storage_profile=storage_profile, cancellation_context=cancellation_context, tags=tags)
def get_custom_image_reference(self, name, resource_group=None): try: if resource_group: vm_images = self.compute_client.images.list_by_resource_group( resource_group) else: vm_images = self.compute_client.images.list() except Exception as exc: self.fail( "Error fetching custom images from subscription - {0}".format( str(exc))) for vm_image in vm_images: if vm_image.name == name: self.log("Using custom image id {0}".format(vm_image.id)) return ImageReference(id=vm_image.id) self.fail("Error could not find image with name {0}".format(name))
def test_enable_encryption_error_cases_handling( self, mock_get_keyvault_key_url, mock_compute_client_factory): faked_keyvault = '/subscriptions/01234567-1bf0-4dda-aec3-cb9272f09590/resourceGroups/rg1/providers/Microsoft.KeyVault/vaults/v1' os_disk = OSDisk(None, OperatingSystemTypes.linux) existing_disk = DataDisk(lun=1, vhd='https://someuri', name='d1', create_option=DiskCreateOptionTypes.empty) vm = FakedVM(None, [existing_disk], os_disk=os_disk) compute_client_mock = mock.MagicMock() compute_client_mock.virtual_machines.get.return_value = vm mock_compute_client_factory.return_value = compute_client_mock mock_get_keyvault_key_url.return_value = 'https://somevaults.vault.azure.net/' # throw when VM has disks, but no --volume-type is specified with self.assertRaises(CLIError) as context: enable('rg1', 'vm1', 'client_id', faked_keyvault, 'client_secret') self.assertTrue("supply --volume-type" in str(context.exception)) # throw when no AAD client secrets with self.assertRaises(CLIError) as context: enable('rg1', 'vm1', 'client_id', faked_keyvault) self.assertTrue("--aad-client-id or --aad-client-cert-thumbprint" in str(context.exception)) # throw when the linux image does not support encryptions vm.storage_profile.image_reference = ImageReference( publisher='OpenLogic', offer='centos', sku='7.1') with self.assertRaises(CLIError) as context: enable('rg1', 'vm1', 'client_id', faked_keyvault, 'client_secret', volume_type='DATA') self.assertTrue( "Encryption is not suppored for current VM. Supported are" in str( context.exception))
def update_vmss(vmScaleSetName,resourceGroupName,location,custom_data,image_id): ######## vmss_data = compute_client.virtual_machine_scale_sets.get(resourceGroupName,vmScaleSetName) """ StorageAccount = vmss_data.virtual_machine_profile.extension_profile.extensions[0].settings['StorageAccount'] sas_token = get_sas_token(resourceGroupName,StorageAccount) protected_settings = { 'storageAccountName': StorageAccount, 'storageAccountSasToken': sas_token } print('SAS Token ====> {}'.format(sas_token)) extension_profile=VirtualMachineScaleSetExtensionProfile( extensions=[ VirtualMachineScaleSetExtension( name=vmss_data.virtual_machine_profile.extension_profile.extensions[0].name, protected_settings=protected_settings, )] ) """ ######## os_profile = VirtualMachineScaleSetOSProfile( custom_data=custom_data) image_reference = ImageReference(id=image_id) storage_profile = VirtualMachineScaleSetStorageProfile(image_reference=image_reference) virtual_machine_profile = VirtualMachineScaleSetVMProfile( os_profile=os_profile, storage_profile=storage_profile #extension_profile=extension_profile ) update_parameters = VirtualMachineScaleSet( location=location, virtual_machine_profile=virtual_machine_profile ) vmss = compute_client.virtual_machine_scale_sets.create_or_update( resource_group_name=resourceGroupName, vm_scale_set_name=vmScaleSetName, parameters=update_parameters ) vmss.wait() vmss_data = vmss.result() vmss_id = vmss_data.id return vmss_id
def request_instance(call=None, kwargs=None): # pylint: disable=unused-argument ''' Request that Azure spin up a new instance ''' global compconn # pylint: disable=global-statement,invalid-name if not compconn: compconn = get_conn() vm_ = kwargs if vm_.get('driver') is None: vm_['driver'] = 'azurearm' if vm_.get('location') is None: vm_['location'] = get_location() if vm_.get('resource_group') is None: vm_['resource_group'] = config.get_cloud_config_value( 'resource_group', vm_, __opts__, search_global=True) if vm_.get('name') is None: vm_['name'] = config.get_cloud_config_value('name', vm_, __opts__, search_global=True) os_kwargs = {} userdata = None userdata_file = config.get_cloud_config_value('userdata_file', vm_, __opts__, search_global=False, default=None) if userdata_file is None: userdata = config.get_cloud_config_value('userdata', vm_, __opts__, search_global=False, default=None) else: if os.path.exists(userdata_file): with salt.utils.fopen(userdata_file, 'r') as fh_: userdata = fh_.read() if userdata is not None: os_kwargs['custom_data'] = base64.b64encode(userdata) iface_data = create_interface(kwargs=vm_) vm_['iface_id'] = iface_data['id'] disk_name = '{0}-vol0'.format(vm_['name']) vm_username = config.get_cloud_config_value( 'ssh_username', vm_, __opts__, search_global=True, default=config.get_cloud_config_value('win_username', vm_, __opts__, search_global=True)) vm_password = config.get_cloud_config_value( 'ssh_password', vm_, __opts__, search_global=True, default=config.get_cloud_config_value('win_password', vm_, __opts__, search_global=True)) win_installer = config.get_cloud_config_value('win_installer', vm_, __opts__, search_global=True) if vm_['image'].startswith('http'): # https://{storage_account}.blob.core.windows.net/{path}/{vhd} source_image = VirtualHardDisk(uri=vm_['image']) img_ref = None if win_installer: os_type = 'Windows' else: os_type = 'Linux' else: img_pub, img_off, img_sku, img_ver = vm_['image'].split('|') source_image = None os_type = None img_ref = ImageReference( publisher=img_pub, offer=img_off, sku=img_sku, version=img_ver, ) params = VirtualMachine( name=vm_['name'], location=vm_['location'], plan=None, hardware_profile=HardwareProfile(vm_size=getattr( VirtualMachineSizeTypes, vm_['size'].lower()), ), storage_profile=StorageProfile( os_disk=OSDisk( caching=CachingTypes.none, create_option=DiskCreateOptionTypes.from_image, name=disk_name, vhd=VirtualHardDisk( uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'. format( vm_['storage_account'], disk_name, ), ), os_type=os_type, image=source_image, ), image_reference=img_ref, ), os_profile=OSProfile(admin_username=vm_username, admin_password=vm_password, computer_name=vm_['name'], **os_kwargs), network_profile=NetworkProfile(network_interfaces=[ NetworkInterfaceReference(vm_['iface_id']), ], ), ) poller = compconn.virtual_machines.create_or_update( vm_['resource_group'], vm_['name'], params) poller.wait() try: return show_instance(vm_['name'], call='action') except CloudError: return {}
def temp_vm(): # Generate random value to avoid duplicate resource group hash = random.getrandbits(16) # Defining vars base_name = 'rescue' + str(hash) storage_name = base_name group_name = base_name vm_name = base_name vnet_name = base_name subnet_name = base_name nic_name = base_name os_disk_name = base_name pub_ip_name = base_name computer_name = base_name admin_username='******' admin_password='******' region = orig_vm_location image_publisher = 'Canonical' image_offer = 'UbuntuServer' image_sku = '16.04.0-LTS' image_version = 'latest' # Helper function to create a network interface and vnet def create_network_interface(network_client, region, group_name, interface_name, network_name, subnet_name, ip_name): result = network_client.virtual_networks.create_or_update( group_name, network_name, VirtualNetwork( location=region, address_space=AddressSpace( address_prefixes=[ '10.1.0.0/16', ], ), subnets=[ Subnet( name=subnet_name, address_prefix='10.1.0.0/24', ), ], ), ) print('Creating Virtual Network...') result.wait() # async operation subnet = network_client.subnets.get(group_name, network_name, subnet_name) result = network_client.public_ip_addresses.create_or_update( group_name, ip_name, PublicIPAddress( location=region, public_ip_allocation_method=IPAllocationMethod.dynamic, idle_timeout_in_minutes=4, ), ) print('Creating Subnet...') result.wait() # async operation # Creating Public IP public_ip_address = network_client.public_ip_addresses.get(group_name, ip_name) public_ip_id = public_ip_address.id print('Creating Public IP...') result.wait() # async operation result = network_client.network_interfaces.create_or_update( group_name, interface_name, NetworkInterface( location=region, ip_configurations=[ NetworkInterfaceIPConfiguration( name='default', private_ip_allocation_method=IPAllocationMethod.dynamic, subnet=subnet, public_ip_address=PublicIPAddress( id=public_ip_id, ), ), ], ), ) print('Creating Network Interface...') result.wait() # async operation network_interface = network_client.network_interfaces.get( group_name, interface_name, ) return network_interface.id # 1. Create a resource group print('Creating resource group ' + base_name + '...') result = res_client.resource_groups.create_or_update( group_name, ResourceGroup( location=region, ), ) # 2. Create a storage account print('Creating storage group ' + base_name + '...') result = storage_client.storage_accounts.create( group_name, storage_name, StorageAccountCreateParameters( location=region, sku=Sku(SkuName.premium_lrs), kind=Kind.storage, ), ) result.result() # 3. Create the network interface using a helper function (defined below) nic_id = create_network_interface( network_client, region, group_name, nic_name, vnet_name, subnet_name, pub_ip_name, ) # 4. Create the virtual machine print('Creating temporary VM ' + vm_name + '...') result = compute_client.virtual_machines.create_or_update( group_name, vm_name, VirtualMachine( location=region, os_profile=OSProfile( admin_username=admin_username, admin_password=admin_password, computer_name=computer_name, ), hardware_profile=HardwareProfile( vm_size='Standard_DS1_v2' ), network_profile=NetworkProfile( network_interfaces=[ NetworkInterfaceReference( id=nic_id, ), ], ), storage_profile=StorageProfile( os_disk=OSDisk( caching=CachingTypes.none, create_option=DiskCreateOptionTypes.from_image, name=os_disk_name, vhd=VirtualHardDisk( uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.format( storage_name, os_disk_name, ), ), ), image_reference = ImageReference( publisher=image_publisher, offer=image_offer, sku=image_sku, version=image_version, ), ), ), ) result.wait() # async operation # Display the public ip address # You can now connect to the machine using SSH public_ip_address = network_client.public_ip_addresses.get(group_name, pub_ip_name) print('\n' + vm_name + ' has started.') print('VM\'s public IP is {}'.format(public_ip_address.ip_address)) print('SSH Username: '******'SSH Password ' + admin_password) print('ssh ' + admin_username + '@' + public_ip_address.ip_address) # The process of shutting down the VM, deleting it, then removing/attaching OS disk to the temp def disk_attach(): # Delete VM print('Deleting VM and freeing OS disk from ' + orig_vm_name) print('OS Disk Location ' + orig_vm_os_disk) result = compute_client.virtual_machines.delete(sys.argv[2], orig_vm_name) result.wait() # Ensures no lingering lease issues time.sleep(5) # Attach OS disk to temporary VM print('Attaching original OS disk to {0}'.format(vm_name)) result = compute_client.virtual_machines.create_or_update( group_name, vm_name, VirtualMachine( location=orig_vm_location, storage_profile=StorageProfile( data_disks=[DataDisk( lun=0, caching=CachingTypes.none, create_option=DiskCreateOptionTypes.attach, name=orig_vm_name, vhd=VirtualHardDisk( uri=orig_vm_os_disk ) )] ) ) ) result.wait() disk_attach()
def exec_module(self, **kwargs): for key in list(self.module_arg_spec.keys()) + ['tags']: setattr(self, key, kwargs[key]) # make sure options are lower case self.remove_on_absent = set( [resource.lower() for resource in self.remove_on_absent]) changed = False results = dict() vmss = None disable_ssh_password = None vmss_dict = None virtual_network = None subnet = None resource_group = self.get_resource_group(self.resource_group) if not self.location: # Set default location self.location = resource_group.location if self.state == 'present': # Verify parameters and resolve any defaults if self.vm_size and not self.vm_size_is_valid(): self.fail( "Parameter error: vm_size {0} is not valid for your subscription and location." .format(self.vm_size)) # if self.virtual_network_name: # virtual_network = self.get_virtual_network(self.virtual_network_name) if self.ssh_public_keys: msg = "Parameter error: expecting ssh_public_keys to be a list of type dict where " \ "each dict contains keys: path, key_data." for key in self.ssh_public_keys: if not isinstance(key, dict): self.fail(msg) if not key.get('path') or not key.get('key_data'): self.fail(msg) if self.image: if not self.image.get('publisher') or not self.image.get('offer') or not self.image.get('sku') \ or not self.image.get('version'): self.error( "parameter error: expecting image to contain publisher, offer, sku and version keys." ) image_version = self.get_image_version() if self.image['version'] == 'latest': self.image['version'] = image_version.name self.log("Using image version {0}".format( self.image['version'])) disable_ssh_password = not self.ssh_password_enabled try: self.log("Fetching virtual machine scale set {0}".format( self.name)) vmss = self.compute_client.virtual_machine_scale_sets.get( self.resource_group, self.name) self.check_provisioning_state(vmss, self.state) vmss_dict = self.serialize_vmss(vmss) if self.state == 'present': differences = [] results = vmss_dict if self.os_disk_caching and \ self.os_disk_caching != vmss_dict['properties']['virtualMachineProfile']['storageProfile']['osDisk']['caching']: self.log( 'CHANGED: virtual machine scale set {0} - OS disk caching' .format(self.name)) differences.append('OS Disk caching') changed = True vmss_dict['properties']['virtualMachineProfile'][ 'storageProfile']['osDisk'][ 'caching'] = self.os_disk_caching if self.capacity and \ self.capacity != vmss_dict['sku']['capacity']: self.log( 'CHANGED: virtual machine scale set {0} - Capacity'. format(self.name)) differences.append('Capacity') changed = True vmss_dict['sku']['capacity'] = self.capacity if self.data_disks and \ len(self.data_disks) != len(vmss_dict['properties']['virtualMachineProfile']['storageProfile']['dataDisks']): self.log( 'CHANGED: virtual machine scale set {0} - Data Disks'. format(self.name)) differences.append('Data Disks') changed = True update_tags, vmss_dict['tags'] = self.update_tags( vmss_dict.get('tags', dict())) if update_tags: differences.append('Tags') changed = True self.differences = differences elif self.state == 'absent': self.log( "CHANGED: virtual machine scale set {0} exists and requested state is 'absent'" .format(self.name)) results = dict() changed = True except CloudError: self.log('Virtual machine scale set {0} does not exist'.format( self.name)) if self.state == 'present': self.log( "CHANGED: virtual machine scale set {0} does not exist but state is 'present'." .format(self.name)) changed = True self.results['changed'] = changed self.results['ansible_facts']['azure_vmss'] = results if self.check_mode: return self.results if changed: if self.state == 'present': if not vmss: # Create the VMSS self.log("Create virtual machine scale set {0}".format( self.name)) self.results['actions'].append('Created VMSS {0}'.format( self.name)) # Validate parameters if not self.admin_username: self.fail( "Parameter error: admin_username required when creating a virtual machine scale set." ) if self.os_type == 'Linux': if disable_ssh_password and not self.ssh_public_keys: self.fail( "Parameter error: ssh_public_keys required when disabling SSH password." ) if self.subnet_name: subnet = self.get_subnet(self.virtual_network_name, self.subnet_name) if not self.virtual_network_name: default_vnet = self.create_default_vnet() virtual_network = default_vnet.id if not self.short_hostname: self.short_hostname = self.name managed_disk = VirtualMachineScaleSetManagedDiskParameters( storage_account_type=self.managed_disk_type) vmss_resource = VirtualMachineScaleSet( self.location, tags=self.tags, upgrade_policy=UpgradePolicy(mode=self.upgrade_policy), sku=Sku( name=self.vm_size, capacity=self.capacity, tier=self.tier, ), virtual_machine_profile=VirtualMachineScaleSetVMProfile( os_profile=VirtualMachineScaleSetOSProfile( admin_username=self.admin_username, computer_name_prefix=self.short_hostname, ), storage_profile= VirtualMachineScaleSetStorageProfile( os_disk=VirtualMachineScaleSetOSDisk( managed_disk=managed_disk, create_option=DiskCreateOptionTypes. from_image, caching=self.os_disk_caching, ), image_reference=ImageReference( publisher=self.image['publisher'], offer=self.image['offer'], sku=self.image['sku'], version=self.image['version'], ), ), network_profile= VirtualMachineScaleSetNetworkProfile( network_interface_configurations=[ VirtualMachineScaleSetNetworkConfiguration( name=self.name, primary=True, ip_configurations=[ VirtualMachineScaleSetIPConfiguration( name='default', subnet=ApiEntityReference( id=subnet.id)) ]) ]))) if self.admin_password: vmss_resource.virtual_machine_profile.os_profile.admin_password = self.admin_password if self.os_type == 'Linux': vmss_resource.virtual_machine_profile.os_profile.linux_configuration = LinuxConfiguration( disable_password_authentication=disable_ssh_password ) if self.ssh_public_keys: ssh_config = SshConfiguration() ssh_config.public_keys = \ [SshPublicKey(path=key['path'], key_data=key['key_data']) for key in self.ssh_public_keys] vmss_resource.virtual_machine_profile.os_profile.linux_configuration.ssh = ssh_config if self.data_disks: data_disks = [] for data_disk in self.data_disks: data_disk_managed_disk = VirtualMachineScaleSetManagedDiskParameters( storage_account_type=data_disk[ 'managed_disk_type']) data_disk['caching'] = data_disk.get( 'caching', CachingTypes.read_only) data_disks.append( VirtualMachineScaleSetDataDisk( lun=data_disk['lun'], caching=data_disk['caching'], create_option=DiskCreateOptionTypes.empty, disk_size_gb=data_disk['disk_size_gb'], managed_disk=data_disk_managed_disk, )) vmss_resource.virtual_machine_profile.storage_profile.data_disks = data_disks self.log("Create virtual machine with parameters:") self.create_or_update_vmss(vmss_resource) elif self.differences and len(self.differences) > 0: self.log("Update virtual machine scale set {0}".format( self.name)) self.results['actions'].append('Updated VMSS {0}'.format( self.name)) vmss_resource = self.get_vmss() vmss_resource.virtual_machine_profile.storage_profile.os_disk.caching = self.os_disk_caching vmss_resource.sku.capacity = self.capacity data_disks = [] for data_disk in self.data_disks: data_disks.append( VirtualMachineScaleSetDataDisk( lun=data_disk['lun'], caching=data_disk['caching'], create_option=DiskCreateOptionTypes.empty, disk_size_gb=data_disk['disk_size_gb'], managed_disk= VirtualMachineScaleSetManagedDiskParameters( storage_account_type=data_disk[ 'managed_disk_type']), )) vmss_resource.virtual_machine_profile.storage_profile.data_disks = data_disks self.log("Update virtual machine with parameters:") self.create_or_update_vmss(vmss_resource) self.results['ansible_facts'][ 'azure_vmss'] = self.serialize_vmss(self.get_vmss()) elif self.state == 'absent': # delete the VM self.log("Delete virtual machine scale set {0}".format( self.name)) self.results['ansible_facts']['azure_vmss'] = None self.delete_vmss(vmss) # until we sort out how we want to do this globally del self.results['actions'] return self.results