Exemplo n.º 1
0
    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()
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
 def get_storage(self, vhd_url, vhd_name):
     """ Get Storage for given VHD name and VHD URL"""
     vm_name = self.vm_json.get('name')
     vhd_name = "%s-osDisk-%s" % (vm_name, vhd_name)
     return StorageProfile(
         os_disk=OSDisk(name=vm_name,
                        image=VirtualHardDisk(uri=vhd_url),
                        vhd=VirtualHardDisk(
                            uri='https://%s.blob.core.windows.net/%s/%s' %
                            (self.storage_account, self.container_name,
                             vhd_name)),
                        disk_size_gb=64,
                        create_option=DiskCreateOptionTypes.from_image,
                        caching='ReadWrite',
                        os_type=OperatingSystemTypes.linux))
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    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.')
Exemplo n.º 6
0
 def __init__(self, nics=None, disks=None, os_disk=None):
     self.network_profile = NetworkProfile(nics)
     self.storage_profile = StorageProfile(data_disks=disks,
                                           os_disk=os_disk)
     self.location = 'westus'
Exemplo n.º 7
0
  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)
Exemplo n.º 8
0
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 {}
Exemplo n.º 9
0
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 __init__(self, nics=None, disks=None):
     self.network_profile = NetworkProfile(nics)
     self.storage_profile = StorageProfile(data_disks=disks)
Exemplo n.º 11
0
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()