1 |
#!/bin/bash |
2 |
|
3 |
# Script to concat files to a config file. |
4 |
# |
5 |
# Given a directory like this: |
6 |
# /path/to/conf.d |
7 |
# |-- fragments |
8 |
# | |-- 00_named.conf |
9 |
# | |-- 10_domain.net |
10 |
# | `-- zz_footer |
11 |
# |
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. |
15 |
# |
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. |
18 |
# |
19 |
# OPTIONS: |
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 |
29 |
# |
30 |
# the command: |
31 |
# |
32 |
# concatfragments.sh -o /path/to/conffile.cfg -d /path/to/conf.d |
33 |
# |
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. |
37 |
# |
38 |
# The script does error checking on the various dirs and files to make |
39 |
# sure things don't fail. |
40 |
|
41 |
OUTFILE="" |
42 |
WORKDIR="" |
43 |
TEST="" |
44 |
FORCE="" |
45 |
WARN="" |
46 |
SORTARG="-z" |
47 |
|
48 |
PATH=/sbin:/usr/sbin:/bin:/usr/bin |
49 |
|
50 |
while getopts "o:s:d:tnw:f" options; do |
51 |
case $options in |
52 |
o ) OUTFILE=$OPTARG;; |
53 |
d ) WORKDIR=$OPTARG;; |
54 |
n ) SORTARG="-zn";; |
55 |
w ) WARNMSG="$OPTARG";; |
56 |
f ) FORCE="true";; |
57 |
t ) TEST="true";; |
58 |
* ) echo "Specify output file with -o and fragments directory with -d" |
59 |
exit 1;; |
60 |
esac |
61 |
done |
62 |
|
63 |
# do we have -o? |
64 |
if [ x${OUTFILE} = "x" ]; then |
65 |
echo "Please specify an output file with -o" |
66 |
exit 1 |
67 |
fi |
68 |
|
69 |
# do we have -d? |
70 |
if [ x${WORKDIR} = "x" ]; then |
71 |
echo "Please fragments directory with -d" |
72 |
exit 1 |
73 |
fi |
74 |
|
75 |
# can we write to -o? |
76 |
if [ -a ${OUTFILE} ]; then |
77 |
if [ ! -w ${OUTFILE} ]; then |
78 |
echo "Cannot write to ${OUTFILE}" |
79 |
exit 1 |
80 |
fi |
81 |
else |
82 |
if [ ! -w `dirname ${OUTFILE}` ]; then |
83 |
echo "Cannot write to `dirname ${OUTFILE}` to create ${OUTFILE}" |
84 |
exit 1 |
85 |
fi |
86 |
fi |
87 |
|
88 |
# do we have a fragments subdir inside the work dir? |
89 |
if [ ! -d "${WORKDIR}/fragments" ] && [ ! -x "${WORKDIR}/fragments" ]; then |
90 |
echo "Cannot access the fragments directory" |
91 |
exit 1 |
92 |
fi |
93 |
|
94 |
# are there actually any fragments? |
95 |
if [ ! "$(ls -A ${WORKDIR}/fragments)" ]; then |
96 |
if [ x${FORCE} = "x" ]; then |
97 |
echo "The fragments directory is empty, cowardly refusing to make empty config files" |
98 |
exit 1 |
99 |
fi |
100 |
fi |
101 |
|
102 |
cd ${WORKDIR} |
103 |
|
104 |
if [ x${WARNMSG} = "x" ]; then |
105 |
: > "fragments.concat" |
106 |
else |
107 |
echo -e "$WARNMSG" > "fragments.concat" |
108 |
fi |
109 |
|
110 |
# find all the files in the fragments directory, sort them numerically and concat to fragments.concat in the working dir |
111 |
find fragments/ -type f -follow -print0 |sort ${SORTARG}|xargs -0 cat >>"fragments.concat" |
112 |
|
113 |
if [ x${TEST} = "x" ]; then |
114 |
# This is a real run, copy the file to outfile |
115 |
cp fragments.concat ${OUTFILE} |
116 |
RETVAL=$? |
117 |
else |
118 |
# Just compare the result to outfile to help the exec decide |
119 |
cmp ${OUTFILE} fragments.concat |
120 |
RETVAL=$? |
121 |
fi |
122 |
|
123 |
exit $RETVAL |