renamed utils.check_cmd to utils.cmd, updated host_cmd to allow for shell commands for output redirection
This commit is contained in:
		
							parent
							
								
									16b7e70c33
								
							
						
					
					
						commit
						78f981463d
					
				
					 15 changed files with 34 additions and 31 deletions
				
			
		|  | @ -878,7 +878,7 @@ class CoreHandler(socketserver.BaseRequestHandler): | ||||||
|                 ): |                 ): | ||||||
|                     if message.flags & MessageFlags.LOCAL.value: |                     if message.flags & MessageFlags.LOCAL.value: | ||||||
|                         try: |                         try: | ||||||
|                             res = utils.check_cmd(command) |                             res = utils.cmd(command) | ||||||
|                             status = 0 |                             status = 0 | ||||||
|                         except CoreCommandError as e: |                         except CoreCommandError as e: | ||||||
|                             res = e.stderr |                             res = e.stderr | ||||||
|  |  | ||||||
|  | @ -140,7 +140,7 @@ class EmaneManager(ModelManager): | ||||||
|         try: |         try: | ||||||
|             # check for emane |             # check for emane | ||||||
|             args = "emane --version" |             args = "emane --version" | ||||||
|             emane_version = utils.check_cmd(args) |             emane_version = utils.cmd(args) | ||||||
|             logging.info("using EMANE: %s", emane_version) |             logging.info("using EMANE: %s", emane_version) | ||||||
|             self.session.distributed.execute(lambda x: x.remote_cmd(args)) |             self.session.distributed.execute(lambda x: x.remote_cmd(args)) | ||||||
| 
 | 
 | ||||||
|  | @ -594,7 +594,7 @@ class EmaneManager(ModelManager): | ||||||
|         log_file = os.path.join(path, "emane.log") |         log_file = os.path.join(path, "emane.log") | ||||||
|         platform_xml = os.path.join(path, "platform.xml") |         platform_xml = os.path.join(path, "platform.xml") | ||||||
|         emanecmd += f" -f {log_file} {platform_xml}" |         emanecmd += f" -f {log_file} {platform_xml}" | ||||||
|         utils.check_cmd(emanecmd, cwd=path) |         utils.cmd(emanecmd, cwd=path) | ||||||
|         self.session.distributed.execute(lambda x: x.remote_cmd(emanecmd, cwd=path)) |         self.session.distributed.execute(lambda x: x.remote_cmd(emanecmd, cwd=path)) | ||||||
|         logging.info("host emane daemon running: %s", emanecmd) |         logging.info("host emane daemon running: %s", emanecmd) | ||||||
| 
 | 
 | ||||||
|  | @ -618,8 +618,8 @@ class EmaneManager(ModelManager): | ||||||
| 
 | 
 | ||||||
