introduce Controller model
Tony Mack [Sun, 30 Nov 2014 20:33:35 +0000 (15:33 -0500)]
14 files changed:
planetstack/core/models/__init__.py
planetstack/core/models/controlleruser.py [new file with mode: 0644]
planetstack/core/models/image.py
planetstack/core/models/network.py
planetstack/core/models/node.py
planetstack/core/models/serviceclass.py
planetstack/core/models/serviceresource.py
planetstack/core/models/site.py
planetstack/core/models/slice.py
planetstack/core/models/sliver.py
planetstack/core/models/user.py
planetstack/core/models/userdeployments.py [deleted file]
planetstack/openstack_observer/steps/sync_controller_images.py [new file with mode: 0644]
planetstack/openstack_observer/steps/sync_controller_networks.py [new file with mode: 0644]

index 2070e16..ae5c89b 100644 (file)
@@ -5,15 +5,14 @@ from .service import Service
 from .service import ServiceAttribute
 from .tag import Tag
 from .role import Role
-from .site import Site,Deployment, DeploymentRole, DeploymentPrivilege, SiteDeployments
+from .site import Site,Deployment, DeploymentRole, DeploymentPrivilege, SiteDeployments, ControllerSites
 from .dashboard import DashboardView
 from .user import User, UserDashboardView
 from .serviceclass import ServiceClass
-from .site import DeploymentLinkManager,DeploymentLinkDeletionManager
-from .slice import Slice, SliceDeployments
-from .site import SitePrivilege, SiteDeployments
-from .userdeployments import UserDeployments
-from .image import Image, ImageDeployments
+from .site import ControllerLinkManager,ControllerLinkDeletionManager
+from .slice import Slice, ControllerSlices
+from .controllerusers import ControllerUsers
+from .image import Image, ControllerImages
 from .node import Node
 from .serviceresource import ServiceResource
 from .slice import SliceRole
@@ -27,6 +26,6 @@ from .flavor import Flavor
 from .sliver import Sliver
 from .reservation import ReservedResource
 from .reservation import Reservation
-from .network import Network, NetworkParameterType, NetworkParameter, NetworkSliver, NetworkTemplate, Router, NetworkSlice, NetworkDeployments
+from .network import Network, NetworkParameterType, NetworkParameter, NetworkSliver, NetworkTemplate, Router, NetworkSlice, ControllerNetworks
 from .billing import Account, Invoice, Charge, UsableObject, Payment
 
diff --git a/planetstack/core/models/controlleruser.py b/planetstack/core/models/controlleruser.py
new file mode 100644 (file)
index 0000000..5a3568a
--- /dev/null
@@ -0,0 +1,26 @@
+import os
+import datetime
+from collections import defaultdict
+from django.db import models
+from django.db.models import F, Q
+from core.models import PlCoreBase,User,Controller
+from core.models import Controller,ControllerLinkManager,ControllerLinkDeletionManager
+
+class ControllerUsers(PlCoreBase):
+    objects = ControllerLinkManager()
+    deleted_objects = ControllerLinkDeletionManager()
+
+    user = models.ForeignKey(User,related_name='controllerusers')
+    controller = models.ForeignKey(Controller,related_name='controllersusers')
+    kuser_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone user id")
+
+    def __unicode__(self):  return u'%s %s' % (self.controller, self.user)
+
+    @staticmethod
+    def select_by_user(user):
+        if user.is_admin:
+            qs = ControllerUsers.objects.all()
+        else:
+            users = Users.select_by_user(user)
+            qs = ControllerUsers.objects.filter(user__in=users)
+        return qs
index fdeb2cc..a7b63b6 100644 (file)
@@ -1,8 +1,7 @@
 import os
 from django.db import models
 from core.models import PlCoreBase
-from core.models import Deployment
-from core.models import Deployment,DeploymentLinkManager,DeploymentLinkDeletionManager
+from core.models import Controller,ControllerLinkManager,ControllerLinkDeletionManager
 
 # Create your models here.
 
@@ -14,13 +13,13 @@ class Image(PlCoreBase):
 
     def __unicode__(self):  return u'%s' % (self.name)
 
