#!/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 # # $Id$ # from plc_config import PLCConfiguration import os, sys def writepid(prog): """ Check PID file. Exit if already running. Update PID file. """ try: pidfile = file("/var/run/%s.pid" % prog, "r") pid = pidfile.readline().strip() pidfile.close() if os.path.isdir("/proc/" + pid): print "Error: Another copy of %s is still running (%s)" % (prog, pid) sys.exit(1) except IOError: pass pidfile = file("/var/run/%s.pid" % prog, "w") pidfile.write(str(os.getpid())) pidfile.close() def removepid(prog): os.unlink("/var/run/%s.pid" % prog) def main(): writepid("dns-config") cfg = PLCConfiguration() cfg.load() variables = cfg.variables() (category, variablelist) = variables['plc_dns'] plc_dns = dict(zip(variablelist.keys(), [variable['value'] for variable in 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 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: if not interface['is_primary']: continue hostname = nodes[interface['node_id']]['hostname'] if hosts.has_key(interface['ip']): 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 writen and removed by automatic scripts\n") for ip, hostnames in hosts.iteritems(): 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. # file("/etc/dnsmasq.conf", "w").write(""" domain-needed bogus-priv addn-hosts=/etc/plc_hosts resolv-file=/etc/plc_resolv.conf conf-dir=/etc/dnsmasq.d """.lstrip()) # Reload dnsmasq os.system("killall -q -HUP dnsmasq") removepid("dns-config") if __name__ == '__main__': main()