d5f615fbab2935f369cd78c113f9b0b3cf4e09ba
[myplc.git] / plc.d / network
1 #!/bin/bash
2 #
3 # priority: 100
4 #
5 # Manage network related configuration files
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id$
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 case "$1" in
21     start)
22         MESSAGE=$"Generating network files"
23         dialog "$MESSAGE"
24
25         mv -f /etc/hosts /etc/hosts_plc.backup
26         hfile=$(mktemp)
27         # Minimal /etc/hosts
28         (
29             for server in DB API BOOT WWW ; do
30                 hostname=PLC_${server}_HOST
31                 ip=PLC_${server}_IP
32                 if [ -n "${!ip}" ] ; then
33                     echo "${!ip}        ${!hostname}"
34                 else
35                     ip=$(gethostbyname ${!hostname})
36                     if [ -n "$ip" ] ; then
37                         echo "$ip       ${!hostname}"
38                     fi
39                 fi
40             done
41         ) > $hfile
42         grep -v "^#" /etc/hosts >> $hfile
43         echo "#generated by /etc/plc.d/network" >/etc/hosts
44         echo "127.0.0.1 localhost.localdomain localhost" >> $hfile
45         sort -u $hfile >> /etc/hosts
46         chmod +r /etc/hosts
47         rm -f $hfile
48
49         result "$MESSAGE"
50         ;;
51
52     stop)
53         MESSAGE=$"Reverting network files"
54         if [ -f /etc/hosts_plc.backup ] ; then
55             mv -f /etc/hosts_plc.backup /etc/hosts
56         fi
57         result "$MESSAGE"
58         ;;
59 esac
60
61 exit $ERRORS