check-hrns: Use SFA_INTERFACE_HRN as toplevel if possible
[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
11 # Source function library and configuration
12 . /etc/plc.d/functions
13 . /etc/planetlab/plc_config
14
15 # Be verbose
16 set -x
17
18 [ "$PLC_HOSTS_ENABLED" -ne 1 ] && exit 0
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         for server in DB API BOOT WWW ; do
29             hostname=PLC_${server}_HOST
30             ip=PLC_${server}_IP
31             if [ -n "${!ip}" ] ; then
32                 echo "${!ip}    ${!hostname}"
33             else
34                 ip=$(gethostbyname ${!hostname})
35                 if [ -n "$ip" ] ; then
36                     echo "$ip   ${!hostname}"
37                 fi
38             fi
39         done > $hfile
40         echo "#generated by /etc/plc.d/network" >/etc/hosts
41         echo "127.0.0.1 localhost.localdomain localhost" >> /etc/hosts
42         sort -u $hfile >> /etc/hosts
43         chmod +r /etc/hosts
44         rm -f $hfile
45
46         result "$MESSAGE"
47         ;;
48
49     stop)
50         MESSAGE=$"Reverting network files"
51         if [ -f /etc/hosts_plc.backup ] ; then
52             mv -f /etc/hosts_plc.backup /etc/hosts
53         fi
54         dialog "$MESSAGE"
55         result "$MESSAGE"
56         ;;
57 esac
58
59 exit $ERRORS