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 cdist
from cdist import core
log = logging.getLogger(__name__)
def run(argv):
"""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:
logging.root.setLevel(logging.DEBUG)
else:
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)
for parameter in cdist.file_to_list(os.path.join(param_path, "optional")):
for parameter in cdist_type.optional_parameters:
argument = "--" + parameter
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
parser.add_argument(argument, action='store', required=True)
# 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)
# And finally verify parameter
args = parser.parse_args(argv[1:])
# Setup object_id
if os.path.isfile(os.path.join(type_path, "singleton")):
if cdist_type.is_singleton:
object_id = "singleton"
else:
object_id = args.object_id[0]
del args.object_id
# FIXME: / hardcoded - better portable solution available?
if object_id[0] == '/':
object_id = object_id[1:]
# strip leading slash from object_id
object_id = object_id.lstrip('/')
# Instantiate the cdist object whe are defining
cdist_object = core.Object(cdist_type, object_base_path, object_id)
# 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)
# FIXME: verify object id
log.debug(args)
object_path = os.path.join(global_path, "object", cdist_type,
object_id, cdist.DOT_CDIST)
log.debug("Object output dir = " + object_path)
param_out_dir = os.path.join(object_path, "parameter")
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()
# Create object with given parameters
parameters = vars(args)
if cdist_object.exists:
if cdist_object.parameters != parameters:
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)
)
else:
object_exists = False
try:
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()
cdist_object.create()
cdist_object.parameters = parameters
# Record requirements
if "require" in os.environ:
requirements = os.environ['require']
log.debug(object_id + ":Writing requirements: " + requirements)
require_fd = open(os.path.join(object_path, "require"), "a")
require_fd.write(requirements.replace(" ","\n"))
require_fd.close()
log.debug("%s:Writing requirements: %s" % (cdist_object.path, requirements))
cdist_object.requirements.extend(requirements.split(" "))
# Record / Append source
source_fd = open(os.path.join(object_path, "source"), "a")
source_fd.writelines(object_source)
source_fd.close()
# FIXME: source should be list
cdist_object.source.append(object_source)
log.debug("Finished " + cdist_type + "/" + object_id + repr(params))
log.debug("Finished %s %s" % (cdist_object.path, parameters))