rewrite emulator to use Type and Object classes

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2011-10-11 12:27:08 +02:00
parent 753936b7c3
commit bcde8683af

View file

@ -24,122 +24,80 @@ import logging
import os import os
import cdist import cdist
from cdist import core
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def run(argv): def run(argv):
"""Emulate type commands (i.e. __file and co)""" """Emulate type commands (i.e. __file and co)"""
cdist_type = os.path.basename(argv[0])
type_path = os.path.join(os.environ['__cdist_type_base_path'], cdist_type)
param_path = os.path.join(type_path, "parameter")
global_path = os.environ['__global']
object_source = os.environ['__cdist_manifest']
if '__debug' in os.environ: if '__debug' in os.environ:
logging.root.setLevel(logging.DEBUG) logging.root.setLevel(logging.DEBUG)
else: else:
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
global_path = os.environ['__global']
object_source = os.environ['__cdist_manifest']
type_name = os.path.basename(argv[0])
object_base_path = os.path.join(global_path, "object")
type_base_path = os.environ['__cdist_type_base_path']
cdist_type = core.Type(type_base_path, type_name)
parser = argparse.ArgumentParser(add_help=False) parser = argparse.ArgumentParser(add_help=False)
for parameter in cdist.file_to_list(os.path.join(param_path, "optional")): for parameter in cdist_type.optional_parameters:
argument = "--" + parameter argument = "--" + parameter
parser.add_argument(argument, action='store', required=False) parser.add_argument(argument, action='store', required=False)
for parameter in cdist.file_to_list(os.path.join(param_path, "required")): for parameter in cdist_type.required_parameters:
argument = "--" + parameter argument = "--" + parameter
parser.add_argument(argument, action='store', required=True) parser.add_argument(argument, action='store', required=True)
# If not singleton support one positional parameter # If not singleton support one positional parameter
if not os.path.isfile(os.path.join(type_path, "singleton")): if not cdist_type.is_singleton:
parser.add_argument("object_id", nargs=1) parser.add_argument("object_id", nargs=1)
# And finally verify parameter # And finally verify parameter
args = parser.parse_args(argv[1:]) args = parser.parse_args(argv[1:])
# Setup object_id # Setup object_id
if os.path.isfile(os.path.join(type_path, "singleton")): if cdist_type.is_singleton:
object_id = "singleton" object_id = "singleton"
else: else:
object_id = args.object_id[0] object_id = args.object_id[0]
del args.object_id del args.object_id
# FIXME: / hardcoded - better portable solution available? # strip leading slash from object_id
if object_id[0] == '/': object_id = object_id.lstrip('/')
object_id = object_id[1:]
# Instantiate the cdist object whe are defining
cdist_object = core.Object(cdist_type, object_base_path, object_id)
# Prefix output by object_self # Prefix output by object_self
logformat = '%(levelname)s: ' + cdist_type + '/' + object_id + ': %(message)s' logformat = '%%(levelname)s: %s: %%(message)s' % cdist_object.path
logging.basicConfig(format=logformat) logging.basicConfig(format=logformat)
# FIXME: verify object id # FIXME: verify object id
log.debug(args) log.debug(args)
object_path = os.path.join(global_path, "object", cdist_type, # Create object with given parameters
object_id, cdist.DOT_CDIST) parameters = vars(args)
log.debug("Object output dir = " + object_path) if cdist_object.exists:
if cdist_object.parameters != parameters:
param_out_dir = os.path.join(object_path, "parameter") raise cdist.Error("Object %s already exists with conflicting parameters:\n%s: %s\n%s: %s"
% (cdist_object, " ".join(cdist_object.source), cdist_object.parameters, object_source, parameters)
object_source_file = os.path.join(object_path, "source") )
if os.path.exists(object_path):
object_exists = True
old_object_source_fd = open(object_source_file, "r")
old_object_source = old_object_source_fd.readlines()
old_object_source_fd.close()
else: else:
object_exists = False cdist_object.create()
try: cdist_object.parameters = parameters
os.makedirs(object_path, exist_ok=False)
log.debug("Object param dir = " + param_out_dir)
os.makedirs(param_out_dir, exist_ok=False)
except OSError as error:
raise cdist.Error(param_out_dir + ": " + error.args[1])
# Record parameter
params = vars(args)
for param in params:
value = getattr(args, param)
if value:
file = os.path.join(param_out_dir, param)
log.debug(file + "<-" + param + " = " + value)
# Already exists, verify all parameter are the same
if object_exists:
if not os.path.isfile(file):
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[0] != value):
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)
param_fd.close()
# Record requirements # Record requirements
if "require" in os.environ: if "require" in os.environ:
requirements = os.environ['require'] requirements = os.environ['require']
log.debug(object_id + ":Writing requirements: " + requirements) log.debug("%s:Writing requirements: %s" % (cdist_object.path, requirements))
require_fd = open(os.path.join(object_path, "require"), "a") cdist_object.requirements.extend(requirements.split(" "))
require_fd.write(requirements.replace(" ","\n"))
require_fd.close()
# Record / Append source # Record / Append source
source_fd = open(os.path.join(object_path, "source"), "a") # FIXME: source should be list
source_fd.writelines(object_source) cdist_object.source.append(object_source)
source_fd.close()
log.debug("Finished " + cdist_type + "/" + object_id + repr(params)) log.debug("Finished %s %s" % (cdist_object.path, parameters))