forked from ungleich-public/cdist
Merge branch 'master' into install
Conflicts: bin/cdist-config Signed-off-by: Nico Schottelius <nico@kr.ethz.ch>
This commit is contained in:
commit
9a3d9334d5
57 changed files with 1612 additions and 693 deletions
204
bin/cdist
204
bin/cdist
|
|
@ -62,37 +62,24 @@ VERSION = "2.0.0"
|
|||
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
||||
log = logging.getLogger()
|
||||
|
||||
class TypeEmulator:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.type = os.path.basename(name)
|
||||
|
||||
def file_to_list(filename):
|
||||
"""Return list from \n seperated file"""
|
||||
if os.path.isfile(filename):
|
||||
file_fd = open(filename, "r")
|
||||
lines = file_fd.readlines()
|
||||
file_fd.close()
|
||||
|
||||
def type_emulator(self):
|
||||
type = basename(sys.argv[0])
|
||||
# Remove \n from all lines
|
||||
lines = map(lambda s: s.strip(), lines)
|
||||
else:
|
||||
lines = []
|
||||
|
||||
type_is_singleton(type)
|
||||
return lines
|
||||
|
||||
# Check object id
|
||||
|
||||
# Prevent double slash if id begins with /
|
||||
|
||||
# Record parameter: opt_file="${opt#--}"
|
||||
# [ $# -ge 1 ] || __cdist_usage "Missing value for $opt"
|
||||
# echo "${value}" > "${__cdist_parameter_dir}/${opt_file}"
|
||||
|
||||
# Record requirements
|
||||
# echo $requirement >> "$(__cdist_object_require "$__cdist_object_self")"
|
||||
|
||||
# Ensure required parameters are given
|
||||
# Ensure that only optional or required parameters are given
|
||||
# [ "$is_valid" ] || __cdist_usage "Unknown parameter $parameter"
|
||||
|
||||
# Merge object (creating twice with the same parameter + requirements == allowed)
|
||||
|
||||
# diff -ru "${__cdist_new_object_dir}/${__cdist_name_parameter}
|
||||
# # Add "I was here message"
|
||||
# _cdist_object_source_add "${__cdist_object_dir}"
|
||||
def exit_error(*args):
|
||||
log.error(*args)
|
||||
sys.exit(1)
|
||||
|
||||
class Cdist:
|
||||
"""Cdist main class to hold arbitrary data"""
|
||||
|
|
@ -160,10 +147,6 @@ class Cdist:
|
|||
shutil.rmtree(self.cache_dir)
|
||||
shutil.move(self.temp_dir, self.cache_dir)
|
||||
|
||||
def exit_error(self, *args):
|
||||
log.error(*args)
|
||||
sys.exit(1)
|
||||
|
||||
def remote_mkdir(self, directory):
|
||||
"""Create directory on remote side"""
|
||||
self.run_or_fail(["mkdir", "-p", directory], remote=True)
|
||||
|
|
@ -198,7 +181,9 @@ class Cdist:
|
|||
print(script_fd.read())
|
||||
script_fd.close()
|
||||
|
||||
self.exit_error("Command failed (shell): " + " ".join(*args))
|
||||
exit_error("Command failed (shell): " + " ".join(*args))
|
||||
except OSError as error:
|
||||
exit_error(" ".join(*args) + ": " + error.args[1])
|
||||
|
||||
def run_or_fail(self, *args, **kargs):
|
||||
if "remote" in kargs:
|
||||
|
|
@ -211,7 +196,10 @@ class Cdist:
|
|||
try:
|
||||
subprocess.check_call(*args, **kargs)
|
||||
except subprocess.CalledProcessError:
|
||||
self.exit_error("Command failed: " + " ".join(*args))
|
||||
exit_error("Command failed: " + " ".join(*args))
|
||||
except OSError as error:
|
||||
exit_error(" ".join(*args) + ": " + error.args[1])
|
||||
|
||||
|
||||
def remove_remote_dir(self, destination):
|
||||
self.run_or_fail(["rm", "-rf", destination], remote=True)
|
||||
|
|
@ -366,8 +354,8 @@ class Cdist:
|
|||
|
||||
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():
|
||||
source = os.path.join(self.lib_dir, "cdist-type-emulator")
|
||||
destination = os.path.join(self.bin_dir, type)
|
||||
log.debug("Linking %s to %s", source, destination)
|
||||
os.symlink(source, destination)
|
||||
|
|
@ -376,7 +364,7 @@ class Cdist:
|
|||
"""Run global explorers"""
|
||||
explorers = self.list_global_explorers()
|
||||
if(len(explorers) == 0):
|
||||
self.exit_error("No explorers found in", self.global_explorer_dir)
|
||||
exit_error("No explorers found in", self.global_explorer_dir)
|
||||
|
||||
self.transfer_global_explorers()
|
||||
for explorer in explorers:
|
||||
|
|
@ -450,14 +438,17 @@ class Cdist:
|
|||
env = os.environ.copy()
|
||||
env['PATH'] = self.bin_dir + ":" + env['PATH']
|
||||
|
||||
# Information required in every manifest
|
||||
env['__target_host'] = self.target_host
|
||||
env['__global'] = self.out_dir
|
||||
|
||||
# Legacy stuff to make cdist-type-emulator work
|
||||
env['__cdist_conf_dir'] = self.conf_dir
|
||||
env['__cdist_core_dir'] = os.path.join(self.base_dir, "core")
|
||||
env['__cdist_local_base_dir'] = self.temp_dir
|
||||
env['__cdist_manifest'] = self.initial_manifest
|
||||
|
||||
# Submit information to new type emulator
|
||||
env['__cdist_manifest'] = manifest
|
||||
env['__cdist_type_base_dir'] = self.type_base_dir
|
||||
|
||||
# Other environment stuff
|
||||
if extra_env:
|
||||
|
|
@ -465,28 +456,11 @@ class Cdist:
|
|||
|
||||
self.shell_run_or_debug_fail(manifest, [manifest], env=env)
|
||||
|
||||
def list_object_requirements(self, cdist_object):
|
||||
"""Return list of requirements for specific object"""
|
||||
file=os.path.join(self.object_dir(cdist_object), "require")
|
||||
|
||||
if os.path.isfile(file):
|
||||
file_fd = open(file, "r")
|
||||
requirements = file_fd.readlines()
|
||||
file_fd.close()
|
||||
|
||||
# Remove \n from all lines
|
||||
requirements = map(lambda s: s.strip(), requirements)
|
||||
|
||||
log.debug("Requirements for %s: %s", cdist_object, requirements)
|
||||
else:
|
||||
requirements = []
|
||||
|
||||
return requirements
|
||||
|
||||
def object_run(self, cdist_object, mode):
|
||||
"""Run gencode or code for an object"""
|
||||
log.debug("Running %s from %s", mode, cdist_object)
|
||||
requirements = self.list_object_requirements(cdist_object)
|
||||
file=os.path.join(self.object_dir(cdist_object), "require")
|
||||
requirements = file_to_list(file)
|
||||
type = self.get_type_from_object(cdist_object)
|
||||
|
||||
for requirement in requirements:
|
||||
|
|
@ -636,6 +610,108 @@ def install(args):
|
|||
def emulator():
|
||||
"""Emulate type commands (i.e. __file and co)"""
|
||||
type = os.path.basename(sys.argv[0])
|
||||
type_dir = os.path.join(os.environ['__cdist_type_base_dir'], type)
|
||||
param_dir = os.path.join(type_dir, "parameter")
|
||||
global_dir = os.environ['__global']
|
||||
object_source = os.environ['__cdist_manifest']
|
||||
|
||||
parser = argparse.ArgumentParser(add_help=False)
|
||||
|
||||
# Setup optional parameters
|
||||
for parameter in file_to_list(os.path.join(param_dir, "optional")):
|
||||
argument = "--" + parameter
|
||||
parser.add_argument(argument, action='store', required=False)
|
||||
|
||||
# Setup required parameters
|
||||
for parameter in file_to_list(os.path.join(param_dir, "required")):
|
||||
argument = "--" + parameter
|
||||
parser.add_argument(argument, action='store', required=True)
|
||||
|
||||
# Setup positional parameter, if not singleton
|
||||
|
||||
if not os.path.isfile(os.path.join(type_dir, "singleton")):
|
||||
parser.add_argument("object_id", nargs=1)
|
||||
|
||||
# And finally verify parameter
|
||||
args = parser.parse_args(sys.argv[1:])
|
||||
|
||||
# Setup object_id
|
||||
if os.path.isfile(os.path.join(type_dir, "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:]
|
||||
|
||||
# FIXME: verify object id
|
||||
log.debug(args)
|
||||
|
||||
object_dir = os.path.join(global_dir, "object", type,
|
||||
object_id, DOT_CDIST)
|
||||
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:
|
||||
exit_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):
|
||||
print("New parameter + " + param + "specified, aborting")
|
||||
print("Source = " + old_object_source + "new =" + object_source)
|
||||
sys.exit(1)
|
||||
else:
|
||||
param_fd = open(file, "r")
|
||||
param_old = param_fd.realines()
|
||||
param_fd.close()
|
||||
|
||||
if(param_old != param):
|
||||
print("Parameter differs: " + param_old + "vs," + param)
|
||||
print("Source = " + old_object_source + "new =" + object_source)
|
||||
sys.exit(1)
|
||||
else:
|
||||
param_fd = open(file, "w")
|
||||
param_fd.writelines(value)
|
||||
param_fd.close()
|
||||
|
||||
# Record requirements
|
||||
if "__require" in os.environ:
|
||||
requirements = os.environ['__require']
|
||||
print(object_id + ":Writing requirements: " + requirements)
|
||||
require_fd = open(os.path.join(object_dir, "require"), "a")
|
||||
require_fd.writelines(requirements.split(" "))
|
||||
require_fd.close()
|
||||
|
||||
# Record / Append source
|
||||
source_fd = open(os.path.join(object_dir, "source"), "a")
|
||||
source_fd.writelines(object_source)
|
||||
source_fd.close()
|
||||
|
||||
# sys.exit(1)
|
||||
print("Finished " + type + "/" + object_id + repr(params))
|
||||
|
||||
|
||||
def commandline():
|
||||
"""Parse command line"""
|
||||
|
|
@ -696,13 +772,13 @@ def commandline():
|
|||
logging.root.setLevel(logging.DEBUG)
|
||||
log.debug(args)
|
||||
|
||||
try:
|
||||
args.func(args)
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(0)
|
||||
args.func(args)
|
||||
|
||||
if __name__ == "__main__":
|
||||
if re.match(TYPE_PREFIX, os.path.basename(sys.argv[0])):
|
||||
emulator()
|
||||
else:
|
||||
commandline()
|
||||
try:
|
||||
if re.match(TYPE_PREFIX, os.path.basename(sys.argv[0])):
|
||||
emulator()
|
||||
else:
|
||||
commandline()
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(0)
|
||||
|
|
|
|||
438
bin/cdist-config
438
bin/cdist-config
|
|
@ -1,438 +0,0 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# 2010-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/>.
|
||||
#
|
||||
#
|
||||
|
||||
__cdist_version="1.7.0"
|
||||
|
||||
# Fail if something bogus is going on
|
||||
set -u
|
||||
|
||||
################################################################################
|
||||
# cconf standard vars prefixed with cdist
|
||||
|
||||
__cdist_pwd="$(pwd -P)"
|
||||
__cdist_mydir="${0%/*}";
|
||||
__cdist_abs_mydir="$(cd "$__cdist_mydir" && pwd -P)"
|
||||
__cdist_myname=${0##*/};
|
||||
__cdist_abs_myname="$__cdist_abs_mydir/$__cdist_myname"
|
||||
|
||||
################################################################################
|
||||
# Names / Constants
|
||||
#
|
||||
# Most values can be overriden from outside, so you can
|
||||
# customise paths as you like (for distributors, geeks and hackers)
|
||||
#
|
||||
|
||||
: ${__cdist_name_bin:=bin}
|
||||
: ${__cdist_name_cache:=cache}
|
||||
: ${__cdist_name_code:=code}
|
||||
: ${__cdist_name_conf_dir:=conf}
|
||||
: ${__cdist_name_dot_cdist:=.cdist}
|
||||
: ${__cdist_name_explorer:=explorer}
|
||||
: ${__cdist_name_gencode:=gencode}
|
||||
: ${__cdist_name_gencode_local:=local}
|
||||
: ${__cdist_name_gencode_remote:=remote}
|
||||
: ${__cdist_name_global:=global}
|
||||
: ${__cdist_name_host:=host}
|
||||
: ${__cdist_name_init:=init}
|
||||
: ${__cdist_name_manifest:=manifest}
|
||||
: ${__cdist_name_object:=object}
|
||||
: ${__cdist_name_object_finished:=done}
|
||||
: ${__cdist_name_object_prepared:=prepared}
|
||||
: ${__cdist_name_object_id:=object_id}
|
||||
: ${__cdist_name_object_source:=source}
|
||||
: ${__cdist_name_objects_created:=.objects_created}
|
||||
: ${__cdist_name_out_dir:=out}
|
||||
: ${__cdist_name_parameter:=parameter}
|
||||
: ${__cdist_name_parameter_required:=required}
|
||||
: ${__cdist_name_parameter_optional:=optional}
|
||||
: ${__cdist_name_require:=require}
|
||||
: ${__cdist_name_self:=self}
|
||||
: ${__cdist_name_singleton:=singleton}
|
||||
: ${__cdist_name_target_host:=target_host}
|
||||
: ${__cdist_name_target_user:=target_user}
|
||||
: ${__cdist_name_type:=type}
|
||||
: ${__cdist_name_type_bin:=type_bin}
|
||||
: ${__cdist_name_type_explorer:=type_explorer}
|
||||
: ${__cdist_name_type_explorer_pushed:=.explorer_pushed}
|
||||
|
||||
# Used for IDs: Allow everything not starting with - and .
|
||||
: ${__cdist_sane_regexp:=[^-\.].*}
|
||||
|
||||
# Default remote user
|
||||
: ${__cdist_remote_user:=root}
|
||||
|
||||
|
||||
################################################################################
|
||||
# Exported variable names (usable for non core
|
||||
#
|
||||
: ${__cdist_name_var_explorer:=__$__cdist_name_explorer}
|
||||
: ${__cdist_name_var_type_explorer:=__$__cdist_name_type_explorer}
|
||||
: ${__cdist_name_var_global:=__$__cdist_name_global}
|
||||
: ${__cdist_name_var_manifest:=__$__cdist_name_manifest}
|
||||
: ${__cdist_name_var_target_host:=__$__cdist_name_target_host}
|
||||
: ${__cdist_name_var_target_user:=__$__cdist_name_target_user}
|
||||
: ${__cdist_name_var_object:=__$__cdist_name_object}
|
||||
: ${__cdist_name_var_object_id:=__$__cdist_name_object_id}
|
||||
: ${__cdist_name_var_self:=__$__cdist_name_self}
|
||||
: ${__cdist_name_var_type:=__$__cdist_name_type}
|
||||
|
||||
|
||||
################################################################################
|
||||
# Tempfiles
|
||||
#
|
||||
: ${__cdist_tmp_base_dir=/tmp}
|
||||
__cdist_tmp_dir=$(mktemp -d "$__cdist_tmp_base_dir/cdist.XXXXXXXXXXXX")
|
||||
__cdist_tmp_file=$(mktemp "$__cdist_tmp_dir/cdist.XXXXXXXXXXXX")
|
||||
|
||||
################################################################################
|
||||
# Local Base
|
||||
#
|
||||
: ${__cdist_local_base_dir:=$__cdist_tmp_dir}
|
||||
|
||||
# Cache may *NOT* be below __cdist_local_base_dir!
|
||||
: ${__cdist_local_base_cache_dir:=$__cdist_abs_mydir/../$__cdist_name_cache}
|
||||
|
||||
: ${__cdist_conf_dir:="$(cd "$__cdist_abs_mydir/../conf" && pwd -P)"}
|
||||
|
||||
: ${__cdist_explorer_dir:=$__cdist_conf_dir/$__cdist_name_explorer}
|
||||
: ${__cdist_manifest_dir:=$__cdist_conf_dir/$__cdist_name_manifest}
|
||||
: ${__cdist_manifest_init:=$__cdist_manifest_dir/$__cdist_name_init}
|
||||
: ${__cdist_type_dir:=$__cdist_conf_dir/$__cdist_name_type}
|
||||
|
||||
################################################################################
|
||||
# Local output
|
||||
#
|
||||
: ${__cdist_out_dir:=$__cdist_local_base_dir/$__cdist_name_out_dir}
|
||||
: ${__cdist_out_explorer_dir:=$__cdist_out_dir/$__cdist_name_explorer}
|
||||
: ${__cdist_out_object_dir:=$__cdist_out_dir/$__cdist_name_object}
|
||||
: ${__cdist_out_type_dir:=$__cdist_out_dir/$__cdist_name_type}
|
||||
: ${__cdist_out_type_bin_dir:=$__cdist_out_dir/$__cdist_name_type_bin}
|
||||
|
||||
: ${__cdist_objects_created:=$__cdist_out_object_dir/$__cdist_name_objects_created}
|
||||
|
||||
################################################################################
|
||||
# Remote base
|
||||
#
|
||||
: ${__cdist_remote_base_dir:=/var/lib/cdist}
|
||||
: ${__cdist_remote_bin_dir:=$__cdist_remote_base_dir/$__cdist_name_bin}
|
||||
: ${__cdist_remote_conf_dir:=$__cdist_remote_base_dir/$__cdist_name_conf_dir}
|
||||
|
||||
: ${__cdist_remote_explorer_dir:=$__cdist_remote_conf_dir/$__cdist_name_explorer}
|
||||
: ${__cdist_remote_type_dir:=$__cdist_remote_conf_dir/$__cdist_name_type}
|
||||
|
||||
################################################################################
|
||||
# Remote output
|
||||
#
|
||||
: ${__cdist_remote_out_dir:=$__cdist_remote_base_dir/$__cdist_name_out_dir}
|
||||
: ${__cdist_remote_out_explorer_dir:=$__cdist_remote_out_dir/$__cdist_name_explorer}
|
||||
: ${__cdist_remote_out_object_dir:=$__cdist_remote_out_dir/$__cdist_name_object}
|
||||
|
||||
|
||||
################################################################################
|
||||
# Internal functions
|
||||
#
|
||||
__cdist_echo()
|
||||
{
|
||||
__cdist_echo_type="$1"; shift
|
||||
|
||||
set +u
|
||||
if [ "$__cdist_object_self" ]; then
|
||||
__cdist_echo_prefix="${__cdist_object_self}:"
|
||||
else
|
||||
__cdist_echo_prefix="core: "
|
||||
fi
|
||||
set -u
|
||||
|
||||
case "$__cdist_echo_type" in
|
||||
debug)
|
||||
set +u
|
||||
if [ "$__cdist_debug" ]; then
|
||||
echo $__cdist_echo_prefix "Debug: $@"
|
||||
fi
|
||||
set -u
|
||||
;;
|
||||
info)
|
||||
echo $__cdist_echo_prefix "$@"
|
||||
;;
|
||||
warn)
|
||||
echo $__cdist_echo_prefix "Warning: $@"
|
||||
;;
|
||||
error)
|
||||
echo $__cdist_echo_prefix "Error: $@" >&2
|
||||
;;
|
||||
*)
|
||||
echo "CORE BUG, who created the broken commit in $0?" >&2
|
||||
exit 23
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
__cdist_exec_fail_on_error()
|
||||
{
|
||||
set +e
|
||||
sh -e "$@"
|
||||
if [ "$?" -ne 0 ]; then
|
||||
__cdist_echo error "$1 exited non-zero"
|
||||
__cdist_echo warn "Faulty code:"
|
||||
cat "$1"
|
||||
__cdist_exit_err "Aborting due to non-zero exit code."
|
||||
fi
|
||||
}
|
||||
|
||||
__cdist_exit_err()
|
||||
{
|
||||
__cdist_echo error "$@"
|
||||
exit 1
|
||||
}
|
||||
|
||||
__cdist_usage()
|
||||
{
|
||||
__cdist_exit_err "$__cdist_myname: $@"
|
||||
}
|
||||
|
||||
__cdist_init_deploy()
|
||||
{
|
||||
__cdist_echo info "Creating clean directory structure "
|
||||
|
||||
# Ensure there is no old stuff, neither local nor remote
|
||||
rm -rf "$__cdist_local_base_dir"
|
||||
ssh "${__cdist_remote_user}@$1" "rm -rf ${__cdist_remote_base_dir}"
|
||||
|
||||
# Init base
|
||||
mkdir -p "$__cdist_local_base_dir"
|
||||
ssh "${__cdist_remote_user}@$1" "mkdir -p ${__cdist_remote_base_dir}"
|
||||
|
||||
# Link configuration source directory - consistent with remote
|
||||
ln -sf "$__cdist_conf_dir" "$__cdist_local_base_dir/$__cdist_name_conf_dir"
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Cache
|
||||
#
|
||||
__cdist_cache_dir()
|
||||
{
|
||||
cd "${__cdist_local_base_cache_dir}" && pwd -P
|
||||
}
|
||||
|
||||
__cdist_host_cache_dir()
|
||||
{
|
||||
echo "$(__cdist_cache_dir)/$1"
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Object
|
||||
#
|
||||
|
||||
__cdist_object_code()
|
||||
{
|
||||
echo "$(__cdist_object_dir "$1")/${__cdist_name_code}-$2"
|
||||
}
|
||||
|
||||
__cdist_object_prepared()
|
||||
{
|
||||
echo "$(__cdist_object_dir "$1")/${__cdist_name_object_prepared}"
|
||||
}
|
||||
|
||||
__cdist_object_finished()
|
||||
{
|
||||
echo "$(__cdist_object_dir "$1")/${__cdist_name_object_finished}"
|
||||
}
|
||||
|
||||
__cdist_object_dir()
|
||||
{
|
||||
echo "$(__cdist_object_base_dir "$1")/${__cdist_name_dot_cdist}"
|
||||
}
|
||||
|
||||
__cdist_object_base_dir()
|
||||
{
|
||||
echo "${__cdist_out_object_dir}/$1"
|
||||
}
|
||||
|
||||
|
||||
__cdist_object_id_from_object()
|
||||
{
|
||||
echo "${1#*/}"
|
||||
}
|
||||
|
||||
# Find objects, remove ./ and /MARKER
|
||||
__cdist_object_list()
|
||||
{
|
||||
local basedir="$1"; shift
|
||||
|
||||
# Use subshell to prevent changing cwd in program
|
||||
(
|
||||
cd "${basedir}"
|
||||
|
||||
find . -name "$__cdist_name_dot_cdist" | \
|
||||
sed -e 's;^./;;' -e "s;/${__cdist_name_dot_cdist}\$;;"
|
||||
)
|
||||
}
|
||||
|
||||
__cdist_object_parameter_dir()
|
||||
{
|
||||
echo "$(__cdist_object_dir "$1")/${__cdist_name_parameter}"
|
||||
}
|
||||
|
||||
__cdist_object_require()
|
||||
{
|
||||
echo "$(__cdist_object_dir "$1")/${__cdist_name_require}"
|
||||
}
|
||||
|
||||
__cdist_object_source_name()
|
||||
{
|
||||
echo "$1/${__cdist_name_object_source}"
|
||||
}
|
||||
|
||||
__cdist_object_source()
|
||||
{
|
||||
cat "$(__cdist_object_source_name "$1")"
|
||||
}
|
||||
|
||||
__cdist_object_source_add()
|
||||
{
|
||||
echo "$__cdist_manifest" >> "$(__cdist_object_source_name "$1")"
|
||||
}
|
||||
|
||||
__cdist_object_type_explorer_dir()
|
||||
{
|
||||
echo "$(__cdist_object_dir "$1")/${__cdist_name_explorer}"
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Remote
|
||||
#
|
||||
|
||||
__cdist_remote_object_base_dir()
|
||||
{
|
||||
echo "${__cdist_remote_out_object_dir}/$1"
|
||||
}
|
||||
|
||||
__cdist_remote_object_dir()
|
||||
{
|
||||
echo "$(__cdist_remote_object_base_dir "$1")/${__cdist_name_dot_cdist}"
|
||||
}
|
||||
|
||||
__cdist_remote_object_parameter_dir()
|
||||
{
|
||||
echo "$(__cdist_remote_object_dir "$1")/${__cdist_name_parameter}"
|
||||
}
|
||||
|
||||
__cdist_remote_object_type_explorer_dir()
|
||||
{
|
||||
echo "$(__cdist_remote_object_dir "$1")/${__cdist_name_explorer}"
|
||||
}
|
||||
|
||||
|
||||
__cdist_remote_type_explorer_dir()
|
||||
{
|
||||
echo "${__cdist_remote_type_dir}/$1/${__cdist_name_explorer}"
|
||||
}
|
||||
|
||||
|
||||
################################################################################
|
||||
# Traps
|
||||
#
|
||||
__cdist_tmp_removal()
|
||||
{
|
||||
rm -rf "${__cdist_tmp_dir}"
|
||||
}
|
||||
|
||||
# Does not work in children, will be called again in every script!
|
||||
# Use only in interactive "front end" scripts
|
||||
__cdist_kill_on_interrupt()
|
||||
{
|
||||
__cdist_tmp_removal
|
||||
kill 0
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Remove tempfiles at normal exit
|
||||
trap __cdist_tmp_removal EXIT
|
||||
|
||||
|
||||
################################################################################
|
||||
# Type
|
||||
#
|
||||
__cdist_type_dir()
|
||||
{
|
||||
echo "${__cdist_type_dir}/$1"
|
||||
}
|
||||
|
||||
__cdist_type_explorer_dir()
|
||||
{
|
||||
echo "${__cdist_type_dir}/$1/${__cdist_name_explorer}"
|
||||
}
|
||||
|
||||
__cdist_type_from_object()
|
||||
{
|
||||
echo "${1%%/*}"
|
||||
}
|
||||
|
||||
__cdist_type_has_explorer()
|
||||
{
|
||||
# We only create output, if there's at least one explorer
|
||||
# and can thus be used as a boolean ;-)
|
||||
if [ -d "$(__cdist_type_explorer_dir "$1")" ]; then
|
||||
ls -1 "$(__cdist_type_explorer_dir "$1")"
|
||||
fi
|
||||
}
|
||||
|
||||
__cdist_type_explorer_pushed()
|
||||
{
|
||||
[ -f "${__cdist_out_type_dir}/${__cdist_name_type_explorer_pushed}" ] \
|
||||
&& grep -x -q "$1" "${__cdist_out_type_dir}/${__cdist_name_type_explorer_pushed}"
|
||||
}
|
||||
|
||||
__cdist_type_explorer_pushed_add()
|
||||
{
|
||||
[ -d "$__cdist_out_type_dir" ] || mkdir "$__cdist_out_type_dir"
|
||||
echo "$1" >> "${__cdist_out_type_dir}/${__cdist_name_type_explorer_pushed}"
|
||||
}
|
||||
|
||||
__cdist_type_gencode()
|
||||
{
|
||||
echo "${__cdist_type_dir}/$1/${__cdist_name_gencode}-$2"
|
||||
}
|
||||
|
||||
__cdist_type_manifest()
|
||||
{
|
||||
echo "${__cdist_type_dir}/$1/${__cdist_name_manifest}"
|
||||
}
|
||||
|
||||
__cdist_type_parameter_dir()
|
||||
{
|
||||
echo "$(__cdist_type_dir "$1")/${__cdist_name_parameter}"
|
||||
}
|
||||
|
||||
__cdist_type_parameter_optional()
|
||||
{
|
||||
echo "$(__cdist_type_parameter_dir "$1")/$__cdist_name_parameter_optional"
|
||||
}
|
||||
|
||||
__cdist_type_parameter_required()
|
||||
{
|
||||
echo "$(__cdist_type_parameter_dir "$1")/$__cdist_name_parameter_required"
|
||||
}
|
||||
|
||||
__cdist_type_singleton()
|
||||
{
|
||||
echo "${__cdist_type_dir}/$1/${__cdist_name_singleton}"
|
||||
}
|
||||
|
|
@ -1,310 +0,0 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# 2010-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/>.
|
||||
#
|
||||
#
|
||||
# Give the user an introduction into cdist
|
||||
#
|
||||
|
||||
. cdist-config
|
||||
set -eu
|
||||
|
||||
banner="cdist-quickstart>"
|
||||
continue="Press enter to continue or ctrl-c to abort."
|
||||
create_continue="Press enter to create the described files/directories"
|
||||
|
||||
__prompt()
|
||||
{
|
||||
echo -n "$banner" "$@"
|
||||
read answer
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Intro of quickstart
|
||||
#
|
||||
cat << eof
|
||||
$banner cdist version $__cdist_version
|
||||
|
||||
Welcome to the interactive guide to cdist!
|
||||
This is the interactive tutorial and beginners help for cdist and here's
|
||||
our schedule:
|
||||
|
||||
- Stages: How cdist operates
|
||||
- Explorer: Explore facts of the target host
|
||||
- Manifest: Map configurations to hosts
|
||||
- Types: Bundled functionality
|
||||
- Deploy a configuration to the local host!
|
||||
|
||||
eof
|
||||
__prompt "$continue"
|
||||
|
||||
################################################################################
|
||||
# Stages
|
||||
#
|
||||
cat << eof
|
||||
|
||||
To deploy configurations to a host, you call
|
||||
|
||||
cdist-deploy-to <hostname>
|
||||
|
||||
which makes calls to other scripts, which realise the so called "stages".
|
||||
Usually you'll not notice this, but in case you want to debug or hack cdist,
|
||||
you can run each stage on its own. Besides that, you just need to remember
|
||||
that the command cdist-deploy-to is the main cdist command.
|
||||
|
||||
See also:
|
||||
|
||||
Source of cdist-deploy-to(1), cdist-stages(7)
|
||||
|
||||
eof
|
||||
__prompt "$continue"
|
||||
|
||||
################################################################################
|
||||
# Explorer
|
||||
#
|
||||
cat << eof
|
||||
|
||||
The first thing cdist always does is running different explorers on the
|
||||
target host. The explorers can be found in the directory
|
||||
|
||||
${__cdist_explorer_dir}
|
||||
|
||||
An explorer is executed on the target host and its output is saved to a file.
|
||||
You can use these files later to decide what or how to configure the host.
|
||||
|
||||
For a demonstration, we'll call the OS explorer locally now, but remember:
|
||||
This is only for demonstration, normally it is run on the target host.
|
||||
The os explorer will which either displays the detected operating system or
|
||||
nothing if it does not know your OS.
|
||||
|
||||
See also:
|
||||
|
||||
cdist-explorer(7)
|
||||
|
||||
eof
|
||||
explorer="${__cdist_explorer_dir}/os"
|
||||
|
||||
__prompt "Press enter to execute $explorer"
|
||||
|
||||
set -x
|
||||
"$explorer"
|
||||
set +x
|
||||
|
||||
################################################################################
|
||||
# Manifest
|
||||
#
|
||||
cat << eof
|
||||
|
||||
The initial manifest is the entry point for cdist to find out, what you would
|
||||
like to have configured. It is located at
|
||||
|
||||
${__cdist_manifest_init}
|
||||
|
||||
And can be as simple as
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
__file /etc/cdist-configured --type file
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
See also:
|
||||
|
||||
cdist-manifest(7)
|
||||
|
||||
eof
|
||||
__prompt "$continue"
|
||||
|
||||
cat << eof
|
||||
|
||||
Let's take a deeper look at the initial manifest to understand what it means:
|
||||
|
||||
__file /etc/cdist-configured --type file
|
||||
| | | \\
|
||||
| | The parameter type \\ With the value file
|
||||
| |
|
||||
| |
|
||||
| | This is the object id
|
||||
|
|
||||
__file is a so called "type"
|
||||
|
||||
|
||||
This essentially looks like a standard command executed in the shell.
|
||||
eof
|
||||
__prompt "$continue"
|
||||
|
||||
cat << eof
|
||||
|
||||
And that's exactly true. Manifests are shell snippets that can use
|
||||
types as commands with arguments. cdist prepends a special path
|
||||
that contain links to the cdist-type-emulator, to \$PATH, so you
|
||||
can use your types as a command.
|
||||
|
||||
This is also the reason why types should always be prefixed with
|
||||
"__", to prevent collisions with existing binaries.
|
||||
|
||||
The object id is unique per type and used to prevent you from creating
|
||||
the same object twice.
|
||||
|
||||
Parameters are type specific and are always specified as --parameter <value>.
|
||||
|
||||
See also:
|
||||
|
||||
cdist-type-build-emulation(1), cdist-type-emulator(1)
|
||||
|
||||
eof
|
||||
__prompt "$continue"
|
||||
|
||||
################################################################################
|
||||
# Types
|
||||
#
|
||||
cat << eof
|
||||
|
||||
Types are bundled functionality and are the main component of cdist.
|
||||
If you want to have a feature x, you write the type __x. Types are stored in
|
||||
|
||||
${__cdist_type_dir}
|
||||
|
||||
And cdist ships with some types already!
|
||||
|
||||
See also:
|
||||
|
||||
cdist-type(7)
|
||||
|
||||
eof
|
||||
__prompt "Press enter to see available types"
|
||||
|
||||
set -x
|
||||
ls ${__cdist_type_dir}
|
||||
set +x
|
||||
|
||||
cat << eof
|
||||
|
||||
Types consist of the following parts:
|
||||
|
||||
- ${__cdist_name_parameter} (${__cdist_name_parameter_required}/${__cdist_name_parameter_optional}
|
||||
- ${__cdist_name_manifest}
|
||||
- ${__cdist_name_explorer}
|
||||
- ${__cdist_name_gencode}
|
||||
|
||||
eof
|
||||
__prompt "$continue"
|
||||
|
||||
|
||||
cat << eof
|
||||
|
||||
Every type must have a directory named ${__cdist_name_parameter}, which
|
||||
contains required or optional parameters (in newline seperated files).
|
||||
|
||||
If an object of a specific type was created in the initial manifest,
|
||||
the manifest of the type is run and may create other objects.
|
||||
|
||||
A type may have ${__cdist_name_explorer}, which are very similar to the
|
||||
${__cdist_name_explorer} seen above, but with a different purpose:
|
||||
They are specific to the type and are not relevant for other types.
|
||||
|
||||
You may use them for instance to find out details on the target host,
|
||||
so you can decide what to do on the target host eventually.
|
||||
|
||||
After the ${__cdist_name_manifest} and the ${__cdist_name_explorer} of
|
||||
a type have been run, ${__cdist_name_gencode} is executed, which creates
|
||||
code to be executed on the target on stdout.
|
||||
|
||||
eof
|
||||
__prompt "$continue"
|
||||
|
||||
################################################################################
|
||||
# Deployment
|
||||
#
|
||||
|
||||
cat << eof
|
||||
|
||||
Now you've got some basic knowledge about cdist, let's configure your a host!
|
||||
|
||||
Ensure that you have a ssh server running on the host and that you can login as root.
|
||||
|
||||
eof
|
||||
|
||||
__prompt "Enter hostname or press enter for localhost: "
|
||||
|
||||
if [ "$answer" ]; then
|
||||
host="$answer"
|
||||
else
|
||||
host="localhost"
|
||||
fi
|
||||
|
||||
manifestinit="conf/manifest/init"
|
||||
cat << eof
|
||||
|
||||
I'll now setup $manifestinit, containing the following code:
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
# Every machine becomes a marker, so sysadmins know that automatic
|
||||
# configurations are happening
|
||||
__file /etc/cdist-configured
|
||||
|
||||
case "\$__target_host" in
|
||||
$host)
|
||||
__link /tmp/cdist-testfile --source /etc/cdist-configured --type symbolic
|
||||
__addifnosuchline /tmp/cdist-welcome --line "Welcome to cdist"
|
||||
;;
|
||||
esac
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
WARNING: This will overwrite ${manifestinit}.
|
||||
|
||||
eof
|
||||
|
||||
cat > "$__cdist_abs_mydir/../$manifestinit" << eof
|
||||
|
||||
# Every machine becomes a marker, so sysadmins know that automatic
|
||||
# configurations are happening
|
||||
__file /etc/cdist-configured
|
||||
|
||||
case "\$__target_host" in
|
||||
$host)
|
||||
__link /tmp/cdist-testfile --source /etc/cdist-configured --type symbolic
|
||||
__addifnosuchline /tmp/cdist-welcome --line "Welcome to cdist"
|
||||
;;
|
||||
esac
|
||||
|
||||
eof
|
||||
|
||||
chmod u+x "$__cdist_abs_mydir/../$manifestinit"
|
||||
|
||||
cmd="cdist-deploy-to $host"
|
||||
|
||||
__prompt "Press enter to run \"$cmd\""
|
||||
|
||||
# No quotes, we need field splitting
|
||||
$cmd
|
||||
|
||||
################################################################################
|
||||
# End
|
||||
#
|
||||
|
||||
cat << eof
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
That's it, this is the end of the cdist-quickstart.
|
||||
|
||||
I hope you've got some impression on how cdist works, here are again some
|
||||
pointers on where to continue to read:
|
||||
|
||||
cdist(7), cdist-deploy-to(1), cdist-type(7), cdist-stages(7)
|
||||
|
||||
eof
|
||||
Loading…
Add table
Add a link
Reference in a new issue