예제 #1
0
    def _create_storage_profile(self):
        """
        Create the storage profile for the instance.

        Image reference can be a custom image name or a published urn.
        """
        if self.image_publisher:
            storage_profile = {
                'image_reference': {
                    'publisher': self.image_publisher,
                    'offer': self.image_offer,
                    'sku': self.image_sku,
                    'version': self.image_version
                },
            }
        else:
            for image in self.compute.images.list():
                if image.name == self.image_id:
                    image_id = image.id
                    break
            else:
                raise AzureCloudException(
                    'Image with name {0} not found.'.format(self.image_id))

            storage_profile = {'image_reference': {'id': image_id}}

        return storage_profile
예제 #2
0
 def _terminate_instance(self):
     """
     Terminate the resource group and instance.
     """
     try:
         self.resource.resource_groups.delete(self.running_instance_id)
     except Exception as error:
         raise AzureCloudException(
             'Unable to terminate resource group: {0}.'.format(error))
예제 #3
0
    def _get_management_client(self, client_class):
        """
        Return instance of resource management client.
        """
        try:
            client = get_client_from_auth_file(
                client_class, auth_path=self.service_account_file)
        except ValueError as error:
            raise AzureCloudException(
                'Service account file format is invalid: {0}.'.format(error))
        except KeyError as error:
            raise AzureCloudException(
                'Service account file missing key: {0}.'.format(error))
        except Exception as error:
            raise AzureCloudException(
                'Unable to create resource management client: '
                '{0}.'.format(error))

        return client
예제 #4
0
    def post_init(self):
        """Initialize Azure cloud framework class."""

        self.vnet_name = (self.custom_args.get('vnet_name')
                          or self.ipa_config['vnet_name'])
        self.vnet_resource_group = (self.custom_args.get('vnet_resource_group')
                                    or self.ipa_config['vnet_resource_group'])

        subnet_args = [
            self.subnet_id, self.vnet_name, self.vnet_resource_group
        ]
        if any(subnet_args) and not all(subnet_args):
            raise AzureCloudException(
                'subnet_id, vnet_resource_group and vnet_name'
                ' are all required to use an existing subnet.')

        self.service_account_file = (
            self.custom_args.get('service_account_file')
            or self.ipa_config['service_account_file'])
        if not self.service_account_file:
            raise AzureCloudException(
                'Service account file is required to connect to Azure.')
        else:
            self.service_account_file = os.path.expanduser(
                self.service_account_file)

        if not self.ssh_private_key_file:
            raise AzureCloudException(
                'SSH private key file is required to connect to instance.')

        self.accelerated_networking = (
            self.custom_args.get('accelerated_networking')
            or self.ipa_config['accelerated_networking'])
        self.ssh_user = self.ssh_user or AZURE_DEFAULT_USER
        self.ssh_public_key = self._get_ssh_public_key()

        self.compute = self._get_management_client(ComputeManagementClient)
        self.network = self._get_management_client(NetworkManagementClient,
                                                   wrap_creds=False)
        self.resource = self._get_management_client(ResourceManagementClient)

        if self.running_instance_id:
            self._set_default_resource_names()
예제 #5
0
    def _stop_instance(self):
        """
        Stop the instance.
        """
        try:
            vm_stop = self.compute.virtual_machines.power_off(
                self.running_instance_id, self.running_instance_id)
        except Exception as error:
            raise AzureCloudException(
                'Unable to stop instance: {0}.'.format(error))

        vm_stop.wait()
예제 #6
0
    def _start_instance(self):
        """
        Start the instance.
        """
        try:
            vm_start = self.compute.virtual_machines.start(
                self.running_instance_id, self.running_instance_id)
        except Exception as error:
            raise AzureCloudException(
                'Unable to start instance: {0}.'.format(error))

        vm_start.wait()
예제 #7
0
    def _create_resource_group(self, region, resource_group_name):
        """
        Create resource group if it does not exist.
        """
        resource_group_config = {'location': region}

        try:
            self.resource.resource_groups.create_or_update(
                resource_group_name, resource_group_config)
        except Exception as error:
            raise AzureCloudException(
                'Unable to create resource group: {0}.'.format(error))
예제 #8
0
    def _create_vm(self, vm_config):
        """
        Attempt to create or update VM instance based on vm_parameters config.
        """
        try:
            vm_setup = self.compute.virtual_machines.create_or_update(
                self.running_instance_id, self.running_instance_id, vm_config)
        except Exception as error:
            raise AzureCloudException(
                'An exception occurred creating virtual machine: {0}'.format(
                    error))

        vm_setup.wait()
예제 #9
0
    def _get_management_client(self, client_class, wrap_creds=True):
        """
        Return instance of resource management client.
        """
        credentials = ipa_utils.load_json(self.service_account_file)

        try:
            client = self._get_client_from_json(client_class,
                                                credentials,
                                                wrap_creds=wrap_creds)
        except ValueError as error:
            raise AzureCloudException(
                'Service account file format is invalid: {0}.'.format(error))
        except KeyError as error:
            raise AzureCloudException(
                'Service account file missing key: {0}.'.format(error))
        except Exception as error:
            raise AzureCloudException(
                'Unable to create resource management client: '
                '{0}.'.format(error))

        return client
예제 #10
0
    def _get_instance(self):
        """
        Return the instance matching the running_instance_id.
        """
        try:
            instance = self.compute.virtual_machines.get(
                self.running_instance_id,
                self.running_instance_id,
                expand='instanceView')
        except Exception as error:
            raise AzureCloudException(
                'Unable to retrieve instance: {0}'.format(error))

        return instance
