Index: nodes/gformat.py
===================================================================
--- nodes/gformat.py	(revision 8585)
+++ nodes/gformat.py	(revision 8588)
@@ -14,4 +14,5 @@
 import sys
 import time
+import make_map
 from pprint import pprint
 try:
@@ -21,4 +22,11 @@
   print "[ERROR] Please install the python-yaml or devel/py-yaml package"
   exit(1)
+
+try:
+    from yaml import CLoader as Loader
+    from yaml import CDumper as Dumper
+except ImportError:
+    from yaml import Loader, Dumper
+
   
 
@@ -65,4 +73,43 @@
   """ Combined hosts and proxy list"""
   return get_nodelist() + get_proxylist()
+
+def angle_between_points(lat1,lat2,long1,long2):
+  """ 
+  Return Angle in radians between two GPS coordinates
+  See: http://stackoverflow.com/questions/3809179/angle-between-2-gps-coordinates
+  """
+  dy = lat2 - lat1
+  dx = math.cos(math.pi/180*lat1)*(long2 - long1)
+  angle = math.atan2(dy,dx)
+  return angle
+
+def angle_to_cd(angle):
+  """ Return Dutch Cardinal Direction estimation in 'one digit' of radian angle """
+
+  # For easy conversion get positive degree
+  degrees = math.degrees(angle)
+  if degrees < 0:
+    360 - abs(degrees)
+
+  # Numbers can be confusing calculate from the 4 main directions
+  p = 22.5
+  if degrees < p:
+    return "n"
+  elif degrees < (90 - p): 
+    return "no"
+  elif degrees < (90 + p): 
+    return "o"
+  elif degrees < (180 - p): 
+    return "zo"
+  elif degrees < (180 + p): 
+    return "z"
+  elif degrees < (270 - p): 
+    return "zw"
+  elif degrees < (270 + p): 
+    return "w"
+  elif degrees < (360 - p): 
+    return "nw"
+  else:
+    return "n"
 
 
@@ -300,8 +347,16 @@
 
   f = open(gfile, 'r')
-  datadump = yaml.load(f)
+  datadump = yaml.load(f,Loader=Loader)
   f.close()
 
   return datadump
+
+def store_yaml(datadump):
+  """ Store configuration yaml for 'item'"""
+  gfile = NODE_DIR + '/%s/wleiden.yaml' % item
+
+  f = open(gfile, 'w')
+  f.write(generate_wleiden_yaml(datadump))
+  f.close()
 
 
@@ -408,4 +463,8 @@
   return output
 
+
+def generate_yaml(datadump):
+  return generate_config(datadump['nodename'], "wleiden.yaml", datadump)
+  
 
 
@@ -472,4 +531,88 @@
     assert False, "Invalid option"
   print output
+
+def get_fqdn(datadump):
+  # Proxy naming convention is special
+  if datadump['nodetype'] == 'Proxy':
+    fqdn = datadump['nodename']
+  else:
+    # By default the full name is listed and also a shortname CNAME for easy use.
+    fqdn = datadump['nodetype'] + datadump['nodename']
+  return(fqdn)
+  
+  
+
+def make_dns():
+  items = dict()
+  # hostname is key, IP is value
+  wleiden_zone = dict()
+  wleiden_cname = dict()
+  pool = dict()
+  for node in get_hostlist():
+    datadump = get_yaml(node)
+  
+    # Proxy naming convention is special
+    fqdn = get_fqdn(datadump)
+    if datadump['nodetype'] == 'CNode':
+      wleiden_cname[datadump['nodename']] = fqdn
+
+    wleiden_zone[fqdn] = datadump['masterip']
+
+    #items['node'] = node
+    #items['wdir'] = "./static/%(node)s" % items
+    #if not os.path.isdir(items['wdir']):
+    #  os.makedirs(items['wdir'])
+    for iface_key in get_interface_keys(datadump):
+      iface_name = datadump[iface_key]['interface'].replace(':',"_alias_")
+      (ip, netmask) = datadump[iface_key]['ip'].split('/')
+      try:
+        (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
+        datadump[iface_key]['subnet'] = netmask2subnet(netmask)
+        dhcp_part = ".".join(ip.split('.')[0:3])
+        if ip != datadump['masterip']:
+          wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)] = ip
+        for i in range(int(dhcp_start), int(dhcp_stop) + 1):
+          wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)] = "%s.%s" % (dhcp_part, i)
+      except (AttributeError, ValueError):
+        # First push it into a pool, to indentify the counter-part later on
+        addr = parseaddr(ip)
+        netmask = int(netmask)
+        addr = addr & ~((1 << (32 - netmask)) - 1)
+        if pool.has_key(addr): 
+          pool[addr] += [(iface_name, fqdn, ip)]
+        else: 
+          pool[addr] = [(iface_name, fqdn, ip)]
+        continue
+
+  # wleiden_zone["2%s-%s.%s" % ("unused", iface_name, fqdn)] = ip
+  # #XXX: automatic naming convention namely 2 + remote.lower()
+  for (key,value) in pool.iteritems():
+    if len(value) == 1:
+      (iface_name, fqdn, ip) = value[0]
+      wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)] = ip
+    elif len(value) == 2:
+      (a_iface_name, a_fqdn, a_ip) = value[0]
+      (b_iface_name, b_fqdn, b_ip) = value[1]
+      wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)] = a_ip
+      wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)] = b_ip
+    else:
+      pool_members = [k[1] for k in value]
+      for item in value:
+        (iface_name, fqdn, ip) = item 
+        pool_name = "2pool-" + showaddr(key).replace('.','-') + "-" + "_".join(sorted(list(set(pool_members) - set([fqdn]))))
+        wleiden_zone["%s.%s" % (pool_name, fqdn)] = ip
+      
+    
+  f = open("db.wleiden.net", "w")
+  for host,ip in wleiden_zone.iteritems():
+    f.write("%s.wleiden.net. A %s ~\n" % (host, ip)) 
+  for source,dest in wleiden_cname.iteritems():
+    f.write("%s.wleiden.net. CNAME %s.wleiden.net. ~\n" % (source, dest))
+  f.close()
+  f = open("db.172.in-addr.arpa", "w")
+  for host,ip in wleiden_zone.iteritems():
+    rev_ip = '.'.join(reversed(ip.split('.')))
+    f.write("%s.in-addr.arpa. PTR %s.wleiden.net. ~\n" % (rev_ip, host)) 
+  f.close()
 
 
@@ -537,92 +680,21 @@
           f.close()
     elif sys.argv[1] == "dns":
