def _launch_instance(self): """Launch an instance of the given image.""" self.display_name = ipa_utils.generate_instance_name('oci-ipa-test') try: if not self.subnet_id: self.vcn = self._create_vcn(self.display_name) subnet = self._create_subnet( self.compartment_id, self.availability_domain, self.vcn, self.display_name ) self._create_internet_gateway( self.compartment_id, self.vcn, self.display_name ) self.subnet_id = subnet.id instance_metadata = { 'ssh_authorized_keys': self._get_ssh_public_key() } launch_instance_details = oci.core.models.LaunchInstanceDetails( display_name=self.display_name, compartment_id=self.compartment_id, availability_domain=self.availability_domain, shape=self.instance_type or OCI_DEFAULT_TYPE, metadata=instance_metadata, source_details=oci.core.models.InstanceSourceViaImageDetails( image_id=self.image_id ), create_vnic_details=oci.core.models.CreateVnicDetails( subnet_id=self.subnet_id ) ) response = self.compute_client.launch_instance( launch_instance_details ) instance = response.data except Exception as error: try: self._terminate_instance() except Exception: pass if hasattr(error, 'message'): raise OCICloudException(error.message) else: raise else: self.running_instance_id = instance.id self.logger.debug('ID of instance: %s' % self.running_instance_id) self._wait_on_instance('RUNNING', self.timeout)
def _get_instance(self): """Retrieve instance matching instance_id.""" try: instance = self.compute_client.get_instance( self.running_instance_id).data except Exception: raise OCICloudException( 'Instance with ID: {instance_id} not found.'.format( instance_id=self.running_instance_id)) return instance
def _get_instance_state(self): """ Attempt to retrieve the state of the instance. Raises: OCICloudException: If the instance cannot be found. """ instance = self._get_instance() try: state = instance.lifecycle_state except Exception: raise OCICloudException('Instance with id: {instance_id}, ' 'cannot be found.'.format( instance_id=self.running_instance_id)) return state
def _set_instance_ip(self): """ Retrieve instance ip and cache it. """ instance = self._get_instance() self.instance_ip = None vnic_attachments = self._get_vnic_attachments(instance.compartment_id, instance.id) for attachment in vnic_attachments: vnic = self.vnet_client.get_vnic(attachment.vnic_id).data public_address = vnic.public_ip private_address = vnic.private_ip if public_address or private_address: # Current nic has an IP address, set and finish self.instance_ip = public_address or private_address break if not self.instance_ip: raise OCICloudException('IP address for instance cannot be found.')
def __init__( self, 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, ssh_private_key_file=None, ssh_user=None, subnet_id=None, test_dirs=None, test_files=None, timeout=None, collect_vm_info=None, compartment_id=None, availability_domain=None, signing_key_fingerprint=None, signing_key_file=None, tenancy=None, oci_user_id=None, enable_secure_boot=None, enable_uefi=None ): """Initialize OCI cloud framework class.""" super(OCICloud, self).__init__( 'oci', 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.availability_domain = ( availability_domain or self.ipa_config['availability_domain'] ) if not self.availability_domain: raise OCICloudException( 'Availability domain is required to connect to OCI.' ) self.compartment_id = ( compartment_id or self.ipa_config['compartment_id'] ) if not self.compartment_id: raise OCICloudException( 'Compartment ID is required to use OCI.' ) if not self.ssh_private_key_file: raise OCICloudException( 'SSH private key file is required to connect to instance.' ) self.ssh_user = self.ssh_user or OCI_DEFAULT_USER self.subnet_id = subnet_id or self.ipa_config['subnet_id'] self.oci_user_id = oci_user_id or self.ipa_config['oci_user_id'] self.tenancy = tenancy or self.ipa_config['tenancy'] self.signing_key_fingerprint = ( signing_key_fingerprint or self.ipa_config['signing_key_fingerprint'] ) self.signing_key_file = ( signing_key_file or self.ipa_config['signing_key_file'] ) config = self._get_config() self.compute_client = oci.core.ComputeClient(config) self.compute_composite_client = \ oci.core.ComputeClientCompositeOperations( self.compute_client ) self.vnet_client = oci.core.VirtualNetworkClient(config) self.vnet_composite_client = \ oci.core.VirtualNetworkClientCompositeOperations( self.vnet_client )