From d1708c78b63054963550ea4d525e07d90a02ff35 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Mon, 19 Aug 2013 01:38:28 +0200 Subject: [PATCH] refactoring Signed-off-by: Nico Schottelius --- cdist/config_install.py | 16 ++++++------ cdist/exec/local.py | 48 +++++++++++++++++++----------------- cdist/exec/remote.py | 6 +++-- cdist/{context.py => log.py} | 18 ++++++++------ scripts/cdist | 6 +++-- 5 files changed, 52 insertions(+), 42 deletions(-) rename cdist/{context.py => log.py} (73%) diff --git a/cdist/config_install.py b/cdist/config_install.py index 7b38ae97..c7f141dd 100644 --- a/cdist/config_install.py +++ b/cdist/config_install.py @@ -44,9 +44,9 @@ class ConfigInstall(object): self.log = logging.getLogger(self.local.target_host) self.dry_run = dry_run - self.explorer = core.Explorer(self.target_host, self.local, self.remote) - self.manifest = core.Manifest(self.target_host, self.local) - self.code = core.Code(self.target_host, self.local, self.remote) + self.explorer = core.Explorer(self.local.target_host, self.local, self.remote) + self.manifest = core.Manifest(self.local.target_host, self.local) + self.code = core.Code(self.local.target_host, self.local, self.remote) def _init_files_dirs(self): """Prepare files and directories for the run""" @@ -111,16 +111,18 @@ class ConfigInstall(object): @classmethod def onehost(cls, host, args, parallel): """Configure or install ONE system""" + + log = logging.getLogger(host) try: - local = cdist.local.Local( + local = cdist.exec.local.Local( target_host=host, exec_path=sys.argv[0], initial_manifest=args.manifest, - out_base_path=args.out_base_path, + out_path=args.out_path, add_conf_dirs=args.conf_dir) - remote = cdist.remote.Remote( + remote = cdist.exec.remote.Remote( target_host=host, remote_exec=args.remote_exec, remote_copy=args.remote_copy) @@ -129,7 +131,7 @@ class ConfigInstall(object): c.run() except cdist.Error as e: - context.log.error(e) + log.error(e) if parallel: # We are running in our own process here, need to sys.exit! sys.exit(1) diff --git a/cdist/exec/local.py b/cdist/exec/local.py index e74c8a4f..c7a1c28c 100644 --- a/cdist/exec/local.py +++ b/cdist/exec/local.py @@ -27,6 +27,7 @@ import re import subprocess import shutil import logging +import tempfile import cdist from cdist import core @@ -42,11 +43,20 @@ class Local(object): target_host, exec_path, initial_manifest=None, - out_base_path=None, - add_conf_dirs=None) + out_path=None, + add_conf_dirs=None): self.target_host = target_host - self.out_base_path = out_base_path + + # FIXME: stopped: create base that does not require moving later + if out_path: + self.out_path = out_path + else: + self.out_path = tempfile.mkdtemp() + + # FIXME: as well + self._init_cache_dir(None) + self.exec_path = exec_path self.custom_initial_manifest = initial_manifest @@ -55,7 +65,6 @@ class Local(object): self._init_log() self._init_permissions() self._init_paths() - self._init_cache_dir(cache_dir) self._init_conf_dirs() @@ -78,17 +87,11 @@ class Local(object): os.umask(0o077) def _init_paths(self): - - # FIXME: inherited behaviour from old context - if not self.out_base_path: - self.out_base_path = tempfile.mkdtemp() - - # Depending on out_path - self.bin_path = os.path.join(self.out_base_path, "bin") - self.conf_path = os.path.join(self.out_base_path, "conf") - self.global_explorer_out_path = os.path.join(self.out_base_path, "explorer") - self.object_path = os.path.join(self.out_base_path, "object") + self.bin_path = os.path.join(self.out_path, "bin") + self.conf_path = os.path.join(self.out_path, "conf") + self.global_explorer_out_path = os.path.join(self.out_path, "explorer") + self.object_path = os.path.join(self.out_path, "object") # Depending on conf_path self.global_explorer_path = os.path.join(self.conf_path, "explorer") @@ -117,6 +120,11 @@ class Local(object): if self._add_conf_dirs: self.conf_dirs.extend(self._add_conf_dirs) + def _init_directories(self): + self.mkdir(self.conf_path) + self.mkdir(self.global_explorer_out_path) + self.mkdir(self.bin_path) + def _init_cache_dir(self, cache_dir): if cache_dir: self.cache_path = cache_dir @@ -170,22 +178,16 @@ class Local(object): return self.run(command, env, return_output) def create_files_dirs(self): - self._create_context_dirs() + self._init_directories() self._create_conf_path_and_link_conf_dirs() self._link_types_for_emulator() def save_cache(self): destination = os.path.join(self.cache_path, self.target_host) - self.log.debug("Saving " + self.out_base_path + " to " + destination) + self.log.debug("Saving " + self.out_path + " to " + destination) if os.path.exists(destination): shutil.rmtree(destination) - shutil.move(self.out_base_path, destination) - - - def _create_context_dirs(self): - self.mkdir(self.conf_path) - self.mkdir(self.global_explorer_out_path) - self.mkdir(self.bin_path) + shutil.move(self.out_path, destination) def _create_conf_path_and_link_conf_dirs(self): # Link destination directories diff --git a/cdist/exec/remote.py b/cdist/exec/remote.py index 6d89324b..647474fa 100644 --- a/cdist/exec/remote.py +++ b/cdist/exec/remote.py @@ -60,8 +60,10 @@ class Remote(object): self._init_env() def _init_env(self): - os.environ['__remote_copy'] = self.remote_copy - os.environ['__remote_exec'] = self.remote_exec + """Setup environment for scripts - HERE????""" + # FIXME: better do so in exec functions that require it! + os.environ['__remote_copy'] = self._copy + os.environ['__remote_exec'] = self._exec def create_files_dirs(self): diff --git a/cdist/context.py b/cdist/log.py similarity index 73% rename from cdist/context.py rename to cdist/log.py index 0c11502e..063059a2 100644 --- a/cdist/context.py +++ b/cdist/log.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# 2010-2012 Nico Schottelius (nico-cdist at schottelius.org) +# 2010-2013 Nico Schottelius (nico-cdist at schottelius.org) # # This file is part of cdist. # @@ -21,22 +21,24 @@ # import logging -import os -import sys -class Context(object): +class Log(logging.Logger): """Hold information about current context""" - def __init__(self, target_host) + def __init__(self, name): # Context logging - self.log = logging.getLogger(self.target_host) - self.log.addFilter(self) + self.name = name + # Init real logger + super().__init__(name) + + # Add ourselves as a filter + self.addFilter(self) def filter(self, record): """Add hostname to logs via logging Filter""" - record.msg = self.target_host + ": " + str(record.msg) + record.msg = self.name + ": " + str(record.msg) return True diff --git a/scripts/cdist b/scripts/cdist index e9a5833e..bbf45d17 100755 --- a/scripts/cdist +++ b/scripts/cdist @@ -65,7 +65,7 @@ def commandline(): dest='manifest', required=False) parser['configinstall'].add_argument('-n', '--dry-run', help='Do not execute code', action='store_true') - parser['configinstall'].add_argument('-o', '--output-base-path', + parser['configinstall'].add_argument('-o', '--out-path', help='Directory prefix to save cdist output in') parser['configinstall'].add_argument('-p', '--parallel', help='Operate on multiple hosts in parallel', @@ -159,9 +159,11 @@ if __name__ == "__main__": import os import re import cdist + import cdist.log - log = logging.getLogger("cdist") + logging.setLoggerClass(cdist.log.Log) logging.basicConfig(format='%(levelname)s: %(message)s') + log = logging.getLogger("cdist") if re.match("__", os.path.basename(sys.argv[0])): emulator()