-class ImageDeployments(PlCoreBase):
-    objects = DeploymentLinkManager()
-    deleted_objects = DeploymentLinkDeletionManager()
-    image = models.ForeignKey(Image,related_name='imagedeployments')
-    deployment = models.ForeignKey(Deployment,related_name='imagedeployments')
+class ControllerImages(PlCoreBase):
+    objects = ControllerLinkManager()
+    deleted_objects = ControllerLinkDeletionManager()
+    image = models.ForeignKey(Image,related_name='controllerimages')
+    controller = models.ForeignKey(Controller,related_name='controllerimages')
     glance_image_id = models.CharField(null=True, blank=True, max_length=200, help_text="Glance image id") 
 
-    def __unicode__(self):  return u'%s %s' % (self.image, self.deployment)
+    def __unicode__(self):  return u'%s %s' % (self.image, self.controller)
 
     
index 0b3400a..d2e6411 100644 (file)
@@ -1,8 +1,8 @@
 import os
 import socket
 from django.db import models
-from core.models import PlCoreBase, Site, Slice, Sliver, Deployment
-from core.models import DeploymentLinkManager,DeploymentLinkDeletionManager
+from core.models import PlCoreBase, Site, Slice, Sliver, Controller
+from core.models import ControllerLinkManager,ControllerLinkDeletionManager
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
 from django.core.exceptions import ValidationError
@@ -128,13 +128,13 @@ class Network(PlCoreBase):
             qs = Network.objects.filter(owner__in=slices)
         return qs
 
-class NetworkDeployments(PlCoreBase):
-    objects = DeploymentLinkManager()
-    deleted_objects = DeploymentLinkDeletionManager()
+class ControllerNetworks(PlCoreBase):
+    objects = ControllerLinkManager()
+    deleted_objects = ControllerLinkDeletionManager()
 
-    # Stores the openstack ids at various deployments
-    network = models.ForeignKey(Network, related_name='networkdeployments')
-    deployment = models.ForeignKey(Deployment, related_name='networkdeployments')
+    # Stores the openstack ids at various controllers
+    network = models.ForeignKey(Network, related_name='controllernetworks')
+    controller = models.ForeignKey(Controller, related_name='controllernetworks')
     net_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
     router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
     subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
@@ -146,11 +146,11 @@ class NetworkDeployments(PlCoreBase):
     @staticmethod
     def select_by_user(user):
         if user.is_admin:
-            qs = NetworkDeployments.objects.all()
+            qs = NetworkControllers.objects.all()
         else:
             slices = Slice.select_by_user(user)
             networks = Network.objects.filter(owner__in=slices)
-            qs = NetworkDeployments.objects.filter(network__in=networks)
+            qs = NetworkControllers.objects.filter(network__in=networks)
         return qs
 
 class NetworkSlice(PlCoreBase):
index 9271268..903c25f 100644 (file)
@@ -1,7 +1,7 @@
 import os
 from django.db import models
 from core.models import PlCoreBase
-from core.models import Site,Deployment
+from core.models import SiteDeployment, Controller
 from core.models import Tag
 from django.contrib.contenttypes import generic
 
@@ -9,8 +9,7 @@ from django.contrib.contenttypes import generic
 
 class Node(PlCoreBase):
     name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
-    site  = models.ForeignKey(Site, related_name='nodes')
-    deployment = models.ForeignKey(Deployment, related_name='nodes')
+    site_deployment = models.ForeignKey(SiteDeployment, related_name='nodes')
     tags = generic.GenericRelation(Tag)
 
     def __unicode__(self):  return u'%s' % (self.name)
index c339b67..4268568 100644 (file)
@@ -1,8 +1,6 @@
 import os
 from django.db import models
 from core.models import PlCoreBase
-from core.models import Site
-from core.models import Deployment
 
 def get_default_serviceclass():
     try:
index 2f88dc9..d5c86cd 100644 (file)
@@ -1,8 +1,6 @@
 import os
 from django.db import models
 from core.models import PlCoreBase
-from core.models import Site
-from core.models import Deployment
 from core.models import ServiceClass
 
 # Create your models here.
