Merge remote-tracking branch 'telmich/master'

This commit is contained in:
Steven Armstrong 2011-10-07 13:53:08 +02:00
commit aafdd62698
6 changed files with 70 additions and 80 deletions

View file

@ -112,12 +112,9 @@ if __name__ == "__main__":
import cdist.emulator import cdist.emulator
cdist.emulator.run(sys.argv) cdist.emulator.run(sys.argv)
else: else:
import cdist
import cdist.banner import cdist.banner
import cdist.config import cdist.config
import cdist.exec
import cdist.install import cdist.install
import cdist.path
commandline() commandline()

View file

@ -8,6 +8,8 @@
- Create GlobalExplorer - Create GlobalExplorer
- base_dir passing in config/config_install superseeded by __cdist_base_dir?
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
- insert prefix into logger to distinguish between modules - insert prefix into logger to distinguish between modules

View file

@ -42,7 +42,7 @@ def config(args):
os.environ['__remote_copy'] = "scp -o User=root -q" os.environ['__remote_copy'] = "scp -o User=root -q"
for host in args.host: for host in args.host:
c = Config(host, initial_manifest=args.manifest, home=args.cdist_home, debug=args.debug) c = Config(host, initial_manifest=args.manifest, base_dir=args.cdist_home, debug=args.debug)
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)

View file

