From 29bb5f383cc8a6bbbc5f0184ea40deb84f683e63 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 11 Oct 2011 16:39:18 +0200 Subject: [PATCH 1/5] use context in cdist/bin Signed-off-by: Nico Schottelius --- bin/cdist | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/bin/cdist b/bin/cdist index ac863e9e..aee96e6e 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) From 2ec3753d945c45829659fd4f3fb5f5c287f6c2be Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 11 Oct 2011 16:45:18 +0200 Subject: [PATCH 2/5] cleanup context in main Signed-off-by: Nico Schottelius --- bin/cdist | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/cdist b/bin/cdist index aee96e6e..fae83ff1 100755 --- a/bin/cdist +++ b/bin/cdist @@ -130,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) From dbd31252a868c35435ce27180a42a99907e12ebb Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 11 Oct 2011 16:45:36 +0200 Subject: [PATCH 3/5] remove CODE_HEADER Signed-off-by: Nico Schottelius --- lib/cdist/config_install.py | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/lib/cdist/config_install.py b/lib/cdist/config_install.py index ef37eb86..cb903a93 100644 --- a/lib/cdist/config_install.py +++ b/lib/cdist/config_install.py @@ -29,26 +29,15 @@ import tempfile import time import cdist.core +import cdist.context import cdist.exec -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, @@ -70,9 +59,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""" @@ -95,7 +84,10 @@ class ConfigInstall: 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""" From 73f1190d55c1cbd0732167b4153c3eed5c21ccfd Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 11 Oct 2011 16:45:45 +0200 Subject: [PATCH 4/5] cleanup context internally Signed-off-by: Nico Schottelius --- lib/cdist/context.py | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) 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""" From 588731661e8a2f13effe29daaa65382de2d3edd4 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 11 Oct 2011 16:49:50 +0200 Subject: [PATCH 5/5] use context in explorer Signed-off-by: Nico Schottelius --- lib/cdist/config_install.py | 19 ++++++++++--------- lib/cdist/explorer.py | 17 +++-------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/lib/cdist/config_install.py b/lib/cdist/config_install.py index cb903a93..2a1c828d 100644 --- a/lib/cdist/config_install.py +++ b/lib/cdist/config_install.py @@ -31,6 +31,8 @@ import time import cdist.core import cdist.context import cdist.exec +import cdist.explorer +#import cdist.manifest class ConfigInstall(object): """Cdist main class to hold arbitrary data""" @@ -40,12 +42,11 @@ class ConfigInstall(object): 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) @@ -79,7 +80,7 @@ class ConfigInstall(object): # 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" @@ -114,7 +115,7 @@ class ConfigInstall(object): # 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 @@ -178,7 +179,7 @@ class ConfigInstall(object): 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() @@ -188,7 +189,7 @@ class ConfigInstall(object): 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/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