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

Last change on this file since 14325 was 14172, checked in by rick, 7 years ago

Add override work-around for BASEURL if others fail

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