diff --git a/bin/cdist b/bin/cdist index ac863e9e..fae83ff1 100755 --- a/bin/cdist +++ b/bin/cdist @@ -115,10 +115,14 @@ def configinstall(args, mode): time_start = time.time() for host in args.host: - c = mode(host, - initial_manifest=args.manifest, - base_path=args.cdist_home, - debug=args.debug) + context = cdist.context.Context( + target_host=host, + initial_manifest=args.manifest, + base_path=args.cdist_home, + exec_path=sys.argv[0], + debug=args.debug) + + c = mode(context) if args.parallel: log.debug("Creating child process for %s", host) process[host] = multiprocessing.Process(target=c.deploy_and_cleanup) @@ -126,6 +130,8 @@ def configinstall(args, mode): else: c.deploy_and_cleanup() + context.cleanup() + if args.parallel: for p in process.keys(): log.debug("Joining process %s", p) diff --git a/lib/cdist/config_install.py b/lib/cdist/config_install.py index ef37eb86..2a1c828d 100644 --- a/lib/cdist/config_install.py +++ b/lib/cdist/config_install.py @@ -29,34 +29,24 @@ import tempfile import time import cdist.core +import cdist.context import cdist.exec +import cdist.explorer +#import cdist.manifest -CODE_HEADER = "#!/bin/sh -e\n" - -class ConfigInstall: +class ConfigInstall(object): """Cdist main class to hold arbitrary data""" - def __init__(self, - target_host, - initial_manifest=False, - base_path=False, - exec_path=sys.argv[0], - debug=False): + def __init__(self, context): - self.context = cdist.context.Context( - target_host=target_host, - initial_manifest=initial_manifest, - base_path=base_path, - exec_path=sys.argv[0], - debug=debug) + self.context = context self.exec_wrapper = cdist.exec.Wrapper( - targe_host = self.target_host, - remote_exec=os.environ['__remote_exec'].split(), - remote_copy=os.environ['__remote_copy'].split() - ) + target_host = self.context.target_host, + remote_exec=self.context.remote_exec, + remote_copy=self.context.remote_copy) - self.explorer = cdist.explorer.Explorer() + self.explorer = cdist.explorer.Explorer(self.context) self.manifest = cdist.manifest.Mamifest() self.log = logging.getLogger(self.context.target_host) @@ -70,9 +60,9 @@ class ConfigInstall: def __init_remote_paths(self): """Initialise remote directory structure""" - self.remove_remote_path(self.context.remote_base_path) - self.remote_mkdir(self.context.remote_base_path) - self.remote_mkdir(self.context.remote_conf_path) + self.exec_wrapper.remove_remote_path(self.context.remote_base_path) + self.exec_wrapper.remote_mkdir(self.context.remote_base_path) + self.exec_wrapper.remote_mkdir(self.context.remote_conf_path) def __init_local_paths(self): """Initialise local directory structure""" @@ -90,12 +80,15 @@ class ConfigInstall: # explicitly! def __init_env(self): """Environment usable for other stuff""" - os.environ['__target_host'] = self.target_host + os.environ['__target_host'] = self.context.target_host if self.debug: os.environ['__debug'] = "yes" def cleanup(self): - self.context.cleanup() + log.debug("Saving " + self.out_path + " to " + self.cache_path) + if os.path.exists(self.context.cache_path): + shutil.rmtree(self.context.cache_path) + shutil.move(self.context.out_path, self.context.cache_path) def object_prepare(self, cdist_object): """Prepare object: Run type explorer + manifest""" @@ -122,7 +115,7 @@ class ConfigInstall: # Setup env Variable: # env = os.environ.copy() - env['__target_host'] = self.target_host + env['__target_host'] = self.context.target_host env['__global'] = self.out_path env["__object"] = os.path.join(self.object_base_path, cdist_object.path) env["__object_id"] = cdist_object.object_id @@ -186,7 +179,7 @@ class ConfigInstall: def deploy_to(self): """Mimic the old deploy to: Deploy to one host""" - log.info("Deploying to " + self.target_host) + log.info("Deploying to " + self.context.target_host) self.stage_prepare() self.stage_run() @@ -196,7 +189,7 @@ class ConfigInstall: self.deploy_to() self.cleanup() log.info("Finished run of %s in %s seconds", - self.target_host, time.time() - start_time) + self.context.target_host, time.time() - start_time) def stage_prepare(self): """Do everything for a deploy, minus the actual code stage""" diff --git a/lib/cdist/context.py b/lib/cdist/context.py index 9550b96e..7bb700e8 100644 --- a/lib/cdist/context.py +++ b/lib/cdist/context.py @@ -22,24 +22,24 @@ import logging import os +import sys +import tempfile #import stat #import shutil -#import sys -#import tempfile #import time # #import cdist.core #import cdist.exec -class Context: +class Context(object): """Hold information about current context""" - def __init__(self, + def __init__(self, target_host, initial_manifest=False, base_path=False, exec_path=sys.argv[0], - debug): + debug=False): self.target_host = target_host @@ -52,8 +52,8 @@ class Context: # Base and Temp Base self.base_path = (base_path or - self.base_path = os.path.abspath(os.path.join( - os.path.dirname(__file__), os.pardir, os.pardir)) + os.path.abspath(os.path.join(os.path.dirname(__file__), + os.pardir, os.pardir))) # Local input self.cache_path = os.path.join(self.base_path, "cache", @@ -71,8 +71,11 @@ class Context: # Local output if '__cdist_out_dir' in os.environ: self.out_path = os.environ['__cdist_out_dir'] + self.temp_dir = None else: - self.out_path = os.path.join(tempfile.mkdtemp(), "out") + self.temp_dir = tempfile.mkdtemp() + self.out_path = os.path.join(self.temp_dir, "out") + self.bin_path = os.path.join(self.out_path, "bin") self.global_explorer_out_path = os.path.join(self.out_path, "explorer") self.object_base_path = os.path.join(self.out_path, "object") @@ -100,17 +103,9 @@ class Context: self.remote_copy = "scp -o User=root -q" def cleanup(self): - # Do not use in __del__: - # http://docs.python.org/reference/datamodel.html#customization - # "other globals referenced by the __del__() method may already have been deleted - # or in the process of being torn down (e.g. the import machinery shutting down)" - # - log.debug("Saving " + self.out_path + " to " + self.cache_path) - # FIXME: raise more beautiful exception / Steven: handle exception - # Remove previous cache - if os.path.exists(self.cache_path): - shutil.rmtree(self.cache_path) - shutil.move(self.out_path, self.cache_path) + """Remove temp stuff""" + if self.temp_dir: + shutil.rmtree(self.temp_dir) def filter(self, record): """Add hostname to logs via logging Filter""" diff --git a/lib/cdist/explorer.py b/lib/cdist/explorer.py index e2c41f8a..b0db60bc 100644 --- a/lib/cdist/explorer.py +++ b/lib/cdist/explorer.py @@ -37,21 +37,10 @@ log = logging.getLogger(__name__) class Explorer: """Execute explorers""" - def __init__(self, - object_base_path, - remote_global_explorer_path, - remote_type_path, - remote_object_path, - type_path, - exec_hint): + def __init__(self, context): + self.context = context - self.object_base_path = object_base_path - self.remote_global_explorer_path = remote_global_explorer_path - self.remote_type_path = remote_type_path - self.remote_object_path = remote_object_path - self.exec_hint = exec_hint - - def run_type_explorer(self, cdist_object) + def run_type_explorer(self, cdist_object): """Run type specific explorers for objects""" cdist_type = cdist_object.type