refactoring
Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
This commit is contained in:
		
					parent
					
						
							
								eaf1721212
							
						
					
				
			
			
				commit
				
					
						d1708c78b6
					
				
			
		
					 5 changed files with 52 additions and 42 deletions
				
			
		| 
						 | 
					@ -44,9 +44,9 @@ class ConfigInstall(object):
 | 
				
			||||||
        self.log        = logging.getLogger(self.local.target_host)
 | 
					        self.log        = logging.getLogger(self.local.target_host)
 | 
				
			||||||
        self.dry_run    = dry_run
 | 
					        self.dry_run    = dry_run
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.explorer = core.Explorer(self.target_host, self.local, self.remote)
 | 
					        self.explorer = core.Explorer(self.local.target_host, self.local, self.remote)
 | 
				
			||||||
        self.manifest = core.Manifest(self.target_host, self.local)
 | 
					        self.manifest = core.Manifest(self.local.target_host, self.local)
 | 
				
			||||||
        self.code     = core.Code(self.target_host, self.local, self.remote)
 | 
					        self.code     = core.Code(self.local.target_host, self.local, self.remote)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _init_files_dirs(self):
 | 
					    def _init_files_dirs(self):
 | 
				
			||||||
        """Prepare files and directories for the run"""
 | 
					        """Prepare files and directories for the run"""
 | 
				
			||||||