-      items = dict()
-      # hostname is key, IP is value
-      wleiden_zone = dict()
-      wleiden_cname = dict()
-      pool = dict()
-      for node in get_hostlist():
-        datadump = get_yaml(node)
-  
-        # Proxy naming convention is special
-        if datadump['nodetype'] == 'Proxy':
-          fqdn = datadump['nodename']
-        else:
-          # By default the full name is listed and also a shortname CNAME for easy use.
-          fqdn = datadump['nodetype'] + datadump['nodename']
-          wleiden_cname[datadump['nodename']] = fqdn
-        wleiden_zone[fqdn] = datadump['masterip']
-
-        #items['node'] = node
-        #items['wdir'] = "./static/%(node)s" % items
-        #if not os.path.isdir(items['wdir']):
-        #  os.makedirs(items['wdir'])
-        for iface_key in get_interface_keys(datadump):
-          iface_name = datadump[iface_key]['interface'].replace(':',"_alias_")
-          (ip, netmask) = datadump[iface_key]['ip'].split('/')
-          try:
-            (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
-            datadump[iface_key]['subnet'] = netmask2subnet(netmask)
-            dhcp_part = ".".join(ip.split('.')[0:3])
-            if ip != datadump['masterip']:
-              wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)] = ip
-            for i in range(int(dhcp_start), int(dhcp_stop) + 1):
-              wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)] = "%s.%s" % (dhcp_part, i)
-          except (AttributeError, ValueError):
-            # First push it into a pool, to indentify the counter-part later on
-            addr = parseaddr(ip)
-            netmask = int(netmask)
-            addr = addr & ~((1 << (32 - netmask)) - 1)
-            if pool.has_key(addr): 
-              pool[addr] += [(iface_name, fqdn, ip)]
-            else: 
-              pool[addr] = [(iface_name, fqdn, ip)]
-            continue
-
-      # wleiden_zone["2%s-%s.%s" % ("unused", iface_name, fqdn)] = ip
-      # #XXX: automatic naming convention namely 2 + remote.lower()
-      for (key,value) in pool.iteritems():
-        if len(value) == 1:
-          (iface_name, fqdn, ip) = value[0]
-          wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)] = ip
-        elif len(value) == 2:
-          (a_iface_name, a_fqdn, a_ip) = value[0]
-          (b_iface_name, b_fqdn, b_ip) = value[1]
-          wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)] = a_ip
-          wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)] = b_ip
-        else:
-          pool_members = [k[1] for k in value]
-          for item in value:
-            (iface_name, fqdn, ip) = item 
-            pool_name = "2pool-" + showaddr(key).replace('.','-') + "-" + "_".join(sorted(list(set(pool_members) - set([fqdn]))))
-            wleiden_zone["%s.%s" % (pool_name, fqdn)] = ip
-          
-        
-      f = open("db.wleiden.net", "w")
-      for host,ip in wleiden_zone.iteritems():
-        f.write("%s.wleiden.net. A %s ~\n" % (host, ip)) 
-      for source,dest in wleiden_cname.iteritems():
-        f.write("%s.wleiden.net. CNAME %s.wleiden.net.\n" % (source, dest))
-      f.close()
-      f = open("db.172.in-addr.arpa", "w")
-      for host,ip in wleiden_zone.iteritems():
-        rev_ip = '.'.join(reversed(ip.split('.')))
-        f.write("%s.in-addr.arpa. PTR %s.wleiden.net. ~\n" % (rev_ip, host)) 
-      f.close()
-      #pprint(pool)
-      #pprint(wleiden_zone)
-        #for config in files:
-        #  items['config'] = config
-        #  print "## Generating %(node)s %(config)s" % items
-        #  f = open("%(wdir)s/%(config)s" % items, "w")
-        #  f.write(generate_config(node, config, datadump))
-        #  f.close()
-    else:
-      usage()
-  else:
-    cgitb.enable()
-    process_cgi_request()
-    
-
-if __name__ == "__main__":
-  main()
+      make_dns()
+    elif sys.argv[1] == "cleanup":    
+      # First generate all datadumps
+      datadumps = dict()
+      for host in get_hostlist():
+        datadump = get_yaml(host)
+        datadumps[get_fqdn(datadump)] = datadump
+      
+        datadump['latitude'], datadump['longitude'] = make_map.rd2etrs(datadump['rdnap_x'], datadump['rdnap_y'])
+        write_yaml(host, datadump)
+    else:                             
+      usage()                         
+  else:                               
+    cgitb.enable()                    
+    process_cgi_request()             
+                                      
+                                  
+if __name__ == "__main__":            
+  main()                              
