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

Last change on this file since 4986 was 4986, checked in by rick, 19 years ago

Minor bug fixes and RO/RW automation

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