2011-09-26 09:18:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# 2011 Nico Schottelius (nico-cdist at schottelius.org)
|
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
2011-09-26 22:49:12 +00:00
|
|
|
import cdist
|
2011-10-11 10:27:08 +00:00
|
|
|
from cdist import core
|
2011-09-26 09:21:04 +00:00
|
|
|
|
2011-09-26 09:18:36 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2011-10-14 13:52:53 +00:00
|
|
|
|
|
|
|
class IllegalRequirementError(cdist.Error):
|
|
|
|
def __init__(self, requirement, message=None):
|
|
|
|
self.requirement = requirement
|
|
|
|
self.message = message or 'Illegal requirement'
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '%s: %s' % (self.message, self.requirement)
|
|
|
|
|
|
|
|
|
2011-09-27 12:01:09 +00:00
|
|
|
def run(argv):
|
2011-09-26 09:18:36 +00:00
|
|
|
"""Emulate type commands (i.e. __file and co)"""
|
2011-10-11 10:27:08 +00:00
|
|
|
global_path = os.environ['__global']
|
|
|
|
object_source = os.environ['__cdist_manifest']
|
2011-10-11 10:40:44 +00:00
|
|
|
target_host = os.environ['__target_host']
|
2011-10-11 10:27:08 +00:00
|
|
|
type_name = os.path.basename(argv[0])
|
|
|
|
|
2011-10-10 22:42:03 +00:00
|
|
|
# Logsetup - FIXME: add object_fq as soon as setup!
|
|
|
|
#id = target_host + ": " + cdist_type + '/' + object_id
|
|
|
|
id = target_host + ": "
|
|
|
|
# logformat = '%(levelname)s: ' + target_host + ": " + cdist_type + '/' + object_id + ': %(message)s'
|
|
|
|
logformat = '%(levelname)s: ' + id + ': %(message)s'
|
|
|
|
logging.basicConfig(format=logformat)
|
|
|
|
|
2011-09-27 14:20:46 +00:00
|
|
|
if '__debug' in os.environ:
|
|
|
|
logging.root.setLevel(logging.DEBUG)
|
2011-09-27 14:40:50 +00:00
|
|
|
else:
|
2011-10-10 22:42:03 +00:00
|
|
|
logging.root.setLevel(logging.INFO)
|
|
|
|
|
2011-10-11 10:27:08 +00:00
|
|
|
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)
|
|
|
|
|
2011-10-10 22:42:03 +00:00
|
|
|
if '__install' in os.environ:
|
2011-10-11 10:40:44 +00:00
|
|
|
if not cdist_type.is_install:
|
2011-10-10 22:42:03 +00:00
|
|
|
log.debug("Running in install mode, ignoring non install type")
|
|
|
|
return True
|
2011-09-27 14:20:46 +00:00
|
|
|
|
2011-09-26 09:18:36 +00:00
|
|
|
parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
|
2011-10-11 10:27:08 +00:00
|
|
|
for parameter in cdist_type.optional_parameters:
|
2011-09-26 09:18:36 +00:00
|
|
|
argument = "--" + parameter
|
|
|
|
parser.add_argument(argument, action='store', required=False)
|
2011-10-11 10:27:08 +00:00
|
|
|
for parameter in cdist_type.required_parameters:
|
2011-09-26 09:18:36 +00:00
|
|
|
argument = "--" + parameter
|
|
|
|
parser.add_argument(argument, action='store', required=True)
|
|
|
|
|
2011-09-27 14:40:50 +00:00
|
|
|
# If not singleton support one positional parameter
|
2011-10-11 10:27:08 +00:00
|
|
|
if not cdist_type.is_singleton:
|
2011-09-26 09:18:36 +00:00
|
|
|
parser.add_argument("object_id", nargs=1)
|
|
|
|
|
|
|
|
# And finally verify parameter
|
|
|
|
args = parser.parse_args(argv[1:])
|
|
|
|
|
|
|
|
# Setup object_id
|
2011-10-11 10:27:08 +00:00
|
|
|
if cdist_type.is_singleton:
|
2011-09-26 09:18:36 +00:00
|
|
|
object_id = "singleton"
|
|
|
|
else:
|
|
|
|
object_id = args.object_id[0]
|
|
|
|
del args.object_id
|
|
|
|
|
2011-10-11 10:27:08 +00:00
|
|
|
# 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)
|
2011-09-26 09:18:36 +00:00
|
|
|
|
|
|
|
# FIXME: verify object id
|
2011-10-11 10:40:44 +00:00
|
|
|
log.debug('#### emulator args: %s' % args)
|
2011-09-26 09:18:36 +00:00
|
|
|
|
2011-10-11 10:27:08 +00:00
|
|
|
# Create object with given parameters
|
2011-10-11 10:40:44 +00:00
|
|
|
parameters = {}
|
|
|
|
for key,value in vars(args).items():
|
|
|
|
if value is not None:
|
|
|
|
parameters[key] = value
|
|
|
|
|
2011-10-11 10:27:08 +00:00
|
|
|
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)
|
|
|
|
)
|
2011-09-26 09:18:36 +00:00
|
|
|
else:
|
2011-10-11 10:27:08 +00:00
|
|
|
cdist_object.create()
|
|
|
|
cdist_object.parameters = parameters
|
2011-09-26 09:18:36 +00:00
|
|
|
|
|
|
|
# Record requirements
|
2011-10-06 17:50:45 +00:00
|
|
|
if "require" in os.environ:
|
2011-10-07 15:03:38 +00:00
|
|
|
requirements = os.environ['require']
|
2011-10-14 09:39:55 +00:00
|
|
|
for requirement in requirements.split(" "):
|
|
|
|
requirement_parts = requirement.split(os.sep, 1)
|
|
|
|
requirement_parts.reverse()
|
|
|
|
requirement_type_name = requirement_parts.pop()
|
|
|
|
try:
|
|
|
|
requirement_object_id = requirement_parts.pop()
|
|
|
|
except IndexError:
|
|
|
|
# no object id, must be singleton
|
|
|
|
requirement_object_id = 'singleton'
|
|
|
|
if requirement_object_id.startswith('/'):
|
2011-10-14 13:52:53 +00:00
|
|
|
raise IllegalRequirementError(requirement, 'requirements object_id may not start with /')
|
2011-10-14 09:39:55 +00:00
|
|
|
log.debug("Recording requirement: %s -> %s" % (cdist_object.path, requirement))
|
2011-10-14 09:49:37 +00:00
|
|
|
cdist_object.requirements.append(requirement)
|
2011-09-26 09:18:36 +00:00
|
|
|
|
|
|
|
# Record / Append source
|
2011-10-11 10:27:08 +00:00
|
|
|
cdist_object.source.append(object_source)
|
2011-09-26 09:18:36 +00:00
|
|
|
|
2011-10-11 10:27:08 +00:00
|
|
|
log.debug("Finished %s %s" % (cdist_object.path, parameters))
|