++logs and migrate context -> local

Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
This commit is contained in:
Nico Schottelius 2013-08-19 00:33:58 +02:00
parent 58bd230a61
commit f79727559a
5 changed files with 55 additions and 54 deletions

View file

@ -28,26 +28,30 @@ import time
import pprint
import cdist
import cdist.context
import cdist.exec.local
import cdist.exec.remote
from cdist import core
class ConfigInstall(object):
"""Cdist main class to hold arbitrary data"""
def __init__(self, context, dry_run=False):
def __init__(self, local, remote, dry_run=False):
self.context = context
self.local = local
self.remote = remote
self.log = logging.getLogger(self.context.target_host)
self.dry_run = dry_run
self.explorer = core.Explorer(self.context.target_host, self.context.local, self.context.remote)
self.manifest = core.Manifest(self.context.target_host, self.context.local)
self.code = core.Code(self.context.target_host, self.context.local, self.context.remote)
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)
def _init_files_dirs(self):
"""Prepare files and directories for the run"""
self.context.local.create_files_dirs()
self.context.remote.create_files_dirs()
self.local.create_files_dirs()
self.remote.create_files_dirs()
@classmethod
def commandline(cls, args):
@ -110,16 +114,23 @@ class ConfigInstall(object):
try:
context = cdist.context.Context(
local = cdist.local.Local(
target_host=host,
remote_copy=args.remote_copy,
remote_exec=args.remote_exec,
initial_manifest=args.manifest,
add_conf_dirs=args.conf_dir,
out_path=FIXME-OUT_PATH,
exec_path=sys.argv[0],
debug=args.debug)
add_conf_dirs=args.conf_dir,
cache_dir=args.cache_dir)
c = cls(context)
remote = cdist.remote.Remote(
target_host=host,
remote_exec=args.remote_exec,
remote_copy=args.remote_copy)
#initial_manifest=args.manifest,
#debug=args.debug)
c = cls(local, remote)
c.run()
context.cleanup()
@ -149,7 +160,7 @@ class ConfigInstall(object):
self.manifest.run_initial_manifest(self.context.initial_manifest)
self.iterate_until_finished()
self.context.local.save_cache()
self.local.save_cache()
self.log.info("Finished successful run in %s seconds", time.time() - start_time)

View file

@ -23,12 +23,6 @@
import logging
import os
import sys
import tempfile
import shutil
from cdist.exec import local
from cdist.exec import remote
class Context(object):
"""Hold information about current context"""
@ -52,19 +46,6 @@ class Context(object):
self.log = logging.getLogger(self.target_host)
self.log.addFilter(self)
# Local temp directory
# FIXME: if __cdist_out_dir can be given from the outside, the same directory will be used for all hosts
if '__cdist_out_dir' in os.environ:
self.out_path = os.environ['__cdist_out_dir']
self.temp_dir = None
else:
self.temp_dir = tempfile.mkdtemp()
self.out_path = os.path.join(self.temp_dir, "out")
self.local = local.Local(self.target_host, self.out_path,
self.exec_path, add_conf_dirs=add_conf_dirs,
cache_dir=self.cache_dir)
self.initial_manifest = (initial_manifest or
os.path.join(self.local.manifest_path, "init"))
@ -83,11 +64,6 @@ class Context(object):
self.remote = remote.Remote(self.target_host, self.remote_base_path,
self.remote_exec, self.remote_copy)
def cleanup(self):
"""Remove temp stuff"""
if self.temp_dir:
shutil.rmtree(self.temp_dir)
def filter(self, record):
"""Add hostname to logs via logging Filter"""

View file

@ -38,10 +38,10 @@ class Local(object):
Directly accessing the local side from python code is a bug.
"""
def __init__(self, target_host, out_path, exec_path, add_conf_dirs=None, cache_dir=None):
def __init__(self, target_host, exec_path, out_base_path=None, add_conf_dirs=None, cache_dir=None):
self.target_host = target_host
self.out_path = out_path
self.out_base_path = out_base_path
self.exec_path = exec_path
self._add_conf_dirs = add_conf_dirs
@ -71,11 +71,17 @@ 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_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")
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")
# Depending on conf_path
self.global_explorer_path = os.path.join(self.conf_path, "explorer")
@ -160,15 +166,13 @@ class Local(object):
def save_cache(self):
destination = os.path.join(self.cache_path, self.target_host)
self.log.debug("Saving " + self.out_path + " to " + destination)
self.log.debug("Saving " + self.out_base_path + " to " + destination)
if os.path.exists(destination):
shutil.rmtree(destination)
shutil.move(self.out_path, destination)
shutil.move(self.out_base_path, destination)
def _create_context_dirs(self):
self.mkdir(self.out_path)
self.mkdir(self.conf_path)
self.mkdir(self.global_explorer_out_path)
self.mkdir(self.bin_path)

View file

@ -43,9 +43,9 @@ class Remote(object):
Directly accessing the remote side from python code is a bug.
"""
def __init__(self, target_host, remote_base_path, remote_exec, remote_copy):
def __init__(self, target_host, remote_exec, remote_copy):
self.target_host = target_host
self.base_path = remote_base_path
self.base_path = os.environ.get('__cdist_remote_out_dir', "/var/lib/cdist")
self._exec = remote_exec
self._copy = remote_copy

View file

@ -5,3 +5,13 @@
- also add support for a per-host pidfile
- allow user to specify cache dir - to give
full flexibility
- drop context - it is a very small unecessary wrapper
- maye introduce cdist.log instead!
- replace out_path with out_base
- directory under which all the subdirectories are
created
-> by default ~/.cdist/run
-> out_base_path
- drop support for deprecated environment variables
__cdist_out_dir
__cdist_remote_out_dir