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.
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
10 # $Id: api-config,v 1.10 2006/05/24 03:08:55 mlhuang Exp $
14 (plcapi, moreopts, argv) = plcapilib.plcapi(globals())
15 from plc_config import PLCConfiguration
20 Check PID file. Exit if already running. Update PID file.
24 pidfile = file("/var/run/%s.pid" % prog, "r")
25 pid = pidfile.readline().strip()
27 if os.path.isdir("/proc/" + pid):
28 print "Error: Another copy of %s is still running (%s)" % (prog, pid)
33 pidfile = file("/var/run/%s.pid" % prog, "w")
34 pidfile.write(str(os.getpid()))
38 os.unlink("/var/run/%s.pid" % prog)
41 writepid("dns-config")
43 cfg = PLCConfiguration()
45 variables = cfg.variables()
47 (category, variablelist) = variables['plc_dns']
48 plc_dns = dict(zip(variablelist.keys(),
49 [variable['value'] for variable in variablelist.values()]))
51 if plc_dns['enabled'] != "true":
54 # Get the primary IP address for each node
56 nodes = AdmGetNodes([], ['node_id', 'hostname'])
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']
67 hostname = nodes[i]['hostname']
69 if hosts.has_key(nodenetwork['ip']):
70 if hostname not in hosts[nodenetwork['ip']]:
71 hosts[nodenetwork['ip']].append(hostname)
73 hosts[nodenetwork['ip']] = [hostname]
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")
81 # From the default dnsmasq.conf configuration file:
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.
88 file("/etc/dnsmasq.conf", "w").write("""
91 addn-hosts=/etc/plc_hosts
95 os.system("killall -q -HUP dnsmasq")
97 removepid("dns-config")
99 if __name__ == '__main__':