Merge remote-tracking branch 'cdist/master'

This commit is contained in:
Steven Armstrong 2011-09-27 15:40:17 +02:00
commit 840254cdd3
7 changed files with 53 additions and 132 deletions

View file

@ -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

View file

@ -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

View file

@ -42,6 +42,9 @@ case "$($__explorer/os)" in
*bsd|solaris)
uname -r
;;
owl)
cat /etc/owl-release
;;
redhat|centos)
cat /etc/redhat-release
;;

View file

@ -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 }
@ -135,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
@ -299,95 +304,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)

View file

@ -22,14 +22,13 @@
import argparse
import logging
import os
import sys
import cdist
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,18 +100,22 @@ 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)
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,5 +134,13 @@ def emulator(argv):
source_fd.writelines(object_source)
source_fd.close()
# 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)

View file

@ -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)

18
test.py
View file

@ -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()