source: hybrid/branches/releng-9.0/nanobsd/files/tools/wl-config@ 10569

Last change on this file since 10569 was 10569, checked in by rick, 13 years ago

Hybrid Nodes has a funky motd, make sure to fetch it by default.

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