commit
acf3b3575a
30 changed files with 716 additions and 409 deletions
|
|
@ -35,7 +35,8 @@ import cdist.message
|
|||
from cdist import core
|
||||
import cdist.exec.util as exec_util
|
||||
|
||||
CONF_SUBDIRS_LINKED = [ "explorer", "files", "manifest", "type" ]
|
||||
CONF_SUBDIRS_LINKED = ["explorer", "files", "manifest", "type", ]
|
||||
|
||||
|
||||
class Local(object):
|
||||
"""Execute commands locally.
|
||||
|
|
@ -82,7 +83,8 @@ class Local(object):
|
|||
|
||||
@property
|
||||
def dist_conf_dir(self):
|
||||
return os.path.abspath(os.path.join(os.path.dirname(cdist.__file__), "conf"))
|
||||
return os.path.abspath(os.path.join(os.path.dirname(cdist.__file__),
|
||||
"conf"))
|
||||
|
||||
@property
|
||||
def home_dir(self):
|
||||
|
|
@ -109,7 +111,8 @@ class Local(object):
|
|||
# Depending on out_path
|
||||
self.bin_path = os.path.join(self.base_path, "bin")
|
||||
self.conf_path = os.path.join(self.base_path, "conf")
|
||||
self.global_explorer_out_path = os.path.join(self.base_path, "explorer")
|
||||
self.global_explorer_out_path = os.path.join(self.base_path,
|
||||
"explorer")
|
||||
self.object_path = os.path.join(self.base_path, "object")
|
||||
self.messages_path = os.path.join(self.base_path, "messages")
|
||||
self.files_path = os.path.join(self.conf_path, "files")
|
||||
|
|
@ -118,7 +121,7 @@ class Local(object):
|
|||
self.global_explorer_path = os.path.join(self.conf_path, "explorer")
|
||||
self.manifest_path = os.path.join(self.conf_path, "manifest")
|
||||
self.initial_manifest = (self.custom_initial_manifest or
|
||||
os.path.join(self.manifest_path, "init"))
|
||||
os.path.join(self.manifest_path, "init"))
|
||||
|
||||
self.type_path = os.path.join(self.conf_path, "type")
|
||||
|
||||
|
|
@ -164,8 +167,8 @@ class Local(object):
|
|||
with open(self.object_marker_file, 'w') as fd:
|
||||
fd.write("%s\n" % self.object_marker_name)
|
||||
|
||||
self.log.debug("Object marker %s saved in %s" % (self.object_marker_name, self.object_marker_file))
|
||||
|
||||
self.log.debug("Object marker %s saved in %s" % (
|
||||
self.object_marker_name, self.object_marker_file))
|
||||
|
||||
def _init_cache_dir(self, cache_dir):
|
||||
if cache_dir:
|
||||
|
|
@ -174,7 +177,8 @@ class Local(object):
|
|||
if self.home_dir:
|
||||
self.cache_path = os.path.join(self.home_dir, "cache")
|
||||
else:
|
||||
raise cdist.Error("No homedir setup and no cache dir location given")
|
||||
raise cdist.Error(
|
||||
"No homedir setup and no cache dir location given")
|
||||
|
||||
def rmdir(self, path):
|
||||
"""Remove directory on the local side."""
|
||||
|
|
@ -192,7 +196,8 @@ class Local(object):
|
|||
|
||||
"""
|
||||
self.log.debug("Local run: %s", command)
|
||||
assert isinstance(command, (list, tuple)), "list or tuple argument expected, got: %s" % command
|
||||
assert isinstance(command, (list, tuple)), (
|
||||
"list or tuple argument expected, got: %s" % command)
|
||||
|
||||
if env is None:
|
||||
env = os.environ.copy()
|
||||
|
|
@ -220,16 +225,17 @@ class Local(object):
|
|||
if message_prefix:
|
||||
message.merge_messages()
|
||||
|
||||
def run_script(self, script, env=None, return_output=False, message_prefix=None):
|
||||
def run_script(self, script, env=None, return_output=False,
|
||||
message_prefix=None):
|
||||
"""Run the given script with the given environment.
|
||||
Return the output as a string.
|
||||
|
||||
"""
|
||||
command = [ os.environ.get('CDIST_LOCAL_SHELL',"/bin/sh") , "-e"]
|
||||
command = [os.environ.get('CDIST_LOCAL_SHELL', "/bin/sh"), "-e"]
|
||||
command.append(script)
|
||||
|
||||
return self.run(command=command, env=env, return_output=return_output, message_prefix=message_prefix)
|
||||
|
||||
return self.run(command=command, env=env, return_output=return_output,
|
||||
message_prefix=message_prefix)
|
||||
|
||||
def save_cache(self):
|
||||
destination = os.path.join(self.cache_path, self.hostdir)
|
||||
|
|
@ -239,7 +245,8 @@ class Local(object):
|
|||
if os.path.exists(destination):
|
||||
shutil.rmtree(destination)
|
||||
except PermissionError as e:
|
||||
raise cdist.Error("Cannot delete old cache %s: %s" % (destination, e))
|
||||
raise cdist.Error(
|
||||
"Cannot delete old cache %s: %s" % (destination, e))
|
||||
|
||||
shutil.move(self.base_path, destination)
|
||||
|
||||
|
|
@ -265,18 +272,21 @@ class Local(object):
|
|||
|
||||
for entry in os.listdir(current_dir):
|
||||
rel_entry_path = os.path.join(sub_dir, entry)
|
||||
src = os.path.abspath(os.path.join(conf_dir, sub_dir, entry))
|
||||
src = os.path.abspath(os.path.join(conf_dir,
|
||||
sub_dir,
|
||||
entry))
|
||||
dst = os.path.join(self.conf_path, sub_dir, entry)
|
||||
|
||||
# Already exists? remove and link
|
||||
if os.path.exists(dst):
|
||||
os.unlink(dst)
|
||||
|
||||
|
||||
self.log.debug("Linking %s to %s ..." % (src, dst))
|
||||
try:
|
||||
os.symlink(src, dst)
|
||||
except OSError as e:
|
||||
raise cdist.Error("Linking %s %s to %s failed: %s" % (sub_dir, src, dst, e.__str__()))
|
||||
raise cdist.Error("Linking %s %s to %s failed: %s" % (
|
||||
sub_dir, src, dst, e.__str__()))
|
||||
|
||||
def _link_types_for_emulator(self):
|
||||
"""Link emulator to types"""
|
||||
|
|
@ -288,4 +298,6 @@ class Local(object):
|
|||
try:
|
||||
os.symlink(src, dst)
|
||||
except OSError as e:
|
||||
raise cdist.Error("Linking emulator from %s to %s failed: %s" % (src, dst, e.__str__()))
|
||||
raise cdist.Error(
|
||||
"Linking emulator from %s to %s failed: %s" % (
|
||||
src, dst, e.__str__()))
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import cdist.exec.util as exec_util
|
|||
|
||||
import cdist
|
||||
|
||||
|
||||
class DecodeError(cdist.Error):
|
||||
def __init__(self, command):
|
||||
self.command = command
|
||||
|
|
@ -75,7 +76,6 @@ class Remote(object):
|
|||
os.environ['__remote_copy'] = self._copy
|
||||
os.environ['__remote_exec'] = self._exec
|
||||
|
||||
|
||||
def create_files_dirs(self):
|
||||
self.rmdir(self.base_path)
|
||||
self.mkdir(self.base_path)
|
||||
|
|
@ -101,11 +101,13 @@ class Remote(object):
|
|||
for f in glob.glob1(source, '*'):
|
||||
command = self._copy.split()
|
||||
path = os.path.join(source, f)
|
||||
command.extend([path, '{0}:{1}'.format(self.target_host, destination)])
|
||||
command.extend([path, '{0}:{1}'.format(
|
||||
self.target_host, destination)])
|
||||
self._run_command(command)
|
||||
else:
|
||||
command = self._copy.split()
|
||||
command.extend([source, '{0}:{1}'.format(self.target_host, destination)])
|
||||
command.extend([source, '{0}:{1}'.format(
|
||||
self.target_host, destination)])
|
||||
self._run_command(command)
|
||||
|
||||
def run_script(self, script, env=None, return_output=False):
|
||||
|
|
@ -114,7 +116,7 @@ class Remote(object):
|
|||
|
||||
"""
|
||||
|
||||
command = [ os.environ.get('CDIST_REMOTE_SHELL',"/bin/sh") , "-e"]
|
||||
command = [os.environ.get('CDIST_REMOTE_SHELL', "/bin/sh"), "-e"]
|
||||
command.append(script)
|
||||
|
||||
return self.run(command, env, return_output)
|
||||
|
|
@ -148,8 +150,8 @@ class Remote(object):
|
|||
# /bin/csh will execute this script in the right way.
|
||||
if env:
|
||||
remote_env = [" export %s=%s;" % item for item in env.items()]
|
||||
string_cmd = ("/bin/sh -c '" + " ".join(remote_env)
|
||||
+ " ".join(command) + "'")
|
||||
string_cmd = ("/bin/sh -c '" + " ".join(remote_env) +
|
||||
" ".join(command) + "'")
|
||||
cmd.append(string_cmd)
|
||||
else:
|
||||
cmd.extend(command)
|
||||
|
|
@ -160,7 +162,8 @@ class Remote(object):
|
|||
Return the output as a string.
|
||||
|
||||
"""
|
||||
assert isinstance(command, (list, tuple)), "list or tuple argument expected, got: %s" % command
|
||||
assert isinstance(command, (list, tuple)), (
|
||||
"list or tuple argument expected, got: %s" % command)
|
||||
|
||||
# export target_host for use in __remote_{exec,copy} scripts
|
||||
os_environ = os.environ.copy()
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import cdist
|
|||
|
||||
STDERR_UNSUPPORTED = '<Not supported in this python version>'
|
||||
|
||||
|
||||
def call_get_output(command, env=None):
|
||||
"""Run the given command with the given environment.
|
||||
Return the tuple of stdout and stderr output as a byte strings.
|
||||
|
|
@ -40,14 +41,16 @@ def call_get_output(command, env=None):
|
|||
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))
|
||||
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.
|
||||
|
|
@ -63,6 +66,7 @@ def call_get_stdout(command, env=None):
|
|||
|
||||
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.
|
||||
|
|
@ -72,7 +76,7 @@ def call_get_out_err(command, env=None):
|
|||
|
||||
with TemporaryFile() as fout, TemporaryFile() as ferr:
|
||||
subprocess.check_call(command, env=env,
|
||||
stdout=fout, stderr=ferr)
|
||||
stdout=fout, stderr=ferr)
|
||||
fout.seek(0)
|
||||
ferr.seek(0)
|
||||
output = (fout.read(), ferr.read())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue