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
RevLine 
[4049]1#!/bin/sh
[4985]2#
[4085]3# (c) Copyright 2002, 2003, 2005 Stichting Wireless Leiden, all
[4049]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#
[4985]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#
[4049]15# If there is a global system configuration file, suck it in.
16#
[4050]17
[4049]18PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
19TMPDIR=${TMPDIR:-/tmp}
20TMPPREFIX=${TMPDIR}/wl-tmp-$$
[4050]21WHOST=${WHOST:-rambo.wleiden.net}
[4985]22HTTP_PROXY_DEFAULT=${HTTP_PROXY:-http://proxy.wleiden.net:3128}
[4049]23HTTP_USER_AGENT=${HTTP_USER_AGENT:-curl.faked.fetch/0.0}
[4985]24VERSION=1.03
[4052]25QUIET=${QUIET:-}
[4985]26PRETEND=no
[4051]27
[4985]28FETCH=${FETCH:-/usr/bin/fetch}
29test -e ${FETCH} || FETCH="curl"
[4051]30
[4985]31if echo ${FETCH} | grep -q curl; then
32 FETCH="${FETCH} --silent"
[4050]33else
[4985]34 FETCH="${FETCH} -q"
[4050]35fi
[4049]36
[4985]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}
[4049]46
[4985]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
[4049]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{
[4985]93 if [ `ps ax | grep -c dhclient` != "1" ] ; then
94 killall dhclient
95 fi
[4085]96
[4985]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\)"
[4049]101
[4985]102 echo -n "IP address or 1/2/3: "
103 read dns_list
[4049]104
[4985]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
[4049]119
[4985]120 cp /etc/resolv.conf /etc/resolv.bak || exit 1
121 echo "nameserver ${resolver}" > /etc/resolv.conf
[4049]122
[4985]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
[4049]139}
140
141log()
142{
[4985]143 if [ -z ${QUIET} ]; then
144 echo "$*"
145 fi
[4049]146}
[4985]147lognlr()
148{
149 if [ -z ${QUIET} ]; then
150 echo -n "$*"
151 fi
152}
[4049]153
154cleanse()
155{
[4985]156 rm -f ${TMPPREFIX}.?
[4049]157}
158
159# Normal exit; but make sure
160# we also clean up any tmp files
161#
162cleanexit()
163{
[4985]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
[4049]173}
174
175safefetch()
176{
[4985]177 url=$1
178 file=$2
179 ${FETCH} -o - ${link}${nodename} > ${TMPPREFIX}.x \
180 || cleanexit 1
[4049]181
[4985]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
[4049]198}
199
[4985]200getvalidnodenames()
201{
202 log Fetching list of nodes from ${link}
203 ${FETCH} -o - ${link} > ${nlist} || cleanexit 1
204}
205
[4049]206getvalidnodename()
207{
208while ! grep -q "^${nodename}\$" ${nlist}
209do
[4085]210 echo Nodes:
[4985]211 if [ -x /usr/bin/column ]; then
212 column ${nlist}
213 else
214 cat ${nlist}
215 fi
[4085]216 echo
217 echo -n enter nodename \[default: ${default}\]:
218 if [ -z ${QUIET} ]; then
219 read nodename
220 else
221 nodename=${default}
222 fi
[4049]223
[4085]224 if [ "x${nodename}" = "x" ]; then
225 nodename=${default}
226 fi
[4049]227done
228
229echo Node Selected: ${nodename}
230}
231
232
233do_diff() {
[4985]234 diff -uwbB $1 $1.new
[4049]235}
236
237do_move() {
[4985]238if [ -e $1 ]; then
239 mv $1 $1.bak || cleanexit 1
[4054]240fi
[4985]241cp $1.new $1 || cleanexit 1
242rm $1.new || cleanexit 1
243}
[4049]244
[4985]245linkin() {
246 symdir=$1
247 file=$2
[4054]248
[4985]249 if [ ${PRETEND} = 'yes' ]; then
250 echo "** $CMD $*"
251 else
252 $CMD $dir/$file || exit 1
253 fi
[4049]254
[4985]255 test -e $symdir/$file || echo WARNING: Symlink $symdir/$file not in place.
[4049]256}
257
258dir=${lcd}
259
260echo Config Node -- Version: $VERSION
261
262#check config dir
263#
264if [ ! -d ${dir} ]; then
[4085]265 mkdir -p ${dir} || cleanexit 1
[4049]266fi
267
268if [ -z ${HTTP_PROXY} ]; then
[4085]269 (
[4985]270 log Checking DNS for ${WHOST}
[4085]271 host ${WHOST} > /dev/null || exit 1
[4985]272 log Checking if ${WHOST} can be reached
[4085]273 ping -qnoc 1 ${WHOST} > /dev/null || exit 1
[4985]274 log Connection OK
[4085]275 exit 0
276 ) || connset
[4049]277fi
278
279CMD=do_move
280if [ "x$1" = "x-d" ]; then
[4085]281 CMD=do_diff
282 dir=${TMPDIR}
283 shift
[4049]284fi
285
[4985]286if [ -r ${lcd}/myname ]; then
287 default=`cat ${lcd}/myname`
[4049]288else
[4085]289 default=`hostname -s`
290 test -z $default && default=none
[4049]291fi
292
293nlist=${TMPPREFIX}.l
294
295test -z $default && default=$nodename
296
[4985]297getvalidnodenames || exit 1
298
[4049]299test -z $nodename && getvalidnodename
300
[4985]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
[4049]313log Fetching file list from $link for $nodename
314
315safefetch ${link}${nodename} ${dir}/${list}
316
[4985]317lognlr "Fetching:"
[4049]318for i in `cat ${dir}/${list}`
319do
[4985]320 lognlr " ${i}"
321 $FETCH -o - ${link}${nodename}/${i} > ${dir}/${i}.new \
[4085]322 || cleanexit 1
[4049]323done
[4985]324log .
[4049]325
326for i in `cat ${dir}/${list}`
327do
[4985]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
[4049]360done
361
362if [ -e /etc/rc.local ]; then
[4085]363 # See if we are in rc.local
364 if grep -q /config-node.sh /etc/rc.local; then
[4049]365
[4085]366 echo As this node now has real configs - do enter a root password
367 echo
368 passwd \
369 || cleanexit 1
[4049]370
[4085]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
[4049]376fi
377
378# Record our name.
379echo ${nodename} > ${dir}/myname
380
381# Rebuild reverse lookups
[4985]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
[4049]389if [ -r /etc/namedb/make-localhost ]; then
[4085]390 (
391 cd /etc/namedb || exit 1
392 sh /etc/namedb/make-localhost || exit 1
393 ) || exit 1
[4049]394fi
395
[4985]396cleanse || exit 1
[4049]397
398if [ -e /etc/rc.empty.conf ] ; then
[4985]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
[4049]403fi
404
405exit 0
Note: See TracBrowser for help on using the repository browser.