@ -25,10 +25,9 @@ import os
import stat import stat
import sys import sys
import cdist.emulator import cdist.context
import cdist.path
import cdist.core import cdist.core
import cdist.emulator
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -38,6 +37,7 @@ class ConfigInstall:
"""Cdist main class to hold arbitrary data""" """Cdist main class to hold arbitrary data"""
def __init__(self, target_host, initial_manifest=False, def __init__(self, target_host, initial_manifest=False,
base_dir=False,
exec_path=sys.argv[0], exec_path=sys.argv[0],
debug=False): debug=False):
@ -48,7 +48,7 @@ class ConfigInstall:
self.exec_path = exec_path self.exec_path = exec_path
self.context = cdist.context.Context(self.target_host, self.context = cdist.context.Context(self.target_host,
initial_manifest=initial_manifest, initial_manifest=initial_manifest, base_dir=base_dir,
debug=debug) debug=debug)
def cleanup(self): def cleanup(self):
@ -83,8 +83,7 @@ class ConfigInstall:
# Information required in every manifest # Information required in every manifest
env['__target_host'] = self.target_host env['__target_host'] = self.target_host
# FIXME: __global == __cdist_out_dir # FIXME: __global == __cdist_out_dir - duplication
# FIXME: __global? shouldn't this be $global?
env['__global'] = self.context.out_dir env['__global'] = self.context.out_dir
# Submit debug flag to manifest, can be used by emulator and types # Submit debug flag to manifest, can be used by emulator and types
@ -95,7 +94,7 @@ class ConfigInstall:
env['__cdist_manifest'] = manifest env['__cdist_manifest'] = manifest
# Required to find types # Required to find types
env['__cdist_type_base_dir'] = self.path.type_base_dir env['__cdist_type_base_dir'] = type.path
# Other environment stuff # Other environment stuff
if extra_env: if extra_env:
@ -103,17 +102,17 @@ class ConfigInstall:
cdist.exec.shell_run_or_debug_fail(manifest, [manifest], env=env) cdist.exec.shell_run_or_debug_fail(manifest, [manifest], env=env)
################################################################################ def object_run(self, cdist_object):
def object_run(self, cdist_object, mode):
"""Run gencode or code for an object""" """Run gencode or code for an object"""
log.debug("Running %s from %s", mode, cdist_object) log.debug("Running %s from %s", mode, cdist_object)
# FIXME: replace with new object interface # Catch requirements, which re-call us
file=os.path.join(self.path.object_dir(cdist_object), "require") if cdist_object.ran:
requirements = cdist.path.file_to_list(file) return
type = self.path.get_type_from_object(cdist_object)
for requirement in requirements: type = cdist_object.type
for requirement in cdist_object.requirements:
log.debug("Object %s requires %s", cdist_object, requirement) log.debug("Object %s requires %s", cdist_object, requirement)
self.object_run(requirement, mode=mode) self.object_run(requirement, mode=mode)
@ -122,22 +121,18 @@ class ConfigInstall:
# #
env = os.environ.copy() env = os.environ.copy()
env['__target_host'] = self.target_host env['__target_host'] = self.target_host
env['__global'] = self.path.out_dir env['__global'] = self.context.out_dir
env["__object"] = self.path.object_dir(cdist_object) env["__object"] = cdist_object.path
env["__object_id"] = self.path.get_object_id_from_object(cdist_object) env["__object_id"] = cdist_object.object_id
env["__object_fq"] = cdist_object env["__object_fq"] = cdist_object.name
env["__type"] = self.path.type_dir(type) env["__type"] = type.name
# gencode
for cmd in ["local", "remote"]:
bin = getattr(type, "gencode_" + cmd)
if mode == "gencode":
paths = [
self.path.type_dir(type, "gencode-local"),
self.path.type_dir(type, "gencode-remote")
]
for bin in paths:
if os.path.isfile(bin): if os.path.isfile(bin):
# omit "gen" from gencode and use it for output base outfile = getattr(cdist_object, "code_" + cmd)
outfile=os.path.join(self.path.object_dir(cdist_object),
os.path.basename(bin)[3:])
outfile_fd = open(outfile, "w") outfile_fd = open(outfile, "w")
@ -156,26 +151,22 @@ class ConfigInstall:
else: else:
# Add header and make executable - identically to 0o700 # Add header and make executable - identically to 0o700
os.chmod(outfile, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR) os.chmod(outfile, stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR)
cdist_object.changed=True
# Mark object as changed # code local
open(os.path.join(self.path.object_dir(cdist_object), "changed"), "w").close() code_local = cdist_object.code_local
if os.path.isfile(code_local):
cdist.exec.run_or_fail([code_local])
# code remote
if mode == "code": local_remote_code = cdist_object.code_remote
local_dir = self.path.object_dir(cdist_object) remote_remote_code = cdist_object.remote_code_remote
remote_dir = self.path.remote_object_dir(cdist_object)
bin = os.path.join(local_dir, "code-local")
if os.path.isfile(bin):
cdist.exec.run_or_fail([bin])
local_remote_code = os.path.join(local_dir, "code-remote")
remote_remote_code = os.path.join(remote_dir, "code-remote")
if os.path.isfile(local_remote_code): if os.path.isfile(local_remote_code):
self.path.transfer_file(local_remote_code, remote_remote_code) self.context.transfer_file(local_remote_code, remote_remote_code)
cdist.exec.run_or_fail([remote_remote_code], remote_prefix=True) cdist.exec.run_or_fail([remote_remote_code], remote_prefix=True)
cdist_object.ran = True
### Cleaned / check functions: Round 1 :-) ################################# ### Cleaned / check functions: Round 1 :-) #################################
def run_type_explorer(self, cdist_object): def run_type_explorer(self, cdist_object):
"""Run type specific explorers for objects""" """Run type specific explorers for objects"""
@ -237,11 +228,9 @@ class ConfigInstall:
def stage_run(self): def stage_run(self):
"""The final (and real) step of deployment""" """The final (and real) step of deployment"""
log.info("Generating and executing code") log.info("Generating and executing code")
# Now do the final steps over the existing objects
for cdist_object in cdist.core.Object.list_objects(): for cdist_object in cdist.core.Object.list_objects():
log.debug("Run object: %s", cdist_object) log.debug("Run object: %s", cdist_object)
self.object_run(cdist_object, mode="gencode") self.object_run(cdist_object)
self.object_run(cdist_object, mode="code")
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"""
@ -258,8 +247,8 @@ class ConfigInstall:
"""Ensure the base directories are cleaned up""" """Ensure the base directories are cleaned up"""
log.debug("Creating clean directory structure") log.debug("Creating clean directory structure")
self.path.remove_remote_dir(cdist.path.REMOTE_BASE_DIR) self.context.remove_remote_dir(self.context.remote_base_dir)
self.path.remote_mkdir(cdist.path.REMOTE_BASE_DIR) self.context.remote_mkdir(self.context.remote_base_dir)
self.link_emulator() self.link_emulator()
def stage_prepare(self): def stage_prepare(self):

View file

@ -33,22 +33,25 @@ import cdist.exec
class Context: class Context:
"""Storing context dependent information""" """Storing context dependent information"""
def __init__(self, target_host, initial_manifest=False, debug=False): def __init__(self, target_host, initial_manifest=False, base_dir=False,
debug=False):
self.target_host = target_host self.target_host = target_host
# Base and Temp Base # Base and Temp Base
if "__cdist_base_dir" in os.environ: if base_dir:
self.base_dir = base_dir
elif "__cdist_base_dir" in os.environ:
self.base_dir = os.environ['__cdist_base_dir'] self.base_dir = os.environ['__cdist_base_dir']
else: else:
self.base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) self.base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
# Input directories # Input directories
self.cache_dir = os.path.join(self.base_dir, "cache", target_host) self.cache_dir = os.path.join(self.base_dir, "cache", target_host)
self.conf_dir = os.path.join(self.base_dir, "conf")
self.manifest_dir = os.path.join(self.conf_dir, "manifest") self.manifest_dir = os.path.join(self.conf_dir, "manifest")
# Probably unused paths # Probably unused paths
# self.conf_dir = os.path.join(self.base_dir, "conf")
# self.global_explorer_dir = os.path.join(self.conf_dir, "explorer") # self.global_explorer_dir = os.path.join(self.conf_dir, "explorer")
# self.lib_dir = os.path.join(self.base_dir, "lib") # self.lib_dir = os.path.join(self.base_dir, "lib")
# self.type_base_dir = os.path.join(self.conf_dir, "type") # self.type_base_dir = os.path.join(self.conf_dir, "type")
@ -103,7 +106,7 @@ class Context:
"""Initialise output directory structure""" """Initialise output directory structure"""
# Create base dir, if user supplied and not existing # Create base dir, if user supplied and not existing
if not os.isdir(self.base_dir): if not os.path.isdir(self.base_dir):
os.mkdir(self.base_dir) os.mkdir(self.base_dir)
os.mkdir(self.out_dir) os.mkdir(self.out_dir)

View file

@ -24,7 +24,6 @@ import logging
import os import os
import cdist import cdist
import cdist.path
log = logging.getLogger(__name__) log = logging.getLogger(__name__)