|         if stop_emane_on_host: |         if stop_emane_on_host: | ||||||
|             try: |             try: | ||||||
|                 utils.check_cmd(kill_emaned) |                 utils.cmd(kill_emaned) | ||||||
|                 utils.check_cmd(kill_transortd) |                 utils.cmd(kill_transortd) | ||||||
|                 self.session.distributed.execute(lambda x: x.remote_cmd(kill_emaned)) |                 self.session.distributed.execute(lambda x: x.remote_cmd(kill_emaned)) | ||||||
|                 self.session.distributed.execute(lambda x: x.remote_cmd(kill_transortd)) |                 self.session.distributed.execute(lambda x: x.remote_cmd(kill_transortd)) | ||||||
|             except CoreCommandError: |             except CoreCommandError: | ||||||
|  |  | ||||||
|  | @ -63,4 +63,4 @@ class EmaneTdmaModel(emanemodel.EmaneModel): | ||||||
|             "setting up tdma schedule: schedule(%s) device(%s)", schedule, event_device |             "setting up tdma schedule: schedule(%s) device(%s)", schedule, event_device | ||||||
|         ) |         ) | ||||||
|         args = f"emaneevent-tdmaschedule -i {event_device} {schedule}" |         args = f"emaneevent-tdmaschedule -i {event_device} {schedule}" | ||||||
|         utils.check_cmd(args) |         utils.cmd(args) | ||||||
|  |  | ||||||
|  | @ -1187,6 +1187,6 @@ class Ns2ScriptedMobility(WayPointMobility): | ||||||
|             return |             return | ||||||
|         filename = self.findfile(filename) |         filename = self.findfile(filename) | ||||||
|         args = f"/bin/sh {filename} {typestr}" |         args = f"/bin/sh {filename} {typestr}" | ||||||
|         utils.check_cmd( |         utils.cmd( | ||||||
|             args, cwd=self.session.session_dir, env=self.session.get_environment() |             args, cwd=self.session.session_dir, env=self.session.get_environment() | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  | @ -79,7 +79,7 @@ class NodeBase(object): | ||||||
|         """ |         """ | ||||||
|         raise NotImplementedError |         raise NotImplementedError | ||||||
| 
 | 
 | ||||||
|     def host_cmd(self, args, env=None, cwd=None, wait=True): |     def host_cmd(self, args, env=None, cwd=None, wait=True, shell=False): | ||||||
|         """ |         """ | ||||||
|         Runs a command on the host system or distributed server. |         Runs a command on the host system or distributed server. | ||||||
| 
 | 
 | ||||||
|  | @ -87,12 +87,13 @@ class NodeBase(object): | ||||||
|         :param dict env: environment to run command with |         :param dict env: environment to run command with | ||||||
|         :param str cwd: directory to run command in |         :param str cwd: directory to run command in | ||||||
|         :param bool wait: True to wait for status, False otherwise |         :param bool wait: True to wait for status, False otherwise | ||||||
|  |         :param bool shell: True to use shell, False otherwise | ||||||
|         :return: combined stdout and stderr |         :return: combined stdout and stderr | ||||||
|         :rtype: str |         :rtype: str | ||||||
|         :raises CoreCommandError: when a non-zero exit status occurs |         :raises CoreCommandError: when a non-zero exit status occurs | ||||||
|         """ |         """ | ||||||
|         if self.server is None: |         if self.server is None: | ||||||
|             return utils.check_cmd(args, env, cwd, wait) |             return utils.cmd(args, env, cwd, wait, shell) | ||||||
|         else: |         else: | ||||||
|             return self.server.remote_cmd(args, env, cwd, wait) |             return self.server.remote_cmd(args, env, cwd, wait) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -65,4 +65,4 @@ class VnodeClient(object): | ||||||
|         """ |         """ | ||||||
|         self._verify_connection() |         self._verify_connection() | ||||||
|         args = self.create_cmd(args) |         args = self.create_cmd(args) | ||||||
|         return utils.check_cmd(args, wait=wait) |         return utils.cmd(args, wait=wait) | ||||||
|  |  | ||||||
|  | @ -45,14 +45,14 @@ class DockerClient(object): | ||||||
| 
 | 
 | ||||||
|     def check_cmd(self, cmd): |     def check_cmd(self, cmd): | ||||||
|         logging.info("docker cmd output: %s", cmd) |         logging.info("docker cmd output: %s", cmd) | ||||||
|         return utils.check_cmd(f"docker exec {self.name} {cmd}") |         return utils.cmd(f"docker exec {self.name} {cmd}") | ||||||
| 
 | 
 | ||||||
|     def create_ns_cmd(self, cmd): |     def create_ns_cmd(self, cmd): | ||||||
|         return f"nsenter -t {self.pid} -u -i -p -n {cmd}" |         return f"nsenter -t {self.pid} -u -i -p -n {cmd}" | ||||||
| 
 | 
 | ||||||
|     def ns_cmd(self, cmd, wait): |     def ns_cmd(self, cmd, wait): | ||||||
|         args = f"nsenter -t {self.pid} -u -i -p -n {cmd}" |         args = f"nsenter -t {self.pid} -u -i -p -n {cmd}" | ||||||
|         return utils.check_cmd(args, wait=wait) |         return utils.cmd(args, wait=wait) | ||||||
| 
 | 
 | ||||||
|     def get_pid(self): |     def get_pid(self): | ||||||
|         args = f"docker inspect -f '{{{{.State.Pid}}}}' {self.name}" |         args = f"docker inspect -f '{{{{.State.Pid}}}}' {self.name}" | ||||||
|  | @ -153,7 +153,7 @@ class DockerNode(CoreNode): | ||||||
|     def nsenter_cmd(self, args, wait=True): |     def nsenter_cmd(self, args, wait=True): | ||||||
|         if self.server is None: |         if self.server is None: | ||||||
|             args = self.client.create_ns_cmd(args) |             args = self.client.create_ns_cmd(args) | ||||||
|             return utils.check_cmd(args, wait=wait) |             return utils.cmd(args, wait=wait) | ||||||
|         else: |         else: | ||||||
|             args = self.client.create_ns_cmd(args) |             args = self.client.create_ns_cmd(args) | ||||||
|             return self.server.remote_cmd(args, wait=wait) |             return self.server.remote_cmd(args, wait=wait) | ||||||
|  |  | ||||||
|  | @ -48,7 +48,7 @@ class CoreInterface(object): | ||||||
|         use_ovs = session.options.get_config("ovs") == "True" |         use_ovs = session.options.get_config("ovs") == "True" | ||||||
|         self.net_client = get_net_client(use_ovs, self.host_cmd) |         self.net_client = get_net_client(use_ovs, self.host_cmd) | ||||||
| 
 | 
 | ||||||
|     def host_cmd(self, args, env=None, cwd=None, wait=True): |     def host_cmd(self, args, env=None, cwd=None, wait=True, shell=False): | ||||||
|         """ |         """ | ||||||
|         Runs a command on the host system or distributed server. |         Runs a command on the host system or distributed server. | ||||||
| 
 | 
 | ||||||
|  | @ -56,12 +56,13 @@ class CoreInterface(object): | ||||||
|         :param dict env: environment to run command with |         :param dict env: environment to run command with | ||||||
|         :param str cwd: directory to run command in |         :param str cwd: directory to run command in | ||||||
|         :param bool wait: True to wait for status, False otherwise |         :param bool wait: True to wait for status, False otherwise | ||||||
|  |         :param bool shell: True to use shell, False otherwise | ||||||
|         :return: combined stdout and stderr |         :return: combined stdout and stderr | ||||||
|         :rtype: str |         :rtype: str | ||||||
|         :raises CoreCommandError: when a non-zero exit status occurs |         :raises CoreCommandError: when a non-zero exit status occurs | ||||||
|         """ |         """ | ||||||
|         if self.server is None: |         if self.server is None: | ||||||
|             return utils.check_cmd(args, env, cwd, wait) |             return utils.cmd(args, env, cwd, wait, shell) | ||||||
|         else: |         else: | ||||||
|             return self.server.remote_cmd(args, env, cwd, wait) |             return self.server.remote_cmd(args, env, cwd, wait) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -49,7 +49,7 @@ class LxdClient(object): | ||||||
| 
 | 
 | ||||||
|     def check_cmd(self, cmd, wait=True): |     def check_cmd(self, cmd, wait=True): | ||||||
|         args = self.create_cmd(cmd) |         args = self.create_cmd(cmd) | ||||||
|         return utils.check_cmd(args, wait=wait) |         return utils.cmd(args, wait=wait) | ||||||
| 
 | 
 | ||||||
|     def copy_file(self, source, destination): |     def copy_file(self, source, destination): | ||||||
|         if destination[0] != "/": |         if destination[0] != "/": | ||||||
|  |  | ||||||
|  | @ -2,8 +2,6 @@ | ||||||
| Clients for dealing with bridge/interface commands. | Clients for dealing with bridge/interface commands. | ||||||
| """ | """ | ||||||
| 
 | 
 | ||||||
| import os |  | ||||||
| 
 |  | ||||||
| from core.constants import BRCTL_BIN, ETHTOOL_BIN, IP_BIN, OVS_BIN, TC_BIN | from core.constants import BRCTL_BIN, ETHTOOL_BIN, IP_BIN, OVS_BIN, TC_BIN | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -236,10 +234,10 @@ class LinuxNetClient(object): | ||||||
|         self.device_up(name) |         self.device_up(name) | ||||||
| 
 | 
 | ||||||
|         # turn off multicast snooping so forwarding occurs w/o IGMP joins |         # turn off multicast snooping so forwarding occurs w/o IGMP joins | ||||||
|         snoop = f"/sys/devices/virtual/net/{name}/bridge/multicast_snooping" |         snoop_file = "multicast_snooping" | ||||||
|         if os.path.exists(snoop): |         snoop = f"/sys/devices/virtual/net/{name}/bridge/{snoop_file}" | ||||||
|             with open(snoop, "w") as f: |         self.run(f"echo 0 > /tmp/{snoop_file}", shell=True) | ||||||
|                 f.write("0") |         self.run(f"cp /tmp/{snoop_file} {snoop}") | ||||||
| 
 | 
 | ||||||
|     def delete_bridge(self, name): |     def delete_bridge(self, name): | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|  | @ -270,7 +270,7 @@ class CoreNetwork(CoreNetworkBase): | ||||||
|             self.startup() |             self.startup() | ||||||
|             ebq.startupdateloop(self) |             ebq.startupdateloop(self) | ||||||
| 
 | 
 | ||||||
|     def host_cmd(self, args, env=None, cwd=None, wait=True): |     def host_cmd(self, args, env=None, cwd=None, wait=True, shell=False): | ||||||
|         """ |         """ | ||||||
|         Runs a command that is used to configure and setup the network on the host |         Runs a command that is used to configure and setup the network on the host | ||||||
|         system and all configured distributed servers. |         system and all configured distributed servers. | ||||||
|  | @ -279,12 +279,13 @@ class CoreNetwork(CoreNetworkBase): | ||||||
|         :param dict env: environment to run command with |         :param dict env: environment to run command with | ||||||
|         :param str cwd: directory to run command in |         :param str cwd: directory to run command in | ||||||
|         :param bool wait: True to wait for status, False otherwise |         :param bool wait: True to wait for status, False otherwise | ||||||
|  |         :param bool shell: True to use shell, False otherwise | ||||||
|         :return: combined stdout and stderr |         :return: combined stdout and stderr | ||||||
|         :rtype: str |         :rtype: str | ||||||
|         :raises CoreCommandError: when a non-zero exit status occurs |         :raises CoreCommandError: when a non-zero exit status occurs | ||||||
|         """ |         """ | ||||||
|         logging.info("network node(%s) cmd", self.name) |         logging.info("network node(%s) cmd", self.name) | ||||||
|         output = utils.check_cmd(args, env, cwd, wait) |         output = utils.cmd(args, env, cwd, wait, shell) | ||||||
|         self.session.distributed.execute(lambda x: x.remote_cmd(args, env, cwd, wait)) |         self.session.distributed.execute(lambda x: x.remote_cmd(args, env, cwd, wait)) | ||||||
|         return output |         return output | ||||||
| 
 | 
 | ||||||
|  | @ -765,7 +766,7 @@ class CtrlNet(CoreNetwork): | ||||||
|         """ |         """ | ||||||
|         use_ovs = self.session.options.get_config("ovs") == "True" |         use_ovs = self.session.options.get_config("ovs") == "True" | ||||||
|         current = f"{address}/{self.prefix.prefixlen}" |         current = f"{address}/{self.prefix.prefixlen}" | ||||||
|         net_client = get_net_client(use_ovs, utils.check_cmd) |         net_client = get_net_client(use_ovs, utils.cmd) | ||||||
|         net_client.create_address(self.brname, current) |         net_client.create_address(self.brname, current) | ||||||
|         servers = self.session.distributed.servers |         servers = self.session.distributed.servers | ||||||
|         for name in servers: |         for name in servers: | ||||||
|  |  | ||||||
|  | @ -415,7 +415,7 @@ class HttpService(UtilService): | ||||||
|         Detect the apache2 version using the 'a2query' command. |         Detect the apache2 version using the 'a2query' command. | ||||||
|         """ |         """ | ||||||
|         try: |         try: | ||||||
|             result = utils.check_cmd("a2query -v") |             result = utils.cmd("a2query -v") | ||||||
|             status = 0 |             status = 0 | ||||||
|         except CoreCommandError as e: |         except CoreCommandError as e: | ||||||
|             status = e.returncode |             status = e.returncode | ||||||
|  |  | ||||||
|  | @ -191,7 +191,7 @@ def mute_detach(args, **kwargs): | ||||||
|     return Popen(args, **kwargs).pid |     return Popen(args, **kwargs).pid | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def check_cmd(args, env=None, cwd=None, wait=True): | def cmd(args, env=None, cwd=None, wait=True, shell=False): | ||||||
|     """ |     """ | ||||||
|     Execute a command on the host and return a tuple containing the exit status and |     Execute a command on the host and return a tuple containing the exit status and | ||||||
|     result string. stderr output is folded into the stdout result string. |     result string. stderr output is folded into the stdout result string. | ||||||
|  | @ -200,15 +200,17 @@ def check_cmd(args, env=None, cwd=None, wait=True): | ||||||
|     :param dict env: environment to run command with |     :param dict env: environment to run command with | ||||||
|     :param str cwd: directory to run command in |     :param str cwd: directory to run command in | ||||||
|     :param bool wait: True to wait for status, False otherwise |     :param bool wait: True to wait for status, False otherwise | ||||||
|  |     :param bool shell: True to use shell, False otherwise | ||||||
|     :return: combined stdout and stderr |     :return: combined stdout and stderr | ||||||
|     :rtype: str |     :rtype: str | ||||||
|     :raises CoreCommandError: when there is a non-zero exit status or the file to |     :raises CoreCommandError: when there is a non-zero exit status or the file to | ||||||
|         execute is not found |         execute is not found | ||||||
|     """ |     """ | ||||||
|     logging.info("command cwd(%s) wait(%s): %s", cwd, wait, args) |     logging.info("command cwd(%s) wait(%s): %s", cwd, wait, args) | ||||||
|  |     if shell is False: | ||||||
|         args = shlex.split(args) |         args = shlex.split(args) | ||||||
|     try: |     try: | ||||||
|         p = Popen(args, stdout=PIPE, stderr=PIPE, env=env, cwd=cwd) |         p = Popen(args, stdout=PIPE, stderr=PIPE, env=env, cwd=cwd, shell=shell) | ||||||
|         if wait: |         if wait: | ||||||
|             stdout, stderr = p.communicate() |             stdout, stderr = p.communicate() | ||||||
|             status = p.wait() |             status = p.wait() | ||||||
|  |  | ||||||
|  | @ -69,7 +69,7 @@ def get_ipv4_addresses(hostname): | ||||||
|     if hostname == "localhost": |     if hostname == "localhost": | ||||||
|         addresses = [] |         addresses = [] | ||||||
|         args = f"{IP_BIN} -o -f inet address show" |         args = f"{IP_BIN} -o -f inet address show" | ||||||
|         output = utils.check_cmd(args) |         output = utils.cmd(args) | ||||||
|         for line in output.split(os.linesep): |         for line in output.split(os.linesep): | ||||||
|             split = line.split() |             split = line.split() | ||||||
|             if not split: |             if not split: | ||||||
|  |  | ||||||
|  | @ -67,4 +67,4 @@ class TestNodes: | ||||||
|         # then |         # then | ||||||
|         assert node |         assert node | ||||||
|         assert node.up |         assert node.up | ||||||
|         assert utils.check_cmd(f"brctl show {node.brname}") |         assert utils.cmd(f"brctl show {node.brname}") | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue