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
11 from plc_config import PLCConfiguration
16 Check PID file. Exit if already running. Update PID file.
20 pidfile = file("/var/run/%s.pid" % prog, "r")
21 pid = pidfile.readline().strip()
23 if os.path.isdir("/proc/" + pid):
24 print "Error: Another copy of %s is still running (%s)" % (prog, pid)
29 pidfile = file("/var/run/%s.pid" % prog, "w")
30 pidfile.write(str(os.getpid()))
34 os.unlink("/var/run/%s.pid" % prog)
37 writepid("dns-config")
39 cfg = PLCConfiguration()
41 variables = cfg.variables()
43 (category, variablelist) = variables['plc_dns']
44 plc_dns = dict(zip(variablelist.keys(),
45 [variable['value'] for variable in variablelist.values()]))
47 if plc_dns['enabled'] != "true":
50 # Get the primary IP address for each node
54 for node in GetNodes():
55 nodes[node['node_id']] = node
58 for node in nodes.values():
59 interface_ids.update(node['interface_ids'])
61 for interface in GetInterfaces(list(interface_ids)):
62 if not interface['ip']:
65 if interface['hostname']:
66 hostname = interface['hostname']
68 hostname = nodes[interface['node_id']]['hostname']
70 if hosts.has_key(interface['ip']):
71 if hostname not in hosts[interface['ip']]:
72 hosts[interface['ip']].append(hostname)
74 hosts[interface['ip']] = [hostname]
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")
83 # From the default dnsmasq.conf configuration file:
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.
90 file("/etc/dnsmasq.conf", "w").write("""
93 addn-hosts=/etc/plc_hosts
94 resolv-file=/etc/resolv.conf
98 os.system("killall -q -HUP dnsmasq")
100 removepid("dns-config")
102 if __name__ == '__main__':