From da08eb5915135af95298b8fde31d49f57c70eaf7 Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Fri, 26 May 2006 19:57:30 +0000 Subject: [PATCH 1/1] - move DNS configuration/update to separate Python script that runs both at startup and periodically, so that /etc/plc_hosts and dnsmasq are kept up-to-date --- api-config | 29 +-------------- dns-config | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ plc.d/crond | 5 ++- plc.d/dns | 19 ++-------- 4 files changed, 108 insertions(+), 45 deletions(-) create mode 100755 dns-config diff --git a/api-config b/api-config index b2b0e78..330a80a 100755 --- a/api-config +++ b/api-config @@ -6,7 +6,7 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: api-config,v 1.9 2006/05/23 18:09:21 mlhuang Exp $ +# $Id: api-config,v 1.10 2006/05/24 03:08:55 mlhuang Exp $ # import plcapilib @@ -94,33 +94,6 @@ def main(): AdmGrantRoleToPerson(admin['person_id'], 10) AdmGrantRoleToPerson(admin['person_id'], 20) - # Get the primary IP address for each node - hosts = {} - nodes = AdmGetNodes([], ['node_id', 'hostname']) - plcapi.begin() - for node in nodes: - AdmGetAllNodeNetworks(node['node_id']) - nodenetworks_list = plcapi.commit() - if nodenetworks_list is not None: - for i, nodenetworks in enumerate(nodenetworks_list): - for nodenetwork in nodenetworks: - if nodenetwork['hostname']: - hostname = nodenetwork['hostname'] - else: - hostname = nodes[i]['hostname'] - - if hosts.has_key(nodenetwork['ip']): - if hostname not in hosts[nodenetwork['ip']]: - hosts[nodenetwork['ip']].append(hostname) - else: - hosts[nodenetwork['ip']] = [hostname] - - # Write /etc/plc_hosts - plc_hosts = open("/etc/plc_hosts", "w") - for ip, hostnames in hosts.iteritems(): - plc_hosts.write(ip + "\t" + " ".join(hostnames) + "\n") - plc_hosts.close() - # Setup default PlanetLabConf entries default_conf_files = [ # NTP configuration diff --git a/dns-config b/dns-config new file mode 100755 index 0000000..bc2bf46 --- /dev/null +++ b/dns-config @@ -0,0 +1,100 @@ +#!/usr/bin/python +# +# 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: api-config,v 1.10 2006/05/24 03:08:55 mlhuang Exp $ +# + +import plcapilib +(plcapi, moreopts, argv) = plcapilib.plcapi(globals()) +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 = AdmGetNodes([], ['node_id', 'hostname']) + plcapi.begin() + for node in nodes: + AdmGetAllNodeNetworks(node['node_id']) + nodenetworks_list = plcapi.commit() + if nodenetworks_list is not None: + for i, nodenetworks in enumerate(nodenetworks_list): + for nodenetwork in nodenetworks: + if nodenetwork['hostname']: + hostname = nodenetwork['hostname'] + else: + hostname = nodes[i]['hostname'] + + if hosts.has_key(nodenetwork['ip']): + if hostname not in hosts[nodenetwork['ip']]: + hosts[nodenetwork['ip']].append(hostname) + else: + hosts[nodenetwork['ip']] = [hostname] + + # Write /etc/plc_hosts + plc_hosts = open("/etc/plc_hosts", "w") + 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 +""".lstrip()) + + # Reload dnsmasq + os.system("killall -q -HUP dnsmasq") + + removepid("dns-config") + +if __name__ == '__main__': + main() diff --git a/plc.d/crond b/plc.d/crond index 5fb4120..48fcf38 100755 --- a/plc.d/crond +++ b/plc.d/crond @@ -7,7 +7,7 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: crond,v 1.2 2006/04/25 21:18:19 mlhuang Exp $ +# $Id: crond,v 1.3 2006/05/22 21:12:17 mlhuang Exp $ # # Source function library and configuration @@ -34,6 +34,7 @@ HOME=/ */5 * * * * root gen-slices-xml-05.py */15 * * * * root gen-sites-xml.py */15 * * * * root gen-static-content.py +*/15 * * * * root dns-config 5 5 * * * root vacuumdb -U postgres --all --analyze --quiet EOF @@ -44,6 +45,8 @@ EOF check gen-static-content.py check + dns-config + check vacuumdb -U postgres --all --analyze --quiet check diff --git a/plc.d/dns b/plc.d/dns index 446c21f..ac639e4 100755 --- a/plc.d/dns +++ b/plc.d/dns @@ -8,7 +8,7 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: mail,v 1.2 2006/04/25 21:18:19 mlhuang Exp $ +# $Id: dns,v 1.1 2006/05/23 18:10:08 mlhuang Exp $ # # Source function library and configuration @@ -24,21 +24,8 @@ case "$1" in MESSAGE=$"Starting DNS server" dialog "$MESSAGE" - # 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. - # - # The api-config bootstrap script writes node hostnames to - # /etc/plc_hosts. - # - cat >/etc/dnsmasq.conf <