2010-12-01 18:52:34 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
2011-02-02 14:02:26 +00:00
|
|
|
# 2010-2011 Nico Schottelius (nico-cdist at schottelius.org)
|
2010-12-01 18:52:34 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Wrapper script that generates cconfig from arguments
|
|
|
|
#
|
2011-01-14 11:46:51 +00:00
|
|
|
# This script will be called everytime the manifest decides to create
|
|
|
|
# a new type
|
|
|
|
#
|
2010-12-01 18:52:34 +00:00
|
|
|
|
|
|
|
. cdist-config
|
2011-02-05 18:10:48 +00:00
|
|
|
set -eu
|
|
|
|
|
2011-03-23 09:47:13 +00:00
|
|
|
################################################################################
|
|
|
|
# Prepare object and type
|
|
|
|
#
|
|
|
|
|
2011-02-23 08:20:34 +00:00
|
|
|
__cdist_type="$__cdist_myname"
|
2010-12-01 19:33:25 +00:00
|
|
|
|
2011-03-10 12:21:21 +00:00
|
|
|
# Find out whether type is a singleton or regular type
|
2011-03-09 21:09:44 +00:00
|
|
|
if [ -f "$(__cdist_type_singleton "$__cdist_type")" ]; then
|
2011-03-23 09:47:13 +00:00
|
|
|
__cdist_object_id="$__cdist_name_singleton"
|
2011-03-09 21:09:44 +00:00
|
|
|
else
|
|
|
|
[ $# -ge 1 ] || __cdist_usage "<id> <options>"
|
|
|
|
__cdist_object_id="$1"; shift
|
|
|
|
fi
|
|
|
|
|
2011-03-23 09:47:13 +00:00
|
|
|
# Verify object id
|
|
|
|
__cdist_object_id_sane=$(echo "$__cdist_object_id" | grep "^${__cdist_sane_regexp}\$")
|
|
|
|
if [ -z "$__cdist_object_id_sane" ]; then
|
2011-02-07 22:59:21 +00:00
|
|
|
__cdist_usage "Insane object id, ${__cdist_object_id}."
|
2011-03-23 09:47:13 +00:00
|
|
|
fi
|
2010-12-01 19:33:25 +00:00
|
|
|
|
2011-03-23 09:47:13 +00:00
|
|
|
# Prevent double slash if id begins with /
|
2011-03-22 07:57:19 +00:00
|
|
|
if [ "$(echo $__cdist_object_id | grep "^/")" ]; then
|
2011-03-23 09:47:13 +00:00
|
|
|
__cdist_object_self="${__cdist_type}${__cdist_object_id}"
|
2011-03-22 07:57:19 +00:00
|
|
|
else
|
2011-03-23 09:47:13 +00:00
|
|
|
__cdist_object_self="${__cdist_type}/${__cdist_object_id}"
|
2011-03-22 07:57:19 +00:00
|
|
|
fi
|
|
|
|
|
2011-03-23 09:47:13 +00:00
|
|
|
################################################################################
|
2011-03-23 10:05:02 +00:00
|
|
|
# Internal quirks
|
2011-03-23 09:47:13 +00:00
|
|
|
#
|
|
|
|
|
2011-03-23 10:05:02 +00:00
|
|
|
# Append id for error messages
|
|
|
|
__cdist_myname="$__cdist_myname ($__cdist_object_id)"
|
2011-03-23 09:47:13 +00:00
|
|
|
|
2011-03-23 10:05:02 +00:00
|
|
|
################################################################################
|
2011-03-23 10:39:17 +00:00
|
|
|
# Create object in tmpdir first
|
2011-03-23 10:05:02 +00:00
|
|
|
#
|
|
|
|
|
2011-03-23 10:39:17 +00:00
|
|
|
# Save original destination
|
|
|
|
__cdist_out_object_dir_orig="$__cdist_out_object_dir"
|
2011-03-23 10:05:02 +00:00
|
|
|
|
2011-03-23 10:39:17 +00:00
|
|
|
# Store to tmp now
|
|
|
|
__cdist_out_object_dir="$__cdist_tmp_dir"
|
2011-03-23 09:47:13 +00:00
|
|
|
|
2011-03-23 10:39:17 +00:00
|
|
|
__cdist_object_dir="$(__cdist_object_dir "$__cdist_object_self")"
|
2010-12-01 19:53:26 +00:00
|
|
|
|
2011-03-23 10:39:17 +00:00
|
|
|
# Record source
|
|
|
|
mkdir -p "${__cdist_object_dir}"
|
|
|
|
__cdist_object_source_add "${__cdist_object_dir}"
|
2011-03-23 10:05:02 +00:00
|
|
|
|
|
|
|
|
2011-03-23 10:39:17 +00:00
|
|
|
# Record parameter
|
2011-03-23 10:44:33 +00:00
|
|
|
__cdist_parameter_dir="$(__cdist_object_parameter_dir "$__cdist_object_self")"
|
2011-02-24 20:37:55 +00:00
|
|
|
mkdir -p "${__cdist_parameter_dir}"
|
2010-12-01 19:48:15 +00:00
|
|
|
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
opt="$1"; shift
|
|
|
|
|
2011-02-16 22:04:33 +00:00
|
|
|
echo "$opt" | grep -q "^--${__cdist_sane_regexp}\$" || \
|
|
|
|
__cdist_usage "Provide sane options"
|
2010-12-01 19:48:15 +00:00
|
|
|
|
2011-03-23 10:44:33 +00:00
|
|
|
opt_file="${opt#--}"
|
2010-12-01 19:48:15 +00:00
|
|
|
|
|
|
|
[ $# -ge 1 ] || __cdist_usage "Missing value for $opt"
|
|
|
|
|
|
|
|
value="$1"; shift
|
|
|
|
|
2011-03-23 10:44:33 +00:00
|
|
|
echo "${value}" > "${__cdist_parameter_dir}/${opt_file}"
|
2010-12-01 19:48:15 +00:00
|
|
|
done
|
2011-02-16 22:04:33 +00:00
|
|
|
|
2011-03-23 10:56:19 +00:00
|
|
|
# Record requirements
|
|
|
|
# it's fine, if it's not set
|
|
|
|
set +u
|
|
|
|
for requirement in $require; do
|
|
|
|
echo $requirement >> "$(__cdist_object_require "$__cdist_object_self")"
|
|
|
|
echo Object $__cdist_object_self requires $requirement
|
|
|
|
done
|
2011-03-23 11:12:03 +00:00
|
|
|
set -u
|
2011-03-23 10:39:17 +00:00
|
|
|
|
|
|
|
################################################################################
|
2011-03-23 10:56:19 +00:00
|
|
|
# Check newly created object
|
2011-03-23 10:39:17 +00:00
|
|
|
#
|
|
|
|
|
2011-03-23 11:12:03 +00:00
|
|
|
set -x
|
|
|
|
|
|
|
|
#
|
2011-02-16 22:04:33 +00:00
|
|
|
# Ensure required parameters are given
|
2011-03-23 11:12:03 +00:00
|
|
|
#
|
2011-03-10 16:41:08 +00:00
|
|
|
if [ -f "$(__cdist_type_parameter_required "$__cdist_type")" ]; then
|
|
|
|
while read required; do
|
2011-03-23 11:12:03 +00:00
|
|
|
if [ ! -f "${__cdist_parameter_dir}/${required}" ]; then
|
2011-03-10 16:41:08 +00:00
|
|
|
__cdist_usage "Missing required parameter $required"
|
|
|
|
fi
|
|
|
|
done < "$(__cdist_type_parameter_required "$__cdist_type")"
|
|
|
|
fi
|
2011-02-16 22:04:33 +00:00
|
|
|
|
2011-03-23 11:12:03 +00:00
|
|
|
#
|
|
|
|
# Ensure that only optional or required parameters are given
|
|
|
|
#
|
|
|
|
|
2011-03-10 16:41:08 +00:00
|
|
|
if [ -f "$(__cdist_type_parameter_optional "$__cdist_type")" ]; then
|
2011-03-23 11:12:03 +00:00
|
|
|
cat "$(__cdist_type_parameter_optional "$__cdist_type")" > \
|
|
|
|
"$__cdist_tmp_file"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f "$(__cdist_type_parameter_required "$__cdist_type")" ]; then
|
|
|
|
cat "$(__cdist_type_parameter_required "$__cdist_type")" >> \
|
|
|
|
"$__cdist_tmp_file"
|
2011-03-10 16:41:08 +00:00
|
|
|
fi
|
2011-03-23 11:12:03 +00:00
|
|
|
|
|
|
|
cd "$__cdist_parameter_dir"
|
|
|
|
for parameter in $(ls -1); do
|
|
|
|
is_valid=$(grep "^$parameter\$" "$__cdist_tmp_file")
|
2011-02-16 22:04:33 +00:00
|
|
|
|
2011-03-23 11:12:03 +00:00
|
|
|
[ "$is_valid" ] || "Unknown parameter $parameter"
|
|
|
|
done
|
2011-02-16 22:04:33 +00:00
|
|
|
|
2011-03-23 10:56:19 +00:00
|
|
|
exit 1
|
|
|
|
|
2011-03-23 11:12:03 +00:00
|
|
|
################################################################################
|
|
|
|
# Merge object
|
|
|
|
#
|
2011-03-23 11:15:00 +00:00
|
|
|
# Save original destination
|
|
|
|
__cdist_out_object_dir="$__cdist_out_object_dir_orig"
|
|
|
|
|
|
|
|
__cdist_object_destination_dir="$(__cdist_object_dir "$__cdist_object_self")"
|
2011-03-23 10:39:17 +00:00
|
|
|
|
2011-03-23 11:12:03 +00:00
|
|
|
#
|
2011-03-23 11:15:00 +00:00
|
|
|
# If the object already exists and is exactly the same, merge it. Otherwise fail.
|
2011-03-23 11:12:03 +00:00
|
|
|
#
|
2011-03-23 11:15:00 +00:00
|
|
|
if [ -e "${__cdist_new_object_dir}" ]; then
|
|
|
|
source="$(__cdist_object_source "${__cdist_new_object_dir}")"
|
|
|
|
__cdist_exit_err "${__cdist_object} already exists (source: $source)"
|
2011-03-23 10:39:17 +00:00
|
|
|
|
2011-03-23 11:15:00 +00:00
|
|
|
|
|
|
|
fi
|
2011-03-23 10:39:17 +00:00
|
|
|
|
2011-03-23 11:20:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Verify no conflicting objects have been created
|
|
|
|
while read __cdist_new_object; do
|
|
|
|
__cdist_object_found=$(grep "^$__cdist_new_object\$" "$__cdist_objects_list" || true)
|
|
|
|
|
|
|
|
if [ "$__cdist_object_found" ]; then
|
|
|
|
__cdist_object_source="$(cat "$(__cdist_object_dir "$__cdist_new_object")/${__cdist_name_object_source}")"
|
|
|
|
__cdist_exit_err "${__cdist_manifest}: ${__cdist_new_object} already exists (source: $__cdist_object_source)"
|
|
|
|
fi
|
|
|
|
done < "$__cdist_new_objects_list"
|
|
|
|
|
|
|
|
# Safe harbour: We can merge all objects into main tree
|
|
|
|
# Merge = mkdir + mv parameters and source information
|
|
|
|
while read __cdist_new_object; do
|
|
|
|
[ "$__cdist_new_objects_created" = "n" ] && __cdist_new_objects_created="y"
|
|
|
|
|
|
|
|
# where to save the newly created object
|
|
|
|
__cdist_object_dir="$__cdist_out_object_dir/$__cdist_new_object"
|
|
|
|
mkdir -p "$__cdist_object_dir"
|
|
|
|
|
|
|
|
# Move parts of the object (CANNOT MOVE COMPLETLEY, HIERACHY!)
|
|
|
|
__cdist_new_object_dir="$__cdist_new_objects_dir/$__cdist_new_object"
|
|
|
|
|
|
|
|
# Source
|
|
|
|
mv "${__cdist_new_object_dir}/${__cdist_name_object_source}" \
|
|
|
|
"$__cdist_object_dir"
|
|
|
|
|
|
|
|
# Explorer, Parameter, Require
|
|
|
|
for __cdist_object_file in \
|
|
|
|
${__cdist_name_explorer} \
|
|
|
|
${__cdist_name_parameter} \
|
|
|
|
${__cdist_name_require} \
|
|
|
|
; do
|
|
|
|
|
|
|
|
if [ -e "${__cdist_new_object_dir}/${__cdist_object_file}" ]; then
|
|
|
|
mv "${__cdist_new_object_dir}/${__cdist_object_file}" \
|
|
|
|
"$__cdist_object_dir"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done < "$__cdist_new_objects_list"
|
|
|
|
|
|
|
|
# Remove listing and objects, otherwise the next type will reuse it...
|
|
|
|
rm -rf "$__cdist_new_objects_dir" "$__cdist_new_objects_list"
|
|
|
|
else
|
|
|
|
# Be nice, warn user if manifests exists, but is not executable
|
|
|
|
if [ -f "${__cdist_manifest}" ]; then
|
|
|
|
echo "Warning ${__cdist_manifest} exists, but is not executable." >&2
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|