index 2404e34..5381a08 100644 (file)
@@ -10,7 +10,7 @@ from planetstack.config import Config
 
 config = Config()
 
-class DeploymentLinkDeletionManager(PlCoreBaseDeletionManager):
+class ControllerLinkDeletionManager(PlCoreBaseDeletionManager):
     def get_queryset(self):
         parent=super(DeploymentLinkDeletionManager, self)
         try:
@@ -20,7 +20,7 @@ class DeploymentLinkDeletionManager(PlCoreBaseDeletionManager):
 
         parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
         if (backend_type):
-            return parent_queryset.filter(Q(deployment__backend_type=backend_type))
+            return parent_queryset.filter(Q(controller__backend_type=backend_type))
         else:
             return parent_queryset
 
@@ -29,9 +29,9 @@ class DeploymentLinkDeletionManager(PlCoreBaseDeletionManager):
         return self.get_queryset()
 
 
-class DeploymentDeletionManager(PlCoreBaseDeletionManager):
+class ControllerDeletionManager(PlCoreBaseDeletionManager):
     def get_queryset(self):
-        parent=super(DeploymentDeletionManager, self)
+        parent=super(ControllerDeletionManager, self)
 
         try:
             backend_type = config.observer_backend_type
@@ -49,9 +49,9 @@ class DeploymentDeletionManager(PlCoreBaseDeletionManager):
     def get_query_set(self):
         return self.get_queryset()
 
-class DeploymentLinkManager(PlCoreBaseManager):
+class ControllerLinkManager(PlCoreBaseManager):
     def get_queryset(self):
-        parent=super(DeploymentLinkManager, self)
+        parent=super(ControllerLinkManager, self)
 
         try:
             backend_type = config.observer_backend_type
@@ -61,7 +61,7 @@ class DeploymentLinkManager(PlCoreBaseManager):
         parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
 
         if backend_type:
-            return parent_queryset.filter(Q(deployment__backend_type=backend_type))
+            return parent_queryset.filter(Q(controller__backend_type=backend_type))
         else:
             return parent_queryset
 
@@ -70,7 +70,7 @@ class DeploymentLinkManager(PlCoreBaseManager):
         return self.get_queryset()
 
 
-class DeploymentManager(PlCoreBaseManager):
+class ControllerManager(PlCoreBaseManager):
     def get_queryset(self):
         parent=super(DeploymentManager, self)
 
@@ -166,15 +166,15 @@ class SitePrivilege(PlCoreBase):
         return qs
 
 class Deployment(PlCoreBase):
-    objects = DeploymentManager()
-    deleted_objects = DeploymentDeletionManager()
+    #objects = DeploymentManager()
+    #deleted_objects = DeploymentDeletionManager()
     name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
-    admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
-    admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")\r
-    admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")\r
-    auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
-    backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version")
-    availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
+    #admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
+    #admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")\r
+    #admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")\r
+    #auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
+    #backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version")
+    #availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
 
     # smbaker: the default of 'allow all' is intended for evolutions of existing
     #    deployments. When new deployments are created via the GUI, they are
@@ -216,7 +216,7 @@ class Deployment(PlCoreBase):
 
     def __unicode__(self):  return u'%s' % (self.name)
 
-class DeploymentRole(PlCoreBase):
+class ControllerRole(PlCoreBase):
     #objects = DeploymentLinkManager()
     #deleted_objects = DeploymentLinkDeletionManager()
 
@@ -225,45 +225,63 @@ class DeploymentRole(PlCoreBase):
 
     def __unicode__(self):  return u'%s' % (self.role)
 
-class DeploymentPrivilege(PlCoreBase):
-    objects = DeploymentLinkManager()
-    deleted_objects = DeploymentLinkDeletionManager()
+class ControllerPrivilege(PlCoreBase):
+    objects = ControllerLinkManager()
+    deleted_objects = ControllerLinkDeletionManager()
 
-    user = models.ForeignKey('User', related_name='deploymentprivileges')
-    deployment = models.ForeignKey('Deployment', related_name='deploymentprivileges')
-    role = models.ForeignKey('DeploymentRole',related_name='deploymentprivileges')
+    user = models.ForeignKey('User', related_name='controllerprivileges')
+    controller = models.ForeignKey('Controller', related_name='controllerprivileges')
+    role = models.ForeignKey('ControllerRole',related_name='controllerprivileges')
 
