def reboot(self, context, instance, network_info, reboot_type, block_device_info=None, bad_volumes_callback=None): pod_id = self.hyper.find_pod_id_by_uuid(instance["uuid"]) if not pod_id: return self._stop(pod_id, instance) try: network.teardown_network(pod_id, network_info) if network_info: self.unplug_vifs(instance, network_info) except Exception as e: LOG.warning(_('Cannot destroy the pod network' ' during reboot {0}').format(e), exc_info=True) return binds = self._get_key_binds(pod_id, instance) dns = self._extract_dns_entries(network_info) self.hyper.start(pod_id, binds=binds, dns=dns) try: if network_info: self.plug_vifs(instance, network_info) #self._attach_vifs(instance, network_info) #todo: remove? except Exception as e: LOG.warning(_('Cannot setup network on reboot: {0}'), e, exc_info=True) return
def plug(self, instance, vif): vif_type = vif['type'] LOG.debug('plug vif_type=%(vif_type)s instance=%(instance)s ' 'vif=%(vif)s', {'vif_type': vif_type, 'instance': instance, 'vif': vif}) if vif_type is None: raise exception.NovaException( _("vif_type parameter must be present " "for this vif_driver implementation")) if vif_type == network_model.VIF_TYPE_BRIDGE: self.plug_bridge(instance, vif) # elif vif_type == network_model.VIF_TYPE_OVS: # if self.ovs_hybrid_required(vif): # self.plug_ovs_hybrid(instance, vif) # else: # self.plug_ovs(instance, vif) # elif vif_type == network_model.VIF_TYPE_MIDONET: # self.plug_midonet(instance, vif) # elif vif_type == network_model.VIF_TYPE_IOVISOR: # self.plug_iovisor(instance, vif) else: raise exception.NovaException( _("Unexpected vif_type=%s") % vif_type)
def unpause(self, instance): """Unpause paused VM instance. :param instance: nova.objects.instance.Instance """ try: pod_id = self.hyper.find_pod_id_by_uuid(instance["uuid"]) if not self.hyper.unpause(pod_id): raise exception.NovaException except Exception as e: LOG.debug(_('Error unpause pod: %s'), e, instance=instance, exc_info=True) msg = _('Cannot unpause pod: {0}') raise exception.NovaException(msg.format(e), instance_id=instance['name'])
def _get_image_name(self, context, instance, image): fmt = image['container_format'] if fmt != 'docker': msg = _('Image container format not supported ({0})') raise exception.InstanceDeployFailure(msg.format(fmt), instance_id=instance['name']) return image['name']
def get_available_resource(self, nodename): if not hasattr(self, '_nodename'): self._nodename = nodename if nodename != self._nodename: LOG.error(_('Hostname has changed from %(old)s to %(new)s. ' 'A restart is required to take effect.' ), {'old': self._nodename, 'new': nodename}) memory = hostinfo.get_memory_usage() disk = hostinfo.get_disk_usage() cur_hv_type = hv_type.HYPER stats = { 'vcpus': hostinfo.get_total_vcpus(), 'vcpus_used': hostinfo.get_vcpus_used(self.list_instances(True)), 'memory_mb': memory['total'] / units.Mi, 'memory_mb_used': memory['used'] / units.Mi, 'local_gb': disk['total'] / units.Gi, 'local_gb_used': disk['used'] / units.Gi, 'disk_available_least': disk['available'] / units.Gi, 'hypervisor_type': 'hyper', #todo: check - seems OK 'hypervisor_version': utils.convert_version_to_int('1.0'), 'hypervisor_hostname': self._nodename, 'cpu_info': '?', #todo? - maybe not 'numa_topology': None, 'supported_instances': jsonutils.dumps([ (arch.I686, cur_hv_type, vm_mode.EXE), (arch.X86_64, cur_hv_type, vm_mode.EXE) ]) } return stats
def find_fixed_ip(instance, network_info): for subnet in network_info["subnets"]: netmask = subnet["cidr"].split("/")[1] for ip in subnet["ips"]: if ip["type"] == "fixed" and ip["address"]: return ip["address"] + "/" + netmask raise exception.InstanceDeployFailure(_("Cannot find fixed ip"), instance_id=instance["uuid"])
def _start_pod(self, pod_id, instance, network_info=None): binds = self._get_key_binds(pod_id, instance) dns = self._extract_dns_entries(network_info) self.hyper.start(pod_id, binds=binds, dns=dns, privileged=CONF.hyper.privileged) if not network_info: return try: self.plug_vifs(instance, network_info) #self._attach_vifs(instance, network_info) #todo? except Exception as e: LOG.warning(_('Cannot setup network: %s'), e, instance=instance, exc_info=True) msg = _('Cannot setup network: {0}') self.hyper.kill(pod_id) self.hyper.remove_pod(pod_id, force=True) raise exception.InstanceDeployFailure(msg.format(e), instance_id=instance['name'])
def _pull_missing_image(self, context, image_meta, instance): msg = 'Image name "%s" does not exist, fetching it...' LOG.debug(msg, image_meta['name']) shared_directory = CONF.hyper.shared_directory #todo: check image location if (shared_directory and os.path.exists(os.path.join(shared_directory, image_meta['id']))): try: self.hyper.load_image( self._encode_utf8(image_meta['name']), os.path.join(shared_directory, image_meta['id'])) return self.hyper.inspect_image( self._encode_utf8(image_meta['name'])) except Exception as e: # If failed to load image from shared_directory, continue # to download the image from glance then load. LOG.warning(_('Cannot load repository file from shared ' 'directory: %s'), e, instance=instance, exc_info=True) snapshot_directory = CONF.hyper.snapshots_directory fileutils.ensure_tree(snapshot_directory) with utils.tempdir(dir=snapshot_directory) as tmpdir: try: out_path = os.path.join(tmpdir, uuid.uuid4().hex) images.fetch(context, image_meta['id'], out_path, instance['user_id'], instance['project_id']) self.hyper.load_image( self._encode_utf8(image_meta['name']), out_path ) except Exception as e: LOG.warning(_('Cannot load repository file: %s'), e, instance=instance, exc_info=True) msg = _('Cannot load repository file: {0}') raise exception.NovaException(msg.format(e), instance_id=image_meta['name']) return self.hyper.inspect_image(self._encode_utf8(image_meta['name']))
def teardown_network(pod_id, network_info): bridge = "" try: done = [] for vif in network_info: bridge = vif["network"]["bridge"] if bridge not in done: utils.execute("ip", "link", "delete", bridge, run_as_root=True) done.append(bridge) except processutils.ProcessExecutionError: LOG.warning(_("Cannot remove bridge, id: %s, pod_id: %s"), bridge, pod_id)
def power_on(self, context, instance, network_info, block_device_info=None): pod_id = self.hyper.find_pod_id_by_uuid(instance["uuid"]) if not pod_id: return binds = self._get_key_binds(pod_id, instance) dns = self._extract_dns_entries(network_info) self.hyper.start(pod_id, binds=binds, dns=dns) if not network_info: return try: self.plug_vifs(instance, network_info) #self._attach_vifs(instance, network_info) #todo: remove? except Exception as e: LOG.debug(_('Cannot setup network: %s'), e, instance=instance, exc_info=True) msg = _('Cannot setup network: {0}') self.hyper.kill(pod_id) self.hyper.remove_pod(pod_id, force=True) raise exception.InstanceDeployFailure(msg.format(e), instance_id=instance['name'])
def plug_bridge(self, instance, vif): if_local_name = 'tap%s' % vif['id'][:11] if_remote_name = 'ns%s' % vif['id'][:11] bridge = vif['network']['bridge'] gateway = network.find_gateway(instance, vif['network']) vlan = vif.get('vlan') if vlan is not None: iface = (CONF.vlan_interface or vif['network'].get_meta('bridge_interface')) linux_net.LinuxBridgeInterfaceDriver.ensure_vlan_bridge( vlan, bridge, iface, net_attrs=vif, mtu=vif.get('mtu')) iface = 'vlan%s' % vlan else: iface = (CONF.flat_interface or vif['network'].get_meta('bridge_interface')) LOG.debug('Ensuring bridge for %s - %s' % (iface, bridge)) linux_net.LinuxBridgeInterfaceDriver.ensure_bridge( bridge, iface, net_attrs=vif, gateway=gateway) # Device already exists so return. if linux_net.device_exists(if_local_name): return undo_mgr = utils.UndoManager() try: mac_addr = vif["network"]["mac_addr"] utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) # NOTE(samalba): Deleting the interface will delete all # associated resources (remove from the bridge, its pair, etc...) utils.execute('ip', 'link', 'set', if_local_name, 'address', mac_addr, run_as_root=True) utils.execute('brctl', 'addif', bridge, if_local_name, run_as_root=True) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) except Exception: LOG.exception("Failed to configure network") msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def spawn(self, context, instance, image_meta, injected_files, admin_password, network_info=None, block_device_info=None, flavor=None): image_name = self._get_image_name(context, instance, image_meta) args = { 'hostname': instance['name'], 'mem_limit': self._get_memory_limit_mbytes(instance), 'cpu_shares': self._get_cpu_shares(instance), 'network_disabled': False, } try: image = self.hyper.inspect_image(self._encode_utf8(image_name)) except errors.APIError: image = None if not image: image = self._pull_missing_image(context, image_meta, instance) # Glance command-line overrides any set in the Docker image #todo: adapt command support if (image_meta and image_meta.get('properties', {}).get('os_command_line')): args['command'] = image_meta['properties'].get('os_command_line') if 'metadata' in instance: args['environment'] = nova_utils.instance_meta(instance) if CONF.hyper.inject_key and instance.get('key_data'): args["sshdir"] = os.path.join(CONF.instances_path, instance["uuid"], '.ssh') if network_info: for vif in network_info: vif["network"]["mac_addr"] = self.vif_driver.fe_random_mac() vif["network"]["if_local_name"] = 'tap%s' % vif['id'][:11] vif["network"]["if_remote_name"] = 'ns%s' % vif['id'][:11] pod_id = self._create_pod(instance, image_name, network_info, args).get("ID") if not pod_id: raise exception.InstanceDeployFailure( _('Cannot create pod'), instance_id=instance['name']) self._start_pod(pod_id, instance, network_info)
def init_host(self, host): if self._is_daemon_running() is False: raise exception.NovaException( _('Hyper daemon is not running or is not reachable' ' (check the rights on /var/run/hyper.sock)'))
def find_gateway(instance, network_info): for subnet in network_info["subnets"]: return subnet["gateway"]["address"] raise exception.InstanceDeployFailure(_("Cannot find gateway"), instance_id=instance["uuid"])