| 
						 | 
					@ -111,16 +111,18 @@ class ConfigInstall(object):
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def onehost(cls, host, args, parallel):
 | 
					    def onehost(cls, host, args, parallel):
 | 
				
			||||||
        """Configure or install ONE system"""
 | 
					        """Configure or install ONE system"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        log = logging.getLogger(host)
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            local = cdist.local.Local(
 | 
					            local = cdist.exec.local.Local(
 | 
				
			||||||
                target_host=host,
 | 
					                target_host=host,
 | 
				
			||||||
                exec_path=sys.argv[0],
 | 
					                exec_path=sys.argv[0],
 | 
				
			||||||
                initial_manifest=args.manifest,
 | 
					                initial_manifest=args.manifest,
 | 
				
			||||||
                out_base_path=args.out_base_path,
 | 
					                out_path=args.out_path,
 | 
				
			||||||
                add_conf_dirs=args.conf_dir)
 | 
					                add_conf_dirs=args.conf_dir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            remote = cdist.remote.Remote(
 | 
					            remote = cdist.exec.remote.Remote(
 | 
				
			||||||
                target_host=host,
 | 
					                target_host=host,
 | 
				
			||||||
                remote_exec=args.remote_exec,
 | 
					                remote_exec=args.remote_exec,
 | 
				
			||||||
                remote_copy=args.remote_copy)
 | 
					                remote_copy=args.remote_copy)
 | 
				
			||||||
| 
						 | 
					@ -129,7 +131,7 @@ class ConfigInstall(object):
 | 
				
			||||||
            c.run()
 | 
					            c.run()
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        except cdist.Error as e:
 | 
					        except cdist.Error as e:
 | 
				
			||||||
            context.log.error(e)
 | 
					            log.error(e)
 | 
				
			||||||
            if parallel:
 | 
					            if parallel:
 | 
				
			||||||
                # We are running in our own process here, need to sys.exit!
 | 
					                # We are running in our own process here, need to sys.exit!
 | 
				
			||||||
                sys.exit(1)
 | 
					                sys.exit(1)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,6 +27,7 @@ import re
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
import shutil
 | 
					import shutil
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
 | 
					import tempfile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import cdist
 | 
					import cdist
 | 
				
			||||||
from cdist import core
 | 
					from cdist import core
 | 
				
			||||||
| 
						 | 
					@ -42,11 +43,20 @@ class Local(object):
 | 
				
			||||||
                 target_host,
 | 
					                 target_host,
 | 
				
			||||||
                 exec_path,
 | 
					                 exec_path,
 | 
				
			||||||
                 initial_manifest=None,
 | 
					                 initial_manifest=None,
 | 
				
			||||||
                 out_base_path=None,
 | 
					                 out_path=None,
 | 
				
			||||||
                 add_conf_dirs=None)
 | 
					                 add_conf_dirs=None):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.target_host = target_host
 | 
					        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.exec_path = exec_path
 | 
				
			||||||
        self.custom_initial_manifest = initial_manifest
 | 
					        self.custom_initial_manifest = initial_manifest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -55,7 +65,6 @@ class Local(object):
 | 
				
			||||||
        self._init_log()
 | 
					        self._init_log()
 | 
				
			||||||
        self._init_permissions()
 | 
					        self._init_permissions()
 | 
				
			||||||
        self._init_paths()
 | 
					        self._init_paths()
 | 
				
			||||||
        self._init_cache_dir(cache_dir)
 | 
					 | 
				
			||||||
        self._init_conf_dirs()
 | 
					        self._init_conf_dirs()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -78,17 +87,11 @@ class Local(object):
 | 
				
			||||||
        os.umask(0o077)
 | 
					        os.umask(0o077)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _init_paths(self):
 | 
					    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
 | 
					        # Depending on out_path
 | 
				
			||||||
        self.bin_path = os.path.join(self.out_base_path, "bin")
 | 
					        self.bin_path = os.path.join(self.out_path, "bin")
 | 
				
			||||||
        self.conf_path = os.path.join(self.out_base_path, "conf")
 | 
					        self.conf_path = os.path.join(self.out_path, "conf")
 | 
				
			||||||
        self.global_explorer_out_path = os.path.join(self.out_base_path, "explorer")
 | 
					        self.global_explorer_out_path = os.path.join(self.out_path, "explorer")
 | 
				
			||||||
        self.object_path = os.path.join(self.out_base_path, "object")
 | 
					        self.object_path = os.path.join(self.out_path, "object")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Depending on conf_path
 | 
					        # Depending on conf_path
 | 
				
			||||||
        self.global_explorer_path = os.path.join(self.conf_path, "explorer")
 | 
					        self.global_explorer_path = os.path.join(self.conf_path, "explorer")
 | 
				
			||||||
| 
						 | 
					@ -117,6 +120,11 @@ class Local(object):
 | 
				
			||||||
        if self._add_conf_dirs:
 | 
					        if self._add_conf_dirs:
 | 
				
			||||||
            self.conf_dirs.extend(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):
 | 
					    def _init_cache_dir(self, cache_dir):
 | 
				
			||||||
        if cache_dir:
 | 
					        if cache_dir:
 | 
				
			||||||
            self.cache_path = cache_dir
 | 
					            self.cache_path = cache_dir
 | 
				
			||||||
| 
						 | 
					@ -170,22 +178,16 @@ class Local(object):
 | 
				
			||||||
        return self.run(command, env, return_output)
 | 
					        return self.run(command, env, return_output)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def create_files_dirs(self):
 | 
					    def create_files_dirs(self):
 | 
				
			||||||
        self._create_context_dirs()
 | 
					        self._init_directories()
 | 
				
			||||||
        self._create_conf_path_and_link_conf_dirs()
 | 
					        self._create_conf_path_and_link_conf_dirs()
 | 
				
			||||||
        self._link_types_for_emulator()
 | 
					        self._link_types_for_emulator()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def save_cache(self):
 | 
					    def save_cache(self):
 | 
				
			||||||
        destination = os.path.join(self.cache_path, self.target_host)
 | 
					        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):
 | 
					        if os.path.exists(destination):
 | 
				
			||||||
            shutil.rmtree(destination)
 | 
					            shutil.rmtree(destination)
 | 
				
			||||||
        shutil.move(self.out_base_path, destination)
 | 
					        shutil.move(self.out_path, destination)
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def _create_context_dirs(self):
 | 
					 | 
				
			||||||
        self.mkdir(self.conf_path)
 | 
					 | 
				
			||||||
        self.mkdir(self.global_explorer_out_path)
 | 
					 | 
				
			||||||
        self.mkdir(self.bin_path)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _create_conf_path_and_link_conf_dirs(self):
 | 
					    def _create_conf_path_and_link_conf_dirs(self):
 | 
				
			||||||
        # Link destination directories
 | 
					        # Link destination directories
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -60,8 +60,10 @@ class Remote(object):
 | 
				
			||||||
        self._init_env()
 | 
					        self._init_env()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _init_env(self):
 | 
					    def _init_env(self):
 | 
				
			||||||
        os.environ['__remote_copy'] = self.remote_copy
 | 
					        """Setup environment for scripts - HERE????"""
 | 
				
			||||||
        os.environ['__remote_exec'] = self.remote_exec
 | 
					        # 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):
 | 
					    def create_files_dirs(self):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,7 @@
 | 
				
			||||||
#!/usr/bin/env python3
 | 
					#!/usr/bin/env python3
 | 
				
			||||||
# -*- coding: utf-8 -*-
 | 
					# -*- 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.
 | 
					# This file is part of cdist.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
| 
						 | 
					@ -21,22 +21,24 @@
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import os
 | 
					 | 
				
			||||||
import sys
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Context(object):
 | 
					class Log(logging.Logger):
 | 
				
			||||||
    """Hold information about current context"""
 | 
					    """Hold information about current context"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, target_host)
 | 
					    def __init__(self, name):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Context logging
 | 
					        # Context logging
 | 
				
			||||||
        self.log = logging.getLogger(self.target_host)
 | 
					        self.name = name
 | 
				
			||||||
        self.log.addFilter(self)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Init real logger
 | 
				
			||||||
 | 
					        super().__init__(name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Add ourselves as a filter
 | 
				
			||||||
 | 
					        self.addFilter(self)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def filter(self, record):
 | 
					    def filter(self, record):
 | 
				
			||||||
        """Add hostname to logs via logging Filter"""
 | 
					        """Add hostname to logs via logging Filter"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        record.msg = self.target_host + ": " + str(record.msg)
 | 
					        record.msg = self.name + ": " + str(record.msg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return True
 | 
					        return True
 | 
				
			||||||
| 
						 | 
					@ -65,7 +65,7 @@ def commandline():
 | 
				
			||||||
         dest='manifest', required=False)
 | 
					         dest='manifest', required=False)
 | 
				
			||||||
    parser['configinstall'].add_argument('-n', '--dry-run',
 | 
					    parser['configinstall'].add_argument('-n', '--dry-run',
 | 
				
			||||||
         help='Do not execute code', action='store_true')
 | 
					         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')
 | 
					         help='Directory prefix to save cdist output in')
 | 
				
			||||||
    parser['configinstall'].add_argument('-p', '--parallel',
 | 
					    parser['configinstall'].add_argument('-p', '--parallel',
 | 
				
			||||||
         help='Operate on multiple hosts in parallel',
 | 
					         help='Operate on multiple hosts in parallel',
 | 
				
			||||||
| 
						 | 
					@ -159,9 +159,11 @@ if __name__ == "__main__":
 | 
				
			||||||
        import os
 | 
					        import os
 | 
				
			||||||
        import re
 | 
					        import re
 | 
				
			||||||
        import cdist
 | 
					        import cdist
 | 
				
			||||||
 | 
					        import cdist.log
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        log = logging.getLogger("cdist")
 | 
					        logging.setLoggerClass(cdist.log.Log)
 | 
				
			||||||
        logging.basicConfig(format='%(levelname)s: %(message)s')
 | 
					        logging.basicConfig(format='%(levelname)s: %(message)s')
 | 
				
			||||||
 | 
					        log = logging.getLogger("cdist")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if re.match("__", os.path.basename(sys.argv[0])):
 | 
					        if re.match("__", os.path.basename(sys.argv[0])):
 | 
				
			||||||
            emulator()
 | 
					            emulator()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue