def begin_update(self, resource_group_name: str, vm_name: str, parameters: Dict[str, Any]) -> WaitableObject: try: with open(self.path / resource_group_name / f"vm-{vm_name}.json", "r", encoding="utf-8") as file: v_m = json.loads(file.read()) except FileNotFoundError: raise ResourceNotFoundError("Virtual Machine not found") from None tags = parameters.pop("tags") if "tags" in parameters else {} v_m["properties"].update(**dict_keys_to_camel_case(parameters)) v_m["tags"].update(**tags) VirtualMachine.deserialize(v_m) with open(self.path / resource_group_name / f"vm-{vm_name}.json", "w", encoding="utf-8") as file: json.dump(v_m, fp=file, indent=2) return WaitableObject()
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()
def list(self, resource_group_name: str) -> List[VirtualMachine]: try: files = [file for file in os.listdir(self.path / resource_group_name) if file.startswith("vm-")] except FileNotFoundError: raise ResourceNotFoundError("No resource group") from None elements = [] for fle in files: with open(self.path / resource_group_name / fle, "r", encoding="utf-8") as file: elements.append(VirtualMachine.deserialize(json.load(file))) return elements
def _create_vm(self, compute_management_client, region, group_name, vm_name, hardware_profile, network_profile, os_profile, storage_profile, cancellation_context, tags, vm_plan=None): """Create and deploy Azure VM from the given parameters :param compute_management_client: azure.mgmt.compute.compute_management_client.ComputeManagementClient :param region: (str) Azure region :param group_name: (str) resource group name (reservation id) :param vm_name: (str) Azure VM resource name :param hardware_profile: azure.mgmt.compute.models.HardwareProfile instance :param network_profile: azure.mgmt.compute.models.NetworkProfile instance :param os_profile: azure.mgmt.compute.models.OSProfile instance :param storage_profile: azure.mgmt.compute.models.StorageProfile instance :param cancellation_context cloudshell.shell.core.driver_context.CancellationContext instance :param tags: azure tags :rtype: azure.mgmt.compute.models.VirtualMachine """ virtual_machine = VirtualMachine( location=region, tags=tags, os_profile=os_profile, hardware_profile=hardware_profile, network_profile=network_profile, storage_profile=storage_profile, diagnostics_profile=DiagnosticsProfile( boot_diagnostics=BootDiagnostics(enabled=False)), plan=vm_plan) operation_poller = compute_management_client.virtual_machines.create_or_update( group_name, vm_name, virtual_machine) return self.task_waiter_service.wait_for_task( operation_poller=operation_poller, cancellation_context=cancellation_context)
def vm_parameters(self, vhd_url, nic_id, vhd_name): ''' :param1 vhd_url: controller vhd url :param2 args: command line arguments :param3 pb: testbed file details :param4 roles: roles from testbed file - controller, se, client and server :param5 nic_id: Azure controller NIC resource id :param6 vhd_name: desired vhd name ''' vm_size = self.vm_json.get('vm_size', 'Standard_F4s') # jay-smoke-ctrl-1-osDisk-controller-18.1.1-5220.vhd vm_name = self.vm_json.get('name') vhd_name = vm_name + '-osDisk-' + vhd_name self.delete_vm_vhd(vm_name=vm_name, vhd_name=vhd_name) return VirtualMachine( location=self.configuration.get('location'), hardware_profile=HardwareProfile(vm_size=vm_size), storage_profile=self.get_storage(vhd_url, vhd_name), os_profile=self.get_os(), network_profile=self.get_network(nic_id))
def json_parse(self, compute_client): """Parses the local .json file for previously attached disks""" with open(self.json_path) as fp: ingest = json.load(fp) dd = [] for disk in ingest['storageProfile']['dataDisks']: a_disk = DataDisk(lun=disk['lun'], caching=disk['caching'].lower(), create_option=DiskCreateOptionTypes.attach, name=disk['name'], vhd=VirtualHardDisk(uri=disk['vhd']['uri'])) dd.append(a_disk) print( 'Attaching data disk {0} with name {1}, waiting until complete...' .format(a_disk.lun, a_disk.name)) result = compute_client.virtual_machines.create_or_update( self.rg_name, self.vm_name, VirtualMachine(location=ingest['location'], storage_profile=StorageProfile(data_disks=dd))) result.wait() print('All disks should be attached now.')
client_secret="F16AKdM4z5r", # os.environ["AZURE_CLIENT_SECRET"], tenant_id="0ae705af731", # os.environ["AZURE_TENANT_ID"], ) compute_client: ComputeManagementClient = ComputeManagementClient( credentials, AZURE_SUBSCRIPTION_ID) print("VM List") for vm in compute_client.virtual_machines.list_all(): print(vm.name, vm.id.split("/")[4]) compute_client.virtual_machines.\ begin_create_or_update("test", "linux-marian", VirtualMachine(location="westeurope", tags={ "owner" : "MarianWitkowski" }) ).wait() print("Get info about VM") details = compute_client.virtual_machines.get("TEST", "linux-marian") print(details) operation = compute_client.virtual_machines.begin_power_off( "TEST", "linux-marian") operation.wait() print("Po power-off") operation = compute_client.virtual_machines.begin_start("TEST", "linux-marian") operation.wait() print("Po start")
def test_remediate_success(self): compute_client = Mock() nw_profile = NetworkProfile(network_interfaces=[ NetworkInterfaceReference( id= "/subscriptions/subscription_id/resourceGroups/resource_group/providers/Microsoft.Network" "/networkInterfaces/vm_nameVMNic ") ]) compute_client.virtual_machines.get.return_value = VirtualMachine( id= "/subscriptions/subscription_id/resourceGroups/resource_group/providers/Microsoft.Compute" "/virtualMachines/vm_name", location="eastus", network_profile=nw_profile, ) nw_client = Mock() nw_client.network_interfaces.get.return_value = NetworkInterface( id= "/subscriptions/subscription_id/resourceGroups/resource_group/providers/Microsoft.Network" "/networkInterfaces/vm_nameVMNic", network_security_group=NetworkSecurityGroup( id= "/subscriptions/subscription_id/resourceGroups/resource_name/providers/Microsoft.Network" "/networkSecurityGroups/vm_nameNSG "), ) nw_client.network_security_groups.get.return_value = NetworkSecurityGroup( id= "/subscriptions/subscription_id/resourceGroups/resource_name/providers/Microsoft.Network" "/networkSecurityGroups/vm_nameNSG", security_rules=[ SecurityRule( protocol="All", access="Allow", direction="Inbound", source_address_prefix="*", destination_port_ranges=["22", "3389"], ), SecurityRule( protocol="All", access="Allow", direction="Inbound", source_address_prefix="*", destination_port_ranges=["20-30", "3389"], ), SecurityRule( protocol="All", access="Allow", direction="Inbound", source_address_prefix="*", destination_port_range="22", ), ], ) action = VMSecurityGroupClosePort22() assert (action.remediate(compute_client, nw_client, "resource_name", "vm_name") == 0) assert nw_client.network_security_groups.create_or_update.call_count == 1 call_args = nw_client.network_security_groups.create_or_update.call_args updated_sg = call_args.args[2] security_rules = updated_sg.security_rules assert len(security_rules) == 2 assert security_rules[0].destination_port_ranges == ["3389"] assert security_rules[1].destination_port_ranges == [ "20-21", "23-30", "3389" ]
def create_virtual_machine(self, credentials, network_client, network_id, parameters, vm_network_name): """ Creates an Azure virtual machine using the network interface created. Args: credentials: A ServicePrincipalCredentials instance, that can be used to access or create any resources. network_client: A NetworkManagementClient instance. network_id: The network id of the network interface created. parameters: A dict, containing all the parameters necessary to authenticate this user with Azure. vm_network_name: The name of the virtual machine to use. """ resource_group = parameters[self.PARAM_RESOURCE_GROUP] storage_account = parameters[self.PARAM_STORAGE_ACCOUNT] zone = parameters[self.PARAM_ZONE] utils.log("Creating a Virtual Machine '{}'".format(vm_network_name)) subscription_id = str(parameters[self.PARAM_SUBSCRIBER_ID]) azure_instance_type = parameters[self.PARAM_INSTANCE_TYPE] compute_client = ComputeManagementClient(credentials, subscription_id) auth_keys_path = self.AUTHORIZED_KEYS_FILE.format(self.ADMIN_USERNAME) with open(auth_keys_path, 'r') as pub_ssh_key_fd: pub_ssh_key = pub_ssh_key_fd.read() public_keys = [SshPublicKey(path=auth_keys_path, key_data=pub_ssh_key)] ssh_config = SshConfiguration(public_keys=public_keys) linux_config = LinuxConfiguration(disable_password_authentication=True, ssh=ssh_config) os_profile = OSProfile(admin_username=self.ADMIN_USERNAME, computer_name=vm_network_name, linux_configuration=linux_config) hardware_profile = HardwareProfile(vm_size=azure_instance_type) network_profile = NetworkProfile( network_interfaces=[NetworkInterfaceReference(id=network_id)]) virtual_hd = VirtualHardDisk( uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'. format(storage_account, vm_network_name)) image_hd = VirtualHardDisk(uri=parameters[self.PARAM_IMAGE_ID]) os_type = OperatingSystemTypes.linux os_disk = OSDisk(os_type=os_type, caching=CachingTypes.read_write, create_option=DiskCreateOptionTypes.from_image, name=vm_network_name, vhd=virtual_hd, image=image_hd) compute_client.virtual_machines.create_or_update( resource_group, vm_network_name, VirtualMachine( location=zone, os_profile=os_profile, hardware_profile=hardware_profile, network_profile=network_profile, storage_profile=StorageProfile(os_disk=os_disk))) # Sleep until an IP address gets associated with the VM. while True: public_ip_address = network_client.public_ip_addresses.get(resource_group, vm_network_name) if public_ip_address.ip_address: utils.log('Azure VM is available at {}'. format(public_ip_address.ip_address)) break utils.log("Waiting {} second(s) for IP address to be available". format(self.SLEEP_TIME)) time.sleep(self.SLEEP_TIME)
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 get_VirtualMachinePaged(self, count=10): return list(VirtualMachine(location='UKsouth') for x in range(count))
def get_VirtualMachine(self): return VirtualMachine(location='UKsouth')
credentials = ClientSecretCredential( client_id=CLIENT_ID, client_secret=CLIENT_SECRET, tenant_id=TENANT_ID ) compute_client : ComputeManagementClient = \ ComputeManagementClient(credentials, SUBSCRIPTION_ID) for vm in compute_client.virtual_machines.list_all(): pprint(json.dumps(vm.as_dict()), indent=4) compute_client.virtual_machines.begin_create_or_update( "test", "linux-marian", VirtualMachine(location="westeurope", tags={ "departament" : "IT", "location" : "Warsaw" }) ).wait() print("="*80) print("Poczatek restartu") event = compute_client.virtual_machines.begin_restart("test","linux-marian") event.wait() print("Koniec restartu") print("="*80) print("Poczatek shutdown") event = compute_client.virtual_machines.begin_power_off("test","linux-marian") event.wait() print("Koniec shutdown")
def get(self, resource_group_name: str, vm_name: str) -> VirtualMachine: try: with open(self.path / resource_group_name / f"vm-{vm_name}.json", "r", encoding="utf-8") as file: return VirtualMachine.deserialize(json.load(file)) except FileNotFoundError: raise ResourceNotFoundError("Virtual Machine not found") from None
def register_image_from_snapshot(root_snapshot_id,root_device,home_snapshot_id,home_device,mi_name): ensure_resource_group(test_group_name) print("2 minute delay") time.sleep(120) ensure_virtual_network(test_group_name) print("2 minute delay") time.sleep(120) nic_name="nic_"+(str(datetime.datetime.now().time())).replace(":","").replace(".","") nic_params={'location':location, 'ipConfigurations': [ { "name": "ipconfig1", "properties": { "privateIpAddressVersion": "IPv4", "privateIPAllocationMethod": "Dynamic", "subnet": { "id": "/subscriptions/"+subscription_id+"/resourceGroups/"+test_group_name+"/providers/Microsoft.Network/virtualNetworks/"+test_group_name+"-vnet/subnets/default" } } } ] } network_client.network_interfaces.begin_create_or_update(test_group_name, nic_name, nic_params) root_snapshot = compute_client.snapshots.get(test_group_name, root_snapshot_id) compute_client.disks.begin_create_or_update( test_group_name, root_device, { 'location': location, 'creation_data': { 'create_option': DiskCreateOption.copy, 'source_resource_id': root_snapshot.id } } ) home_snapshot = compute_client.snapshots.get(test_group_name, home_snapshot_id) compute_client.disks.begin_create_or_update( test_group_name, home_device, { 'location': location, 'creation_data': { 'create_option': DiskCreateOption.copy, 'source_resource_id': home_snapshot.id } } ) print("5 minute delay") time.sleep(300) print("Create vm") os_disk = compute_client.disks.get(test_group_name, root_device) home_disk = compute_client.disks.get(test_group_name, home_device) net_interface=network_client.network_interfaces.get(test_group_name,nic_name) hp = HardwareProfile(vm_size='Standard_DS2_v2') compute_client.virtual_machines.begin_create_or_update( test_group_name, mi_name, VirtualMachine( location=location, name=mi_name, hardware_profile=hp, network_profile=NetworkProfile( network_interfaces=[ { 'id': net_interface.id } ], ), storage_profile=StorageProfile( os_disk={ 'os_type': "Linux", 'name': os_disk.name, 'create_option': DiskCreateOptionTypes.attach, 'managed_disk': { 'id': os_disk.id } }, data_disks=[{ 'lun': 0, 'name': home_disk.name, 'create_option': DiskCreateOptionTypes.attach, 'managed_disk': { 'id': home_disk.id } }] ), ), ) print("2 minute delay") time.sleep(180) print("Create vm image") create_image_from_vm(mi_name) print("2 minute delay") time.sleep(180) # delete vm compute_client.virtual_machines.begin_delete(test_group_name,mi_name) print("2 minute delay") time.sleep(180) # delete disk compute_client.disks.begin_delete(test_group_name,home_device) compute_client.disks.begin_delete(test_group_name,root_device) # delete nic network_client.network_interfaces.begin_delete(test_group_name, nic_name)
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()