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-09-26 09:21:04 +00:00
|
|
|
|
2011-09-26 09:18:36 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
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-07 14:14:29 +00:00
|
|
|
cdist_type = os.path.basename(argv[0])
|
|
|
|
type_path = os.path.join(os.environ['__cdist_type_base_path'], cdist_type)
|
|
|
|
param_dir = os.path.join(type_path, "parameter")
|
2011-09-26 09:18:36 +00:00
|
|
|
global_dir = os.environ['__global']
|
|
|
|
object_source = os.environ['__cdist_manifest']
|
|
|
|
|
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:
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
2011-09-27 14:20:46 +00:00
|
|
|
|
2011-09-26 09:18:36 +00:00
|
|
|
parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
|
2011-10-07 07:29:22 +00:00
|
|
|
for parameter in cdist.file_to_list(os.path.join(param_dir, "optional")):
|
2011-09-26 09:18:36 +00:00
|
|
|
argument = "--" + parameter
|
|
|
|
parser.add_argument(argument, action='store', required=False)
|
2011-10-07 07:29:22 +00:00
|
|
|
for parameter in cdist.file_to_list(os.path.join(param_dir, "required")):
|
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-07 14:14:29 +00:00
|
|
|
if not os.path.isfile(os.path.join(type_path, "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-07 14:14:29 +00:00
|
|
|
if os.path.isfile(os.path.join(type_path, "singleton")):
|
2011-09-26 09:18:36 +00:00
|
|
|
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:]
|
|
|
|
|
2011-09-27 14:40:50 +00:00
|
|
|
# Prefix output by object_self
|
2011-10-07 14:14:29 +00:00
|
|
|
logformat = '%(levelname)s: ' + cdist_type + '/' + object_id + ': %(message)s'
|
2011-09-27 14:40:50 +00:00
|
|
|
logging.basicConfig(format=logformat)
|
|
|
|
|
2011-09-26 09:18:36 +00:00
|
|
|
# FIXME: verify object id
|
|
|
|
log.debug(args)
|
|
|
|
|
2011-10-07 14:14:29 +00:00
|
|
|
object_dir = os.path.join(global_dir, "object", cdist_type,
|
2011-10-07 14:15:48 +00:00
|
|
|
object_id, cdist.DOT_CDIST)
|
2011-10-06 18:32:47 +00:00
|
|
|
log.debug("Object output dir = " + object_dir)
|
|
|
|
|
2011-09-26 09:18:36 +00:00
|
|
|
param_out_dir = os.path.join(object_dir, "parameter")
|
|
|
|
|
|
|
|
object_source_file = os.path.join(object_dir, "source")
|
|
|
|
|
|
|
|
if os.path.exists(param_out_dir):
|
|
|
|
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:
|
|
|
|
object_exists = False
|
|
|
|
try:
|
|
|
|
os.makedirs(param_out_dir, exist_ok=True)
|
|
|
|
except OSError as error:
|
2011-09-26 22:49:12 +00:00
|
|
|
raise cdist.Error(param_out_dir + ": " + error.args[1])
|
2011-09-26 09:18:36 +00:00
|
|
|
|
|
|
|
# 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):
|
2011-09-27 12:08:11 +00:00
|
|
|
raise cdist.Error("New parameter \"" +
|
|
|
|
param + "\" specified, aborting\n" +
|
|
|
|
"Source = " +
|
|
|
|
" ".join(old_object_source)
|
|
|
|
+ " new =" + object_source)
|
2011-09-26 09:18:36 +00:00
|
|
|
else:
|
|
|
|
param_fd = open(file, "r")
|
2011-09-26 22:02:04 +00:00
|
|
|
value_old = param_fd.readlines()
|
2011-09-26 09:18:36 +00:00
|
|
|
param_fd.close()
|
|
|
|
|
2011-09-27 14:10:53 +00:00
|
|
|
if(value_old[0] != value):
|
|
|
|
raise cdist.Error("Parameter\"" + param +
|
2011-09-27 12:08:11 +00:00
|
|
|
"\" differs: " + " ".join(value_old) + " vs. " +
|
|
|
|
value +
|
|
|
|
"\nSource = " + " ".join(old_object_source)
|
2011-09-27 14:10:53 +00:00
|
|
|
+ " new = " + object_source)
|
2011-09-26 09:18:36 +00:00
|
|
|
else:
|
|
|
|
param_fd = open(file, "w")
|
|
|
|
param_fd.writelines(value)
|
|
|
|
param_fd.close()
|
|
|
|
|
|
|
|
# Record requirements
|
2011-10-06 17:50:45 +00:00
|
|
|
if "require" in os.environ:
|
2011-09-26 09:18:36 +00:00
|
|
|
requirements = os.environ['__require']
|
2011-09-27 14:25:58 +00:00
|
|
|
log.debug(object_id + ":Writing requirements: " + requirements)
|
2011-09-26 09:18:36 +00:00
|
|
|
require_fd = open(os.path.join(object_dir, "require"), "a")
|
2011-10-07 14:07:02 +00:00
|
|
|
require_fd.write(requirements.replace(" ","\n"))
|
2011-09-26 09:18:36 +00:00
|
|
|
require_fd.close()
|
|
|
|
|
|
|
|
# Record / Append source
|
|
|
|
source_fd = open(os.path.join(object_dir, "source"), "a")
|
|
|
|
source_fd.writelines(object_source)
|
|
|
|
source_fd.close()
|
|
|
|
|
2011-10-07 14:14:29 +00:00
|
|
|
log.debug("Finished " + cdist_type + "/" + object_id + repr(params))
|