def create_vifs(self, instance, networks=None): """ Creates vifs for an instance """ vm_opaque_ref = self._get_vm_opaque_ref(instance.id) logging.debug(_("creating vif(s) for vm: |%s|"), vm_opaque_ref) if networks is None: networks = db.network_get_all_by_instance(admin_context, instance['id']) # TODO(tr3buchet) - remove comment in multi-nic # this bit here about creating the vifs will be updated # in multi-nic to handle multiple IPs on the same network # and multiple networks # for now it works as there is only one of each for network in networks: bridge = network['bridge'] network_ref = \ NetworkHelper.find_network_with_bridge(self._session, bridge) if network_ref: try: device = "1" if instance._rescue else "0" except AttributeError: device = "0" VMHelper.create_vif( self._session, vm_opaque_ref, network_ref, instance.mac_address, device)
def setup_compute_network(self, context, instance_id): """Sets up matching networks for compute hosts. this code is run on and by the compute host, not on network hosts """ networks = db.network_get_all_by_instance(context, instance_id) for network in networks: self.driver.ensure_bridge(network['bridge'], network['bridge_interface'])
def inject_network_info(self, instance): """ Generate the network info and make calls to place it into the xenstore and the xenstore param list """ # TODO(tr3buchet) - remove comment in multi-nic # I've decided to go ahead and consider multiple IPs and networks # at this stage even though they aren't implemented because these will # be needed for multi-nic and there was no sense writing it for single # network/single IP and then having to turn around and re-write it vm_opaque_ref = self._get_vm_opaque_ref(instance.id) logging.debug(_("injecting network info to xenstore for vm: |%s|"), vm_opaque_ref) admin_context = context.get_admin_context() IPs = db.fixed_ip_get_all_by_instance(admin_context, instance['id']) networks = db.network_get_all_by_instance(admin_context, instance['id']) for network in networks: network_IPs = [ip for ip in IPs if ip.network_id == network.id] def ip_dict(ip): return { "ip": ip.address, "netmask": network["netmask"], "enabled": "1"} def ip6_dict(ip6): return { "ip": ip6.addressV6, "netmask": ip6.netmaskV6, "gateway": ip6.gatewayV6, "enabled": "1"} mac_id = instance.mac_address.replace(':', '') location = 'vm-data/networking/%s' % mac_id mapping = { 'label': network['label'], 'gateway': network['gateway'], 'mac': instance.mac_address, 'dns': [network['dns']], 'ips': [ip_dict(ip) for ip in network_IPs], 'ip6s': [ip6_dict(ip) for ip in network_IPs]} self.write_to_param_xenstore(vm_opaque_ref, {location: mapping}) try: self.write_to_xenstore(vm_opaque_ref, location, mapping['location']) except KeyError: # catch KeyError for domid if instance isn't running pass return networks
def get_network_info(instance): # TODO(tr3buchet): this function needs to go away! network info # MUST be passed down from compute # TODO(adiantum) If we will keep this function # we should cache network_info admin_context = context.get_admin_context() fixed_ips = db.fixed_ip_get_by_instance(admin_context, instance['id']) vifs = db.virtual_interface_get_by_instance(admin_context, instance['id']) networks = db.network_get_all_by_instance(admin_context, instance['id']) flavor = db.instance_type_get_by_id(admin_context, instance['instance_type_id']) network_info = [] for vif in vifs: network = vif['network'] # determine which of the instance's IPs belong to this network network_ips = [fixed_ip['address'] for fixed_ip in fixed_ips if fixed_ip['network_id'] == network['id']] def ip_dict(ip): return { 'ip': ip, 'netmask': network['netmask'], 'enabled': '1'} def ip6_dict(): prefix = network['cidr_v6'] mac = vif['address'] project_id = instance['project_id'] return { 'ip': ipv6.to_global(prefix, mac, project_id), 'netmask': network['netmask_v6'], 'enabled': '1'} mapping = { 'label': network['label'], 'gateway': network['gateway'], 'broadcast': network['broadcast'], 'mac': vif['address'], 'rxtx_cap': flavor['rxtx_cap'], 'dns': [network['dns']], 'ips': [ip_dict(ip) for ip in network_ips]} if FLAGS.use_ipv6: mapping['ip6s'] = [ip6_dict()] mapping['gateway6'] = network['gateway_v6'] network_info.append((network, mapping)) return network_info
def get_network_info(instance): # TODO(adiantum) If we will keep this function # we should cache network_info admin_context = context.get_admin_context() ip_addresses = db.fixed_ip_get_all_by_instance(admin_context, instance['id']) networks = db.network_get_all_by_instance(admin_context, instance['id']) flavor = db.instance_type_get_by_id(admin_context, instance['instance_type_id']) network_info = [] for network in networks: network_ips = [ip for ip in ip_addresses if ip['network_id'] == network['id']] def ip_dict(ip): return { 'ip': ip['address'], 'netmask': network['netmask'], 'enabled': '1'} def ip6_dict(): prefix = network['cidr_v6'] mac = instance['mac_address'] project_id = instance['project_id'] return { 'ip': ipv6.to_global(prefix, mac, project_id), 'netmask': network['netmask_v6'], 'enabled': '1'} mapping = { 'label': network['label'], 'gateway': network['gateway'], 'broadcast': network['broadcast'], 'mac': instance['mac_address'], 'rxtx_cap': flavor['rxtx_cap'], 'dns': [network['dns']], 'ips': [ip_dict(ip) for ip in network_ips]} if FLAGS.use_ipv6: mapping['ip6s'] = [ip6_dict()] mapping['gateway6'] = network['gateway_v6'] network_info.append((network, mapping)) return network_info
def _get_network_info(self, instance): """creates network info list for instance""" admin_context = context.get_admin_context() IPs = db.fixed_ip_get_all_by_instance(admin_context, instance['id']) networks = db.network_get_all_by_instance(admin_context, instance['id']) flavor = db.instance_type_get_by_name(admin_context, instance['instance_type']) network_info = [] for network in networks: network_IPs = [ip for ip in IPs if ip.network_id == network.id] def ip_dict(ip): return { "ip": ip.address, "netmask": network["netmask"], "enabled": "1" } def ip6_dict(ip6): return { "ip": utils.to_global_ipv6(network['cidr_v6'], instance['mac_address']), "netmask": network['netmask_v6'], "gateway": network['gateway_v6'], "enabled": "1" } info = { 'label': network['label'], 'gateway': network['gateway'], 'mac': instance.mac_address, 'rxtx_cap': flavor['rxtx_cap'], 'dns': [network['dns']], 'ips': [ip_dict(ip) for ip in network_IPs] } if network['cidr_v6']: info['ip6s'] = [ip6_dict(ip) for ip in network_IPs] network_info.append((network, info)) return network_info
def _get_network_info(self, instance): """Creates network info list for instance.""" admin_context = context.get_admin_context() ips = db.fixed_ip_get_all_by_instance(admin_context, instance['id']) networks = db.network_get_all_by_instance(admin_context, instance['id']) inst_type = db.instance_type_get_by_id(admin_context, instance['instance_type_id']) network_info = [] for network in networks: network_ips = [ip for ip in ips if ip.network_id == network.id] def ip_dict(ip): return { "ip": ip.address, "netmask": network["netmask"], "enabled": "1"} def ip6_dict(): return { "ip": ipv6.to_global(network['cidr_v6'], instance['mac_address'], instance['project_id']), "netmask": network['netmask_v6'], "enabled": "1"} info = { 'label': network['label'], 'gateway': network['gateway'], 'broadcast': network['broadcast'], 'mac': instance.mac_address, 'rxtx_cap': inst_type['rxtx_cap'], 'dns': [network['dns']], 'ips': [ip_dict(ip) for ip in network_ips]} if network['cidr_v6']: info['ip6s'] = [ip6_dict()] if network['gateway_v6']: info['gateway6'] = network['gateway_v6'] network_info.append((network, info)) return network_info
def _get_network_info(self, instance): """creates network info list for instance""" admin_context = context.get_admin_context() IPs = db.fixed_ip_get_all_by_instance(admin_context, instance['id']) networks = db.network_get_all_by_instance(admin_context, instance['id']) flavor = db.instance_type_get_by_name(admin_context, instance['instance_type']) network_info = [] for network in networks: network_IPs = [ip for ip in IPs if ip.network_id == network.id] def ip_dict(ip): return { "ip": ip.address, "netmask": network["netmask"], "enabled": "1"} def ip6_dict(ip6): return { "ip": utils.to_global_ipv6(network['cidr_v6'], instance['mac_address']), "netmask": network['netmask_v6'], "gateway": network['gateway_v6'], "enabled": "1"} info = { 'label': network['label'], 'gateway': network['gateway'], 'mac': instance.mac_address, 'rxtx_cap': flavor['rxtx_cap'], 'dns': [network['dns']], 'ips': [ip_dict(ip) for ip in network_IPs]} if network['cidr_v6']: info['ip6s'] = [ip6_dict(ip) for ip in network_IPs] network_info.append((network, info)) return network_info
def spawn(self, instance): """Create VM instance""" vm = VMHelper.lookup(self._session, instance.name) if vm is not None: raise exception.Duplicate(_('Attempted to create' ' non-unique name %s') % instance.name) #ensure enough free memory is available if not VMHelper.ensure_free_mem(self._session, instance): name = instance['name'] LOG.exception(_('instance %(name)s: not enough free memory') % locals()) db.instance_set_state(context.get_admin_context(), instance['id'], power_state.SHUTDOWN) return user = AuthManager().get_user(instance.user_id) project = AuthManager().get_project(instance.project_id) #if kernel is not present we must download a raw disk if instance.kernel_id: disk_image_type = ImageType.DISK else: disk_image_type = ImageType.DISK_RAW vdi_uuid = VMHelper.fetch_image(self._session, instance.id, instance.image_id, user, project, disk_image_type) vdi_ref = self._session.call_xenapi('VDI.get_by_uuid', vdi_uuid) #Have a look at the VDI and see if it has a PV kernel pv_kernel = False if not instance.kernel_id: pv_kernel = VMHelper.lookup_image(self._session, instance.id, vdi_ref) kernel = None if instance.kernel_id: kernel = VMHelper.fetch_image(self._session, instance.id, instance.kernel_id, user, project, ImageType.KERNEL_RAMDISK) ramdisk = None if instance.ramdisk_id: ramdisk = VMHelper.fetch_image(self._session, instance.id, instance.ramdisk_id, user, project, ImageType.KERNEL_RAMDISK) vm_ref = VMHelper.create_vm(self._session, instance, kernel, ramdisk, pv_kernel) VMHelper.create_vbd(self._session, vm_ref, vdi_ref, 0, True) # write network info admin_context = context.get_admin_context() # TODO(tr3buchet) - remove comment in multi-nic # I've decided to go ahead and consider multiple IPs and networks # at this stage even though they aren't implemented because these will # be needed for multi-nic and there was no sense writing it for single # network/single IP and then having to turn around and re-write it IPs = db.fixed_ip_get_all_by_instance(admin_context, instance['id']) for network in db.network_get_all_by_instance(admin_context, instance['id']): network_IPs = [ip for ip in IPs if ip.network_id == network.id] def ip_dict(ip): return {'netmask': network['netmask'], 'enabled': '1', 'ip': ip.address} mac_id = instance.mac_address.replace(':', '') location = 'vm-data/networking/%s' % mac_id mapping = {'label': network['label'], 'gateway': network['gateway'], 'mac': instance.mac_address, 'dns': [network['dns']], 'ips': [ip_dict(ip) for ip in network_IPs]} self.write_to_param_xenstore(vm_ref, {location: mapping}) # TODO(tr3buchet) - remove comment in multi-nic # this bit here about creating the vifs will be updated # in multi-nic to handle multiple IPs on the same network # and multiple networks # for now it works as there is only one of each bridge = network['bridge'] network_ref = \ NetworkHelper.find_network_with_bridge(self._session, bridge) if network_ref: VMHelper.create_vif(self._session, vm_ref, network_ref, instance.mac_address) LOG.debug(_('Starting VM %s...'), vm_ref) self._session.call_xenapi('VM.start', vm_ref, False, False) instance_name = instance.name LOG.info(_('Spawning VM %(instance_name)s created %(vm_ref)s.') % locals()) def _inject_onset_files(): onset_files = instance.onset_files if onset_files: # Check if this is a JSON-encoded string and convert if needed. if isinstance(onset_files, basestring): try: onset_files = json.loads(onset_files) except ValueError: LOG.exception(_("Invalid value for onset_files: '%s'") % onset_files) onset_files = [] # Inject any files, if specified for path, contents in instance.onset_files: LOG.debug(_("Injecting file path: '%s'") % path) self.inject_file(instance, path, contents) # NOTE(armando): Do we really need to do this in virt? # NOTE(tr3buchet): not sure but wherever we do it, we need to call # reset_network afterwards timer = utils.LoopingCall(f=None) def _wait_for_boot(): try: state = self.get_info(instance['name'])['state'] db.instance_set_state(context.get_admin_context(), instance['id'], state) if state == power_state.RUNNING: LOG.debug(_('Instance %s: booted'), instance['name']) timer.stop() _inject_onset_files() return True except Exception, exc: LOG.warn(exc) LOG.exception(_('instance %s: failed to boot'), instance['name']) db.instance_set_state(context.get_admin_context(), instance['id'], power_state.SHUTDOWN) timer.stop() return False