Fix process error handling in local.
This commit is contained in:
		
					parent
					
						
							
								95461cded7
							
						
					
				
			
			
				commit
				
					
						d48b65ed53
					
				
			
		
					 2 changed files with 49 additions and 9 deletions
				
			
		| 
						 | 
					@ -207,14 +207,13 @@ class Local(object):
 | 
				
			||||||
            env.update(message.env)
 | 
					            env.update(message.env)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            output = exec_util.call_get_output(command, env=env)
 | 
					            output, errout = exec_util.call_get_output(command, env=env)
 | 
				
			||||||
            self.log.debug("Local output: {}".format(output))
 | 
					            self.log.debug("Local stdout: {}".format(output))
 | 
				
			||||||
 | 
					            self.log.debug("Local stderr: {}".format(errout))
 | 
				
			||||||
            if return_output:
 | 
					            if return_output:
 | 
				
			||||||
                return output.decode()
 | 
					                return output.decode()
 | 
				
			||||||
        except subprocess.CalledProcessError as e:
 | 
					        except subprocess.CalledProcessError as e:
 | 
				
			||||||
            raise cdist.Error("Command failed: " + " ".join(command)
 | 
					            exec_util.handle_called_process_error(e, command)
 | 
				
			||||||
                    + " with returncode: {} and output: {}".format(
 | 
					 | 
				
			||||||
                        e.returncode, e.output))
 | 
					 | 
				
			||||||
        except OSError as error:
 | 
					        except OSError as error:
 | 
				
			||||||
            raise cdist.Error(" ".join(command) + ": " + error.args[1])
 | 
					            raise cdist.Error(" ".join(command) + ": " + error.args[1])
 | 
				
			||||||
        finally:
 | 
					        finally:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,20 +20,61 @@
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
from tempfile import TemporaryFile
 | 
					from tempfile import TemporaryFile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import cdist
 | 
					import cdist
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					STDERR_UNSUPPORTED = 'Not supported in this python version'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def call_get_output(command, env=None):
 | 
					def call_get_output(command, env=None):
 | 
				
			||||||
    """Run the given command with the given environment.
 | 
					    """Run the given command with the given environment.
 | 
				
			||||||
    Return the stdout and stderr output as a byte string.
 | 
					    Return the tuple of stdout and stderr output as a byte strings.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    assert isinstance(command, (list, tuple)), "list or tuple argument expected, got: {}".format(command)
 | 
					
 | 
				
			||||||
 | 
					    assert isinstance(command, (list, tuple)), (
 | 
				
			||||||
 | 
					            "list or tuple argument expected, got: {}".format(command))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if sys.version_info >= (3, 5):
 | 
				
			||||||
 | 
					        return call_get_out_err(command, env)
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        return (call_get_stdout(command, env), STDERR_UNSUPPORTED)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def handle_called_process_error(err, command):
 | 
				
			||||||
 | 
					    if sys.version_info >= (3, 5):
 | 
				
			||||||
 | 
					        errout = err.stderr
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        errout = STDERR_UNSUPPORTED
 | 
				
			||||||
 | 
					    raise cdist.Error("Command failed: " + " ".join(command)
 | 
				
			||||||
 | 
					        + " with returncode: {} and stdout: {}, stderr: {}".format(
 | 
				
			||||||
 | 
					            err.returncode, err.output, errout))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def call_get_stdout(command, env=None):
 | 
				
			||||||
 | 
					    """Run the given command with the given environment.
 | 
				
			||||||
 | 
					    Return the stdout output as a byte string, stderr is ignored.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    assert isinstance(command, (list, tuple)), (
 | 
				
			||||||
 | 
					        "list or tuple argument expected, got: {}".format(command))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    with TemporaryFile() as fout:
 | 
					    with TemporaryFile() as fout:
 | 
				
			||||||
        subprocess.check_call(command, env=env,
 | 
					        subprocess.check_call(command, env=env, stdout=fout)
 | 
				
			||||||
                stdout=fout, stderr=subprocess.STDOUT)
 | 
					 | 
				
			||||||
        fout.seek(0)
 | 
					        fout.seek(0)
 | 
				
			||||||
        output = fout.read()
 | 
					        output = fout.read()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return output
 | 
					    return output
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def call_get_out_err(command, env=None):
 | 
				
			||||||
 | 
					    """Run the given command with the given environment.
 | 
				
			||||||
 | 
					    Return the tuple of stdout and stderr output as a byte strings.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    assert isinstance(command, (list, tuple)), (
 | 
				
			||||||
 | 
					        "list or tuple argument expected, got: {}".format(command))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with TemporaryFile() as fout, TemporaryFile() as ferr:
 | 
				
			||||||
 | 
					        subprocess.check_call(command, env=env,
 | 
				
			||||||
 | 
					                stdout=fout, stderr=ferr)
 | 
				
			||||||
 | 
					        fout.seek(0)
 | 
				
			||||||
 | 
					        ferr.seek(0)
 | 
				
			||||||
 | 
					        output = (fout.read(), ferr.read())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return output
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue