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

Last change on this file since 6538 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
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)
[4990]14# 1.04 Add auto read-only detection. (dirkx)
15# 1.05 Cope with WHOST containing a port number. (dirkx).
[4997]16# 1.06 Better diff (dirkx)
[5001]17# 1.07 Check versions of OS and Script
[4985]18#
[4049]19# If there is a global system configuration file, suck it in.
20#
[4050]21
[4049]22PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
23TMPDIR=${TMPDIR:-/tmp}
24TMPPREFIX=${TMPDIR}/wl-tmp-$$
[4050]25WHOST=${WHOST:-rambo.wleiden.net}
[4985]26HTTP_PROXY_DEFAULT=${HTTP_PROXY:-http://proxy.wleiden.net:3128}
[4049]27HTTP_USER_AGENT=${HTTP_USER_AGENT:-curl.faked.fetch/0.0}
[5004]28VERSION=1.07
[4052]29QUIET=${QUIET:-}
[4985]30PRETEND=no
[4986]31FORCE=no
[4987]32CMD=do_move
[5001]33DLV=no
[4051]34
[4987]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
[4985]43FETCH=${FETCH:-/usr/bin/fetch}
44test -e ${FETCH} || FETCH="curl"
[4051]45
[4985]46if echo ${FETCH} | grep -q curl; then
47 FETCH="${FETCH} --silent"
[4050]48else
[4985]49 FETCH="${FETCH} -q"
[4050]50fi
[4049]51
[4985]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"
[4987]56 echo "-D Use DHCP to get an address"
[4985]57 echo "-n Show what would happen - but do not do it"
[4987]58 echo "-d Diff met hudiige config (maar doe niets)"
[4985]59 echo "-q Suppress all output and user interaction"
[4986]60 echo "-F Force disk to write"
[5001]61 echo "-i Ignore all safety checks checks"
[4985]62 exit 1
63}
[4049]64
[4985]65for i in $*
66do
67 case "$i"
68 in
[4987]69 -D) connset || exit 1
[4985]70 ;;
71 -q)
72 QUIET=yes
73 ;;
[5001]74 -i)
75 DLV=
76 ;;
[4987]77 -d)
[4988]78 CMD=do_diff
[4987]79 dir=${TMPDIR}
80 ;;
[4985]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 ;;
[4986]93 -F)
94 FORCE=yes
95 ;;
[4985]96 *)
97 test $# -eq 1 || usage
98 nodename=$i
99 break;
100 ;;
101 esac
102 shift
103done
[4049]104
[4990]105HOST=`echo ${WHOST} | sed -e 's/:.*//'`
[5001]106set `echo $VERSION | sed -e 's/\./ /'`
107VERSION_MAJOR=$1
108VERSION_MINOR=$2
109VERSION_OTHER=$3
[4986]110
[4049]111# connection test function
112connset()
113{
[4985]114 if [ `ps ax | grep -c dhclient` != "1" ] ; then
115 killall dhclient
116 fi
[4085]117
[4985]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\)"
[4049]122
[4985]123 echo -n "IP address or 1/2/3: "
124 read dns_list
[4049]125
[4985]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
[4049]140
[4985]141 cp /etc/resolv.conf /etc/resolv.bak || exit 1
142 echo "nameserver ${resolver}" > /etc/resolv.conf
[4049]143
[4990]144
[4985]145 for nic in `ifconfig -l`
146 do
147 case ${nic} in
148 lo0 | wi*)
149 ;;
150 *)
[4990]151 if ping -qnoc ${HOST}; then
152 echo Connection on interface ${nic} to ${HOST} ok.
[4985]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
[4049]161}
162
163log()
164{
[4985]165 if [ -z ${QUIET} ]; then
166 echo "$*"
167 fi
[4049]168}
[4985]169lognlr()
170{
171 if [ -z ${QUIET} ]; then
172 echo -n "$*"
173 fi
174}
[4049]175
176cleanse()
177{
[4985]178 rm -f ${TMPPREFIX}.?
[4049]179}
180
181# Normal exit; but make sure
182# we also clean up any tmp files
183#
184cleanexit()
185{
[4985]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
[4049]195}
196
197safefetch()
198{
[4985]199 url=$1
200 file=$2
[5003]201 ${FETCH} -o - ${url} > ${TMPPREFIX}.x \
[4985]202 || cleanexit 1
[4049]203
[4985]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
[5003]215 cp ${TMPPREFIX}.x ${file} \
[4985]216 || cleanexit 1
217 rm -f ${TMPPREFIX}.x
218
219 return 0
[4049]220}
221
[4985]222getvalidnodenames()
223{
224 log Fetching list of nodes from ${link}
[5003]225 safefetch ${link} ${nlist} || cleanexit 1
[4985]226}
227
[4049]228getvalidnodename()
229{
230while ! grep -q "^${nodename}\$" ${nlist}
231do
[4085]232 echo Nodes:
[4985]233 if [ -x /usr/bin/column ]; then
234 column ${nlist}
235 else
236 cat ${nlist}
237 fi
[4085]238 echo
239 echo -n enter nodename \[default: ${default}\]:
240 if [ -z ${QUIET} ]; then
241 read nodename
242 else
243 nodename=${default}
244 fi
[4049]245
[4085]246 if [ "x${nodename}" = "x" ]; then
247 nodename=${default}
248 fi
[4049]249done
250
251echo Node Selected: ${nodename}
252}
253
254
255do_diff() {
[4997]256 if test -e $lcd/$1; then
[5003]257 diff -uwbB $lcd/$1 $dir/$1.new
[4990]258 else
[5001]259 echo Warning: $lcd/$1 does not yet exist - no DIFF
[4990]260 fi
[4049]261}
262
263do_move() {
[5003]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
[4985]269}
[4049]270
[4985]271linkin() {
272 symdir=$1
273 file=$2
[4054]274
[4985]275 if [ ${PRETEND} = 'yes' ]; then
276 echo "** $CMD $*"
277 else
[5003]278 $CMD $file || exit 1
[4985]279 fi
[4049]280
[4986]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 )
[4049]286}
287
[4988]288log Config Node -- Version: $VERSION '$Rev: 5004 $'
[4049]289
[4988]290# Make sure we clean up our mess when needed.
291trap "rm -f ${TMPPREFIX}.?; echo Failed; exit 1;" 2 3
[4049]292
[5003]293if [ ${PRETEND} != 'yes' -a ${CMD} != 'do_diff' ]; then
[4988]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
[4049]312#check config dir
313#
314if [ ! -d ${dir} ]; then
[4085]315 mkdir -p ${dir} || cleanexit 1
[4049]316fi
317
318if [ -z ${HTTP_PROXY} ]; then
[4990]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
[5003]324 log Connection: Ok
[4990]325 exit 0
326 ) || connset
327else
328 log Connection not checked because there is an http proxy configured: ${HTTP_PROXY}.
[4049]329fi
330
[4985]331if [ -r ${lcd}/myname ]; then
332 default=`cat ${lcd}/myname`
[4049]333else
[4085]334 default=`hostname -s`
335 test -z $default && default=none
[4049]336fi
337
[4989]338if [ ! -z ${QUIET} ]; then
339 if [ -z ${nodename} ]; then
340 nodename=${default}
341 fi
[4988]342fi
343
[4989]344
[4049]345nlist=${TMPPREFIX}.l
346
347test -z $default && default=$nodename
348
[4985]349getvalidnodenames || exit 1
350
[4049]351test -z $nodename && getvalidnodename
352
[4985]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
[5003]365lognlr "Checking release and OS versions: "
[5001]366
[5003]367safefetch ${link}${nodename}/info $dir/info.last \
[5001]368 || cleanexit 1
369
370OS=`uname -s`
371REL=`uname -r`
[5003]372set -- `head -1 $dir/info.last`
373
[5001]374# FreeBSD 5.0-RELEASE 1 YES
375if [ $# != 4 ]; then
376 echo Info verification failed.
377 cleanexit 1
378fi
[5003]379if [ x$4 != 'xYES' ]; then
[5001]380 echo Genesis marked as disabled for this machine.
381 test -z ${DLV} || cleanexit 1
382fi
[5003]383if [ 0$3 -ne $VERSION_MAJOR ]; then
[5001]384 echo This script is version $VERSION, genesis info is for version $3.xx
385 test -z ${DLV} || cleanexit 1
386fi
[5003]387if [ x$1 != x$OS ]; then
[5001]388 echo Operating system mismatch; this machine: $OS, but config is for $1
389 test -z ${DLV} || cleanexit 1
390fi
[5003]391if [ x$2 != x$REL ]; then
[5001]392 echo This machine runs $REL, but the configuration is for $2
393 test -z ${DLV} || cleanexit 2
394fi
395
[5003]396log Ok
397
[4049]398log Fetching file list from $link for $nodename
399
400safefetch ${link}${nodename} ${dir}/${list}
401
[4985]402lognlr "Fetching:"
[4049]403for i in `cat ${dir}/${list}`
404do
[4985]405 lognlr " ${i}"
[5003]406# $FETCH -o - ${link}${nodename}/${i} > ${dir}/${i}.new || cleanexit 1
407 safefetch ${link}${nodename}/${i} ${dir}/${i}.new
[4049]408done
[4985]409log .
[4049]410
411for i in `cat ${dir}/${list}`
412do
[4985]413 case ${i} in
414 linux.sh | config | txtconfig )
415 # log obsolete file: ${i} - skipped
416 ;;
417 resolv.conf | rc.node.local | rc.local)
[4986]418 linkin /etc ${i}
[4985]419 ;;
420 snmpd.local.conf)
[4986]421 linkin /usr/local/share/snmp ${i}
[4985]422 ;;
423 named.conf)
[4986]424 linkin /etc/namedb ${i}
[4985]425 ;;
426 dhcpd.conf)
[4986]427 linkin /usr/local/etc ${i}
[4985]428 ;;
429 zebra.conf | ospfd.conf)
[4986]430 linkin /usr/local/etc/zebra ${i}
[4985]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
[4049]445done
446
447if [ -e /etc/rc.local ]; then
[4085]448 # See if we are in rc.local
449 if grep -q /config-node.sh /etc/rc.local; then
[4049]450
[4085]451 echo As this node now has real configs - do enter a root password
452 echo
453 passwd \
454 || cleanexit 1
[4049]455
[4085]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
[4049]461fi
462
463# Record our name.
464echo ${nodename} > ${dir}/myname
465
466# Rebuild reverse lookups
[4985]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
[5003]474if [ -r /etc/namedb/make-localhost -a ${CMD} != 'do_diff' ]; then
[4085]475 (
476 cd /etc/namedb || exit 1
477 sh /etc/namedb/make-localhost || exit 1
478 ) || exit 1
[4049]479fi
480
[4985]481cleanse || exit 1
[4049]482
483if [ -e /etc/rc.empty.conf ] ; then
[4985]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
[4049]488fi
489
[4986]490test ${FORCE} = 'rw' && mount -u -r /
[4049]491exit 0
Note: See TracBrowser for help on using the repository browser.