From 11d6d1bb6ef4c11f6e023cc3859b9c04f6062a09 Mon Sep 17 00:00:00 2001
From: Joshua McKenty <jmckenty@joshua-mckentys-macbook-pro.local>
Date: Wed, 7 Jul 2010 12:15:11 -0700
Subject: [PATCH] Got dhcpleasor working, with test ENV for testing, and
 rpc.cast for real world.

---
 bin/dhcpleasor.py | 36 +++++++++++++++++++++++++++---------
 docs/conf.py      |  2 +-
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/bin/dhcpleasor.py b/bin/dhcpleasor.py
index 07e63884..63ed5242 100755
--- a/bin/dhcpleasor.py
+++ b/bin/dhcpleasor.py
@@ -27,32 +27,50 @@ sys.path.append(os.path.abspath(os.path.join(__file__, "../../")))
 logging.debug(sys.path)
 import getopt
 from os import environ
+from nova.compute import linux_net
 from nova.compute import network
+from nova import rpc
+
 from nova import flags
 FLAGS = flags.FLAGS
 
 
 def add_lease(mac, ip, hostname, interface):
-    pass
+    if FLAGS.fake_rabbit:
+        network.lease_ip(ip)     
+    else:
+        rpc.cast(FLAGS.cloud_topic, {"method": "lease_ip",
+                "args" : {"address": ip}})
 
 def old_lease(mac, ip, hostname, interface):
-    pass
+    logging.debug("Adopted old lease or got a change of mac/hostname")
     
 def del_lease(mac, ip, hostname, interface):
-    # TODO - get net from interface instead
-    net = network.get_network_by_address(ip)
-    net.release_ip(ip)    
+    if FLAGS.fake_rabbit:
+        network.release_ip(ip)     
+    else:
+        rpc.cast(FLAGS.cloud_topic, {"method": "release_ip",
+                "args" : {"address": ip}})   
     
 def init_leases(interface):
-    return ""
+    net = network.get_network_by_interface(interface)
+    res = ""
+    for host_name in net.hosts:
+        res += "%s\n" % linux_net.hostDHCP(net, host_name, net.hosts[host_name])
+    return res
 
 
 def main(argv=None):
     if argv is None:
         argv = sys.argv
     interface = environ.get('DNSMASQ_INTERFACE', 'br0')
-    old_redis_db = FLAGS.redis_db
-    FLAGS.redis_db = int(environ.get('REDIS_DB', '0'))
+    if int(environ.get('TESTING', '0')):
+        FLAGS.fake_rabbit = True
+        FLAGS.redis_db = 8
+        FLAGS.network_size = 32
+        FLAGS.fake_libvirt=True
+        FLAGS.fake_network=True
+        FLAGS.fake_users = True
     action = argv[1]
     if action in ['add','del','old']:
         mac = argv[2]
@@ -62,7 +80,7 @@ def main(argv=None):
         globals()[action+'_lease'](mac, ip, hostname, interface)
     else:
         print init_leases(interface)
-    FLAGS.redis_db = old_redis_db
+    exit(0)
 
 if __name__ == "__main__":
     sys.exit(main())
diff --git a/docs/conf.py b/docs/conf.py
index 9dfdfc8b..bc61f438 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -16,7 +16,7 @@ import sys, os
 # If extensions (or modules to document with autodoc) are in another directory,
 # add these directories to sys.path here. If the directory is relative to the
 # documentation root, use os.path.abspath to make it absolute, like shown here.
-#sys.path.append(os.path.abspath('.'))
+sys.path.append(os.path.abspath('/Users/jmckenty/Projects/cc'))
 sys.path.append([os.path.abspath('../nova'),os.path.abspath('../'),os.path.abspath('../vendor')])
 from nova import vendor