def test_command_failed_exception(self): returncode = 1 cmd = "foo" stdout = "output" stderr = "error" try: raise exceptions.CommandFailed(returncode, cmd, stdout, stderr) except exceptions.CommandFailed as e: self.assertIn(str(returncode), str(e)) self.assertIn(cmd, str(e)) self.assertIn(stdout, str(e)) self.assertIn(stderr, str(e))
def copy_file_to_host(file_from, dest, host, username, pkey): dest = "%s@%s:%s" % (username, host, dest) cmd = "scp -v -o UserKnownHostsFile=/dev/null " \ "-o StrictHostKeyChecking=no " \ "-i %(pkey)s %(file1)s %(dest)s" % {'pkey': pkey, 'file1': file_from, 'dest': dest} args = shlex.split(cmd.encode('utf-8')) subprocess_args = {'stdout': subprocess.PIPE, 'stderr': subprocess.STDOUT} proc = subprocess.Popen(args, **subprocess_args) stdout, stderr = proc.communicate() if proc.returncode != 0: raise exceptions.CommandFailed(cmd, proc.returncode, stdout, stderr) return stdout
def execute(cmd, action, flags='', params='', fail_ok=False, merge_stderr=False): """Executes specified command for the given action.""" cmd = ' '.join( [os.path.join(CONF.cli.cli_dir, cmd), flags, action, params]) LOG.info("running: '%s'" % cmd) cmd = shlex.split(cmd.encode('utf-8')) result = '' result_err = '' stdout = subprocess.PIPE stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE proc = subprocess.Popen(cmd, stdout=stdout, stderr=stderr) result, result_err = proc.communicate() if not fail_ok and proc.returncode != 0: raise exceptions.CommandFailed(proc.returncode, cmd, result, result_err) return result