-    def __unicode__(self):  return u'%s %s %s' % (self.deployment, self.user, self.role)
+    def __unicode__(self):  return u'%s %s %s' % (self.controller, self.user, self.role)
 
     def can_update(self, user):
         if user.is_readonly:
             return False
         if user.is_admin:
             return True
-        dprivs = DeploymentPrivilege.objects.filter(user=user)
-        for dpriv in dprivs:
-            if dpriv.role.role == 'admin':
+        cprivs = ControllerPrivilege.objects.filter(user=user)
+        for cpriv in dprivs:
+            if cpriv.role.role == 'admin':
                 return True
         return False
 
     @staticmethod
     def select_by_user(user):
         if user.is_admin:
-            qs = DeploymentPrivilege.objects.all()
+            qs = ControllerPrivilege.objects.all()
         else:
-            dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
-            qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
+            cpriv_ids = [cp.id for cp in ControllerPrivilege.objects.filter(user=user)]
+            qs = ControllerPrivilege.objects.filter(id__in=cpriv_ids)
         return qs 
 
 class SiteDeployments(PlCoreBase):
-    objects = DeploymentLinkManager()
-    deleted_objects = DeploymentLinkDeletionManager()
+    #objects = DeploymentLinkManager()
+    #deleted_objects = DeploymentLinkDeletionManager()
+    objects = ControllerManager()
+    deleted_objects = ControllerDeletionManager()
 
     site = models.ForeignKey(Site,related_name='sitedeployments')
     deployment = models.ForeignKey(Deployment,related_name='sitedeployments')
-    tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")    
+    availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
+    #tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")    
+    def __unicode__(self):  return u'%s %s' % (self.deployment, self.site)
+
+class Controller(PlCoreBase):
+    site_deployment = models.ForeignKey(SiteDeployments,related_name='controller')
+
+    backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of compute controller, e.g. EC2, OpenStack, or OpenStack version")
+    auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the compute controller")
+    admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this controller")
+    admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this controller")
+    admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
+
+    def __unicode__(self):  return u'%s %s' % (self.site_deployment, self.backend_type)
 
-    #class Meta:
-    #    db_table = 'core_site_deployments'
-    #    #auto_created = Site
+class ControllerSites(PlCoreBase):
+    objects = ControllerLinkManager()
+    deleted_objects = ControllerLinkDeletionManager() 
 
+    controller = models.ForeignKey(Controller, related_name='controllersites')
+    site_deployment = models.ForeignKey(SiteDeployments, related_name='controllersites')
+    tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
index 6e1d163..8854a75 100644 (file)
@@ -5,13 +5,13 @@ from core.models import Site
 from core.models.site import SitePrivilege
 from core.models import User
 from core.models import Role
-from core.models import Deployment,DeploymentLinkManager,DeploymentLinkDeletionManager
+from core.models import Controller,ControllerLinkManager,ControllerLinkDeletionManager
 from core.models import ServiceClass
 from core.models.serviceclass import get_default_serviceclass
 from core.models import Tag
 from django.contrib.contenttypes import generic
 from core.models import Service
-from core.models import Deployment
+from core.models import Controller
 from django.core.exceptions import ValidationError
 
 # Create your models here.
@@ -92,9 +92,9 @@ class Slice(PlCoreBase):
         from core.models.network import Network
         nets = Network.objects.filter(slices=self)
         nets.delete() 
-        # delete slice deployments
-        slice_deployments = SliceDeployments.objects.filter(slice=self)
-        slice_deployments.delete()
+        # delete slice controllers
+        slice_controllers = ControllerSlices.objects.filter(slice=self)
+        slice_controllers.delete()
         # delete slice privilege
         slice_privileges = SlicePrivilege.objects.filter(slice=self)
         slice_privileges.delete() 
@@ -128,26 +128,23 @@ class SlicePrivilege(PlCoreBase):
             qs = SlicePrivilege.objects.filter(id__in=sp_ids)
         return qs
 
