- move DNS configuration/update to separate Python script that runs
authorMark Huang <mlhuang@cs.princeton.edu>
Fri, 26 May 2006 19:57:30 +0000 (19:57 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Fri, 26 May 2006 19:57:30 +0000 (19:57 +0000)
  both at startup and periodically, so that /etc/plc_hosts and dnsmasq
  are kept up-to-date

api-config
dns-config [new file with mode: 0755]
plc.d/crond
plc.d/dns

index b2b0e78..330a80a 100755 (executable)
@@ -6,7 +6,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # 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 (executable)
index 0000000..bc2bf46
--- /dev/null
@@ -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 <mlhuang@cs.princeton.edu>
+# 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()
index 5fb4120..48fcf38 100755 (executable)
@@ -7,7 +7,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # 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
 
index 446c21f..ac639e4 100755 (executable)
--- a/plc.d/dns
+++ b/plc.d/dns
@@ -8,7 +8,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # 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 <<EOF
-domain-needed
-bogus-priv
-addn-hosts=/etc/plc_hosts
-EOF
+       dns-config
+       check
 
        plc_daemon dnsmasq
        check