From b9335bb7ce221abf09cc96951edd3ae2587d8ae9 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 27 Sep 2011 10:35:25 +0200 Subject: [PATCH 1/6] update os/os_version explorer to support owl (openwall linux) Signed-off-by: Nico Schottelius --- conf/explorer/os | 2 +- conf/explorer/os_version | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/conf/explorer/os b/conf/explorer/os index 3e1582ec..e59301e7 100755 --- a/conf/explorer/os +++ b/conf/explorer/os @@ -65,7 +65,7 @@ if [ -f /etc/SuSE-release ]; then exit 0 fi -if uname -r | grep -s '.owl' >/dev/null 2>&1; then +if [ -f /etc/owl-release ]; then echo owl exit 0 fi diff --git a/conf/explorer/os_version b/conf/explorer/os_version index 08fda60b..ef80e8fc 100755 --- a/conf/explorer/os_version +++ b/conf/explorer/os_version @@ -42,6 +42,9 @@ case "$($__explorer/os)" in *bsd|solaris) uname -r ;; + owl) + cat /etc/owl-release + ;; redhat|centos) cat /etc/redhat-release ;; From 6f1a13b531cc0664721b5763f8f2be17e8beb807 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 27 Sep 2011 13:38:25 +0200 Subject: [PATCH 2/6] move emulator link to emulator module and make source variable (exec_path) Signed-off-by: Nico Schottelius --- lib/cdist/config.py | 115 +++++++----------------------------------- lib/cdist/emulator.py | 9 ++++ lib/cdist/path.py | 10 ---- 3 files changed, 26 insertions(+), 108 deletions(-) diff --git a/lib/cdist/config.py b/lib/cdist/config.py index a5a2252f..cc0f7817 100644 --- a/lib/cdist/config.py +++ b/lib/cdist/config.py @@ -24,9 +24,11 @@ import datetime import logging import os import stat +import sys log = logging.getLogger(__name__) +import cdist.emulator import cdist.path CODE_HEADER = "#!/bin/sh -e\n" @@ -35,12 +37,18 @@ class Config: """Cdist main class to hold arbitrary data""" def __init__(self, target_host, - initial_manifest=False, remote_user="root", - home=None, debug=False): + initial_manifest=False, + remote_user="root", + home=None, + exec_path=sys.argv[0], + debug=False): - self.target_host = target_host - self.debug = debug - self.remote_user = remote_user + self.target_host = target_host + self.debug = debug + self.remote_user = remote_user + self.exec_path = exec_path + + # FIXME: broken - construct elsewhere! self.remote_prefix = ["ssh", self.remote_user + "@" + self.target_host] self.path = cdist.path.Path(self.target_host, @@ -94,7 +102,7 @@ class Config: output = os.path.join(self.path.type_explorer_output_dir(cdist_object), explorer) output_fd = open(output, mode='w') log.debug("%s exploring %s using %s storing to %s", - cdist_object, explorer, remote_cmd, output) + cdist_object, explorer, remote_cmd, output) cdist.exec.run_or_fail(remote_cmd, stdout=output_fd, remote_prefix=self.remote_prefix) output_fd.close() @@ -106,6 +114,9 @@ class Config: self.path.remove_remote_dir(cdist.path.REMOTE_BASE_DIR) self.path.remote_mkdir(cdist.path.REMOTE_BASE_DIR) + cdist.emulator.link(self.exec_path, + self.path.bin_dir, self.path.list_types()) + def run_initial_manifest(self): """Run the initial manifest""" env = { "__manifest" : self.path.manifest_dir } @@ -299,95 +310,3 @@ def config(args): time_end = datetime.datetime.now() log.info("Total processing time for %s host(s): %s", len(args.host), (time_end - time_start).total_seconds()) - -def install(args): - """Install remote system""" - process = {} - -def commandline(): - """Parse command line""" - # Construct parser others can reuse - parser = {} - # Options _all_ parsers have in common - parser['most'] = argparse.ArgumentParser(add_help=False) - parser['most'].add_argument('-d', '--debug', - help='Set log level to debug', action='store_true') - - # Main subcommand parser - parser['main'] = argparse.ArgumentParser(description='cdist ' + cdist.VERSION) - parser['main'].add_argument('-V', '--version', - help='Show version', action='version', - version='%(prog)s ' + cdist.VERSION) - parser['sub'] = parser['main'].add_subparsers(title="Commands") - - # Banner - parser['banner'] = parser['sub'].add_parser('banner', - add_help=False) - parser['banner'].set_defaults(func=cdist.banner.banner) - - # Config and install (common stuff) - parser['configinstall'] = argparse.ArgumentParser(add_help=False) - parser['configinstall'].add_argument('host', nargs='+', - help='one or more hosts to operate on') - parser['configinstall'].add_argument('-c', '--cdist-home', - help='Change cdist home (default: .. from bin directory)', - action='store') - parser['configinstall'].add_argument('-i', '--initial-manifest', - help='Path to a cdist manifest', - dest='manifest', required=False) - parser['configinstall'].add_argument('-p', '--parallel', - help='Operate on multiple hosts in parallel', - action='store_true', dest='parallel') - parser['configinstall'].add_argument('-s', '--sequential', - help='Operate on multiple hosts sequentially (default)', - action='store_false', dest='parallel') - - # Config - parser['config'] = parser['sub'].add_parser('config', - parents=[parser['most'], parser['configinstall']]) - parser['config'].set_defaults(func=config) - - # Install - parser['install'] = parser['sub'].add_parser('install', - parents=[parser['most'], parser['configinstall']]) - parser['install'].set_defaults(func=install) - - for p in parser: - parser[p].epilog = "Get cdist at http://www.nico.schottelius.org/software/cdist/" - - args = parser['main'].parse_args(sys.argv[1:]) - - # Most subcommands have --debug, so handle it here - if 'debug' in args: - if args.debug: - logging.root.setLevel(logging.DEBUG) - log.debug(args) - - args.func(args) - - -if __name__ == "__main__": - try: - logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') - - if re.match(TYPE_PREFIX, os.path.basename(sys.argv[0])): - cdist_lib = os.environ["__cdist_python_lib"] - sys.path.insert(0, cdist_lib) - import cdist.emulator - cdist.emulator.emulator(sys.argv) - else: - cdist_lib = os.path.abspath(os.path.join(os.path.dirname(__file__), - '../lib')) - sys.path.insert(0, cdist_lib) - - import cdist - import cdist.banner - import cdist.exec - import cdist.path - - commandline() - except KeyboardInterrupt: - sys.exit(0) - except cdist.Error as e: - log.error(e) - sys.exit(1) diff --git a/lib/cdist/emulator.py b/lib/cdist/emulator.py index bd97f69b..b96b62dd 100644 --- a/lib/cdist/emulator.py +++ b/lib/cdist/emulator.py @@ -133,3 +133,12 @@ def emulator(argv): # sys.exit(1) print("Finished " + type + "/" + object_id + repr(params)) + + +def link(exec_path, bin_dir, type_list): + """Link type names to cdist-type-emulator""" + source = os.path.abspath(exec_path) + for type in type_list: + destination = os.path.join(bin_dir, type) + log.debug("Linking %s to %s", source, destination) + os.symlink(source, destination) diff --git a/lib/cdist/path.py b/lib/cdist/path.py index 277b1401..e416c42d 100644 --- a/lib/cdist/path.py +++ b/lib/cdist/path.py @@ -94,7 +94,6 @@ class Path: # Setup binary directory + contents self.bin_dir = os.path.join(self.out_dir, "bin") os.mkdir(self.bin_dir) - self.link_type_to_emulator() # List of type explorers transferred self.type_explorers_transferred = {} @@ -275,12 +274,3 @@ class Path: # Ensure that the path exists self.remote_mkdir(remote_base) self.transfer_dir(src, dst) - - - def link_type_to_emulator(self): - """Link type names to cdist-type-emulator""" - source = os.path.abspath(sys.argv[0]) - for type in self.list_types(): - destination = os.path.join(self.bin_dir, type) - log.debug("Linking %s to %s", source, destination) - os.symlink(source, destination) From 7ec9fbbf128e72b16a588007cd2d11d92198a673 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 27 Sep 2011 14:01:04 +0200 Subject: [PATCH 3/6] fix argument passing in tests Signed-off-by: Nico Schottelius --- test.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/test.py b/test.py index a3652c41..908fad5e 100755 --- a/test.py +++ b/test.py @@ -30,6 +30,9 @@ import unittest sys.path.insert(0, os.path.abspath( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))) +cdist_exec_path = os.path.abspath( + os.path.join(os.path.dirname(os.path.realpath(__file__)), "bin/cdist")) + import cdist import cdist.config import cdist.exec @@ -85,13 +88,14 @@ class Config(unittest.TestCase): self.temp_dir = tempfile.mkdtemp() self.init_manifest = os.path.join(self.temp_dir, "manifest") self.config = cdist.config.Config("localhost", - initial_manifest=self.init_manifest) + initial_manifest=self.init_manifest, + exec_path=cdist_exec_path) def test_initial_manifest_different_parameter(self): manifest_fd = open(self.init_manifest, "w") manifest_fd.writelines(["#!/bin/sh\n", - "__file " + self.temp_dir + "--mode 0700\n", - "__file " + self.temp_dir + "--mode 0600\n", + "__file " + self.temp_dir + " --mode 0700\n", + "__file " + self.temp_dir + " --mode 0600\n", ]) manifest_fd.close() @@ -101,7 +105,7 @@ class Config(unittest.TestCase): manifest_fd = open(self.init_manifest, "w") manifest_fd.writelines(["#!/bin/sh\n", "__file " + self.temp_dir + '\n', - "__file " + self.temp_dir + "--mode 0600\n", + "__file " + self.temp_dir + " --mode 0600\n", ]) manifest_fd.close() @@ -110,7 +114,7 @@ class Config(unittest.TestCase): def test_initial_manifest_parameter_removed(self): manifest_fd = open(self.init_manifest, "w") manifest_fd.writelines(["#!/bin/sh\n", - "__file " + self.temp_dir + "--mode 0600\n", + "__file " + self.temp_dir + " --mode 0600\n", "__file " + self.temp_dir + "\n", ]) manifest_fd.close() @@ -120,8 +124,8 @@ class Config(unittest.TestCase): def test_initial_manifest_parameter_twice(self): manifest_fd = open(self.init_manifest, "w") manifest_fd.writelines(["#!/bin/sh\n", - "__file " + self.temp_dir + "--mode 0600\n", - "__file " + self.temp_dir + "--mode 0600\n", + "__file " + self.temp_dir + " --mode 0600\n", + "__file " + self.temp_dir + " --mode 0600\n", ]) manifest_fd.close() From 9d582ae24f1fa4c3697bb0ddfd83a146fcb75432 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 27 Sep 2011 14:01:09 +0200 Subject: [PATCH 4/6] rename emulator to run Signed-off-by: Nico Schottelius --- bin/cdist | 2 +- lib/cdist/emulator.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/cdist b/bin/cdist index d21fda40..245b2fc0 100755 --- a/bin/cdist +++ b/bin/cdist @@ -101,7 +101,7 @@ if __name__ == "__main__": if re.match(TYPE_PREFIX, os.path.basename(sys.argv[0])): import cdist.emulator - cdist.emulator.emulator(sys.argv) + cdist.emulator.run(sys.argv) else: import cdist import cdist.banner diff --git a/lib/cdist/emulator.py b/lib/cdist/emulator.py index b96b62dd..33594e45 100644 --- a/lib/cdist/emulator.py +++ b/lib/cdist/emulator.py @@ -29,7 +29,7 @@ import cdist.path log = logging.getLogger(__name__) -def emulator(argv): +def run(argv): """Emulate type commands (i.e. __file and co)""" type = os.path.basename(argv[0]) type_dir = os.path.join(os.environ['__cdist_type_base_dir'], type) @@ -101,8 +101,8 @@ def emulator(argv): # Already exists, verify all parameter are the same if object_exists: if not os.path.isfile(file): - print("New parameter + " + param + "specified, aborting") - print("Source = " + old_object_source + "new =" + object_source) + print("New parameter + \"" + param + "\" specified, aborting") + print("Source = " + " ".join(old_object_source) + " new =" + object_source) sys.exit(1) else: param_fd = open(file, "r") From 0913bb21dd26ec871a683d77a9e623f2be3716f5 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 27 Sep 2011 14:08:11 +0200 Subject: [PATCH 5/6] use raise instead of sys.exit in emulator Signed-off-by: Nico Schottelius --- lib/cdist/emulator.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/cdist/emulator.py b/lib/cdist/emulator.py index 33594e45..68a67176 100644 --- a/lib/cdist/emulator.py +++ b/lib/cdist/emulator.py @@ -22,7 +22,6 @@ import argparse import logging import os -import sys import cdist import cdist.path @@ -101,18 +100,22 @@ def run(argv): # Already exists, verify all parameter are the same if object_exists: if not os.path.isfile(file): - print("New parameter + \"" + param + "\" specified, aborting") - print("Source = " + " ".join(old_object_source) + " new =" + object_source) - sys.exit(1) + raise cdist.Error("New parameter \"" + + param + "\" specified, aborting\n" + + "Source = " + + " ".join(old_object_source) + + " new =" + object_source) else: param_fd = open(file, "r") value_old = param_fd.readlines() param_fd.close() if(value_old != value): - print("Parameter " + param + " differs: " + " ".join(value_old) + " vs. " + value) - print("Sources: " + " ".join(old_object_source) + " and " + object_source) - sys.exit(1) + raise cdist.Error("Parameter + \"" + param + + "\" differs: " + " ".join(value_old) + " vs. " + + value + + "\nSource = " + " ".join(old_object_source) + + " new =" + object_source) else: param_fd = open(file, "w") param_fd.writelines(value) @@ -131,7 +134,6 @@ def run(argv): source_fd.writelines(object_source) source_fd.close() - # sys.exit(1) print("Finished " + type + "/" + object_id + repr(params)) From cc87573d6b877fb7e49db8cea342204bd02efc18 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 27 Sep 2011 15:35:04 +0200 Subject: [PATCH 6/6] remove lecagy cdist-type-emulator support Signed-off-by: Nico Schottelius --- lib/cdist/config.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/cdist/config.py b/lib/cdist/config.py index cc0f7817..aef0b28a 100644 --- a/lib/cdist/config.py +++ b/lib/cdist/config.py @@ -146,12 +146,6 @@ class Config: env['__target_host'] = self.target_host env['__global'] = self.path.out_dir - # Legacy stuff to make cdist-type-emulator work - env['__cdist_core_dir'] = os.path.join(self.path.base_dir, "core") - env['__cdist_local_base_dir'] = self.path.temp_dir - - # Submit information to new type emulator - # Required for recording source env['__cdist_manifest'] = manifest