Generate hosts and resolv.conf files different depending on whether PLC_DNS_ENABLED=1.
[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         hfile=$(mktemp)
26         # Minimal /etc/hosts
27         (
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
40         ) > $hfile
41         if [ $PLC_DNS_ENABLED -eq 1 ] ; then
42             target=/etc/plc_hosts
43             sort -u $hfile > $target
44         else
45             target=/etc/hosts
46             cat /etc/hosts >> $hfile
47             echo "#generated by /etc/plc.d/network" >/etc/hosts
48             echo "127.0.0.1     localhost.localdomain localhost" >>/etc/hosts
49             sort -u $hfile >> /etc/hosts
50         fi
51         chmod +r $target
52         rm -f $hfile
53
54         rfile=$(mktemp)
55         # Set up nameservers
56         (
57             [ -n "$PLC_NET_DNS1" ] && echo "nameserver $PLC_NET_DNS1"
58             [ -n "$PLC_NET_DNS2" ] && echo "nameserver $PLC_NET_DNS2"
59         ) > $rfile
60
61         if [ $PLC_DNS_ENABLED -eq 1 ] ; then
62             target=/etc/plc_resolv.conf
63         else
64             target=/etc/resolv.conf
65         fi
66         cp $rfile $target
67         chmod +r $target
68
69         result "$MESSAGE"
70         ;;
71 esac
72
73 exit $ERRORS