Merge branch 'master' of ssh://git.planet-lab.org/git/plstackapi
diff --git a/planetstack/core/admin.py b/planetstack/core/admin.py
index 39520ae..d78435d 100644
--- a/planetstack/core/admin.py
+++ b/planetstack/core/admin.py
@@ -28,7 +28,7 @@
 
 def backend_icon(obj): # backend_status, enacted, updated):
     #return "%s %s %s" % (str(obj.updated), str(obj.enacted), str(obj.backend_status))
-    if (obj.enacted is not None) and obj.enacted >= obj.updated:
+    if (obj.enacted is not None) and obj.enacted >= obj.updated or obj.backend_status.startswith("1 -"):
         return '<span style="min-width:16px;"><img src="/static/admin/img/icon_success.gif"></span>'
     else:
         if ((obj.backend_status is not None) and obj.backend_status.startswith("0 -")) or obj.backend_status == "Provisioning in progress" or obj.backend_status=="":
diff --git a/planetstack/openstack_observer/event_loop.py b/planetstack/openstack_observer/event_loop.py
index cc0611c..51be5d3 100644
--- a/planetstack/openstack_observer/event_loop.py
+++ b/planetstack/openstack_observer/event_loop.py
@@ -2,11 +2,14 @@
 import imp
 import inspect
 import time
+import sys
 import traceback
 import commands
 import threading
 import json
 import pdb
+import pprint
+
 
 from datetime import datetime
 from collections import defaultdict
@@ -119,6 +122,12 @@
 		try:
 			# This contains dependencies between records, not sync steps
 			self.model_dependency_graph = json.loads(open(dep_path).read())
+			for lst in self.model_dependency_graph.values():
+				for k in lst:
+					try:
+						deps = self.model_dependency_graph[k]
+					except:
+						self.model_dependency_graph[k] = []
 		except Exception,e:
 			raise e
 
@@ -145,6 +154,9 @@
 		for k,v in self.model_dependency_graph.iteritems():
 			try:
 				for source in provides_dict[k]:
+					if (not v):
+						step_graph[source] = []
+		
 					for m in v:
 						try:
 							for dest in provides_dict[m]:
@@ -187,8 +199,14 @@
 		self.dependency_graph = step_graph
 		self.deletion_dependency_graph = invert_graph(step_graph)
 
+		pp = pprint.PrettyPrinter(indent=4)
+		pp.pprint(step_graph)
 		self.ordered_steps = toposort(self.dependency_graph, map(lambda s:s.__name__,self.sync_steps))
+		#self.ordered_steps = ['SyncRoles', 'SyncControllerSites', 'SyncControllerSitePrivileges','SyncImages', 'SyncControllerImages','SyncControllerUsers','SyncControllerUserSitePrivileges','SyncControllerSlices', 'SyncControllerSlicePrivileges', 'SyncControllerUserSlicePrivileges', 'SyncControllerNetworks','SyncSlivers']
+		#self.ordered_steps = ['SyncControllerSites']
+
 		print "Order of steps=",self.ordered_steps
+
 		self.load_run_times()
 		
 
@@ -266,8 +284,9 @@
 		except KeyError:
 			has_deps = False
 
-		go = False
+		go = True
 
+                failed_dep = None
 		if (has_deps):
 			for d in deps:
                                 if d==step.__name__:
@@ -280,12 +299,19 @@
 				if (self.step_status[d] is STEP_STATUS_WORKING):
                                         logger.info("  step %s wait on dep %s" % (step.__name__, d))
 					cond.wait()
+				elif self.step_status[d] == STEP_STATUS_OK:
+					go = True
+				else:
+					go = False
+                        		failed_dep = d
 				cond.release()
-			go = go or self.step_status[d] == STEP_STATUS_OK
+				if (not go):
+					break
 		else:
 			go = True
 
 		if (not go):
+                        print bcolors.FAIL + "Step %r skipped on %r" % (step,failed_dep) + bcolors.ENDC
                         # SMBAKER: sync_step was not defined here, so I changed
                         #    this from 'sync_step' to 'step'. Verify.
 			self.failed_steps.append(step)
