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

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

Make sure to update the motd after file is downloaded to include the latest
version information.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
File size: 6.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=5
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 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="/etc/local/${FILE}"
47 FILE_HINT="service isc-dhcpd restart"
48 ;;
49 'dnsmasq.conf')
50 STARTUP_LOC="/cfg/local/${FILE}"
51 RUNNING_LOC="/etc/local/${FILE}"
52 FILE_HINT="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 FILE_HINT="service named restart"
63 ;;
64 'rc.conf.local')
65 STARTUP_LOC="/cfg/${FILE}"
66 RUNNING_LOC="/etc/${FILE}"
67 FILE_HINT="service netif restart"
68 ;;
69 'resolv.conf')
70 STARTUP_LOC="/cfg/${FILE}"
71 RUNNING_LOC="/etc/${FILE}"
72 ;;
73 'wleiden.yaml')
74 STARTUP_LOC="/cfg/local/${FILE}"
75 RUNNING_LOC="/etc/local/${FILE}"
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 if [ ${OPT_BATCH} -eq 1 ]; then
166 config_validator "${CONFIG}"
167 if [ $? -eq 1 ]; then
168 echo "ERROR: Please provide valid config" 1>&2
169 exit 1
170 fi
171 else
172 # Provide Nodelist and feedback
173 cat ${TMPDIR}/node_list.txt | column
174 echo ' THIS script adds the config from GENESIS to this operating system'
175 echo ' make sure you know what you are doing, if not press control-C'
176 echo ' ENTER CONFIG NAME ......(and press enter)'
177
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 '^FreeBSD ' -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 "${POST_CMD}" ]; then
259 echo "## Running post_cmd: $POST_CMD"
260 $POST_CMD
261 fi
262 if [ -n "${FILE_HINT}" ]; then
263 echo "# INFO: For instant activate: ${FILE_HINT}"
264 echo ""
265 fi
266 fi
267 fi
268
269 # Direct copy
270 if [ ${OPT_HACK} -eq 1 ]; then
271 # No checking, just dumb try to copy mode
272 cp -v ${RUNNING_LOC} ${STARTUP_LOC}
273 fi
274 done
275
276 exit 0
277}
278
279parse_options $*
280main
Note: See TracBrowser for help on using the repository browser.