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