fix sliver operational status
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Wed, 5 Dec 2012 17:31:21 +0000 (12:31 -0500)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Wed, 5 Dec 2012 17:31:21 +0000 (12:31 -0500)
sfa/openstack/osaggregate.py
sfa/planetlab/plaggregate.py

index dc00694..4506fe9 100644 (file)
@@ -73,11 +73,21 @@ class OSAggregate:
         instances = self.get_instances(urns)
         if len(instances) == 0:
             raise SliverDoesNotExist("You have not allocated any slivers here")
+
+        # lookup the sliver allocations
+        sliver_ids = [sliver['sliver_id'] for sliver in slivers]
+        constraint = SliverAllocation.sliver_id.in_(sliver_ids)
+        sliver_allocations = dbsession.query(SliverAllocation).filter(constraint)
+        sliver_allocation_dict = {}
+        for sliver_allocation in sliver_allocations:
+            sliver_allocation_dict[sliver_allocation.sliver_id] = sliver_allocation
+
         geni_slivers = []
         rspec_nodes = []
         for instance in instances:
             rspec_nodes.append(self.instance_to_rspec_node(instance))
-            geni_slivers.append(self.instance_to_geni_sliver(instance))
+            geni_sliver = self.instance_to_geni_sliver(instance, sliver_sllocation_dict)
+            geni_slivers.append(geni_sliver)
         version_manager = VersionManager()
         version = version_manager.get_version(version)
         rspec_version = version_manager._get_version(version.type, version.version, 'manifest')
@@ -201,22 +211,32 @@ class OSAggregate:
                          'storage':  str(instance.disk)})
         return sliver   
 
-    def instance_to_geni_sliver(self, instance):
-        op_status = "geni_unknown"
-        state = instance.state.lower()
-        if state == 'active':
-            op_status = 'geni_ready'
-        elif state == 'building': 
-            op_status = 'geni_configuring'
-        elif state == 'failed':
-            op_status =' geni_failed'
-        
+    def instance_to_geni_sliver(self, instance, sliver_allocations = {}):
         sliver_hrn = '%s.%s' % (root_hrn, instance.id)
-        sliver_id = Xrn(sliver_hrn, type='sliver').urn 
+        sliver_id = Xrn(sliver_hrn, type='sliver').urn
+        # set sliver allocation and operational status
+        sliver_allocation = sliver_allocations[sliver_id]
+        if sliver_allocation:
+            allocation_status = sliver_allocation.allocation_state
+            if allocation_status == 'geni_allocated':
+                op_status =  'geni_pending_allocation'
+            elif allocation_status == 'geni_provisioned':
+                state = instance.state.lower()
+                if state == 'active':
+                    op_status = 'geni_ready'
+                elif state == 'building':
+                    op_status = 'geni_notready'
+                elif state == 'failed':
+                    op_status =' geni_failed'
+                else:
+                    op_status = 'geni_unknown'
+            else:
+                allocation_status = 'geni_unallocated'    
         # required fields
         geni_sliver = {'geni_sliver_urn': sliver_id, 
                        'geni_expires': None,
-                       'geni_allocation_status': 'geni_provisioned',
+                       'geni_allocation_status': allocation_status,
                        'geni_operational_status': op_status,
                        'geni_error': None,
                        'plos_created_at': datetime_to_string(utcparse(instance.created)),
index f38dd1c..7cb9ad6 100644 (file)
@@ -246,18 +246,27 @@ class PlAggregate:
             nodes_dict[node['node_id']] = node
         return nodes_dict
 
-    def rspec_node_to_geni_sliver(self, rspec_node):
-        op_status = "geni_unknown"
-        state = rspec_node['boot_state'].lower()
-        if state == 'boot':
-            op_status = 'geni_ready'
-        else:
-            op_status =' geni_failed'
-
-
+    def rspec_node_to_geni_sliver(self, rspec_node, sliver_allocations = {}):
+        if rspec_node['sliver_id'] in sliver_allocations:
+            # set sliver allocation and operational status
+            sliver_allocation = sliver_allocations[rspec_node['sliver_id']]
+            if sliver_allocation:
+                allocation_status = sliver_allocation.allocation_state
+                if allocation_status == 'geni_allocated':
+                    op_status =  'geni_pending_allocation'
+                elif allocation_status == 'geni_provisioned':
+                    if rspec_node['boot_state'] == 'boot':
+                        op_status = 'geni_ready'
+                    else:
+                        op_status = 'geni_failed'
+                else:
+                    op_status = 'geni_unknown'
+            else:
+                allocation_status = 'geni_unallocated'    
         # required fields
         geni_sliver = {'geni_sliver_urn': rspec_node['sliver_id'],
                        'geni_expires': rspec_node['expires'],
+                       'geni_allocation_status' : allocation_status,
                        'geni_operational_status': op_status,
                        'geni_error': None,
                        }
@@ -379,14 +388,8 @@ class PlAggregate:
                 rspec_node = self.sliver_to_rspec_node(sliver, sites, interfaces, node_tags, pl_initscripts)
                 # manifest node element shouldn't contain available attribute
                 rspec_node.pop('available')
-                geni_sliver = self.rspec_node_to_geni_sliver(rspec_node)
-                sliver_allocation_record = sliver_allocation_dict.get(sliver['sliver_id'])
-                if sliver_allocation_record:
-                    sliver_allocation = sliver_allocation_record.allocation_state
-                else:
-                    sliver_allocation = 'geni_unallocated'
-                geni_sliver['geni_allocation_status'] = sliver_allocation
                 rspec_nodes.append(rspec_node) 
+                geni_sliver = self.rspec_node_to_geni_sliver(rspec_node, sliver_allocation_dict)
                 geni_slivers.append(geni_sliver)
             rspec.version.add_nodes(rspec_nodes)