-class SliceDeployments(PlCoreBase):
-    objects = DeploymentLinkManager()
-    deleted_objects = DeploymentLinkDeletionManager()
+class ControllerSlices(PlCoreBase):
+    objects = ControllerLinkManager()
+    deleted_objects = ControllerLinkDeletionManager()
 
-    slice = models.ForeignKey(Slice, related_name='slicedeployments')
-    deployment = models.ForeignKey(Deployment, related_name='slicedeployments')
+    controller = models.ForeignKey(Controller, related_name='controllerslices')
+    slice = models.ForeignKey(Slice, related_name='controllerslices')
     tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
-    network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
-    router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
-    subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
 
-    def __unicode__(self):  return u'%s %s'  % (self.slice, self.deployment)
+    def __unicode__(self):  return u'%s %s'  % (self.slice, self.controller)
 
     @staticmethod
     def select_by_user(user):
         if user.is_admin:
-            qs = SliceDeployments.objects.all()
+            qs = ControllerSlices.objects.all()
         else:
             slices = Slice.select_by_user(user)
-            qs = SliceDeployments.objects.filter(slice__in=slices)
+            qs = ControllerSlices.objects.filter(slice__in=slices)
         return qs    
 
     def get_cpu_stats(self):
index 601afbb..c4789d6 100644 (file)
@@ -46,7 +46,7 @@ class SliverDeletionManager(PlCoreBaseDeletionManager):
 
         parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
         if (backend_type):
-            return parent_queryset.filter(Q(node__deployment__backend_type=backend_type))
+            return parent_queryset.filter(Q(node__controller__backend_type=backend_type))
         else:
             return parent_queryset
 
@@ -67,7 +67,7 @@ class SliverManager(PlCoreBaseManager):
         parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
 
         if backend_type:
-            return parent_queryset.filter(Q(node__deployment__backend_type=backend_type))
+            return parent_queryset.filter(Q(node__controller__backend_type=backend_type))
         else:
             return parent_queryset
 
index 7063f4f..42ea652 100644 (file)
@@ -4,7 +4,6 @@ from collections import defaultdict
 from django.db import models
 from django.db.models import F, Q
 from core.models import PlCoreBase,Site, DashboardView, DiffModelMixIn
-from core.models.site import Deployment
 from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
 from timezones.fields import TimeZoneField
 from operator import itemgetter, attrgetter
