3 # Script to concat files to a config file.
5 # Given a directory like this:
12 # The script supports a test option that will build the concat file to a temp location and
13 # use /usr/bin/cmp to verify if it should be run or not. This would result in the concat happening
14 # twice on each run but gives you the option to have an unless option in your execs to inhibit rebuilds.
16 # Without the test option and the unless combo your services that depend on the final file would end up
17 # restarting on each run, or in other manifest models some changes might get missed.
20 # -o The file to create from the sources
21 # -d The directory where the fragments are kept
22 # -t Test to find out if a build is needed, basically concats the files to a temp
23 # location and compare with what's in the final location, return codes are designed
24 # for use with unless on an exec resource
25 # -w Add a shell style comment at the top of the created file to warn users that it
26 # is generated by puppet
27 # -f Enables the creation of empty output files when no fragments are found
28 # -n Sort the output numerically rather than the default alpha sort
32 # concatfragments.sh -o /path/to/conffile.cfg -d /path/to/conf.d
34 # creates /path/to/conf.d/fragments.concat and copies the resulting
35 # file to /path/to/conffile.cfg. The files will be sorted alphabetically
36 # pass the -n switch to sort numerically.
38 # The script does error checking on the various dirs and files to make
39 # sure things don't fail.
48 PATH=/sbin:/usr/sbin:/bin:/usr/bin
50 ## Well, if there's ever a bad way to do things, Nexenta has it.
51 ## http://nexenta.org/projects/site/wiki/Personalities
54 while getopts "o:s:d:tnw:f" options; do
59 w ) WARNMSG="$OPTARG";;
62 * ) echo "Specify output file with -o and fragments directory with -d"
68 if [ x${OUTFILE} = "x" ]; then
69 echo "Please specify an output file with -o"
74 if [ x${WORKDIR} = "x" ]; then
75 echo "Please fragments directory with -d"
80 if [ -f ${OUTFILE} ]; then
81 if [ ! -w ${OUTFILE} ]; then
82 echo "Cannot write to ${OUTFILE}"
86 if [ ! -w `dirname ${OUTFILE}` ]; then
87 echo "Cannot write to `dirname ${OUTFILE}` to create ${OUTFILE}"
92 # do we have a fragments subdir inside the work dir?
93 if [ ! -d "${WORKDIR}/fragments" ] && [ ! -x "${WORKDIR}/fragments" ]; then
94 echo "Cannot access the fragments directory"
98 # are there actually any fragments?
99 if [ ! "$(ls -A ${WORKDIR}/fragments)" ]; then
100 if [ x${FORCE} = "x" ]; then
101 echo "The fragments directory is empty, cowardly refusing to make empty config files"
108 if [ x${WARNMSG} = "x" ]; then
109 : > "fragments.concat"
111 printf '%s\n' "$WARNMSG" > "fragments.concat"
114 # find all the files in the fragments directory, sort them numerically and concat to fragments.concat in the working dir
115 find fragments/ -type f -follow | sort ${SORTARG} | while read -r fragfile; do
116 cat "$fragfile" >> "fragments.concat"
119 if [ x${TEST} = "x" ]; then
120 # This is a real run, copy the file to outfile
121 cp fragments.concat ${OUTFILE}
124 # Just compare the result to outfile to help the exec decide
125 cmp ${OUTFILE} fragments.concat