def plug(self, network_id, port_id, device_name, mac_address, bridge=None, namespace=None, prefix=None): """This method is called by the Dhcp agent or by the L3 agent when a new network is created """ if not ip_lib.device_exists(device_name, self.root_helper, namespace=namespace): ip = ip_lib.IPWrapper(self.root_helper) tap_name = device_name.replace(prefix or 'tap', 'tap') # Create ns_dev in a namespace if one is configured. root_dev, ns_dev = ip.add_veth(tap_name, device_name, namespace2=namespace) ns_dev.link.set_address(mac_address) # Add an interface created by ovs to the namespace. namespace_obj = ip.ensure_namespace(namespace) namespace_obj.add_device_to_namespace(ns_dev) ns_dev.link.set_up() root_dev.link.set_up() cmd = ['mm-ctl', '--bind-port', port_id, device_name] utils.execute(cmd, self.root_helper) else: LOG.info(_("Device %s already exists"), device_name)
def disable(self): pid = self.pid if self.active: cmd = ['kill', '-9', pid] utils.execute(cmd, self.root_helper) elif pid: LOG.debug(_('Process for %(uuid)s pid %(pid)d is stale, ignoring ' 'command'), {'uuid': self.uuid, 'pid': pid}) else: LOG.debug(_('No process started for %s'), self.uuid)
def kill_pids_in_file(root_helper, pid_path): if os.path.exists(pid_path): with open(pid_path, 'r') as pids: for pid in pids: pid = pid.strip() try: utils.execute(['kill', '-9', pid], root_helper) except RuntimeError: LOG.exception( _('Unable to kill haproxy process: %s'), pid )
def _ovs_add_port(self, bridge, device_name, port_id, mac_address, internal=True): cmd = ['ovs-vsctl', '--', '--if-exists', 'del-port', device_name, '--', 'add-port', bridge, device_name] if internal: cmd += ['--', 'set', 'Interface', device_name, 'type=internal'] cmd += ['--', 'set', 'Interface', device_name, 'external-ids:iface-id=%s' % port_id, '--', 'set', 'Interface', device_name, 'external-ids:iface-status=active', '--', 'set', 'Interface', device_name, 'external-ids:attached-mac=%s' % mac_address] utils.execute(cmd, self.root_helper)
def _kill_process(self, pid): try: # A process started by a root helper will be running as # root and need to be killed via the same helper. utils.execute(['kill', '-9', pid], root_helper=self.root_helper) except Exception as ex: stale_pid = (isinstance(ex, RuntimeError) and 'No such process' in str(ex)) if not stale_pid: LOG.exception(_('An error occurred while killing [%s].'), self.cmd) return False return True
def unplug(self, device_name, bridge=None, namespace=None, prefix=None): """Unplug the interface.""" tap_name = self._get_tap_name(device_name, prefix) try: cmd = ['ivs-ctl', 'del-port', tap_name] utils.execute(cmd, self.root_helper) device = ip_lib.IPDevice(device_name, self.root_helper, namespace) device.link.delete() LOG.debug(_("Unplugged interface '%s'"), device_name) except RuntimeError: LOG.error(_("Failed unplugging interface '%s'"), device_name)
def get_bridge_for_iface(root_helper, iface): args = ["ovs-vsctl", "--timeout=%d" % cfg.CONF.ovs_vsctl_timeout, "iface-to-br", iface] try: return utils.execute(args, root_helper=root_helper).strip() except Exception: LOG.exception(_("Interface %s not found."), iface) return None
def get_bridge_external_bridge_id(root_helper, bridge): args = ["ovs-vsctl", "--timeout=2", "br-get-external-id", bridge, "bridge-id"] try: return utils.execute(args, root_helper=root_helper).strip() except Exception: LOG.exception(_("Bridge %s not found."), bridge) return None
def run_ofctl(self, cmd, args, process_input=None): full_args = ["ovs-ofctl", cmd, self.br_name] + args try: return utils.execute(full_args, root_helper=self.root_helper, process_input=process_input) except Exception as e: LOG.error(_("Unable to execute %(cmd)s. Exception: %(exception)s"), {'cmd': full_args, 'exception': e})
def get_bridges(root_helper): args = ["ovs-vsctl", "--timeout=%d" % cfg.CONF.ovs_vsctl_timeout, "list-br"] try: return utils.execute(args, root_helper=root_helper).strip().split("\n") except Exception as e: with excutils.save_and_reraise_exception(): LOG.exception(_("Unable to retrieve bridges. Exception: %s"), e)
def get_installed_ovs_usr_version(root_helper): args = ["ovs-vsctl", "--version"] try: cmd = utils.execute(args, root_helper=root_helper) ver = re.findall("\d+\.\d+", cmd)[0] return ver except Exception: LOG.exception(_("Unable to retrieve OVS userspace version."))
def _execute(cls, options, command, args, root_helper=None, namespace=None): opt_list = ['-%s' % o for o in options] if namespace: ip_cmd = ['ip', 'netns', 'exec', namespace, 'ip'] else: ip_cmd = ['ip'] return utils.execute(ip_cmd + opt_list + [command] + list(args), root_helper=root_helper)
def get_installed_kernel_version(): args = ["uname", "-r"] try: cmd = utils.execute(args) for line in cmd.split('\n'): ver = re.findall("\d+\.\d+\.\d+", line) return ver[0] except Exception: LOG.exception(_("Unable to retrieve installed Linux kernel version."))
def get_xapi_iface_id(self, xs_vif_uuid): args = ["xe", "vif-param-get", "param-name=other-config", "param-key=nicira-iface-id", "uuid=%s" % xs_vif_uuid] try: return utils.execute(args, root_helper=self.root_helper).strip() except Exception as e: with excutils.save_and_reraise_exception(): LOG.error(_("Unable to execute %(cmd)s. " "Exception: %(exception)s"), {'cmd': args, 'exception': e})
def get_installed_ovs_klm_version(): args = ["modinfo", "openvswitch"] try: cmd = utils.execute(args) for line in cmd.split('\n'): if 'version: ' in line and not 'srcversion' in line: ver = re.findall("\d+\.\d+", line) return ver[0] except Exception: LOG.exception(_("Unable to retrieve OVS kernel module version."))
def run_vsctl(self, args, check_error=False): full_args = ["ovs-vsctl", "--timeout=%d" % self.vsctl_timeout] + args try: return utils.execute(full_args, root_helper=self.root_helper) except Exception as e: with excutils.save_and_reraise_exception() as ctxt: LOG.error(_("Unable to execute %(cmd)s. " "Exception: %(exception)s"), {'cmd': full_args, 'exception': e}) if not check_error: ctxt.reraise = False
def execute(self, cmds, addl_env={}, check_exit_code=True): if not self._parent.root_helper: raise exceptions.SudoRequired() ns_params = [] if self._parent.namespace: ns_params = ['ip', 'netns', 'exec', self._parent.namespace] env_params = [] if addl_env: env_params = (['env'] + ['%s=%s' % pair for pair in addl_env.items()]) return utils.execute( ns_params + env_params + list(cmds), root_helper=self._parent.root_helper, check_exit_code=check_exit_code)
def _ivs_add_port(self, device_name, port_id, mac_address): cmd = ['ivs-ctl', 'add-port', device_name] utils.execute(cmd, self.root_helper)
def iproute_arg_supported(command, arg, root_helper=None): command += ['help'] stdout, stderr = utils.execute(command, root_helper=root_helper, check_exit_code=False, return_stderr=True) return any(arg in line for line in stderr.split('\n'))