def test_InstanceMetadata_uses_passed_network_info(self): network_info = {"a": "b"} self.mox.StubOutWithMock(netutils, "get_injected_network_template") netutils.get_injected_network_template(network_info).AndReturn(False) self.mox.ReplayAll() base.InstanceMetadata(INSTANCES[0], network_info=network_info)
def test_InstanceMetadata_uses_passed_network_info(self): network_info = [] self.mox.StubOutWithMock(netutils, "get_injected_network_template") netutils.get_injected_network_template(network_info).AndReturn(False) self.mox.ReplayAll() base.InstanceMetadata(fake_inst_obj(self.context), network_info=network_info)
def test_InstanceMetadata_queries_network_API_when_needed(self): network_info_from_api = [] self.mox.StubOutWithMock(netutils, "get_injected_network_template") netutils.get_injected_network_template(network_info_from_api).AndReturn(False) self.mox.ReplayAll() base.InstanceMetadata(fake_inst_obj(self.context))
def test_InstanceMetadata_queries_network_API_when_needed(self): network_info_from_api = [] self.mox.StubOutWithMock(netutils, "get_injected_network_template") netutils.get_injected_network_template( network_info_from_api).AndReturn(False) self.mox.ReplayAll() base.InstanceMetadata(fake_inst_obj(self.context))
def test_InstanceMetadata_queries_network_API_when_needed(self): network_info_from_api = {"c": "d"} self.mox.StubOutWithMock(network_api.API, "get_instance_nw_info") network_api.API.get_instance_nw_info(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(network_info_from_api) self.mox.StubOutWithMock(netutils, "get_injected_network_template") netutils.get_injected_network_template(network_info_from_api).AndReturn(False) self.mox.ReplayAll() base.InstanceMetadata(INSTANCES[0])
def test_InstanceMetadata_queries_network_API_when_needed(self): network_info_from_api = [] self.mox.StubOutWithMock(network_api.API, "get_instance_nw_info") network_api.API.get_instance_nw_info( mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(network_info_from_api) self.mox.StubOutWithMock(netutils, "get_injected_network_template") netutils.get_injected_network_template( network_info_from_api).AndReturn(False) self.mox.ReplayAll() base.InstanceMetadata(INSTANCES[0])
def _test_injected_network_template(self, should_inject, use_ipv6=False, gateway=True): """Check that netutils properly decides whether to inject based on whether the supplied subnet is static or dynamic. """ network = fake_network_cache_model.new_network({'subnets': []}) subnet_dict = {} if not gateway: subnet_dict['gateway'] = None if not should_inject: subnet_dict['dhcp_server'] = '10.10.0.1' network.add_subnet(fake_network_cache_model.new_subnet(subnet_dict)) if should_inject and use_ipv6: gateway_ip = fake_network_cache_model.new_ip(dict( address='1234:567::1')) ip = fake_network_cache_model.new_ip(dict( address='1234:567::2')) ipv6_subnet_dict = dict( cidr='1234:567::/48', gateway=gateway_ip, ips=[ip]) if not gateway: ipv6_subnet_dict['gateway'] = None network.add_subnet(fake_network_cache_model.new_subnet( ipv6_subnet_dict)) # Behave as though CONF.flat_injected is True network['meta']['injected'] = True vif = fake_network_cache_model.new_vif({'network': network}) ninfo = model.NetworkInfo([vif]) template = netutils.get_injected_network_template(ninfo, use_ipv6=use_ipv6) # will just ignore the improper behavior. if not should_inject: self.assertTrue(template is None) else: self.assertTrue('auto eth0' in template) self.assertTrue('iface eth0 inet static' in template) self.assertTrue('address 10.10.0.2' in template) self.assertTrue('netmask 255.255.255.0' in template) self.assertTrue('broadcast 10.10.0.255' in template) if gateway: self.assertTrue('gateway 10.10.0.1' in template) else: self.assertFalse('gateway' in template) self.assertTrue('dns-nameservers 1.2.3.4 2.3.4.5' in template) if use_ipv6: self.assertTrue('iface eth0 inet6 static' in template) self.assertTrue('address 1234:567::2' in template) self.assertTrue('netmask 48' in template) if gateway: self.assertTrue('gateway 1234:567::1' in template)
def _setup_injected_network_scenario(self, should_inject=True, use_ipv4=True, use_ipv6=False, gateway=True, dns=True, two_interfaces=False, libvirt_virt_type=None): """Check that netutils properly decides whether to inject based on whether the supplied subnet is static or dynamic. """ network = fake_network_cache_model.new_network({'subnets': []}) subnet_dict = {} if not gateway: subnet_dict['gateway'] = None if not dns: subnet_dict['dns'] = None if not should_inject: subnet_dict['dhcp_server'] = '10.10.0.1' if use_ipv4: network.add_subnet( fake_network_cache_model.new_subnet(subnet_dict)) if should_inject and use_ipv6: gateway_ip = fake_network_cache_model.new_ip( dict(address='1234:567::1')) ip = fake_network_cache_model.new_ip(dict(address='1234:567::2')) ipv6_subnet_dict = dict( cidr='1234:567::/48', gateway=gateway_ip, dns=[ fake_network_cache_model.new_ip( dict(address='2001:4860:4860::8888')), fake_network_cache_model.new_ip( dict(address='2001:4860:4860::8844')) ], ips=[ip]) if not gateway: ipv6_subnet_dict['gateway'] = None network.add_subnet( fake_network_cache_model.new_subnet(ipv6_subnet_dict)) # Behave as though CONF.flat_injected is True network['meta']['injected'] = True vif = fake_network_cache_model.new_vif({'network': network}) vifs = [vif] if two_interfaces: vifs.append(vif) nwinfo = model.NetworkInfo(vifs) return netutils.get_injected_network_template( nwinfo, use_ipv6=use_ipv6, libvirt_virt_type=libvirt_virt_type)
def _setup_injected_network_scenario( self, should_inject=True, use_ipv4=True, use_ipv6=False, gateway=True, dns=True, two_interfaces=False, libvirt_virt_type=None, ): """Check that netutils properly decides whether to inject based on whether the supplied subnet is static or dynamic. """ network = fake_network_cache_model.new_network({"subnets": []}) subnet_dict = {} if not gateway: subnet_dict["gateway"] = None if not dns: subnet_dict["dns"] = None if not should_inject: subnet_dict["dhcp_server"] = "10.10.0.1" if use_ipv4: network.add_subnet(fake_network_cache_model.new_subnet(subnet_dict)) if should_inject and use_ipv6: gateway_ip = fake_network_cache_model.new_ip(dict(address="1234:567::1")) ip = fake_network_cache_model.new_ip(dict(address="1234:567::2")) ipv6_subnet_dict = dict( cidr="1234:567::/48", gateway=gateway_ip, dns=[ fake_network_cache_model.new_ip(dict(address="2001:4860:4860::8888")), fake_network_cache_model.new_ip(dict(address="2001:4860:4860::8844")), ], ips=[ip], ) if not gateway: ipv6_subnet_dict["gateway"] = None network.add_subnet(fake_network_cache_model.new_subnet(ipv6_subnet_dict)) # Behave as though CONF.flat_injected is True network["meta"]["injected"] = True vif = fake_network_cache_model.new_vif({"network": network}) vifs = [vif] if two_interfaces: vifs.append(vif) nwinfo = model.NetworkInfo(vifs) return netutils.get_injected_network_template(nwinfo, use_ipv6=use_ipv6, libvirt_virt_type=libvirt_virt_type)
def _test_injected_network_template(self, should_inject, use_ipv6=False, legacy=False): """Check that netutils properly decides whether to inject based on whether the supplied subnet is static or dynamic. """ network = fake_network_cache_model.new_network({'subnets': []}) if should_inject: network.add_subnet(fake_network_cache_model.new_subnet()) if use_ipv6: gateway_ip = fake_network_cache_model.new_ip(dict( address='1234:567::1')) ip = fake_network_cache_model.new_ip(dict( address='1234:567::2')) subnet_dict = dict( cidr='1234:567::/48', gateway=gateway_ip, ips=[ip]) network.add_subnet(fake_network_cache_model.new_subnet( subnet_dict)) else: subnet_dict = dict(dhcp_server='10.10.0.1') network.add_subnet(fake_network_cache_model.new_subnet( subnet_dict)) # Behave as though CONF.flat_injected is True network['meta']['injected'] = True vif = fake_network_cache_model.new_vif({'network': network}) ninfo = model.NetworkInfo([vif]) if legacy: ninfo = ninfo.legacy() template = netutils.get_injected_network_template(ninfo, use_ipv6=use_ipv6) # NOTE(bnemec): There is a bug with legacy network info that causes # it to inject regardless of whether the network is static or dynamic. # This can't be fixed without changes that would potentially break # existing code, so until legacy network info goes away this test # will just ignore the improper behavior. if not should_inject and not legacy: self.assertTrue(template is None) else: self.assertTrue('auto eth0' in template) self.assertTrue('iface eth0 inet static' in template) self.assertTrue('address 10.10.0.2' in template) self.assertTrue('netmask 255.255.255.0' in template) self.assertTrue('broadcast 10.10.0.255' in template) self.assertTrue('gateway 10.10.0.1' in template) self.assertTrue('dns-nameservers 1.2.3.4 2.3.4.5' in template) if use_ipv6: self.assertTrue('iface eth0 inet6 static' in template) self.assertTrue('address 1234:567::2' in template) self.assertTrue('netmask 48' in template) self.assertTrue('gateway 1234:567::1' in template)
def _setup_injected_network_scenario(self, should_inject=True, use_ipv4=True, use_ipv6=False, gateway=True, dns=True, two_interfaces=False, libvirt_virt_type=None): """Check that netutils properly decides whether to inject based on whether the supplied subnet is static or dynamic. """ network = fake_network_cache_model.new_network({'subnets': []}) subnet_dict = {} if not gateway: subnet_dict['gateway'] = None if not dns: subnet_dict['dns'] = None if not should_inject: subnet_dict['dhcp_server'] = '10.10.0.1' if use_ipv4: network.add_subnet( fake_network_cache_model.new_subnet(subnet_dict)) if should_inject and use_ipv6: gateway_ip = fake_network_cache_model.new_ip(dict( address='1234:567::1')) ip = fake_network_cache_model.new_ip(dict( address='1234:567::2')) ipv6_subnet_dict = dict( cidr='1234:567::/48', gateway=gateway_ip, dns=[fake_network_cache_model.new_ip( dict(address='2001:4860:4860::8888')), fake_network_cache_model.new_ip( dict(address='2001:4860:4860::8844'))], ips=[ip]) if not gateway: ipv6_subnet_dict['gateway'] = None network.add_subnet(fake_network_cache_model.new_subnet( ipv6_subnet_dict)) # Behave as though CONF.flat_injected is True network['meta']['injected'] = True vif = fake_network_cache_model.new_vif({'network': network}) vifs = [vif] if two_interfaces: vifs.append(vif) nwinfo = model.NetworkInfo(vifs) return netutils.get_injected_network_template( nwinfo, use_ipv6=use_ipv6, libvirt_virt_type=libvirt_virt_type)
def _fill_templates(self): """ Iterate through each file necessary for creating interfaces on a given platform, open the file and write the contents of the template to the file. """ iface_file = netutils.get_injected_network_template(self.network_info, use_ipv6=CONF.use_ipv6, template=CONF.injected_network_template) for filename in self._filename_factory(): network_file = OVZNetworkFile(filename) with network_file: network_file.append(iface_file) network_file.write()
def _inject_files(self, sdk_ve, boot_hdd, instance, network_info=None, files=None, admin_pass=None): disk_path = boot_hdd.get_image_path() target_partition = CONF.pcs_inject_partition if target_partition == 0: target_partition = None key = str(instance['key_data']) net = netutils.get_injected_network_template(network_info) metadata = instance.get('metadata') if any((key, net, metadata, admin_pass, files)): # If we're not using config_drive, inject into root fs LOG.info('Injecting files') try: img_id = instance['image_ref'] for inj, val in [('key', key), ('net', net), ('metadata', metadata), ('admin_pass', admin_pass), ('files', files)]: if val: LOG.info(_('Injecting %(inj)s into image ' '%(img_id)s'), {'inj': inj, 'img_id': img_id}, instance=instance) with pcsutils.PloopMount(disk_path, chown=True, root_helper=utils._get_root_helper()) as dev: disk.inject_data(dev, key, net, metadata, admin_pass, files, partition=target_partition, use_cow=False, mandatory=('files',)) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_('Error injecting data into image ' '%(img_id)s (%(e)s)'), {'img_id': img_id, 'e': e}, instance=instance)
def _test_injected_network_template(self, should_inject, use_ipv4=True, use_ipv6=False, gateway=True): """Check that netutils properly decides whether to inject based on whether the supplied subnet is static or dynamic. """ network = fake_network_cache_model.new_network({'subnets': []}) subnet_dict = {} if not gateway: subnet_dict['gateway'] = None if not should_inject: subnet_dict['dhcp_server'] = '10.10.0.1' if use_ipv4: network.add_subnet( fake_network_cache_model.new_subnet(subnet_dict)) if should_inject and use_ipv6: gateway_ip = fake_network_cache_model.new_ip( dict(address='1234:567::1')) ip = fake_network_cache_model.new_ip(dict(address='1234:567::2')) ipv6_subnet_dict = dict(cidr='1234:567::/48', gateway=gateway_ip, ips=[ip]) if not gateway: ipv6_subnet_dict['gateway'] = None network.add_subnet( fake_network_cache_model.new_subnet(ipv6_subnet_dict)) # Behave as though CONF.flat_injected is True network['meta']['injected'] = True vif = fake_network_cache_model.new_vif({'network': network}) ninfo = model.NetworkInfo([vif]) template = netutils.get_injected_network_template(ninfo, use_ipv6=use_ipv6) # will just ignore the improper behavior. if not should_inject: self.assertTrue(template is None) else: if use_ipv4: self.assertIn('auto eth0', template) self.assertIn('iface eth0 inet static', template) self.assertIn('address 10.10.0.2', template) self.assertIn('netmask 255.255.255.0', template) self.assertIn('broadcast 10.10.0.255', template) if gateway: self.assertIn('gateway 10.10.0.1', template) else: self.assertNotIn('gateway', template) self.assertIn('dns-nameservers 1.2.3.4 2.3.4.5', template) if use_ipv6: self.assertIn('iface eth0 inet6 static', template) self.assertIn('address 1234:567::2', template) self.assertIn('netmask 48', template) if gateway: self.assertIn('gateway 1234:567::1', template) if not use_ipv4 and not use_ipv6: self.assertTrue(template is None)
def __init__( self, instance, address=None, content=None, extra_md=None, conductor_api=None, network_info=None, vd_driver=None ): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ if not content: content = [] ctxt = context.get_admin_context() # NOTE(danms): This should be removed after bp:compute-manager-objects if not isinstance(instance, obj_base.NovaObject): expected = ["metadata", "system_metadata"] if "info_cache" in instance: expected.append("info_cache") instance = objects.Instance._from_db_object(ctxt, objects.Instance(), instance, expected_attrs=expected) # The default value of mimeType is set to MIME_TYPE_TEXT_PLAIN self.set_mimetype(MIME_TYPE_TEXT_PLAIN) self.instance = instance self.extra_md = extra_md if conductor_api: capi = conductor_api else: capi = conductor.API() self.availability_zone = ec2utils.get_availability_zone_by_host(instance["host"], capi) self.security_groups = objects.SecurityGroupList.get_by_instance(ctxt, instance) self.mappings = _format_instance_mapping(ctxt, instance) if instance.get("user_data", None) is not None: self.userdata_raw = base64.b64decode(instance["user_data"]) else: self.userdata_raw = None self.ec2_ids = capi.get_ec2_ids(ctxt, obj_base.obj_to_primitive(instance)) self.address = address # expose instance metadata. self.launch_metadata = utils.instance_meta(instance) self.password = password.extract_password(instance) self.uuid = instance.get("uuid") self.content = {} self.files = [] # get network info, and the rendered network template if network_info is None: network_info = instance.info_cache.network_info self.ip_info = ec2utils.get_ip_info_for_instance_from_nw_info(network_info) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = {"name": "network_config", "content_path": "/%s/%s" % (CONTENT_DIR, key)} # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. That's how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({"path": path, "content_path": "/%s/%s" % (CONTENT_DIR, key)}) self.content[key] = contents if vd_driver is None: vdclass = importutils.import_class(CONF.vendordata_driver) else: vdclass = vd_driver self.vddriver = vdclass(instance=instance, address=address, extra_md=extra_md, network_info=network_info) self.route_configuration = None
def __init__(self, instance, address=None, content=[], extra_md=None, conductor_api=None): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ self.instance = instance self.extra_md = extra_md if conductor_api: capi = conductor_api else: capi = conductor.API() ctxt = context.get_admin_context() self.availability_zone = ec2utils.get_availability_zone_by_host( instance['host'], capi) self.ip_info = ec2utils.get_ip_info_for_instance(ctxt, instance) self.security_groups = capi.security_group_get_by_instance(ctxt, instance) self.mappings = _format_instance_mapping(capi, ctxt, instance) if instance.get('user_data', None) is not None: self.userdata_raw = base64.b64decode(instance['user_data']) else: self.userdata_raw = None self.ec2_ids = capi.get_ec2_ids(ctxt, instance) self.address = address # expose instance metadata. self.launch_metadata = {} for item in instance.get('metadata', []): self.launch_metadata[item['key']] = item['value'] self.password = password.extract_password(instance) self.uuid = instance.get('uuid') self.content = {} self.files = [] # get network info, and the rendered network template ctxt = context.get_admin_context() network_info = network.API().get_instance_nw_info(ctxt, instance, conductor_api=capi) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = {"name": "network_config", 'content_path': "/%s/%s" % (CONTENT_DIR, key)} # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. Thats how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({'path': path, 'content_path': "/%s/%s" % (CONTENT_DIR, key)}) self.content[key] = contents
def __init__(self, instance, address=None, content=None, extra_md=None, network_info=None, vd_driver=None, network_metadata=None, request_context=None): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ if not content: content = [] ctxt = context.get_admin_context() # The default value of mimeType is set to MIME_TYPE_TEXT_PLAIN self.set_mimetype(MIME_TYPE_TEXT_PLAIN) self.instance = instance self.extra_md = extra_md self.availability_zone = az.get_instance_availability_zone(ctxt, instance) secgroup_api = openstack_driver.get_openstack_security_group_driver() self.security_groups = secgroup_api.get_instance_security_groups( ctxt, instance) self.mappings = _format_instance_mapping(ctxt, instance) if instance.user_data is not None: self.userdata_raw = base64.b64decode(instance.user_data) else: self.userdata_raw = None self.address = address # expose instance metadata. self.launch_metadata = utils.instance_meta(instance) self.password = password.extract_password(instance) self.uuid = instance.uuid self.content = {} self.files = [] # get network info, and the rendered network template if network_info is None: network_info = instance.info_cache.network_info # expose network metadata if network_metadata is None: self.network_metadata = netutils.get_network_metadata(network_info) else: self.network_metadata = network_metadata self.ip_info = \ ec2utils.get_ip_info_for_instance_from_nw_info(network_info) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = {"name": "network_config", 'content_path': "/%s/%s" % (CONTENT_DIR, key)} # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. That's how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({'path': path, 'content_path': "/%s/%s" % (CONTENT_DIR, key)}) self.content[key] = contents if vd_driver is None: vdclass = importutils.import_class(CONF.vendordata_driver) else: vdclass = vd_driver self.vddriver = vdclass(instance=instance, address=address, extra_md=extra_md, network_info=network_info) self.route_configuration = None # NOTE(mikal): the decision to not pass extra_md here like we # do to the StaticJSON driver is deliberate. extra_md will # contain the admin password for the instance, and we shouldn't # pass that to external services. self.vendordata_providers = { 'StaticJSON': vendordata_json.JsonFileVendorData( instance=instance, address=address, extra_md=extra_md, network_info=network_info), 'DynamicJSON': vendordata_dynamic.DynamicVendorData( instance=instance, address=address, network_info=network_info, context=request_context) }
def __init__(self, instance, address=None, content=[], extra_md=None): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ self.instance = instance self.extra_md = extra_md ctxt = context.get_admin_context() services = db.service_get_all_by_host(ctxt.elevated(), instance["host"]) self.availability_zone = ec2utils.get_availability_zone_by_host(services, instance["host"]) self.ip_info = ec2utils.get_ip_info_for_instance(ctxt, instance) self.security_groups = db.security_group_get_by_instance(ctxt, instance["id"]) self.mappings = _format_instance_mapping(ctxt, instance) if instance.get("user_data", None) is not None: self.userdata_raw = base64.b64decode(instance["user_data"]) else: self.userdata_raw = None self.ec2_ids = {} self.ec2_ids["instance-id"] = ec2utils.id_to_ec2_inst_id(instance["uuid"]) self.ec2_ids["ami-id"] = ec2utils.glance_id_to_ec2_id(ctxt, instance["image_ref"]) for image_type in ["kernel", "ramdisk"]: if self.instance.get("%s_id" % image_type): image_id = self.instance["%s_id" % image_type] ec2_image_type = ec2utils.image_type(image_type) ec2_id = ec2utils.glance_id_to_ec2_id(ctxt, image_id, ec2_image_type) self.ec2_ids["%s-id" % image_type] = ec2_id self.address = address # expose instance metadata. self.launch_metadata = {} for item in instance.get("metadata", []): self.launch_metadata[item["key"]] = item["value"] self.uuid = instance.get("uuid") self.content = {} self.files = [] # get network info, and the rendered network template ctxt = context.get_admin_context() network_info = network.API().get_instance_nw_info(ctxt, instance) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = {"name": "network_config", "content_path": "/%s/%s" % (CONTENT_DIR, key)} # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. Thats how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({"path": path, "content_path": "/%s/%s" % (CONTENT_DIR, key)}) self.content[key] = contents
def __init__(self, instance, address=None, content=None, extra_md=None, conductor_api=None, network_info=None, vd_driver=None): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ if not content: content = [] ctxt = context.get_admin_context() # NOTE(danms): This should be removed after bp:compute-manager-objects if not isinstance(instance, instance_obj.Instance): instance = instance_obj.Instance._from_db_object( ctxt, instance_obj.Instance(), instance, expected_attrs=['metadata', 'system_metadata']) self.instance = instance self.extra_md = extra_md if conductor_api: capi = conductor_api else: capi = conductor.API() self.availability_zone = ec2utils.get_availability_zone_by_host( instance['host'], capi) self.security_groups = secgroup_obj.SecurityGroupList.get_by_instance( ctxt, instance) self.mappings = _format_instance_mapping(ctxt, instance) if instance.get('user_data', None) is not None: self.userdata_raw = base64.b64decode(instance['user_data']) else: self.userdata_raw = None self.ec2_ids = capi.get_ec2_ids(ctxt, obj_base.obj_to_primitive(instance)) self.address = address # expose instance metadata. self.launch_metadata = utils.instance_meta(instance) self.password = password.extract_password(instance) self.uuid = instance.get('uuid') self.content = {} self.files = [] # get network info, and the rendered network template if network_info is None: network_info = network.API().get_instance_nw_info(ctxt, instance) self.ip_info = \ ec2utils.get_ip_info_for_instance_from_nw_info(network_info) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = {"name": "network_config", 'content_path': "/%s/%s" % (CONTENT_DIR, key)} # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. Thats how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({'path': path, 'content_path': "/%s/%s" % (CONTENT_DIR, key)}) self.content[key] = contents if vd_driver is None: vdclass = importutils.import_class(CONF.vendordata_driver) else: vdclass = vd_driver self.vddriver = vdclass(instance=instance, address=address, extra_md=extra_md, network_info=network_info) self.route_configuration = None # IBM-ONLY start # For config strategy, we initialize ibm_configuration_data_files with # the files to put in the root of the config disk. self.ibm_configuration_data_files = [] config_data = instance.system_metadata.get('configuration_data') if config_data: config_data = json.loads(config_data) files = config_data['present_disk'].get('files', {}) self.ibm_configuration_data_files = files.items()
def __init__(self, instance, address=None, content=[], extra_md=None): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ self.instance = instance self.extra_md = extra_md ctxt = context.get_admin_context() services = db.service_get_all_by_host(ctxt.elevated(), instance['host']) self.availability_zone = ec2utils.get_availability_zone_by_host( services, instance['host']) self.ip_info = ec2utils.get_ip_info_for_instance(ctxt, instance) self.security_groups = db.security_group_get_by_instance(ctxt, instance['id']) self.mappings = _format_instance_mapping(ctxt, instance) if instance.get('user_data', None) is not None: self.userdata_raw = base64.b64decode(instance['user_data']) else: self.userdata_raw = None self.ec2_ids = {} self.ec2_ids['instance-id'] = ec2utils.id_to_ec2_inst_id( instance['id']) self.ec2_ids['ami-id'] = ec2utils.glance_id_to_ec2_id(ctxt, instance['image_ref']) for image_type in ['kernel', 'ramdisk']: if self.instance.get('%s_id' % image_type): image_id = self.instance['%s_id' % image_type] ec2_image_type = ec2utils.image_type(image_type) ec2_id = ec2utils.glance_id_to_ec2_id(ctxt, image_id, ec2_image_type) self.ec2_ids['%s-id' % image_type] = ec2_id self.address = address # expose instance metadata. self.launch_metadata = {} for item in instance.get('metadata', []): self.launch_metadata[item['key']] = item['value'] self.uuid = instance.get('uuid') self.content = {} self.files = [] # get network info, and the rendered network template ctxt = context.get_admin_context() network_info = network.API().get_instance_nw_info(ctxt, instance) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = {"name": "network_config", 'content_path': "/%s/%s" % (CONTENT_DIR, key)} # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. Thats how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({'path': path, 'content_path': "/%s/%s" % (CONTENT_DIR, key)}) self.content[key] = contents
def __init__(self, instance, address=None, content=None, extra_md=None, conductor_api=None, network_info=None, vd_driver=None): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ if not content: content = [] ctxt = context.get_admin_context() # NOTE(danms): This should be removed after bp:compute-manager-objects if not isinstance(instance, instance_obj.Instance): instance = instance_obj.Instance._from_db_object( ctxt, instance_obj.Instance(), instance, expected_attrs=['metadata', 'system_metadata']) self.instance = instance self.extra_md = extra_md if conductor_api: capi = conductor_api else: capi = conductor.API() self.availability_zone = ec2utils.get_availability_zone_by_host( instance['host'], capi) self.security_groups = secgroup_obj.SecurityGroupList.get_by_instance( ctxt, instance) self.mappings = _format_instance_mapping(capi, ctxt, instance) if instance.get('user_data', None) is not None: self.userdata_raw = base64.b64decode(instance['user_data']) else: self.userdata_raw = None self.ec2_ids = capi.get_ec2_ids(ctxt, obj_base.obj_to_primitive(instance)) self.address = address # expose instance metadata. self.launch_metadata = utils.instance_meta(instance) self.password = password.extract_password(instance) self.uuid = instance.get('uuid') self.content = {} self.files = [] # get network info, and the rendered network template if network_info is None: network_info = network.API().get_instance_nw_info(ctxt, instance) self.ip_info = \ ec2utils.get_ip_info_for_instance_from_nw_info(network_info) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = { "name": "network_config", 'content_path': "/%s/%s" % (CONTENT_DIR, key) } # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. Thats how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({ 'path': path, 'content_path': "/%s/%s" % (CONTENT_DIR, key) }) self.content[key] = contents if vd_driver is None: vdclass = importutils.import_class(CONF.vendordata_driver) else: vdclass = vd_driver self.vddriver = vdclass(instance=instance, address=address, extra_md=extra_md, network_info=network_info)
def __init__(self, instance, address=None, content=None, extra_md=None, conductor_api=None, network_info=None, vd_driver=None): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ if not content: content = [] ctxt = context.get_admin_context() # The default value of mimeType is set to MIME_TYPE_TEXT_PLAIN self.set_mimetype(MIME_TYPE_TEXT_PLAIN) self.instance = instance self.extra_md = extra_md if conductor_api: capi = conductor_api else: capi = conductor.API() self.availability_zone = az.get_instance_availability_zone( ctxt, instance) self.security_groups = objects.SecurityGroupList.get_by_instance( ctxt, instance) self.mappings = _format_instance_mapping(ctxt, instance) if instance.user_data is not None: self.userdata_raw = base64.b64decode(instance.user_data) else: self.userdata_raw = None self.ec2_ids = capi.get_ec2_ids(ctxt, obj_base.obj_to_primitive(instance)) self.address = address # expose instance metadata. self.launch_metadata = utils.instance_meta(instance) self.password = password.extract_password(instance) self.uuid = instance.uuid self.content = {} self.files = [] # get network info, and the rendered network template if network_info is None: network_info = instance.info_cache.network_info self.ip_info = \ ec2utils.get_ip_info_for_instance_from_nw_info(network_info) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = { "name": "network_config", 'content_path': "/%s/%s" % (CONTENT_DIR, key) } # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. That's how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({ 'path': path, 'content_path': "/%s/%s" % (CONTENT_DIR, key) }) self.content[key] = contents if vd_driver is None: vdclass = importutils.import_class(CONF.vendordata_driver) else: vdclass = vd_driver self.vddriver = vdclass(instance=instance, address=address, extra_md=extra_md, network_info=network_info) self.route_configuration = None
def __init__(self, instance, address=None, content=None, extra_md=None, network_info=None, network_metadata=None, request_context=None): """Creation of this object should basically cover all time consuming collection. Methods after that should not cause time delays due to network operations or lengthy cpu operations. The user should then get a single instance and make multiple method calls on it. """ if not content: content = [] ctxt = context.get_admin_context() # NOTE(danms): Sanitize the instance to limit the amount of stuff # inside that may not pickle well (i.e. context). We also touch # some of the things we'll lazy load later to make sure we keep their # values in what we cache. instance.ec2_ids instance.keypairs instance.device_metadata instance.numa_topology instance = objects.Instance.obj_from_primitive( instance.obj_to_primitive()) # The default value of mimeType is set to MIME_TYPE_TEXT_PLAIN self.set_mimetype(MIME_TYPE_TEXT_PLAIN) self.instance = instance self.extra_md = extra_md self.availability_zone = instance.get('availability_zone') self.security_groups = security_group_api.get_instance_security_groups( ctxt, instance) self.mappings = _format_instance_mapping(ctxt, instance) if instance.user_data is not None: self.userdata_raw = base64.decode_as_bytes(instance.user_data) else: self.userdata_raw = None self.address = address # expose instance metadata. self.launch_metadata = utils.instance_meta(instance) self.password = password.extract_password(instance) self.uuid = instance.uuid self.content = {} self.files = [] # get network info, and the rendered network template if network_info is None: network_info = instance.info_cache.network_info # expose network metadata if network_metadata is None: self.network_metadata = netutils.get_network_metadata(network_info) else: self.network_metadata = network_metadata self.ip_info = netutils.get_ec2_ip_info(network_info) self.network_config = None cfg = netutils.get_injected_network_template(network_info) if cfg: key = "%04i" % len(self.content) self.content[key] = cfg self.network_config = { "name": "network_config", 'content_path': "/%s/%s" % (CONTENT_DIR, key) } # 'content' is passed in from the configdrive code in # nova/virt/libvirt/driver.py. That's how we get the injected files # (personalities) in. AFAIK they're not stored in the db at all, # so are not available later (web service metadata time). for (path, contents) in content: key = "%04i" % len(self.content) self.files.append({ 'path': path, 'content_path': "/%s/%s" % (CONTENT_DIR, key) }) self.content[key] = contents self.route_configuration = None # NOTE(mikal): the decision to not pass extra_md here like we # do to the StaticJSON driver is deliberate. extra_md will # contain the admin password for the instance, and we shouldn't # pass that to external services. self.vendordata_providers = { 'StaticJSON': vendordata_json.JsonFileVendorData(instance=instance, address=address, extra_md=extra_md, network_info=network_info), 'DynamicJSON': vendordata_dynamic.DynamicVendorData(instance=instance, address=address, network_info=network_info, context=request_context) }