source: genesis/tools/config-node.sh@ 4985

Last change on this file since 4985 was 4985, checked in by dirkx, 19 years ago

New version of config-node

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.2 KB
Line 
1#!/bin/sh
2#
3# (c) Copyright 2002, 2003, 2005 Stichting Wireless Leiden, all
4# rights reserved. More information can be found on
5# http://wwww.wirelessleiden.nl and the license is at:
6# http://wleiden.webweaving.org:8080/svn/node-config/LICENSE
7#
8# 1.00
9# ?? Marten Vijn 24-03-03
10# ?? new version 14-11-2003
11# 1.03 proxy cleanup, detect faulty files, generalize
12# file list, check node name to be valid, '-n' mode.
13# make moving of final files a bit safer. (dirkx)
14#
15# If there is a global system configuration file, suck it in.
16#
17
18PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
19TMPDIR=${TMPDIR:-/tmp}
20TMPPREFIX=${TMPDIR}/wl-tmp-$$
21WHOST=${WHOST:-rambo.wleiden.net}
22HTTP_PROXY_DEFAULT=${HTTP_PROXY:-http://proxy.wleiden.net:3128}
23HTTP_USER_AGENT=${HTTP_USER_AGENT:-curl.faked.fetch/0.0}
24VERSION=1.03
25QUIET=${QUIET:-}
26PRETEND=no
27
28FETCH=${FETCH:-/usr/bin/fetch}
29test -e ${FETCH} || FETCH="curl"
30
31if echo ${FETCH} | grep -q curl; then
32 FETCH="${FETCH} --silent"
33else
34 FETCH="${FETCH} -q"
35fi
36
37usage() {
38 echo Usage $0 [-q] [-p proxy] [-d] [-D] [-n] [hostname]
39 echo "-p <proxy> Set a proxy"
40 echo "-P Use ${HTTP_PROXY_DEFAULT} as a proxy"
41 echo "-d Use DHCP to get an address"
42 echo "-n Show what would happen - but do not do it"
43 echo "-q Suppress all output and user interaction"
44 exit 1
45}
46
47for i in $*
48do
49 case "$i"
50 in
51 -d) connset || exit 1
52 ;;
53 -q)
54 QUIET=yes
55 ;;
56 -p)
57 shift;
58 HTTP_PROXY=$1
59 export HTTP_PROXY
60 ;;
61 -p)
62 HTTP_PROXY=HTTP_PROXY_DEFAULT
63 export HTTP_PROXY
64 ;;
65 -n)
66 PRETEND=yes
67 ;;
68 *)
69 test $# -eq 1 || usage
70 nodename=$i
71 break;
72 ;;
73 esac
74 shift
75done
76
77export HTTP_USER_AGENT
78export TMPDIR
79
80# Genesis master location.
81link=${GENESIS:-http://${WHOST}/cgi-bin/g_list.pl/}
82list=filelist
83
84# Location for private config
85lcd=${LCDIR:-/lcd}
86
87# Make sure we clean up our mess when needed.
88trap "rm -f ${TMPPREFIX}.?; echo Failed; exit 1;" 2 3
89
90# connection test function
91connset()
92{
93 if [ `ps ax | grep -c dhclient` != "1" ] ; then
94 killall dhclient
95 fi
96
97 echo "Enter an IP address of a nearby Nameserver or use:"
98 echo " 1 to use COPE \(on Wleiden\)"
99 echo " 2 to use XS4All \(on the internet\)"
100 echo " 3 to use the LCP server \(on the internal LCP networ\)"
101
102 echo -n "IP address or 1/2/3: "
103 read dns_list
104
105 case $dns_list in
106 1)
107 resolver="172.17.8.1"
108 ;;
109 2)
110 resolver="194.109.9.99"
111 ;;
112 3)
113 resolver="10.0.0.1"
114 ;;
115 *)
116 resolver=$dns_list
117 ;;
118 esac
119
120 cp /etc/resolv.conf /etc/resolv.bak || exit 1
121 echo "nameserver ${resolver}" > /etc/resolv.conf
122
123 for nic in `ifconfig -l`
124 do
125 case ${nic} in
126 lo0 | wi*)
127 ;;
128 *)
129 if ping -qnoc ${WHOST}; then
130 echo Connection on interface ${nic} ok
131 else
132 killall dhclient
133 echo Trying to get a DHCP lease on ${nic}
134 dhclient -1 ${nic}
135 fi
136 ;;
137 esac
138 done
139}
140
141log()
142{
143 if [ -z ${QUIET} ]; then
144 echo "$*"
145 fi
146}
147lognlr()
148{
149 if [ -z ${QUIET} ]; then
150 echo -n "$*"
151 fi
152}
153
154cleanse()
155{
156 rm -f ${TMPPREFIX}.?
157}
158
159# Normal exit; but make sure
160# we also clean up any tmp files
161#
162cleanexit()
163{
164 E=1
165 if [ $# -gt 0 ]; then
166 E=$1
167 fi
168 cleanse
169 log Exit
170 exit $E
171 # Trap any weird exit codes.
172 exit 1
173}
174
175safefetch()
176{
177 url=$1
178 file=$2
179 ${FETCH} -o - ${link}${nodename} > ${TMPPREFIX}.x \
180 || cleanexit 1
181
182 # Genesis can provide us with corrupted/empty files
183 # with a 200 OK - so insist that they are at least
184 # a few lines long.
185 #
186 set `wc -l ${TMPPREFIX}.x`
187 if [ $1 -lt 2 ]; then
188 echo File ${link}${nodename} is less than 2 lines long.
189 echo Assuming a problem with Genesis.
190 cleanexit 2
191 fi
192
193 cp ${TMPPREFIX}.x ${dir}/${list} \
194 || cleanexit 1
195 rm -f ${TMPPREFIX}.x
196
197 return 0
198}
199
200getvalidnodenames()
201{
202 log Fetching list of nodes from ${link}
203 ${FETCH} -o - ${link} > ${nlist} || cleanexit 1
204}
205
206getvalidnodename()
207{
208while ! grep -q "^${nodename}\$" ${nlist}
209do
210 echo Nodes:
211 if [ -x /usr/bin/column ]; then
212 column ${nlist}
213 else
214 cat ${nlist}
215 fi
216 echo
217 echo -n enter nodename \[default: ${default}\]:
218 if [ -z ${QUIET} ]; then
219 read nodename
220 else
221 nodename=${default}
222 fi
223
224 if [ "x${nodename}" = "x" ]; then
225 nodename=${default}
226 fi
227done
228
229echo Node Selected: ${nodename}
230}
231
232
233do_diff() {
234 diff -uwbB $1 $1.new
235}
236
237do_move() {
238if [ -e $1 ]; then
239 mv $1 $1.bak || cleanexit 1
240fi
241cp $1.new $1 || cleanexit 1
242rm $1.new || cleanexit 1
243}
244
245linkin() {
246 symdir=$1
247 file=$2
248
249 if [ ${PRETEND} = 'yes' ]; then
250 echo "** $CMD $*"
251 else
252 $CMD $dir/$file || exit 1
253 fi
254
255 test -e $symdir/$file || echo WARNING: Symlink $symdir/$file not in place.
256}
257
258dir=${lcd}
259
260echo Config Node -- Version: $VERSION
261
262#check config dir
263#
264if [ ! -d ${dir} ]; then
265 mkdir -p ${dir} || cleanexit 1
266fi
267
268if [ -z ${HTTP_PROXY} ]; then
269 (
270 log Checking DNS for ${WHOST}
271 host ${WHOST} > /dev/null || exit 1
272 log Checking if ${WHOST} can be reached
273 ping -qnoc 1 ${WHOST} > /dev/null || exit 1
274 log Connection OK
275 exit 0
276 ) || connset
277fi
278
279CMD=do_move
280if [ "x$1" = "x-d" ]; then
281 CMD=do_diff
282 dir=${TMPDIR}
283 shift
284fi
285
286if [ -r ${lcd}/myname ]; then
287 default=`cat ${lcd}/myname`
288else
289 default=`hostname -s`
290 test -z $default && default=none
291fi
292
293nlist=${TMPPREFIX}.l
294
295test -z $default && default=$nodename
296
297getvalidnodenames || exit 1
298
299test -z $nodename && getvalidnodename
300
301while test -z $nodename || ! grep -q ${nodename} ${nlist}
302do
303 echo
304 echo Error: Node named \"$nodename\" not known.
305 if [ -z ${QUIET} ]; then
306 exit 1
307 fi
308 echo Please select one from the list.
309 echo
310 getvalidnodename
311done
312
313log Fetching file list from $link for $nodename
314
315safefetch ${link}${nodename} ${dir}/${list}
316
317lognlr "Fetching:"
318for i in `cat ${dir}/${list}`
319do
320 lognlr " ${i}"
321 $FETCH -o - ${link}${nodename}/${i} > ${dir}/${i}.new \
322 || cleanexit 1
323done
324log .
325
326for i in `cat ${dir}/${list}`
327do
328 case ${i} in
329 linux.sh | config | txtconfig )
330 # log obsolete file: ${i} - skipped
331 ;;
332 resolv.conf | rc.node.local | rc.local)
333 linkin /etc/ ${i}
334 ;;
335 snmpd.local.conf)
336 linkin /usr/local/share/snmp/ ${i}
337 ;;
338 named.conf)
339 linkin/etc/namedb/ ${i}
340 ;;
341 dhcpd.conf)
342 linkin /usr/local/etc/ ${i}
343 ;;
344 zebra.conf | ospfd.conf)
345 linkin /usr/local/etc/zebra/ ${i}
346 ;;
347 authorized_keys)
348 linkin /root/.ssh ${i}
349 ;;
350 ssh_known_hosts)
351 linkin /etc/ssh ${i}
352 ;;
353 daemons.sh)
354 linkin /wl ${i}
355 ;;
356 *)
357 echo Script cannot cope with ${i} - ignoring..
358 ;;
359 esac
360done
361
362if [ -e /etc/rc.local ]; then
363 # See if we are in rc.local
364 if grep -q /config-node.sh /etc/rc.local; then
365
366 echo As this node now has real configs - do enter a root password
367 echo
368 passwd \
369 || cleanexit 1
370
371 echo Removing /etc/rc.local
372 rm -f /etc/rc.local
373
374 echo Will drop write perms on the next reboot.
375 fi
376fi
377
378# Record our name.
379echo ${nodename} > ${dir}/myname
380
381# Rebuild reverse lookups
382if test -e /etc/rc.node.local; then
383 H=`cat /etc/rc.node.local | grep hostname | sed -e s/hostname=// | sed -e s/[\"\']//g`
384 hostname $H
385else
386 echo Warning: rc.node.local missing.
387fi
388
389if [ -r /etc/namedb/make-localhost ]; then
390 (
391 cd /etc/namedb || exit 1
392 sh /etc/namedb/make-localhost || exit 1
393 ) || exit 1
394fi
395
396cleanse || exit 1
397
398if [ -e /etc/rc.empty.conf ] ; then
399 rm /etc/rc.empty.conf || exit 1
400 echo removed /etc/rc.empty.conf - and rebooting in 30 seconds \(or press ctrl-C to abort\)
401 read -t 30 DUMMY
402 reboot
403fi
404
405exit 0
Note: See TracBrowser for help on using the repository browser.