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

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

Checks version and diff details - vrsion numer bfix

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id Rev
File size: 9.3 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# 1.04 Add auto read-only detection. (dirkx)
15# 1.05 Cope with WHOST containing a port number. (dirkx).
16# 1.06 Better diff (dirkx)
17# 1.07 Check versions of OS and Script
18#
19# If there is a global system configuration file, suck it in.
20#
21
22PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
23TMPDIR=${TMPDIR:-/tmp}
24TMPPREFIX=${TMPDIR}/wl-tmp-$$
25WHOST=${WHOST:-rambo.wleiden.net}
26HTTP_PROXY_DEFAULT=${HTTP_PROXY:-http://proxy.wleiden.net:3128}
27HTTP_USER_AGENT=${HTTP_USER_AGENT:-curl.faked.fetch/0.0}
28VERSION=1.07
29QUIET=${QUIET:-}
30PRETEND=no
31FORCE=no
32CMD=do_move
33DLV=no
34
35# Genesis master location.
36link=${GENESIS:-http://${WHOST}/cgi-bin/g_list.pl/}
37list=filelist
38
39# Location for private config
40lcd=${LCDIR:-/lcd}
41dir=${lcd}
42
43FETCH=${FETCH:-/usr/bin/fetch}
44test -e ${FETCH} || FETCH="curl"
45
46if echo ${FETCH} | grep -q curl; then
47 FETCH="${FETCH} --silent"
48else
49 FETCH="${FETCH} -q"
50fi
51
52usage() {
53 echo Usage $0 [-q] [-p proxy] [-d] [-D] [-n] [hostname]
54 echo "-p <proxy> Set a proxy"
55 echo "-P Use ${HTTP_PROXY_DEFAULT} as a proxy"
56 echo "-D Use DHCP to get an address"
57 echo "-n Show what would happen - but do not do it"
58 echo "-d Diff met hudiige config (maar doe niets)"
59 echo "-q Suppress all output and user interaction"
60 echo "-F Force disk to write"
61 echo "-i Ignore all safety checks checks"
62 exit 1
63}
64
65for i in $*
66do
67 case "$i"
68 in
69 -D) connset || exit 1
70 ;;
71 -q)
72 QUIET=yes
73 ;;
74 -i)
75 DLV=
76 ;;
77 -d)
78 CMD=do_diff
79 dir=${TMPDIR}
80 ;;
81 -p)
82 shift;
83 HTTP_PROXY=$1
84 export HTTP_PROXY
85 ;;
86 -p)
87 HTTP_PROXY=HTTP_PROXY_DEFAULT
88 export HTTP_PROXY
89 ;;
90 -n)
91 PRETEND=yes
92 ;;
93 -F)
94 FORCE=yes
95 ;;
96 *)
97 test $# -eq 1 || usage
98 nodename=$i
99 break;
100 ;;
101 esac
102 shift
103done
104
105HOST=`echo ${WHOST} | sed -e 's/:.*//'`
106set `echo $VERSION | sed -e 's/\./ /'`
107VERSION_MAJOR=$1
108VERSION_MINOR=$2
109VERSION_OTHER=$3
110
111# connection test function
112connset()
113{
114 if [ `ps ax | grep -c dhclient` != "1" ] ; then
115 killall dhclient
116 fi
117
118 echo "Enter an IP address of a nearby Nameserver or use:"
119 echo " 1 to use COPE \(on Wleiden\)"
120 echo " 2 to use XS4All \(on the internet\)"
121 echo " 3 to use the LCP server \(on the internal LCP networ\)"
122
123 echo -n "IP address or 1/2/3: "
124 read dns_list
125
126 case $dns_list in
127 1)
128 resolver="172.17.8.1"
129 ;;
130 2)
131 resolver="194.109.9.99"
132 ;;
133 3)
134 resolver="10.0.0.1"
135 ;;
136 *)
137 resolver=$dns_list
138 ;;
139 esac
140
141 cp /etc/resolv.conf /etc/resolv.bak || exit 1
142 echo "nameserver ${resolver}" > /etc/resolv.conf
143
144
145 for nic in `ifconfig -l`
146 do
147 case ${nic} in
148 lo0 | wi*)
149 ;;
150 *)
151 if ping -qnoc ${HOST}; then
152 echo Connection on interface ${nic} to ${HOST} ok.
153 else
154 killall dhclient
155 echo Trying to get a DHCP lease on ${nic}
156 dhclient -1 ${nic}
157 fi
158 ;;
159 esac
160 done
161}
162
163log()
164{
165 if [ -z ${QUIET} ]; then
166 echo "$*"
167 fi
168}
169lognlr()
170{
171 if [ -z ${QUIET} ]; then
172 echo -n "$*"
173 fi
174}
175
176cleanse()
177{
178 rm -f ${TMPPREFIX}.?
179}
180
181# Normal exit; but make sure
182# we also clean up any tmp files
183#
184cleanexit()
185{
186 E=1
187 if [ $# -gt 0 ]; then
188 E=$1
189 fi
190 cleanse
191 log Exit
192 exit $E
193 # Trap any weird exit codes.
194 exit 1
195}
196
197safefetch()
198{
199 url=$1
200 file=$2
201 ${FETCH} -o - ${url} > ${TMPPREFIX}.x \
202 || cleanexit 1
203
204 # Genesis can provide us with corrupted/empty files
205 # with a 200 OK - so insist that they are at least
206 # a few lines long.
207 #
208 set `wc -l ${TMPPREFIX}.x`
209 if [ $1 -lt 2 ]; then
210 echo File ${link}${nodename} is less than 2 lines long.
211 echo Assuming a problem with Genesis.
212 cleanexit 2
213 fi
214
215 cp ${TMPPREFIX}.x ${file} \
216 || cleanexit 1
217 rm -f ${TMPPREFIX}.x
218
219 return 0
220}
221
222getvalidnodenames()
223{
224 log Fetching list of nodes from ${link}
225 safefetch ${link} ${nlist} || cleanexit 1
226}
227
228getvalidnodename()
229{
230while ! grep -q "^${nodename}\$" ${nlist}
231do
232 echo Nodes:
233 if [ -x /usr/bin/column ]; then
234 column ${nlist}
235 else
236 cat ${nlist}
237 fi
238 echo
239 echo -n enter nodename \[default: ${default}\]:
240 if [ -z ${QUIET} ]; then
241 read nodename
242 else
243 nodename=${default}
244 fi
245
246 if [ "x${nodename}" = "x" ]; then
247 nodename=${default}
248 fi
249done
250
251echo Node Selected: ${nodename}
252}
253
254
255do_diff() {
256 if test -e $lcd/$1; then
257 diff -uwbB $lcd/$1 $dir/$1.new
258 else
259 echo Warning: $lcd/$1 does not yet exist - no DIFF
260 fi
261}
262
263do_move() {
264 if [ -e $dir/$1 ]; then
265 mv $dir/$1 $dir/$1.bak || cleanexit 1
266 fi
267 cp $dir/$1.new $dir/$1 || cleanexit 1
268 rm $dir/$1.new || cleanexit 1
269}
270
271linkin() {
272 symdir=$1
273 file=$2
274
275 if [ ${PRETEND} = 'yes' ]; then
276 echo "** $CMD $*"
277 else
278 $CMD $file || exit 1
279 fi
280
281 test -e $symdir/$file || (
282 echo WARNING: Symlink $symdir/$file not in place.
283 echo use: ln -s $dir/$file $symdir/$file
284 echo to fix if appropriate.
285 )
286}
287
288log Config Node -- Version: $VERSION '$Rev: 5004 $'
289
290# Make sure we clean up our mess when needed.
291trap "rm -f ${TMPPREFIX}.?; echo Failed; exit 1;" 2 3
292
293if [ ${PRETEND} != 'yes' -a ${CMD} != 'do_diff' ]; then
294 if mount | grep "on / " | grep -q read-only; then
295 if [ ${FORCE} = "yes" ]; then
296 echo Forcing read-only disk into rw.
297 fsck / || exit 2
298 mount -o noatime -u -w / || exit 2 || exit 1
299 trap "mount -u -r /; rm -f ${TMPPREFIX}.?; echo Failed; exit 1;" 2 3
300 FORCE=rw
301 else
302 echo ERROR - disk / is mounted read only. Aborting.
303 exit 1
304 fi
305 fi
306fi
307
308export HTTP_USER_AGENT
309export TMPDIR
310
311
312#check config dir
313#
314if [ ! -d ${dir} ]; then
315 mkdir -p ${dir} || cleanexit 1
316fi
317
318if [ -z ${HTTP_PROXY} ]; then
319 (
320 log Checking DNS for ${HOST}
321 host ${HOST} > /dev/null || exit 1
322 log Checking if ${HOST} can be reached
323 ping -qnoc 1 ${HOST} > /dev/null || exit 1
324 log Connection: Ok
325 exit 0
326 ) || connset
327else
328 log Connection not checked because there is an http proxy configured: ${HTTP_PROXY}.
329fi
330
331if [ -r ${lcd}/myname ]; then
332 default=`cat ${lcd}/myname`
333else
334 default=`hostname -s`
335 test -z $default && default=none
336fi
337
338if [ ! -z ${QUIET} ]; then
339 if [ -z ${nodename} ]; then
340 nodename=${default}
341 fi
342fi
343
344
345nlist=${TMPPREFIX}.l
346
347test -z $default && default=$nodename
348
349getvalidnodenames || exit 1
350
351test -z $nodename && getvalidnodename
352
353while test -z $nodename || ! grep -q ${nodename} ${nlist}
354do
355 echo
356 echo Error: Node named \"$nodename\" not known.
357 if [ -z ${QUIET} ]; then
358 exit 1
359 fi
360 echo Please select one from the list.
361 echo
362 getvalidnodename
363done
364
365lognlr "Checking release and OS versions: "
366
367safefetch ${link}${nodename}/info $dir/info.last \
368 || cleanexit 1
369
370OS=`uname -s`
371REL=`uname -r`
372set -- `head -1 $dir/info.last`
373
374# FreeBSD 5.0-RELEASE 1 YES
375if [ $# != 4 ]; then
376 echo Info verification failed.
377 cleanexit 1
378fi
379if [ x$4 != 'xYES' ]; then
380 echo Genesis marked as disabled for this machine.
381 test -z ${DLV} || cleanexit 1
382fi
383if [ 0$3 -ne $VERSION_MAJOR ]; then
384 echo This script is version $VERSION, genesis info is for version $3.xx
385 test -z ${DLV} || cleanexit 1
386fi
387if [ x$1 != x$OS ]; then
388 echo Operating system mismatch; this machine: $OS, but config is for $1
389 test -z ${DLV} || cleanexit 1
390fi
391if [ x$2 != x$REL ]; then
392 echo This machine runs $REL, but the configuration is for $2
393 test -z ${DLV} || cleanexit 2
394fi
395
396log Ok
397
398log Fetching file list from $link for $nodename
399
400safefetch ${link}${nodename} ${dir}/${list}
401
402lognlr "Fetching:"
403for i in `cat ${dir}/${list}`
404do
405 lognlr " ${i}"
406# $FETCH -o - ${link}${nodename}/${i} > ${dir}/${i}.new || cleanexit 1
407 safefetch ${link}${nodename}/${i} ${dir}/${i}.new
408done
409log .
410
411for i in `cat ${dir}/${list}`
412do
413 case ${i} in
414 linux.sh | config | txtconfig )
415 # log obsolete file: ${i} - skipped
416 ;;
417 resolv.conf | rc.node.local | rc.local)
418 linkin /etc ${i}
419 ;;
420 snmpd.local.conf)
421 linkin /usr/local/share/snmp ${i}
422 ;;
423 named.conf)
424 linkin /etc/namedb ${i}
425 ;;
426 dhcpd.conf)
427 linkin /usr/local/etc ${i}
428 ;;
429 zebra.conf | ospfd.conf)
430 linkin /usr/local/etc/zebra ${i}
431 ;;
432 authorized_keys)
433 linkin /root/.ssh ${i}
434 ;;
435 ssh_known_hosts)
436 linkin /etc/ssh ${i}
437 ;;
438 daemons.sh)
439 linkin /wl ${i}
440 ;;
441 *)
442 echo Script cannot cope with ${i} - ignoring..
443 ;;
444 esac
445done
446
447if [ -e /etc/rc.local ]; then
448 # See if we are in rc.local
449 if grep -q /config-node.sh /etc/rc.local; then
450
451 echo As this node now has real configs - do enter a root password
452 echo
453 passwd \
454 || cleanexit 1
455
456 echo Removing /etc/rc.local
457 rm -f /etc/rc.local
458
459 echo Will drop write perms on the next reboot.
460 fi
461fi
462
463# Record our name.
464echo ${nodename} > ${dir}/myname
465
466# Rebuild reverse lookups
467if test -e /etc/rc.node.local; then
468 H=`cat /etc/rc.node.local | grep hostname | sed -e s/hostname=// | sed -e s/[\"\']//g`
469 hostname $H
470else
471 echo Warning: rc.node.local missing.
472fi
473
474if [ -r /etc/namedb/make-localhost -a ${CMD} != 'do_diff' ]; then
475 (
476 cd /etc/namedb || exit 1
477 sh /etc/namedb/make-localhost || exit 1
478 ) || exit 1
479fi
480
481cleanse || exit 1
482
483if [ -e /etc/rc.empty.conf ] ; then
484 rm /etc/rc.empty.conf || exit 1
485 echo removed /etc/rc.empty.conf - and rebooting in 30 seconds \(or press ctrl-C to abort\)
486 read -t 30 DUMMY
487 reboot
488fi
489
490test ${FORCE} = 'rw' && mount -u -r /
491exit 0
Note: See TracBrowser for help on using the repository browser.