source: hybrid/branches/releng-11/nanobsd/files/tools/wl-config@ 13699

Last change on this file since 13699 was 13697, checked in by rick, 8 years ago

Typo

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