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