1 #!/usr/bin/env /usr/bin/plcsh
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
13 from plc_config import PLCConfiguration
18 Check PID file. Exit if already running. Update PID file.
22 pidfile = file("/var/run/%s.pid" % prog, "r")
23 pid = pidfile.readline().strip()
25 if os.path.isdir("/proc/" + pid):
26 print "Error: Another copy of %s is still running (%s)" % (prog, pid)
31 pidfile = file("/var/run/%s.pid" % prog, "w")
32 pidfile.write(str(os.getpid()))
36 os.unlink("/var/run/%s.pid" % prog)
39 writepid("dns-config")
41 cfg = PLCConfiguration()
43 variables = cfg.variables()
45 (category, variablelist) = variables['plc_dns']
46 plc_dns = dict(zip(variablelist.keys(),
47 [variable['value'] for variable in variablelist.values()]))
49 if plc_dns['enabled'] != "true":
52 # Get the primary IP address for each node
56 for node in GetNodes():
57 nodes[node['node_id']] = node
60 for node in nodes.values():
61 interface_ids.update(node['interface_ids'])
63 for interface in GetInterfaces(list(interface_ids)):
64 if not interface['ip']:
67 if interface['hostname']:
68 hostname = interface['hostname']
70 hostname = nodes[interface['node_id']]['hostname']
72 if hosts.has_key(interface['ip']):
73 if hostname not in hosts[interface['ip']]:
74 hosts[interface['ip']].append(hostname)
76 hosts[interface['ip']] = [hostname]
78 # Write /etc/plc_hosts
79 plc_hosts = open("/etc/plc_hosts", "w")
80 plc_hosts.write("# DO NOT EDIT; File is writen and removed by automatic scripts\n")
81 for ip, hostnames in hosts.iteritems():
82 plc_hosts.write(ip + "\t" + " ".join(hostnames) + "\n")
85 # From the default dnsmasq.conf configuration file:
87 # The [domain-needed and bogus-priv] options make you a better
88 # netizen, since they tell dnsmasq to filter out queries which
89 # the public DNS cannot answer, and which load the servers
90 # (especially the root servers) uneccessarily.
92 file("/etc/dnsmasq.conf", "w").write("""
95 addn-hosts=/etc/plc_hosts
96 resolv-file=/etc/resolv.conf
100 os.system("killall -q -HUP dnsmasq")
102 removepid("dns-config")
104 if __name__ == '__main__':