1 | #!/bin/sh
|
---|
2 | # Wireless Leiden config-update script for FreeBSD 8.0 (nanobsd)
|
---|
3 | # Based on the 'API' of Jasper
|
---|
4 | # Rick van der Zwet
|
---|
5 | # XXX: TODO, some proper error checking for fetch
|
---|
6 |
|
---|
7 |
|
---|
8 | # Slow connection = no connection
|
---|
9 | export HTTP_TIMEOUT=5
|
---|
10 |
|
---|
11 |
|
---|
12 | check_access() {
|
---|
13 | # Direct Access - Internal IP
|
---|
14 | BASEURL="http://172.16.4.46/wleiden/config/"
|
---|
15 | # Connectivity check
|
---|
16 | fetch -o /dev/null -q $BASEURL > /dev/null && return
|
---|
17 | echo "# WARN: Fetch via internal $BASEURL failed"
|
---|
18 |
|
---|
19 | # Direct Access - External DNS
|
---|
20 | BASEURL="http://132.229.112.21/wleiden/config/"
|
---|
21 | fetch -o /dev/null -q $BASEURL > /dev/null && return
|
---|
22 | echo "# CRIT: Fetch via external $BASEURL failed"
|
---|
23 |
|
---|
24 | exit 1
|
---|
25 | }
|
---|
26 | check_access
|
---|
27 |
|
---|
28 |
|
---|
29 | # Default config to fetch
|
---|
30 | CONFIG=`hostname -s`
|
---|
31 |
|
---|
32 | # Determine it's statup and running location and some other hints
|
---|
33 | # Skip named.conf as it not planned in current release
|
---|
34 | FILES="authorized_keys dhcpd.conf dnsmasq.conf motd rc.conf.local resolv.conf pf.hybrid.conf.local wleiden.yaml"
|
---|
35 | file_details() {
|
---|
36 | POST_CMD=""
|
---|
37 | FILE_HINT=""
|
---|
38 |
|
---|
39 | case "$1" in
|
---|
40 | 'authorized_keys')
|
---|
41 | STARTUP_LOC="/cfg/ssh/${FILE}"
|
---|
42 | RUNNING_LOC="/etc/ssh/${FILE}"
|
---|
43 | ;;
|
---|
44 | 'dhcpd.conf')
|
---|
45 | STARTUP_LOC="/cfg/local/${FILE}"
|
---|
46 | RUNNING_LOC="/usr/local/etc/${FILE}"
|
---|
47 | POST_CMD="service isc-dhcpd restart"
|
---|
48 | ;;
|
---|
49 | 'dnsmasq.conf')
|
---|
50 | STARTUP_LOC="/cfg/local/${FILE}"
|
---|
51 | RUNNING_LOC="/usr/local/etc/${FILE}"
|
---|
52 | POST_CMD="service dnsmasq restart"
|
---|
53 | ;;
|
---|
54 | 'motd')
|
---|
55 | STARTUP_LOC="/cfg/${FILE}"
|
---|
56 | RUNNING_LOC="/etc/${FILE}"
|
---|
57 | POST_CMD="/etc/rc.d/motd onestart"
|
---|
58 | ;;
|
---|
59 | 'named.conf')
|
---|
60 | STARTUP_LOC="/cfg/namedb/${FILE}"
|
---|
61 | RUNNING_LOC="/etc/namedb/${FILE}"
|
---|
62 | POST_CMD="service named restart"
|
---|
63 | ;;
|
---|
64 | 'rc.conf.local')
|
---|
65 | STARTUP_LOC="/cfg/${FILE}"
|
---|
66 | RUNNING_LOC="/etc/${FILE}"
|
---|
67 | FILE_HINT="Restart interfaces with: nohup service netif restart"
|
---|
68 | ;;
|
---|
69 | 'resolv.conf')
|
---|
70 | STARTUP_LOC="/cfg/${FILE}"
|
---|
71 | RUNNING_LOC="/etc/${FILE}"
|
---|
72 | FILE_HINT="To get the ordering right run: nameserver-shuffle"
|
---|
73 | ;;
|
---|
74 | 'pf.hybrid.conf.local')
|
---|
75 | STARTUP_LOC="/cfg/${FILE}"
|
---|
76 | RUNNING_LOC="/etc/${FILE}"
|
---|
77 | POST_CMD="service pf reload"
|
---|
78 | ;;
|
---|
79 | 'wleiden.yaml')
|
---|
80 | STARTUP_LOC="/cfg/local/${FILE}"
|
---|
81 | RUNNING_LOC="/usr/local/etc/${FILE}"
|
---|
82 | ;;
|
---|
83 | esac
|
---|
84 | }
|
---|
85 |
|
---|
86 | usage() {
|
---|
87 | (
|
---|
88 | echo "Usage: $0 [-bpn] [-c <config>] [-m <all|startup|testing|running>]"
|
---|
89 | echo " -b = batch mode, no user input"
|
---|
90 | echo " -c <config> = default configuration to fetch"
|
---|
91 | echo " -d = do not run the POST_CMD commands [default]"
|
---|
92 | echo " -p = run the POST_CMD commands to activate the services right-away"
|
---|
93 | echo " -n = do not mount config partition"
|
---|
94 | echo " -m all = copy config files to running & config partition [default]"
|
---|
95 | echo " -m startup = copy config files to config partition"
|
---|
96 | echo " -m testing = do not copy config files"
|
---|
97 | echo " -m running = copy config files to running partition"
|
---|
98 | echo " -m hack = copy running files to config partition"
|
---|
99 | ) 1>&2
|
---|
100 | exit 2
|
---|
101 | }
|
---|
102 |
|
---|
103 | # Argument parsing using getopts
|
---|
104 | USE_API=1 # Whether or not to use the webinterface
|
---|
105 | OPT_MOUNT=1
|
---|
106 | OPT_RUNNING=1
|
---|
107 | OPT_STARTUP=1
|
---|
108 | OPT_HACK=0 # Hack for people without configuration managment and testing
|
---|
109 | OPT_BATCH=0
|
---|
110 | OPT_POSTCMD=false
|
---|
111 |
|
---|
112 | parse_options() {
|
---|
113 | while getopts "bc:nm:dp" OPT; do
|
---|
114 | case "$OPT" in
|
---|
115 | b) OPT_BATCH=1;;
|
---|
116 | c) CONFIG="${OPTARG}";;
|
---|
117 | d) OPT_POSTCMD=false;;
|
---|
118 | n) OPT_MOUNT=0;;
|
---|
119 | m) case "$OPTARG" in
|
---|
120 | all) true;;
|
---|
121 | live) OPT_STARTUP=0;;
|
---|
122 | startup) OPT_RUNNING=0;;
|
---|
123 | testing) OPT_RUNNING=0; OPT_STARTUP=0; OPT_MOUNT=0;;
|
---|
124 | hack) OPT_RUNNING=0; OPT_STARTUP=0; OPT_HACK=1; USE_API=0;;
|
---|
125 | *) usage;;
|
---|
126 | esac;;
|
---|
127 | h) usage;;
|
---|
128 | p) OPT_POSTCMD=true;;
|
---|
129 | \?) usage;;
|
---|
130 | esac
|
---|
131 | done
|
---|
132 | # Allow to override automatic mounting, in case of external mount 'managment'
|
---|
133 | if [ "$1" = "-n" ]; then
|
---|
134 | OPT_MOUNT=0
|
---|
135 | fi
|
---|
136 |
|
---|
137 | if [ "${OPT_RUNNING}" -eq 1 ]; then
|
---|
138 | echo "# INFO: Storing new config files in running configuration"
|
---|
139 | fi
|
---|
140 |
|
---|
141 | if [ "${OPT_STARTUP}" -eq 1 ]; then
|
---|
142 | echo "# INFO: Storing new config files in startup configuration"
|
---|
143 | fi
|
---|
144 |
|
---|
145 | if [ "${OPT_HACK}" -eq 1 ]; then
|
---|
146 | echo "# WARN: Copy running configuration to startup configuration"
|
---|
147 | echo "# WARN: Please do mind to document/mention this changes somewhere"
|
---|
148 | fi
|
---|
149 |
|
---|
150 | if /bin/df / | grep -q "^/dev/md[0-9]"; then
|
---|
151 | OPT_MOUNT=0
|
---|
152 | echo "# WARN: Mount operations disabled as we are running in a md(4) image"
|
---|
153 | fi
|
---|
154 |
|
---|
155 | # New line before the real work gets started
|
---|
156 | echo ""
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 | # test validity of input
|
---|
163 | config_validator() {
|
---|
164 | INPUT="$1"
|
---|
165 | `grep -q "^${INPUT}\$" ${TMPDIR}/node_list.txt`
|
---|
166 | if [ $? -eq 0 ]; then
|
---|
167 | return 0
|
---|
168 | else
|
---|
169 | echo "WARNING: Input '${INPUT}' is not valid, some hints..."
|
---|
170 | grep -i "${INPUT}" ${TMPDIR}/node_list.txt
|
---|
171 | return 1
|
---|
172 | fi
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 | select_node() {
|
---|
178 | # List of all available nodes
|
---|
179 | fetch -q -o ${TMPDIR}/node_list.txt ${BASEURL} || exit 1
|
---|
180 |
|
---|
181 | if [ ${OPT_BATCH} -eq 1 ]; then
|
---|
182 | config_validator "${CONFIG}"
|
---|
183 | if [ $? -eq 1 ]; then
|
---|
184 | echo "ERROR: Please provide valid config" 1>&2
|
---|
185 | exit 1
|
---|
186 | fi
|
---|
187 | else
|
---|
188 | # Provide Nodelist and feedback
|
---|
189 | cat ${TMPDIR}/node_list.txt | column
|
---|
190 | echo ' THIS script adds the config from GENESIS to this operating system'
|
---|
191 | echo ' make sure you know what you are doing, if not press control-C'
|
---|
192 | echo ' ENTER CONFIG NAME ......(and press enter)'
|
---|
193 |
|
---|
194 | # Have the user to select the right node
|
---|
195 | INVALID_CONFIG=1
|
---|
196 | while [ ${INVALID_CONFIG} -eq 1 ]; do
|
---|
197 | # Ask for node name, play around with prev option
|
---|
198 | echo -n "Name [${CONFIG}]: "
|
---|
199 | read INPUT
|
---|
200 | if [ -z "${INPUT}" ]; then
|
---|
201 | INPUT=${CONFIG}
|
---|
202 | else
|
---|
203 | CONFIG=${INPUT}
|
---|
204 | fi
|
---|
205 |
|
---|
206 | config_validator "${INPUT}"
|
---|
207 | if [ $? -eq 0 ]; then
|
---|
208 | INVALID_CONFIG=0
|
---|
209 | fi
|
---|
210 | done
|
---|
211 | fi
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 |
|
---|
216 |
|
---|
217 | # Copy file, saving some bits if no change needed
|
---|
218 | copy_file() {
|
---|
219 | SOURCE=$1
|
---|
220 | TARGET=$2
|
---|
221 | diff -I '^FreeBSD ' -I '^# Generated at ' ${TARGET} ${SOURCE} 2>/dev/null
|
---|
222 | if [ $? -ne 0 ]; then
|
---|
223 | mkdir -p `dirname ${TARGET}` || exit 1
|
---|
224 | cp ${SOURCE} ${TARGET} || exit 1
|
---|
225 | return $?
|
---|
226 | fi
|
---|
227 | return 1
|
---|
228 | }
|
---|
229 |
|
---|
230 | # Main function
|
---|
231 | main() {
|
---|
232 | TMPDIR=`mktemp -d -t $(basename $0)`
|
---|
233 | # Clear out tempdir when done
|
---|
234 | if [ ${OPT_MOUNT} -eq 1 ]; then
|
---|
235 | trap "rm -Rf ${TMPDIR}; umount /cfg; mount -ro noatime /; exit" 0 1 2 3 15
|
---|
236 | else
|
---|
237 | trap "rm -Rf ${TMPDIR}; exit" 0 1 2 3 15
|
---|
238 |
|
---|
239 | fi
|
---|
240 |
|
---|
241 | # Mount if requested
|
---|
242 | if [ ${OPT_MOUNT} -eq 1 ]; then
|
---|
243 | mount -uwo noatime /
|
---|
244 | mount /cfg
|
---|
245 | fi
|
---|
246 |
|
---|
247 | # Select node from web-interface
|
---|
248 | if [ ${USE_API} -eq 1 ]; then
|
---|
249 | select_node
|
---|
250 | fi
|
---|
251 |
|
---|
252 | # Worker, place all files in required directory
|
---|
253 | for FILE in ${FILES}; do
|
---|
254 | if [ ${USE_API} -eq 1 ]; then
|
---|
255 | # Fetch needed file
|
---|
256 | FRESH_LOC=${TMPDIR}/${FILE}
|
---|
257 | fetch -q -o ${FRESH_LOC} ${BASEURL}/${CONFIG}/${FILE} || exit 1
|
---|
258 | fi
|
---|
259 |
|
---|
260 | # Needed file details, like locations and hints
|
---|
261 | file_details ${FILE}
|
---|
262 |
|
---|
263 | echo "# INFO: Working on file: '${FILE}'"
|
---|
264 | # Copy file boot location
|
---|
265 | if [ ${OPT_STARTUP} -eq 1 ]; then
|
---|
266 | copy_file ${FRESH_LOC} ${STARTUP_LOC}
|
---|
267 | fi
|
---|
268 |
|
---|
269 | # Copy file running location
|
---|
270 | if [ ${OPT_RUNNING} -eq 1 ]; then
|
---|
271 | copy_file ${FRESH_LOC} ${RUNNING_LOC}
|
---|
272 | if [ $? -eq 0 ]; then
|
---|
273 | echo "# INFO: '${FILE}' changed"
|
---|
274 | if [ -n "${POST_CMD}" ]; then
|
---|
275 | if $OPT_POSTCMD; then
|
---|
276 | echo "## Running post_cmd: $POST_CMD"
|
---|
277 | $POST_CMD
|
---|
278 | else
|
---|
279 | echo "## To activate run the post_cmd: $POST_CMD"
|
---|
280 | fi
|
---|
281 | fi
|
---|
282 | if [ -n "${FILE_HINT}" ]; then
|
---|
283 | echo "# INFO: ${FILE_HINT}"
|
---|
284 | echo ""
|
---|
285 | fi
|
---|
286 | fi
|
---|
287 | fi
|
---|
288 |
|
---|
289 | # Direct copy
|
---|
290 | if [ ${OPT_HACK} -eq 1 ]; then
|
---|
291 | # No checking, just dumb try to copy mode
|
---|
292 | cp -v ${RUNNING_LOC} ${STARTUP_LOC}
|
---|
293 | fi
|
---|
294 | done
|
---|
295 |
|
---|
296 | exit 0
|
---|
297 | }
|
---|
298 |
|
---|
299 | parse_options $*
|
---|
300 | main
|
---|