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