@@ -326,6 +352,7 @@
 
 					logger.info('Executing step %s' % sync_step.__name__)
 
+					print bcolors.OKBLUE + "Executing step %s" % sync_step.__name__ + bcolors.ENDC
 					failed_objects = sync_step(failed=list(self.failed_step_objects), deletion=deletion)
 
 					self.check_duration(sync_step, duration)
@@ -334,9 +361,11 @@
 						self.failed_step_objects.update(failed_objects)
 
                                         logger.info("Step %r succeeded" % step)
+                                        print bcolors.OKGREEN + "Step %r succeeded" % step + bcolors.ENDC
 					my_status = STEP_STATUS_OK
 					self.update_run_time(sync_step,deletion)
 				except Exception,e:
+                        		print bcolors.FAIL + "Model step %r failed" % (step) + bcolors.ENDC
 					logger.error('Model step %r failed. This seems like a misconfiguration or bug: %r. This error will not be relayed to the user!' % (step, e))
 					logger.log_exc(e)
 					self.failed_steps.append(S)
diff --git a/planetstack/openstack_observer/steps/sync_controller_images.py b/planetstack/openstack_observer/steps/sync_controller_images.py
index 17ffe57..fa0cad1 100644
--- a/planetstack/openstack_observer/steps/sync_controller_images.py
+++ b/planetstack/openstack_observer/steps/sync_controller_images.py
@@ -12,7 +12,7 @@
 logger = Logger(level=logging.INFO)
 
 class SyncControllerImages(OpenStackSyncStep):
-    provides=[ControllerImages, Image]
+    provides=[ControllerImages]
     requested_interval=0
 
     def fetch_pending(self, deleted):
@@ -37,4 +37,5 @@
 
         image_id = res[0]['id']
         controller_image.glance_image_id = image_id
+	controller_image.backend_status = '1 - OK'
         controller_image.save()
diff --git a/planetstack/openstack_observer/steps/sync_controller_networks.py b/planetstack/openstack_observer/steps/sync_controller_networks.py
index 655b07d..1071ef7 100644
--- a/planetstack/openstack_observer/steps/sync_controller_networks.py
+++ b/planetstack/openstack_observer/steps/sync_controller_networks.py
@@ -15,7 +15,7 @@
 
 class SyncControllerNetworks(OpenStackSyncStep):
     requested_interval = 0
-    provides=[ControllerNetwork, Network]
+    provides=[Network]
 
     def alloc_subnet(self, uuid):
         # 16 bits only
@@ -59,6 +59,7 @@
         controller_network.net_id = network_id
         controller_network.subnet = cidr
         controller_network.subnet_id = subnet_id
+	controller_network.backend_status = '1 - OK'
         controller_network.save()
 
 
diff --git a/planetstack/openstack_observer/steps/sync_controller_site_privileges.py b/planetstack/openstack_observer/steps/sync_controller_site_privileges.py
index c87f6d5..89fa27c 100644
--- a/planetstack/openstack_observer/steps/sync_controller_site_privileges.py
+++ b/planetstack/openstack_observer/steps/sync_controller_site_privileges.py
@@ -14,7 +14,7 @@
 logger = Logger(level=logging.INFO)
 
 class SyncControllerSitePrivileges(OpenStackSyncStep):
-    provides=[ControllerSitePrivilege, SitePrivilege]
+    provides=[SitePrivilege]
     requested_interval=0
 
     def fetch_pending(self, deleted):
diff --git a/planetstack/openstack_observer/steps/sync_controller_sites.py b/planetstack/openstack_observer/steps/sync_controller_sites.py
index 3aa20d7..2dc13da 100644
--- a/planetstack/openstack_observer/steps/sync_controller_sites.py
+++ b/planetstack/openstack_observer/steps/sync_controller_sites.py
@@ -8,7 +8,7 @@
 
 class SyncControllerSites(OpenStackSyncStep):
     requested_interval=0
