Change v3 to v4 yumgroups.xml
[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: dns-config,v 1.1 2006/05/26 19:57:30 mlhuang Exp $
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     nodenetwork_ids = set()
60     for node in nodes.values():
61         nodenetwork_ids.update(node['nodenetwork_ids'])
62
63     for nodenetwork in GetNodeNetworks(list(nodenetwork_ids)):
64         if not nodenetwork['ip']:
65             continue
66
67         if nodenetwork['hostname']:
68             hostname = nodenetwork['hostname']
69         else:
70             hostname = nodes[nodenetwork['node_id']]['hostname']
71
72         if hosts.has_key(nodenetwork['ip']):
73             if hostname not in hosts[nodenetwork['ip']]:
74                 hosts[nodenetwork['ip']].append(hostname)
75         else:
76             hosts[nodenetwork['ip']] = [hostname]
77     
78     # Write /etc/plc_hosts
79     plc_hosts = open("/etc/plc_hosts", "w")
80     for ip, hostnames in hosts.iteritems():
81         plc_hosts.write(ip + "\t" + " ".join(hostnames) + "\n")
82     plc_hosts.close()
83
84     # From the default dnsmasq.conf configuration file:
85     #
86     # The [domain-needed and bogus-priv] options make you a better
87     # netizen, since they tell dnsmasq to filter out queries which
88     # the public DNS cannot answer, and which load the servers
89     # (especially the root servers) uneccessarily.
90     #
91     file("/etc/dnsmasq.conf", "w").write("""
92 domain-needed
93 bogus-priv
94 addn-hosts=/etc/plc_hosts
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()