Merge remote-tracking branch 'telmich/master'
This commit is contained in:
commit
823197cfb5
4 changed files with 48 additions and 65 deletions
14
bin/cdist
14
bin/cdist
|
@ -115,10 +115,14 @@ def configinstall(args, mode):
|
||||||
time_start = time.time()
|
time_start = time.time()
|
||||||
|
|
||||||
for host in args.host:
|
for host in args.host:
|
||||||
c = mode(host,
|
context = cdist.context.Context(
|
||||||
initial_manifest=args.manifest,
|
target_host=host,
|
||||||
base_path=args.cdist_home,
|
initial_manifest=args.manifest,
|
||||||
debug=args.debug)
|
base_path=args.cdist_home,
|
||||||
|
exec_path=sys.argv[0],
|
||||||
|
debug=args.debug)
|
||||||
|
|
||||||
|
c = mode(context)
|
||||||
if args.parallel:
|
if args.parallel:
|
||||||
log.debug("Creating child process for %s", host)
|
log.debug("Creating child process for %s", host)
|
||||||
process[host] = multiprocessing.Process(target=c.deploy_and_cleanup)
|
process[host] = multiprocessing.Process(target=c.deploy_and_cleanup)
|
||||||
|
@ -126,6 +130,8 @@ def configinstall(args, mode):
|
||||||
else:
|
else:
|
||||||
c.deploy_and_cleanup()
|
c.deploy_and_cleanup()
|
||||||
|
|
||||||
|
context.cleanup()
|
||||||
|
|
||||||
if args.parallel:
|
if args.parallel:
|
||||||
for p in process.keys():
|
for p in process.keys():
|
||||||
log.debug("Joining process %s", p)
|
log.debug("Joining process %s", p)
|
||||||
|
|
|
@ -29,34 +29,24 @@ import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import cdist.core
|
import cdist.core
|
||||||
|
import cdist.context
|
||||||
import cdist.exec
|
import cdist.exec
|
||||||
|
import cdist.explorer
|
||||||
|
#import cdist.manifest
|
||||||
|
|
||||||
CODE_HEADER = "#!/bin/sh -e\n"
|
class ConfigInstall(object):
|
||||||
|
|
||||||
class ConfigInstall:
|
|
||||||
"""Cdist main class to hold arbitrary data"""
|
"""Cdist main class to hold arbitrary data"""
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self, context):
|
||||||
target_host,
|
|
||||||
initial_manifest=False,
|
|
||||||
base_path=False,
|
|
||||||
exec_path=sys.argv[0],
|
|
||||||
debug=False):
|
|
||||||
|
|
||||||
self.context = cdist.context.Context(
|
self.context = context
|
||||||
target_host=target_host,
|
|
||||||
initial_manifest=initial_manifest,
|
|
||||||
base_path=base_path,
|
|
||||||
exec_path=sys.argv[0],
|
|
||||||
debug=debug)
|
|
||||||
|
|
||||||
self.exec_wrapper = cdist.exec.Wrapper(
|
self.exec_wrapper = cdist.exec.Wrapper(
|
||||||
targe_host = self.target_host,
|
target_host = self.context.target_host,
|
||||||
remote_exec=os.environ['__remote_exec'].split(),
|
remote_exec=self.context.remote_exec,
|
||||||
remote_copy=os.environ['__remote_copy'].split()
|
remote_copy=self.context.remote_copy)
|
||||||
)
|
|
||||||
|
|
||||||
self.explorer = cdist.explorer.Explorer()
|
self.explorer = cdist.explorer.Explorer(self.context)
|
||||||
self.manifest = cdist.manifest.Mamifest()
|
self.manifest = cdist.manifest.Mamifest()
|
||||||
|
|
||||||
self.log = logging.getLogger(self.context.target_host)
|
self.log = logging.getLogger(self.context.target_host)
|
||||||
|
@ -70,9 +60,9 @@ class ConfigInstall:
|
||||||
|
|
||||||
def __init_remote_paths(self):
|
def __init_remote_paths(self):
|
||||||
"""Initialise remote directory structure"""
|
"""Initialise remote directory structure"""
|
||||||
self.remove_remote_path(self.context.remote_base_path)
|
self.exec_wrapper.remove_remote_path(self.context.remote_base_path)
|
||||||
self.remote_mkdir(self.context.remote_base_path)
|
self.exec_wrapper.remote_mkdir(self.context.remote_base_path)
|
||||||
self.remote_mkdir(self.context.remote_conf_path)
|
self.exec_wrapper.remote_mkdir(self.context.remote_conf_path)
|
||||||
|
|
||||||
def __init_local_paths(self):
|
def __init_local_paths(self):
|
||||||
"""Initialise local directory structure"""
|
"""Initialise local directory structure"""
|
||||||
|
@ -90,12 +80,15 @@ class ConfigInstall:
|
||||||
# explicitly!
|
# explicitly!
|
||||||
def __init_env(self):
|
def __init_env(self):
|
||||||
"""Environment usable for other stuff"""
|
"""Environment usable for other stuff"""
|
||||||
os.environ['__target_host'] = self.target_host
|
os.environ['__target_host'] = self.context.target_host
|
||||||
if self.debug:
|
if self.debug:
|
||||||
os.environ['__debug'] = "yes"
|
os.environ['__debug'] = "yes"
|
||||||
|
|
||||||
def cleanup(self):
|
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):
|
def object_prepare(self, cdist_object):
|
||||||
"""Prepare object: Run type explorer + manifest"""
|
"""Prepare object: Run type explorer + manifest"""
|
||||||
|
@ -122,7 +115,7 @@ class ConfigInstall:
|
||||||
# Setup env Variable:
|
# Setup env Variable:
|
||||||
#
|
#
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
env['__target_host'] = self.target_host
|
env['__target_host'] = self.context.target_host
|
||||||
env['__global'] = self.out_path
|
env['__global'] = self.out_path
|
||||||
env["__object"] = os.path.join(self.object_base_path, cdist_object.path)
|
env["__object"] = os.path.join(self.object_base_path, cdist_object.path)
|
||||||
env["__object_id"] = cdist_object.object_id
|
env["__object_id"] = cdist_object.object_id
|
||||||
|
@ -186,7 +179,7 @@ class ConfigInstall:
|
||||||
|
|
||||||
def deploy_to(self):
|
def deploy_to(self):
|
||||||
"""Mimic the old deploy to: Deploy to one host"""
|
"""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_prepare()
|
||||||
self.stage_run()
|
self.stage_run()
|
||||||
|
|
||||||
|
@ -196,7 +189,7 @@ class ConfigInstall:
|
||||||
self.deploy_to()
|
self.deploy_to()
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
log.info("Finished run of %s in %s seconds",
|
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):
|
def stage_prepare(self):
|
||||||
"""Do everything for a deploy, minus the actual code stage"""
|
"""Do everything for a deploy, minus the actual code stage"""
|
||||||
|
|
|
@ -22,16 +22,16 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
#import stat
|
#import stat
|
||||||
#import shutil
|
#import shutil
|
||||||
#import sys
|
|
||||||
#import tempfile
|
|
||||||
#import time
|
#import time
|
||||||
#
|
#
|
||||||
#import cdist.core
|
#import cdist.core
|
||||||
#import cdist.exec
|
#import cdist.exec
|
||||||
|
|
||||||
class Context:
|
class Context(object):
|
||||||
"""Hold information about current context"""
|
"""Hold information about current context"""
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
|
@ -39,7 +39,7 @@ class Context:
|
||||||
initial_manifest=False,
|
initial_manifest=False,
|
||||||
base_path=False,
|
base_path=False,
|
||||||
exec_path=sys.argv[0],
|
exec_path=sys.argv[0],
|
||||||
debug):
|
debug=False):
|
||||||
|
|
||||||
self.target_host = target_host
|
self.target_host = target_host
|
||||||
|
|
||||||
|
@ -52,8 +52,8 @@ class Context:
|
||||||
|
|
||||||
# Base and Temp Base
|
# Base and Temp Base
|
||||||
self.base_path = (base_path or
|
self.base_path = (base_path or
|
||||||
self.base_path = os.path.abspath(os.path.join(
|
os.path.abspath(os.path.join(os.path.dirname(__file__),
|
||||||
os.path.dirname(__file__), os.pardir, os.pardir))
|
os.pardir, os.pardir)))
|
||||||
|
|
||||||
# Local input
|
# Local input
|
||||||
self.cache_path = os.path.join(self.base_path, "cache",
|
self.cache_path = os.path.join(self.base_path, "cache",
|
||||||
|
@ -71,8 +71,11 @@ class Context:
|
||||||
# Local output
|
# Local output
|
||||||
if '__cdist_out_dir' in os.environ:
|
if '__cdist_out_dir' in os.environ:
|
||||||
self.out_path = os.environ['__cdist_out_dir']
|
self.out_path = os.environ['__cdist_out_dir']
|
||||||
|
self.temp_dir = None
|
||||||
else:
|
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.bin_path = os.path.join(self.out_path, "bin")
|
||||||
self.global_explorer_out_path = os.path.join(self.out_path, "explorer")
|
self.global_explorer_out_path = os.path.join(self.out_path, "explorer")
|
||||||
self.object_base_path = os.path.join(self.out_path, "object")
|
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"
|
self.remote_copy = "scp -o User=root -q"
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
# Do not use in __del__:
|
"""Remove temp stuff"""
|
||||||
# http://docs.python.org/reference/datamodel.html#customization
|
if self.temp_dir:
|
||||||
# "other globals referenced by the __del__() method may already have been deleted
|
shutil.rmtree(self.temp_dir)
|
||||||
# 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)
|
|
||||||
|
|
||||||
def filter(self, record):
|
def filter(self, record):
|
||||||
"""Add hostname to logs via logging Filter"""
|
"""Add hostname to logs via logging Filter"""
|
||||||
|
|
|
@ -37,21 +37,10 @@ log = logging.getLogger(__name__)
|
||||||
class Explorer:
|
class Explorer:
|
||||||
"""Execute explorers"""
|
"""Execute explorers"""
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self, context):
|
||||||
object_base_path,
|
self.context = context
|
||||||
remote_global_explorer_path,
|
|
||||||
remote_type_path,
|
|
||||||
remote_object_path,
|
|
||||||
type_path,
|
|
||||||
exec_hint):
|
|
||||||
|
|
||||||
self.object_base_path = object_base_path
|
def run_type_explorer(self, cdist_object):
|
||||||
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)
|
|
||||||
"""Run type specific explorers for objects"""
|
"""Run type specific explorers for objects"""
|
||||||
|
|
||||||
cdist_type = cdist_object.type
|
cdist_type = cdist_object.type
|
||||||
|
|
Loading…
Reference in a new issue