#!/usr/bin/env /usr/bin/plcsh # # Writes IP addresses and hostnames of PlanetLab nodes to # /etc/plc_hosts. Useful for dnsmasq, specify "addn-hosts # /etc/plc_hosts" in /etc/dnsmasq.conf. # # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # # pylint: disable=c0111 import sys import os from plc_config import PLCConfiguration def writepid(prog): """ Check PID file. Exit if already running. Update PID file. """ try: with open("/var/run/%s.pid" % prog, "r") as pidfile: pid = pidfile.readline().strip() if os.path.isdir("/proc/" + pid): print("Error: Another copy of %s is still running (%s)" % (prog, pid)) sys.exit(1) except IOError: pass with open("/var/run/%s.pid" % prog, "w") as pidfile: pidfile.write(str(os.getpid())) def removepid(prog): os.unlink("/var/run/%s.pid" % prog) def main(): writepid("dns-config") cfg = PLCConfiguration() cfg.load() variables = cfg.variables() (_, variablelist) = variables['plc_dns'] plc_dns = dict(list(zip(list(variablelist.keys()), [variable['value'] for variable in list(variablelist.values())]))) if plc_dns['enabled'] != "true": return 0 # Get the primary IP address for each node hosts = {} nodes = {} for node in GetNodes(): nodes[node['node_id']] = node interface_ids = set() for node in list(nodes.values()): interface_ids.update(node['interface_ids']) for interface in GetInterfaces(list(interface_ids)): if not interface['ip']: continue if interface['hostname']: hostname = interface['hostname'] else: hostname = nodes[interface['node_id']]['hostname'] if interface['ip'] in hosts: if hostname not in hosts[interface['ip']]: hosts[interface['ip']].append(hostname) else: hosts[interface['ip']] = [hostname] # Write /etc/plc_hosts plc_hosts = open("/etc/plc_hosts", "w") plc_hosts.write("# DO NOT EDIT; File is written and removed by automatic scripts\n") for ip, hostnames in hosts.items(): plc_hosts.write(ip + "\t" + " ".join(hostnames) + "\n") plc_hosts.close() # From the default dnsmasq.conf configuration file: # # The [domain-needed and bogus-priv] options make you a better # netizen, since they tell dnsmasq to filter out queries which # the public DNS cannot answer, and which load the servers # (especially the root servers) uneccessarily. # with open("/etc/dnsmasq.conf", "w") as writer: writer.write(""" domain-needed bogus-priv addn-hosts=/etc/plc_hosts resolv-file=/etc/resolv.conf """.lstrip()) # Reload dnsmasq os.system("killall -q -HUP dnsmasq") removepid("dns-config") if __name__ == '__main__': main()