rebuild the command string
Signed-off-by: Nico Schottelius <nico@brief.schottelius.org>
This commit is contained in:
		
					parent
					
						
							
								ab48a72ce1
							
						
					
				
			
			
				commit
				
					
						f031a40746
					
				
			
		
					 1 changed files with 19 additions and 30 deletions
				
			
		| 
						 | 
					@ -28,17 +28,6 @@ import logging
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import cdist
 | 
					import cdist
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
class RemoteScriptError(cdist.Error):
 | 
					 | 
				
			||||||
    def __init__(self, script, command, script_content):
 | 
					 | 
				
			||||||
        self.script = script
 | 
					 | 
				
			||||||
        self.command = command
 | 
					 | 
				
			||||||
        self.script_content = script_content
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def __str__(self):
 | 
					 | 
				
			||||||
        plain_command = " ".join(self.command)
 | 
					 | 
				
			||||||
        return "Remote script execution failed: %s" % plain_command
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class DecodeError(cdist.Error):
 | 
					class DecodeError(cdist.Error):
 | 
				
			||||||
    def __init__(self, command):
 | 
					    def __init__(self, command):
 | 
				
			||||||
        self.command = command
 | 
					        self.command = command
 | 
				
			||||||
| 
						 | 
					@ -91,6 +80,17 @@ class Remote(object):
 | 
				
			||||||
        command.extend(["-r", source, self.target_host + ":" + destination])
 | 
					        command.extend(["-r", source, self.target_host + ":" + destination])
 | 
				
			||||||
        self._run_command(command)
 | 
					        self._run_command(command)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def run_script(self, script, env=None, return_output=False):
 | 
				
			||||||
 | 
					        """Run the given script with the given environment on the remote side.
 | 
				
			||||||
 | 
					        Return the output as a string.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        command = ["/bin/sh", "-e"]
 | 
				
			||||||
 | 
					        command.append(script)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return self.run(command, env, return_output)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def run(self, command, env=None, return_output=False):
 | 
					    def run(self, command, env=None, return_output=False):
 | 
				
			||||||
        """Run the given command with the given environment on the remote side.
 | 
					        """Run the given command with the given environment on the remote side.
 | 
				
			||||||
        Return the output as a string.
 | 
					        Return the output as a string.
 | 
				
			||||||
| 
						 | 
					@ -99,7 +99,15 @@ class Remote(object):
 | 
				
			||||||
        # prefix given command with remote_exec
 | 
					        # prefix given command with remote_exec
 | 
				
			||||||
        cmd = self._exec.split()
 | 
					        cmd = self._exec.split()
 | 
				
			||||||
        cmd.append(self.target_host)
 | 
					        cmd.append(self.target_host)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # can't pass environment to remote side, so prepend command with
 | 
				
			||||||
 | 
					        # variable declarations
 | 
				
			||||||
 | 
					        if env:
 | 
				
			||||||
 | 
					            remote_env = ["%s=%s" % item for item in env.items()]
 | 
				
			||||||
 | 
					            cmd.append(remote_env)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        cmd.extend(command)
 | 
					        cmd.extend(command)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return self._run_command(cmd, env=env, return_output=return_output)
 | 
					        return self._run_command(cmd, env=env, return_output=return_output)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _run_command(self, command, env=None, return_output=False):
 | 
					    def _run_command(self, command, env=None, return_output=False):
 | 
				
			||||||
| 
						 | 
					@ -113,14 +121,6 @@ class Remote(object):
 | 
				
			||||||
        os_environ = os.environ.copy()
 | 
					        os_environ = os.environ.copy()
 | 
				
			||||||
        os_environ['__target_host'] = self.target_host
 | 
					        os_environ['__target_host'] = self.target_host
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # can't pass environment to remote side, so prepend command with
 | 
					 | 
				
			||||||
        # variable declarations
 | 
					 | 
				
			||||||
        if env:
 | 
					 | 
				
			||||||
            cmd = ["%s=%s" % item for item in env.items()]
 | 
					 | 
				
			||||||
            cmd.extend(command)
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            cmd = command
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        self.log.debug("Remote run: %s", command)
 | 
					        self.log.debug("Remote run: %s", command)
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            if return_output:
 | 
					            if return_output:
 | 
				
			||||||
| 
						 | 
					@ -133,14 +133,3 @@ class Remote(object):
 | 
				
			||||||
            raise cdist.Error(" ".join(*args) + ": " + error.args[1])
 | 
					            raise cdist.Error(" ".join(*args) + ": " + error.args[1])
 | 
				
			||||||
        except UnicodeDecodeError:
 | 
					        except UnicodeDecodeError:
 | 
				
			||||||
            raise DecodeError(command)
 | 
					            raise DecodeError(command)
 | 
				
			||||||
 | 
					 | 
				
			||||||
    def run_script(self, script, env=None, return_output=False):
 | 
					 | 
				
			||||||
        """Run the given script with the given environment on the remote side.
 | 
					 | 
				
			||||||
        Return the output as a string.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        """
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        command = ["/bin/sh", "-e"]
 | 
					 | 
				
			||||||
        command.append(script)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return self.run(command, env, return_output)
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue