fix resolv.conf issue on plc
[myplc.git] / dns-config
1 #!/usr/bin/env /usr/bin/plcsh
2 #
3 # Writes IP addresses and hostnames of PlanetLab nodes to
4 # /etc/plc_hosts. Useful for dnsmasq, specify "addn-hosts
5 # /etc/plc_hosts" in /etc/dnsmasq.conf.
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id$
11 #
12
13 from plc_config import PLCConfiguration
14 import os, sys
15
16 def writepid(prog):
17     """
18     Check PID file. Exit if already running. Update PID file.
19     """
20
21     try:
22         pidfile = file("/var/run/%s.pid" % prog, "r")
23         pid = pidfile.readline().strip()
24         pidfile.close()
25         if os.path.isdir("/proc/" + pid):
26             print "Error: Another copy of %s is still running (%s)" % (prog, pid)
27             sys.exit(1)
28     except IOError:
29         pass
30
31     pidfile = file("/var/run/%s.pid" % prog, "w")
32     pidfile.write(str(os.getpid()))
33     pidfile.close()
34
35 def removepid(prog):
36     os.unlink("/var/run/%s.pid" % prog)
37
38 def main():
39     writepid("dns-config")
40
41     cfg = PLCConfiguration()
42     cfg.load()
43     variables = cfg.variables()
44
45     (category, variablelist) = variables['plc_dns']
46     plc_dns = dict(zip(variablelist.keys(),
47                        [variable['value'] for variable in variablelist.values()]))
48
49     if plc_dns['enabled'] != "true":
50         return 0
51
52     # Get the primary IP address for each node
53     hosts = {}
54
55     nodes = {}
56     for node in GetNodes():
57         nodes[node['node_id']] = node
58
59     interface_ids = set()
60     for node in nodes.values():
61         interface_ids.update(node['interface_ids'])
62
63     for interface in GetInterfaces(list(interface_ids)):
64         if not interface['ip']:
65             continue
66
67         if interface['hostname']:
68             hostname = interface['hostname']
69         else:       
70             if not interface['is_primary']:
71                 continue
72             hostname = nodes[interface['node_id']]['hostname']
73
74         if hosts.has_key(interface['ip']):
75             if hostname not in hosts[interface['ip']]:
76                 hosts[interface['ip']].append(hostname)
77         else:
78             hosts[interface['ip']] = [hostname]
79     
80     # Write /etc/plc_hosts
81     plc_hosts = open("/etc/plc_hosts", "w")
82     plc_hosts.write("# DO NOT EDIT; File is writen and removed by automatic scripts\n")
83     for ip, hostnames in hosts.iteritems():
84         plc_hosts.write(ip + "\t" + " ".join(hostnames) + "\n")
85     plc_hosts.close()
86
87     # From the default dnsmasq.conf configuration file:
88     #
89     # The [domain-needed and bogus-priv] options make you a better
90     # netizen, since they tell dnsmasq to filter out queries which
91     # the public DNS cannot answer, and which load the servers
92     # (especially the root servers) uneccessarily.
93     #
94     file("/etc/dnsmasq.conf", "w").write("""
95 domain-needed
96 bogus-priv
97 addn-hosts=/etc/plc_hosts
98 resolv-file=/etc/plc_resolv.conf
99 conf-dir=/etc/dnsmasq.d
100 """.lstrip())
101
102     # Reload dnsmasq
103     os.system("killall -q -HUP dnsmasq")
104
105     removepid("dns-config")
106
107 if __name__ == '__main__':
108     main()