rewrite exec
Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
parent
4e00e2573e
commit
e437cd90d6
1 changed files with 62 additions and 58 deletions
|
@ -23,22 +23,39 @@ import logging
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
import cdist
|
import cdist
|
||||||
|
|
||||||
class ExecWrapper(object):
|
log = logging.getLogger(__name__)
|
||||||
def __init__(self, remote_exec, remote_copy, target_host):
|
|
||||||
self.remote_exec = remote_exec
|
|
||||||
|
|
||||||
def shell_run_or_debug_fail(script, *args, remote_prefix=False, **kargs):
|
|
||||||
|
class Wrapper(object):
|
||||||
|
def __init__(self, target_host, remote_exec, remote_copy):
|
||||||
|
self.target_host = target_host
|
||||||
|
self.remote_exec = remote_exec
|
||||||
|
self.remote_copy = remote_copy
|
||||||
|
|
||||||
|
def remote_mkdir(self, directory):
|
||||||
|
"""Create directory on remote side"""
|
||||||
|
self.run_or_fail(["mkdir", "-p", directory], remote=True)
|
||||||
|
|
||||||
|
def remove_remote_path(self, destination):
|
||||||
|
"""Ensure path on remote side vanished"""
|
||||||
|
self.run_or_fail(["rm", "-rf", destination], remote=True)
|
||||||
|
|
||||||
|
def transfer_path(self, source, destination):
|
||||||
|
"""Transfer directory and previously delete the remote destination"""
|
||||||
|
self.remove_remote_path(destination)
|
||||||
|
self.run_or_fail(os.environ['__remote_copy'].split() +
|
||||||
|
["-r", source, self.target_host + ":" + destination])
|
||||||
|
|
||||||
|
def shell_run_or_debug_fail(self, script, *args, remote=False, **kargs):
|
||||||
# Manually execute /bin/sh, because sh -e does what we want
|
# Manually execute /bin/sh, because sh -e does what we want
|
||||||
# and sh -c -e does not exit if /bin/false called
|
# and sh -c -e does not exit if /bin/false called
|
||||||
args[0][:0] = [ "/bin/sh", "-e" ]
|
args[0][:0] = [ "/bin/sh", "-e" ]
|
||||||
|
|
||||||
if remote_prefix:
|
if remote:
|
||||||
remote_prefix = os.environ['__remote_exec'].split()
|
remote_prefix = self.remote_exec.split()
|
||||||
remote_prefix.append(os.environ['__target_host'])
|
remote_prefix.append(self.target_host)
|
||||||
args[0][:0] = remote_prefix
|
args[0][:0] = remote_prefix
|
||||||
|
|
||||||
log.debug("Shell exec cmd: %s", args)
|
log.debug("Shell exec cmd: %s", args)
|
||||||
|
@ -51,8 +68,8 @@ def shell_run_or_debug_fail(script, *args, remote_prefix=False, **kargs):
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
log.error("Code that raised the error:\n")
|
log.error("Code that raised the error:\n")
|
||||||
|
|
||||||
if remote_prefix:
|
if remote:
|
||||||
run_or_fail(["cat", script], remote_prefix=remote_prefix)
|
self.run_or_fail(["cat", script], remote=remote)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
|
@ -66,10 +83,10 @@ def shell_run_or_debug_fail(script, *args, remote_prefix=False, **kargs):
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
|
raise cdist.Error(" ".join(*args) + ": " + error.args[1])
|
||||||
|
|
||||||
def run_or_fail(*args, remote_prefix=False, **kargs):
|
def run_or_fail(self, *args, remote=False, **kargs):
|
||||||
if remote_prefix:
|
if remote:
|
||||||
remote_prefix = os.environ['__remote_exec'].split()
|
remote_prefix = self.remote_exec.split()
|
||||||
remote_prefix.append(os.environ['__target_host'])
|
remote_prefix.append(self.target_host)
|
||||||
args[0][:0] = remote_prefix
|
args[0][:0] = remote_prefix
|
||||||
|
|
||||||
log.debug("Exec: " + " ".join(*args))
|
log.debug("Exec: " + " ".join(*args))
|
||||||
|
@ -82,16 +99,3 @@ def run_or_fail(*args, remote_prefix=False, **kargs):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def remote_mkdir(self, directory):
|
|
||||||
"""Create directory on remote side"""
|
|
||||||
cdist.exec.run_or_fail(["mkdir", "-p", directory], remote_prefix=True)
|
|
||||||
|
|
||||||
def remove_remote_path(self, destination):
|
|
||||||
"""Ensure path on remote side vanished"""
|
|
||||||
cdist.exec.run_or_fail(["rm", "-rf", destination], remote_prefix=True)
|
|
||||||
|
|
||||||
def transfer_path(self, source, destination):
|
|
||||||
"""Transfer directory and previously delete the remote destination"""
|
|
||||||
self.remove_remote_path(destination)
|
|
||||||
cdist.exec.run_or_fail(os.environ['__remote_copy'].split() +
|
|
||||||
["-r", source, self.target_host + ":" + destination])
|
|
||||||
|
|
Loading…
Reference in a new issue