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
10 # pylint: disable=c0111
14 from plc_config import PLCConfiguration
18 Check PID file. Exit if already running. Update PID file.
22 with open("/var/run/%s.pid" % prog, "r") as pidfile:
23 pid = pidfile.readline().strip()
24 if os.path.isdir("/proc/" + pid):
25 print("Error: Another copy of %s is still running (%s)" % (prog, pid))
30 with open("/var/run/%s.pid" % prog, "w") as pidfile:
31 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 (_, variablelist) = variables['plc_dns']
44 plc_dns = dict(list(zip(list(variablelist.keys()),
46 for variable in list(variablelist.values())])))
48 if plc_dns['enabled'] != "true":
51 # Get the primary IP address for each node
55 for node in GetNodes():
56 nodes[node['node_id']] = node
59 for node in list(nodes.values()):
60 interface_ids.update(node['interface_ids'])
62 for interface in GetInterfaces(list(interface_ids)):
63 if not interface['ip']:
66 if interface['hostname']:
67 hostname = interface['hostname']
69 hostname = nodes[interface['node_id']]['hostname']
71 if interface['ip'] in hosts:
72 if hostname not in hosts[interface['ip']]:
73 hosts[interface['ip']].append(hostname)
75 hosts[interface['ip']] = [hostname]
77 # Write /etc/plc_hosts
78 plc_hosts = open("/etc/plc_hosts", "w")
79 plc_hosts.write("# DO NOT EDIT; File is written and removed by automatic scripts\n")
80 for ip, hostnames in hosts.items():
81 plc_hosts.write(ip + "\t" + " ".join(hostnames) + "\n")
84 # From the default dnsmasq.conf configuration file:
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.
91 with open("/etc/dnsmasq.conf", "w") as writer:
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__':