-    provides=[ControllerSite, Site]
+    provides=[Site]
 
     def fetch_pending(self, deleted=False):
         pending = super(OpenStackSyncStep, self).fetch_pending(deleted)
@@ -29,6 +29,7 @@
 	res = run_template('sync_controller_sites.yaml', tenant_fields, path='controller_sites', expected_num=1)
 
 	controller_site.tenant_id = res[0]['id']
+	controller_site.backend_status = '1 - OK'
         controller_site.save()
             
     def delete_record(self, controller_site):
diff --git a/planetstack/openstack_observer/steps/sync_controller_slice_privileges.py b/planetstack/openstack_observer/steps/sync_controller_slice_privileges.py
index 5ec434c..35d1b99 100644
--- a/planetstack/openstack_observer/steps/sync_controller_slice_privileges.py
+++ b/planetstack/openstack_observer/steps/sync_controller_slice_privileges.py
@@ -14,7 +14,7 @@
 logger = Logger(level=logging.INFO)
 
 class SyncControllerSlicePrivileges(OpenStackSyncStep):
-    provides=[ControllerSlicePrivilege, SlicePrivilege]
+    provides=[SlicePrivilege]
     requested_interval=0
 
     def fetch_pending(self, deleted):
diff --git a/planetstack/openstack_observer/steps/sync_controller_slices.py b/planetstack/openstack_observer/steps/sync_controller_slices.py
index 0e37524..9e47669 100644
--- a/planetstack/openstack_observer/steps/sync_controller_slices.py
+++ b/planetstack/openstack_observer/steps/sync_controller_slices.py
@@ -14,7 +14,7 @@
 logger = Logger(level=logging.INFO)
 
 class SyncControllerSlices(OpenStackSyncStep):
-    provides=[ControllerSlice, Slice]
+    provides=[Slice]
     requested_interval=0
 
     def fetch_pending(self, deleted):
@@ -62,6 +62,7 @@
                     raise Exception('Could not update quota for %s'%controller_slice.slice.name)
                 
             controller_slice.tenant_id = tenant_id
+            controller_slice.backend_status = '1 - OK'
             controller_slice.save()
 
 
diff --git a/planetstack/openstack_observer/steps/sync_controller_users.py b/planetstack/openstack_observer/steps/sync_controller_users.py
index ca2909c..d7d79f7 100644
--- a/planetstack/openstack_observer/steps/sync_controller_users.py
+++ b/planetstack/openstack_observer/steps/sync_controller_users.py
@@ -14,7 +14,7 @@
 logger = Logger(level=logging.INFO)
 
 class SyncControllerUsers(OpenStackSyncStep):
-    provides=[ControllerUser, User]
+    provides=[User]
     requested_interval=0
 
     def fetch_pending(self, deleted):
@@ -70,6 +70,7 @@
 	    res = run_template('sync_controller_users.yaml', user_fields,path='controller_users', expected_num=expected_length)
 
             controller_user.kuser_id = res[0]['id']
+            controller_user.backend_status = '1 - OK'
             controller_user.save()
 
     def delete_record(self, controller_user):
diff --git a/planetstack/openstack_observer/toposort.py b/planetstack/openstack_observer/toposort.py
index a2c9389..ad1797e 100644
--- a/planetstack/openstack_observer/toposort.py
+++ b/planetstack/openstack_observer/toposort.py
@@ -45,19 +45,18 @@
 			try:
 				for m in g[n]:
 					if (m in unmarked):
-						if (m not in stack):
-							add = False
-							stack.insert(0,m)
-						else:
-							# Should not happen, if so there's a loop
-							print 'Loop at %s'%m
+					    add = False
+					    stack.insert(0,m)
 			except KeyError:
 				pass
 			if (add):
-				if (n in steps):
+				if (n in steps and n not in order):
 					order.append(n)
 				item = stack.pop(0)
-				unmarked.remove(item)
+				try:
+					unmarked.remove(item)
+				except ValueError:
+					pass
 
 	noorder = list(set(steps) - set(order))
 	return order + noorder