2011-02-03 18:58:09 +01:00
|
|
|
#!/bin/sh
|
2010-05-06 23:55:02 +02:00
|
|
|
|
|
|
|
# Script to concat files to a config file.
|
|
|
|
#
|
|
|
|
# Given a directory like this:
|
|
|
|
# /path/to/conf.d
|
|
|
|
# |-- fragments
|
|
|
|
# | |-- 00_named.conf
|
|
|
|
# | |-- 10_domain.net
|
|
|
|
# | `-- zz_footer
|
|
|
|
#
|
|
|
|
# The script supports a test option that will build the concat file to a temp location and
|
|
|
|
# use /usr/bin/cmp to verify if it should be run or not. This would result in the concat happening
|
|
|
|
# twice on each run but gives you the option to have an unless option in your execs to inhibit rebuilds.
|
|
|
|
#
|
|
|
|
# Without the test option and the unless combo your services that depend on the final file would end up
|
|
|
|
# restarting on each run, or in other manifest models some changes might get missed.
|
|
|
|
#
|
|
|
|
# OPTIONS:
|
|
|
|
# -o The file to create from the sources
|
|
|
|
# -d The directory where the fragments are kept
|
|
|
|
# -t Test to find out if a build is needed, basically concats the files to a temp
|
|
|
|
# location and compare with what's in the final location, return codes are designed
|
|
|
|
# for use with unless on an exec resource
|
|
|
|
# -w Add a shell style comment at the top of the created file to warn users that it
|
|
|
|
# is generated by puppet
|
|
|
|
# -f Enables the creation of empty output files when no fragments are found
|
|
|
|
# -n Sort the output numerically rather than the default alpha sort
|
|
|
|
#
|
|
|
|
# the command:
|
|
|
|
#
|
|
|
|
# concatfragments.sh -o /path/to/conffile.cfg -d /path/to/conf.d
|
|
|
|
#
|
|
|
|
# creates /path/to/conf.d/fragments.concat and copies the resulting
|
|
|
|
# file to /path/to/conffile.cfg. The files will be sorted alphabetically
|
|
|
|
# pass the -n switch to sort numerically.
|
|
|
|
#
|
|
|
|
# The script does error checking on the various dirs and files to make
|
|
|
|
# sure things don't fail.
|
|
|
|
|
|
|
|
OUTFILE=""
|
|
|
|
WORKDIR=""
|
|
|
|
TEST=""
|
|
|
|
FORCE=""
|
|
|
|
WARN=""
|
2012-04-11 23:09:50 +02:00
|
|
|
SORTARG=""
|
2013-08-07 16:48:57 +02:00
|
|
|
ENSURE_NEWLINE=""
|
2010-05-06 23:55:02 +02:00
|
|
|
|
|
|
|
PATH=/sbin:/usr/sbin:/bin:/usr/bin
|
|
|
|
|
2011-12-14 05:24:10 +01:00
|
|
|
## Well, if there's ever a bad way to do things, Nexenta has it.
|
|
|
|
## http://nexenta.org/projects/site/wiki/Personalities
|
|
|
|
unset SUN_PERSONALITY
|
|
|
|
|
2013-08-07 02:26:43 +02:00
|
|
|
while getopts "o:s:d:tnw:fl" options; do
|
2010-05-06 23:55:02 +02:00
|
|
|
case $options in
|
|
|
|
o ) OUTFILE=$OPTARG;;
|
|
|
|
d ) WORKDIR=$OPTARG;;
|
2012-04-11 23:09:50 +02:00
|
|
|
n ) SORTARG="-n";;
|
2010-10-04 23:02:32 +02:00
|
|
|
w ) WARNMSG="$OPTARG";;
|
2010-05-06 23:55:02 +02:00
|
|
|
f ) FORCE="true";;
|
|
|
|
t ) TEST="true";;
|
2013-08-07 16:48:57 +02:00
|
|
|
l ) ENSURE_NEWLINE="true";;
|
2010-05-06 23:55:02 +02:00
|
|
|
* ) echo "Specify output file with -o and fragments directory with -d"
|
|
|
|
exit 1;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# do we have -o?
|
2013-12-17 17:03:38 +01:00
|
|
|
if [ "x${OUTFILE}" = "x" ]; then
|
2010-05-06 23:55:02 +02:00
|
|
|
echo "Please specify an output file with -o"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# do we have -d?
|
2013-12-17 17:03:38 +01:00
|
|
|
if [ "x${WORKDIR}" = "x" ]; then
|
2010-05-06 23:55:02 +02:00
|
|
|
echo "Please fragments directory with -d"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# can we write to -o?
|
2013-12-17 17:03:38 +01:00
|
|
|
if [ -f "${OUTFILE}" ]; then
|
|
|
|
if [ ! -w "${OUTFILE}" ]; then
|
2010-05-06 23:55:02 +02:00
|
|
|
echo "Cannot write to ${OUTFILE}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2013-12-17 17:03:38 +01:00
|
|
|
if [ ! -w `dirname "${OUTFILE}"` ]; then
|
|
|
|
echo "Cannot write to `dirname \"${OUTFILE}\"` to create ${OUTFILE}"
|
2010-05-06 23:55:02 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# do we have a fragments subdir inside the work dir?
|
|
|
|
if [ ! -d "${WORKDIR}/fragments" ] && [ ! -x "${WORKDIR}/fragments" ]; then
|
|
|
|
echo "Cannot access the fragments directory"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# are there actually any fragments?
|
2014-04-04 15:35:06 +02:00
|
|
|
if [ ! "$(ls -A """${WORKDIR}/fragments""")" ]; then
|
2013-12-17 17:03:38 +01:00
|
|
|
if [ "x${FORCE}" = "x" ]; then
|
2010-05-06 23:55:02 +02:00
|
|
|
echo "The fragments directory is empty, cowardly refusing to make empty config files"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2013-12-17 17:03:38 +01:00
|
|
|
cd "${WORKDIR}"
|
2010-05-06 23:55:02 +02:00
|
|
|
|
2013-04-28 13:57:28 +02:00
|
|
|
if [ "x${WARNMSG}" = "x" ]; then
|
2010-10-04 23:02:32 +02:00
|
|
|
: > "fragments.concat"
|
2010-05-06 23:55:02 +02:00
|
|
|
else
|
2011-04-20 23:54:27 +02:00
|
|
|
printf '%s\n' "$WARNMSG" > "fragments.concat"
|
2010-05-06 23:55:02 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# find all the files in the fragments directory, sort them numerically and concat to fragments.concat in the working dir
|
2013-07-13 22:41:14 +02:00
|
|
|
IFS_BACKUP=$IFS
|
|
|
|
IFS='
|
|
|
|
'
|
2014-04-25 10:47:57 +02:00
|
|
|
for fragfile in `find fragments/ -type f -follow -print0 | xargs -0 -n1 basename | LC_ALL=C sort ${SORTARG}`
|
2013-07-13 22:41:14 +02:00
|
|
|
do
|
2014-04-25 10:47:57 +02:00
|
|
|
cat "fragments/$fragfile" >> "fragments.concat"
|
2014-04-22 00:36:59 +02:00
|
|
|
# Handle newlines.
|
|
|
|
if [ "x${ENSURE_NEWLINE}" != "x" ]; then
|
|
|
|
echo >> "fragments.concat"
|
|
|
|
fi
|
2012-04-11 23:09:50 +02:00
|
|
|
done
|
2013-07-13 22:41:14 +02:00
|
|
|
IFS=$IFS_BACKUP
|
2010-05-06 23:55:02 +02:00
|
|
|
|
2013-12-17 17:03:38 +01:00
|
|
|
if [ "x${TEST}" = "x" ]; then
|
2010-05-06 23:55:02 +02:00
|
|
|
# This is a real run, copy the file to outfile
|
2013-12-17 17:03:38 +01:00
|
|
|
cp fragments.concat "${OUTFILE}"
|
2010-05-06 23:55:02 +02:00
|
|
|
RETVAL=$?
|
|
|
|
else
|
|
|
|
# Just compare the result to outfile to help the exec decide
|
2013-12-17 17:03:38 +01:00
|
|
|
cmp "${OUTFILE}" fragments.concat
|
2010-05-06 23:55:02 +02:00
|
|
|
RETVAL=$?
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit $RETVAL
|