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

Last change on this file since 5000 was 4997, checked in by dirkx, 20 years ago

better diff

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