diff --git a/planetstack/core/models/userdeployments.py b/planetstack/core/models/userdeployments.py
deleted file mode 100644 (file)
index d8051bf..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-import os
-import datetime
-from collections import defaultdict
-from django.db import models
-from django.db.models import F, Q
-from core.models import PlCoreBase,Site,User,Deployment
-from core.models import Deployment,DeploymentLinkManager,DeploymentLinkDeletionManager
-
-class UserDeployments(PlCoreBase):
-    objects = DeploymentLinkManager()
-    deleted_objects = DeploymentLinkDeletionManager()
-
-    user = models.ForeignKey(User,related_name='userdeployments')
-    deployment = models.ForeignKey(Deployment,related_name='userdeployments')
-    kuser_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone user id")
-
-    def __unicode__(self):  return u'%s %s' % (self.user, self.deployment.name)
-
-    @staticmethod
-    def select_by_user(user):
-        if user.is_admin:
-            qs = UserDeployments.objects.all()
-        else:
-            users = Users.select_by_user(user)
-            qs = Usereployments.objects.filter(user__in=slices)
-        return qs
diff --git a/planetstack/openstack_observer/steps/sync_controller_images.py b/planetstack/openstack_observer/steps/sync_controller_images.py
new file mode 100644 (file)
index 0000000..20c22a2
--- /dev/null
@@ -0,0 +1,77 @@
+import os
+import base64
+from collections import defaultdict
+from django.db.models import F, Q
+from planetstack.config import Config
+from observer.openstacksyncstep import OpenStackSyncStep
+from core.models import Deployment
+from core.models import Image, ImageDeployments
+from util.logger import Logger, logging
+\r
+logger = Logger(level=logging.INFO)
+
+class SyncImageDeployments(OpenStackSyncStep):
+    provides=[ImageDeployments]
+    requested_interval=0
+
+    def fetch_pending(self, deleted):
+        if (deleted):
+            return []
+         # smbaker: commented out automatic creation of ImageDeployments as
+         #    as they will now be configured in GUI. Not sure if this is
+         #    sufficient.
+
+#        # ensure images are available across all deployments
+#        image_deployments = ImageDeployments.objects.all()
+#        image_deploy_lookup = defaultdict(list)
+#        for image_deployment in image_deployments:
+#            image_deploy_lookup[image_deployment.image].append(image_deployment.deployment)
+#
+#        all_deployments = Deployment.objects.all()
+#        for image in Image.objects.all():
+#            expected_deployments = all_deployments
+#            for expected_deployment in expected_deployments:
+#                if image not in image_deploy_lookup or \
+#                  expected_deployment not in image_deploy_lookup[image]:
+#                    id = ImageDeployments(image=image, deployment=expected_deployment)
+#                    id.save()
+
+        # now we return all images that need to be enacted
+        return ImageDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
+
+    def sync_record(self, image_deployment):
+        logger.info("Working on image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
+        driver = self.driver.admin_driver(deployment=image_deployment.deployment.name)
+        images = driver.shell.glance.get_images()
+        glance_image = None
+        for image in images:
+            if image['name'] == image_deployment.image.name:
+                glance_image = image
+                break
+        if glance_image:
+            logger.info("Found image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
+            image_deployment.glance_image_id = glance_image['id']
+        elif image_deployment.image.path:
+            image = {
+                'name': image_deployment.image.name,
+                'is_public': True,
+                'disk_format': 'raw',
+                'container_format': 'bare',
+                'file': image_deployment.image.path,
+            }
+
+            logger.info("Creating image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
+
+            glance_image = driver.shell.glanceclient.images.create(name=image_deployment.image.name,
+                                                                   is_public=True,
+                                                                   disk_format='raw',
+                                                                   container_format='bare')
+            glance_image.update(data=open(image_deployment.image.path, 'rb'))
+
+            # While the images returned by driver.shell.glance.get_images()
+            #   are dicts, the images returned by driver.shell.glanceclient.images.create
+            #   are not dicts. We have to use getattr() instead of [] operator.
+            if not glance_image or not getattr(glance_image,"id",None):
+                raise Exception, "Add image failed at deployment %s" % image_deployment.deployment.name
+            image_deployment.glance_image_id = getattr(glance_image, "id")
+        image_deployment.save()
diff --git a/planetstack/openstack_observer/steps/sync_controller_networks.py b/planetstack/openstack_observer/steps/sync_controller_networks.py
new file mode 100644 (file)
index 0000000..a6fc389
--- /dev/null
@@ -0,0 +1,136 @@
+import os
+import base64
+from collections import defaultdict
+from netaddr import IPAddress, IPNetwork
+from django.db.models import F, Q
+from planetstack.config import Config
+from observer.openstacksyncstep import OpenStackSyncStep
+from core.models.network import *
+from core.models.slice import *
+from core.models.sliver import Sliver
+from util.logger import Logger, logging
+
+logger = Logger(level=logging.INFO)
+
+class SyncNetworkDeployments(OpenStackSyncStep):
+    requested_interval = 0
+    provides=[Network, NetworkDeployments, Sliver]
+
+    def fetch_pending(self, deleted):
+        if (deleted):
+            return NetworkDeployments.deleted_objects.all()
+        else:
+            return NetworkDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
+
+    def get_next_subnet(self, deployment=None):
+        # limit ourself to 10.0.x.x for now
+        valid_subnet = lambda net: net.startswith('10.0')
+
+        driver = self.driver.admin_driver(deployment=deployment,tenant='admin')
+        subnets = driver.shell.quantum.list_subnets()['subnets']
+        ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
+                if valid_subnet(subnet['cidr'])]
+        ints.sort()
+        if ints:
+            last_ip = IPAddress(ints[-1])
+        else:
+            last_ip = IPAddress('10.0.0.0')
+        last_network = IPNetwork(str(last_ip) + "/24")
+        next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
+        return next_network
+
+    def save_network_deployment(self, network_deployment):
+        if (not network_deployment.net_id) and network_deployment.network.template.sharedNetworkName:
+            # It's a shared network, try to find the shared network id
+
+            quantum_networks = self.driver.shell.quantum.list_networks(name=network_deployment.network.template.sharedNetworkName)["networks"]
+            if quantum_networks:
+                logger.info("set shared network id %s" % quantum_networks[0]["id"])
+                network_deployment.net_id = quantum_networks[0]["id"]
+            else:
+                logger.info("failed to find shared network id for deployment")
+                return
+
+        # At this point, it must be a private network, so create it if it does
+        # not exist.
+
+        if not network_deployment.net_id:
+            network_name = network_deployment.network.name
+
+            # create network
+            os_network = self.driver.create_network(network_name, shared=True)
+            network_deployment.net_id = os_network['id']
+
+            # create router
+            #router = self.driver.create_router(network_name)
+            #network_deployment.router_id = router['id']
+
+            # create subnet
+            next_subnet = self.get_next_subnet(deployment=network_deployment.deployment.name)
+            cidr = str(next_subnet.cidr)
+            ip_version = next_subnet.version
+            start = str(next_subnet[2])
+            end = str(next_subnet[-2])
+            subnet = self.driver.create_subnet(name=network_name,
+                                               network_id = network_deployment.net_id,
+                                               cidr_ip = cidr,
+                                               ip_version = ip_version,
+                                               start = start,
+                                               end = end)
+            network_deployment.subnet = cidr
+            network_deployment.subnet_id = subnet['id']
+            # add subnet as interface to slice's router
+            #self.driver.add_router_interface(router['id'], subnet['id'])
+            # add external route
+            #self.driver.add_external_route(subnet)
+            logger.info("created private subnet (%s) for network: %s" % (cidr, network_deployment.network))
+
+        # Now, figure out the subnet and subnet_id for the network. This works
+        # for both private and shared networks.
+
+        if (not network_deployment.subnet_id) or (not network_deployment.subnet):
+            (network_deployment.subnet_id, network_deployment.subnet) = self.driver.get_network_subnet(network_deployment.net_id)
+            logger.info("sync'ed subnet (%s) for network: %s" % (network_deployment.subnet, network_deployment.network))
+
+        if (not network_deployment.subnet):
+            # this will generate a non-null database constraint error
+            #   ... which in turn leads to transaction errors
+            # it's probably caused by networks that no longer exist at the
+            # quantum level.
+
+            logger.info("null subnet for network %s, skipping save" % network_deployment.network)
+            return
+
+        network_deployment.save()
+
+    def sync_record(self, network_deployment):
+        logger.info("sync'ing network deployment %s for network %s slice %s deployment %s" % (network_deployment, network_deployment.network, str(network_deployment.network.owner), network_deployment.deployment))
+
+        if not network_deployment.deployment.admin_user:
+            logger.info("deployment %r has no admin_user, skipping" % network_deployment.deployment)
+            return
+
+        self.driver = self.driver.admin_driver(deployment=network_deployment.deployment,tenant='admin')
+        if network_deployment.network.owner and network_deployment.network.owner.creator:
+            try:
+                # update manager context
+               # Bring back
+                self.save_network_deployment(network_deployment)
+                logger.info("saved network deployment: %s" % (network_deployment))
+            except Exception,e:
+                logger.log_exc("save network deployment failed: %s" % network_deployment)
+                raise e
+
+
+    def delete_record(self, network_deployment):
+        driver = OpenStackDriver().client_driver(caller=network_deployment.network.owner.creator,
+                                                 tenant=network_deployment.network.owner.name,
+                                                 deployment=network_deployment.deployment.name)
+        if (network_deployment.router_id) and (network_deployment.subnet_id):
+            driver.delete_router_interface(network_deployment.router_id, network_deployment.subnet_id)
+        if network_deployment.subnet_id:
+            driver.delete_subnet(network_deployment.subnet_id)
+        if network_deployment.router_id:
+            driver.delete_router(network_deployment.router_id)
+        if network_deployment.net_id:
+            driver.delete_network(network_deployment.net_id)