def _is_pingable(self, mgmt_ip="", count=5, timeout=1, interval='0.2', **kwargs): """Checks whether an IP address is reachable by pinging. Use linux utils to execute the ping (ICMP ECHO) command. Sends 5 packets with an interval of 0.2 seconds and timeout of 1 seconds. Runtime error implies unreachability else IP is pingable. :param ip: IP to check :return: bool - True or string 'failure' depending on pingability. """ cmd_ping = 'ping' if netaddr.valid_ipv6(mgmt_ip): cmd_ping = 'ping6' ping_cmd = [cmd_ping, '-c', count, '-W', timeout, '-i', interval, mgmt_ip] try: linux_utils.execute(ping_cmd, check_exit_code=True) return True except RuntimeError: LOG.warning("Cannot ping ip address: %s", mgmt_ip) return 'failure'
def plug(self, network_id, port_id, device_name, mac_address, bridge=None, namespace=None, prefix=None): """Driver for creating an interface. 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 _is_pingable(self, mgmt_ip="", count=5, timeout=1, interval='0.2', **kwargs): """Checks whether an IP address is reachable by pinging. Use linux utils to execute the ping (ICMP ECHO) command. Sends 5 packets with an interval of 0.2 seconds and timeout of 1 seconds. Runtime error implies unreachability else IP is pingable. :param ip: IP to check :return: bool - True or string 'failure' depending on pingability. """ cmd_ping = 'ping' if netaddr.valid_ipv6(mgmt_ip): cmd_ping = 'ping6' ping_cmd = [ cmd_ping, '-c', count, '-W', timeout, '-i', interval, mgmt_ip ] try: linux_utils.execute(ping_cmd, check_exit_code=True) return True except RuntimeError: LOG.warning("Cannot ping ip address: %s", mgmt_ip) return 'failure'
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 _config_service(self, mgmt_ip_address, service, config): user = cfg.CONF.openwrt.user password = cfg.CONF.openwrt.password cmd = ["sshpass", "-p", "%s" % password, "ssh", "-o", "StrictHostKeyChecking=no", "%s@%s" % (user, mgmt_ip_address), "uci import %s; /etc/init.d/%s restart" % (service, service)] utils.execute(cmd, process_input=config)
def _config_service(self, mgmt_ip_address, service, config): user = cfg.CONF.msactivator.user password = cfg.CONF.msactivator.password cmd = ["sshpass", "-p", "%s" % password, "ssh", "-o", "StrictHostKeyChecking=no", "%s@%s" % (user, mgmt_ip_address), "uci import %s; /etc/init.d/%s restart" % (service, service)] utils.execute(cmd, process_input=config)
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 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_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 _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 test_encode_process_input(self): str_idata = "%s\n" % self.test_file[:-1] str_odata = "%s\n" % self.test_file if six.PY3: bytes_idata = str_idata.encode(encoding='utf-8') bytes_odata = str_odata.encode(encoding='utf-8') self.mock_popen.return_value = [bytes_odata, b''] result = utils.execute(['cat'], process_input=str_idata) self.mock_popen.assert_called_once_with(bytes_idata) else: self.mock_popen.return_value = [str_odata, ''] result = utils.execute(['cat'], process_input=str_idata) self.mock_popen.assert_called_once_with(str_idata) self.assertEqual(str_odata, result)
def test_return_code_log_error_change_locale(self): ja_output = 'std_out in Japanese' ja_error = 'std_err in Japanese' ja_message_out = oslo_i18n._message.Message(ja_output) ja_message_err = oslo_i18n._message.Message(ja_error) ja_translate_out = oslo_i18n._translate.translate(ja_message_out, 'ja') ja_translate_err = oslo_i18n._translate.translate(ja_message_err, 'ja') self.mock_popen.return_value = (ja_translate_out, ja_translate_err) self.process.return_value.returncode = 1 with mock.patch.object(utils, 'LOG') as log: utils.execute(['ls'], check_exit_code=False) self.assertIn(ja_translate_out, str(log.error.call_args_list)) self.assertIn(ja_translate_err, str(log.error.call_args_list))
def vim_status(self, auth_url): """Checks the VIM health status""" vim_ip = auth_url.split("//")[-1].split(":")[0].split("/")[0] ping_cmd = ['ping', '-c', cfg.CONF.vim_monitor.count, '-W', cfg.CONF.vim_monitor.timeout, '-i', cfg.CONF.vim_monitor.interval, vim_ip] try: linux_utils.execute(ping_cmd, check_exit_code=True) return True except RuntimeError: LOG.warning(_LW("Cannot ping ip address: %s"), vim_ip) return False
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'))
def test_encode_process_input(self): bytes_idata = helpers.compact_byte("%s\n" % self.test_file[:-1]) bytes_odata = helpers.compact_byte("%s\n" % self.test_file) self.mock_popen.return_value = [bytes_odata, b''] result = utils.execute(['cat'], process_input=bytes_idata) self.mock_popen.assert_called_once_with(bytes_idata) self.assertEqual(bytes_odata, result)
def runTest(self): try: result = utils.execute(["bash", self.test_file, 'arg'], self.root_helper) self.assertEqual("0 arg Now is the time for all good men to \ come to the aid of their party.", result) except Exception: LOG.exception("Losing in rootwrap test")
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 runTest(self): try: result = utils.execute(["bash", self.test_file, 'arg'], self.root_helper) self.assertEqual( result, "0 arg Now is the time for all good men to \ come to the aid of their party.") except Exception: LOG.exception("Losing in rootwrap test")
def _ping(self): cmd_ping = 'ping' if netaddr.valid_ipv6(self.targetip): cmd_ping = 'ping6' ping_cmd = [ cmd_ping, '-c', self.count, '-W', self.timeout, '-i', self.interval, self.targetip ] try: # NOTE(gongysh) since it is called in a loop, the debug log # should be disabled to avoid eating up mistral executor. linux_utils.execute(ping_cmd, check_exit_code=True, debuglog=False) return 'REACHABLE' except RuntimeError: LOG.warning(("Cannot ping ip address: %s"), self.targetip) return 'UNREACHABLE'
def _ping(self): cmd_ping = 'ping' if netaddr.valid_ipv6(self.targetip): cmd_ping = 'ping6' ping_cmd = [cmd_ping, '-c', self.count, '-W', self.timeout, '-i', self.interval, self.targetip] try: # NOTE(gongysh) since it is called in a loop, the debug log # should be disabled to avoid eating up mistral executor. linux_utils.execute(ping_cmd, check_exit_code=True, debuglog=False) return 'REACHABLE' except RuntimeError: LOG.warning(("Cannot ping ip address: %s"), self.targetip) return 'UNREACHABLE'
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 _is_pingable(ip): """Checks whether an IP address is reachable by pinging. Use linux utils to execute the ping (ICMP ECHO) command. Sends 5 packets with an interval of 0.2 seconds and timeout of 1 seconds. Runtime error implies unreachability else IP is pingable. :param ip: IP to check :return: bool - True or False depending on pingability. """ ping_cmd = ['ping', '-c', '5', '-W', '1', '-i', '0.2', ip] try: linux_utils.execute(ping_cmd, check_exit_code=True) return True except RuntimeError: LOG.warning(_LW("Cannot ping ip address: %s"), ip) return 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 _is_pingable(self, mgmt_ip="", count=None, timeout=None, interval=None, retry=None, **kwargs): """Checks whether an IP address is reachable by pinging. Use linux utils to execute the ping (ICMP ECHO) command. Sends 5 packets with an interval of 1 seconds and timeout of 1 seconds. Runtime error implies unreachability else IP is pingable. :param ip: IP to check :return: bool - True or string 'failure' depending on pingability. """ cmd_ping = 'ping' if netaddr.valid_ipv6(mgmt_ip): cmd_ping = 'ping6' if not count: count = cfg.CONF.monitor_ping.count if not timeout: timeout = cfg.CONF.monitor_ping.timeout if not interval: interval = cfg.CONF.monitor_ping.interval if not retry: retry = cfg.CONF.monitor_ping.retry ping_cmd = [cmd_ping, '-c', count, '-W', timeout, '-i', interval, mgmt_ip] for retry_range in range(int(retry)): try: linux_utils.execute(ping_cmd, check_exit_code=True) return True except RuntimeError: LOG.warning("Cannot ping ip address: %s", mgmt_ip) return 'failure'
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 test_check_exit_code(self): self.mock_popen.return_value = ["", ""] stdout = utils.execute(["ls", self.test_file[:-1]], check_exit_code=False) self.assertEqual("", stdout)
def test_stderr_true(self): expected = "%s\n" % self.test_file self.mock_popen.return_value = [expected, ""] out = utils.execute(["ls", self.test_file], return_stderr=True) self.assertIsInstance(out, tuple) self.assertEqual(out, (expected, ""))
def test_with_helper(self): expected = "ls %s\n" % self.test_file self.mock_popen.return_value = [expected, ""] result = utils.execute(["ls", self.test_file], root_helper='echo') self.assertEqual(result, expected)
def test_decode_return_data(self): str_data = helpers.compact_byte("%s\n" % self.test_file) result = utils.execute(['ls', self.test_file], return_stderr=True) self.assertEqual((str_data, helpers.compact_byte('')), result)
def test_decode_return_data(self): str_data = bytes("%s\n" % self.test_file, 'utf-8') result = utils.execute(['ls', self.test_file], return_stderr=True) self.assertEqual((str_data, bytes('', 'utf-8')), result)
def test_return_code_log_error_no_raise_runtime(self): self.mock_popen.return_value = ('', '') self.process.return_value.returncode = 1 with mock.patch.object(utils, 'LOG') as log: utils.execute(['ls'], check_exit_code=False) self.assertTrue(log.error.called)
def test_decode_return_data(self): str_data = "%s\n" % self.test_file result = utils.execute(['ls', self.test_file], return_stderr=True) self.assertEqual((str_data, ''), result)
def test_return_code_log_debug(self): self.mock_popen.return_value = ('', '') with mock.patch.object(utils, 'LOG') as log: utils.execute(['ls']) self.assertTrue(log.debug.called)
def test_with_addl_env(self): expected = "%s\n" % self.test_file self.mock_popen.return_value = [expected, ""] result = utils.execute(["ls", self.test_file], addl_env={'foo': 'bar'}) self.assertEqual(result, expected)
def test_process_input(self): expected = "%s\n" % self.test_file[:-1] self.mock_popen.return_value = [expected, ""] result = utils.execute(["cat"], process_input="%s\n" % self.test_file[:-1]) self.assertEqual(result, expected)
def test_return_str_data(self): str_data = "%s\n" % self.test_file self.mock_popen.return_value = [str_data, ''] result = utils.execute(['ls', self.test_file], return_stderr=True) self.assertEqual((str_data, ''), result)
def _ivs_add_port(self, device_name, port_id, mac_address): cmd = ['ivs-ctl', 'add-port', device_name] utils.execute(cmd, self.root_helper)