예제 #11
0
    def _create_subnet(self, resource_group_name, subnet_id, vnet_name):
        """
        Create a subnet in the provided vnet and resource group.
        """
        subnet_config = {'address_prefix': '10.0.0.0/29'}

        try:
            subnet_setup = self.network.subnets.create_or_update(
                resource_group_name, vnet_name, subnet_id, subnet_config)
        except Exception as error:
            raise AzureCloudException(
                'Unable to create subnet: {0}.'.format(error))

        return subnet_setup.result()
예제 #12
0
    def _create_virtual_network(self, region, resource_group_name, vnet_name):
        """
        Create a vnet in the given resource group with default address space.
        """
        vnet_config = {
            'location': region,
            'address_space': {
                'address_prefixes': ['10.0.0.0/27']
            }
        }

        try:
            vnet_setup = self.network.virtual_networks.create_or_update(
                resource_group_name, vnet_name, vnet_config)
        except Exception as error:
            raise AzureCloudException(
                'Unable to create vnet: {0}.'.format(error))

        vnet_setup.wait()
예제 #13
0
    def _create_public_ip(self, public_ip_name, resource_group_name, region):
        """
        Create dynamic public IP address in the resource group.
        """
        public_ip_config = {
            'location': region,
            'public_ip_allocation_method': 'Dynamic'
        }

        try:
            public_ip_setup = \
                self.network.public_ip_addresses.create_or_update(
                    resource_group_name, public_ip_name, public_ip_config
                )
        except Exception as error:
            raise AzureCloudException(
                'Unable to create public IP: {0}.'.format(error))

        return public_ip_setup.result()
예제 #14
0
    def _set_instance_ip(self):
        """
        Get the IP address based on instance ID.

        If public IP address not found attempt to get private IP.
        """
        try:
            ip_address = self.network.public_ip_addresses.get(
                self.running_instance_id, self.public_ip_name).ip_address
        except Exception:
            try:
                ip_address = self.network.network_interfaces.get(
                    self.running_instance_id,
                    self.nic_name).ip_configurations[0].private_ip_address
            except Exception as error:
                raise AzureCloudException(
                    'Unable to retrieve instance IP address: {0}.'.format(
                        error))

        self.instance_ip = ip_address
예제 #15
0
    def _create_network_interface(self,
                                  ip_config_name,
                                  nic_name,
                                  public_ip,
                                  region,
                                  resource_group_name,
                                  subnet,
                                  accelerated_networking=False):
        """
        Create a network interface in the resource group.

        Attach NIC to the subnet and public IP provided.
        """
        nic_config = {
            'location':
            region,
            'ip_configurations': [{
                'name': ip_config_name,
                'private_ip_allocation_method': 'Dynamic',
                'subnet': {
                    'id': subnet.id
                },
                'public_ip_address': {
                    'id': public_ip.id
                },
            }]
        }

        if accelerated_networking:
            nic_config['enable_accelerated_networking'] = True

        try:
            nic_setup = self.network.network_interfaces.create_or_update(
                resource_group_name, nic_name, nic_config)
        except Exception as error:
            raise AzureCloudException(
                'Unable to create network interface: {0}.'.format(error))

        return nic_setup.result()
예제 #16
0
    def __init__(self,
                 accelerated_networking=False,
                 cleanup=None,
                 config=None,
                 description=None,
                 distro_name=None,
                 early_exit=None,
                 history_log=None,
                 image_id=None,
                 inject=None,
                 instance_type=None,
                 log_level=None,
                 no_default_test_dirs=False,
                 cloud_config=None,
                 region=None,
                 results_dir=None,
                 running_instance_id=None,
                 service_account_file=None,
                 ssh_private_key_file=None,
                 ssh_user=None,
                 subnet_id=None,
                 test_dirs=None,
                 test_files=None,
                 timeout=None,
                 vnet_name=None,
                 vnet_resource_group=None,
                 collect_vm_info=None,
                 enable_secure_boot=None,
                 enable_uefi=None):
        """Initialize Azure Cloud class."""
        super(AzureCloud,
              self).__init__('azure', cleanup, config, description,
                             distro_name, early_exit, history_log, image_id,
                             inject, instance_type, log_level,
                             no_default_test_dirs, cloud_config, region,
                             results_dir, running_instance_id, test_dirs,
                             test_files, timeout, collect_vm_info,
                             ssh_private_key_file, ssh_user, subnet_id,
                             enable_secure_boot, enable_uefi)

        self.vnet_name = vnet_name or self.ipa_config['vnet_name']
        self.vnet_resource_group = (vnet_resource_group
                                    or self.ipa_config['vnet_resource_group'])

        subnet_args = [
            self.subnet_id, self.vnet_name, self.vnet_resource_group
        ]
        if any(subnet_args) and not all(subnet_args):
            raise AzureCloudException(
                'subnet_id, vnet_resource_group and vnet_name'
                ' are all required to use an existing subnet.')

        self.service_account_file = (service_account_file or
                                     self.ipa_config['service_account_file'])
        if not self.service_account_file:
            raise AzureCloudException(
                'Service account file is required to connect to Azure.')
        else:
            self.service_account_file = os.path.expanduser(
                self.service_account_file)

        if not self.ssh_private_key_file:
            raise AzureCloudException(
                'SSH private key file is required to connect to instance.')

        self.accelerated_networking = (
            accelerated_networking
            or self.ipa_config['accelerated_networking'])
        self.ssh_user = self.ssh_user or AZURE_DEFAULT_USER
        self.ssh_public_key = self._get_ssh_public_key()

        self.compute = self._get_management_client(ComputeManagementClient)
        self.network = self._get_management_client(NetworkManagementClient)
        self.resource = self._get_management_client(ResourceManagementClient)

        if self.running_instance_id:
            self._set_default_resource_names()