observer for sites, slices and users
Tony Mack [Tue, 11 Jun 2013 02:54:50 +0000 (22:54 -0400)]
planetstack/core/admin.py
planetstack/core/models/site.py
planetstack/core/models/slice.py
planetstack/core/models/user.py
planetstack/openstack/observer.py
planetstack/openstack/siteagent.py [deleted file]
planetstack/plstackapi-debug-server.py

index 2f44f40..0c90b9a 100644 (file)
@@ -126,24 +126,7 @@ class PlainTextWidget(forms.HiddenInput):
 class PlanetStackBaseAdmin(admin.ModelAdmin):
     save_on_top = False
 
-class OSModelAdmin(PlanetStackBaseAdmin):
-    """Attach client connection to openstack on delete() and save()"""
-
-    def save_model(self, request, obj, form, change):
-        if request.user.site:
-            auth = request.session.get('auth', {})
-            auth['tenant'] = request.user.site.login_base
-            obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
-        obj.save()
-
-    def delete_model(self, request, obj):
-        if request.user.site:
-            auth = request.session.get('auth', {})
-            auth['tenant'] = request.user.site.login_base
-            obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
-        obj.delete() 
-
-class RoleAdmin(OSModelAdmin):
+class RoleAdmin(PlanetStackBaseAdmin):
     fieldsets = [
         ('Role', {'fields': ['role_type']})
     ]
@@ -194,7 +177,7 @@ class DeploymentAdmin(PlanetStackBaseAdmin):
             inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
             yield inline.get_formset(request, obj)
 
-class SiteAdmin(OSModelAdmin):
+class SiteAdmin(PlanetStackBaseAdmin):
     fieldsets = [
         (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
         ('Location', {'fields': ['latitude', 'longitude']}),
@@ -221,10 +204,6 @@ class SiteAdmin(OSModelAdmin):
             # hide MyInline in the add view
             if obj is None:
                 continue
-            # give inline object access to driver and caller
-            auth = request.session.get('auth', {})
-            #auth['tenant'] = request.user.site.login_base
-            inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
             yield inline.get_formset(request, obj)
 
 class SitePrivilegeAdmin(PlanetStackBaseAdmin):
@@ -268,20 +247,6 @@ class SitePrivilegeAdmin(PlanetStackBaseAdmin):
             qs = qs.filter(site__in=sites)
         return qs
 
-    def save_model(self, request, obj, form, change):
-        # update openstack connection to use this site/tenant   
-        auth = request.session.get('auth', {})
-        #auth['tenant'] = obj.site.login_base
-        obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
-        obj.save()
-
-    def delete_model(self, request, obj):
-        # update openstack connection to use this site/tenant   
-        auth = request.session.get('auth', {})
-        #auth['tenant'] = obj.site.login_base
-        obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
-        obj.delete()
-
 class SliceAdmin(OSModelAdmin):
     fields = ['name', 'site', 'serviceClass', 'description', 'slice_url']
     list_display = ('name', 'site','serviceClass', 'slice_url')
index db4b1ef..81bf5f3 100644 (file)
@@ -19,22 +19,6 @@ class Site(PlCoreBase):
 
     def __unicode__(self):  return u'%s' % (self.name)
 
-    def save(self, *args, **kwds):
-        if not hasattr(self, 'os_manager'):
-            from openstack.manager import OpenStackManager
-            setattr(self, 'os_manager', OpenStackManager())
-        self.os_manager.save_site(self)
-        super(Site, self).save(*args, **kwds)               
-
-
-    def delete(self, *args, **kwds):
-        if not hasattr(self, 'os_manager'):
-            from openstack.manager import OpenStackManager
-            setattr(self, 'os_manager', OpenStackManager())
-        self.os_manager.delete_site(self)
-        super(Site, self).delete(*args, **kwds)         
-        
-
 class SitePrivilege(PlCoreBase):
 
     user = models.ForeignKey('User', related_name='site_privileges')
index cc7dcce..d6908a7 100644 (file)
@@ -31,19 +31,8 @@ class Slice(PlCoreBase):
             # will fail unless it is allowed. But, we we really don't want it to
             # ever save None, so fix it up here.
             self.serviceClass = ServiceClass.get_default()
-        if not hasattr(self, 'os_manager'):
-            from openstack.manager import OpenStackManager
-            setattr(self, 'os_manager', OpenStackManager())
-        self.os_manager.save_slice(self)
         super(Slice, self).save(*args, **kwds)
 
-    def delete(self, *args, **kwds):
-        if not hasattr(self, 'os_manager'):
-            from openstack.manager import OpenStackManager
-            setattr(self, 'os_manager', OpenStackManager())
-        self.os_manager.delete_slice(self)
-        super(Slice, self).delete(*args, **kwds)    
-
 class SliceMembership(PlCoreBase):
     user = models.ForeignKey('User', related_name='slice_memberships')
     slice = models.ForeignKey('Slice', related_name='slice_memberships')
index f00d5eb..758bcbf 100644 (file)
@@ -68,6 +68,10 @@ class User(AbstractBaseUser):
     is_admin = models.BooleanField(default=True)
     is_staff = models.BooleanField(default=True)
 
+    created = models.DateTimeField(auto_now_add=True)
+    updated = models.DateTimeField(auto_now=True)
+    enacted = models.DateTimeField(null=True, default=None)
+
     timezone = TimeZoneField()
 
     objects = UserManager()
@@ -114,19 +118,6 @@ class User(AbstractBaseUser):
         return roles   
 
     def save(self, *args, **kwds):
-        if not hasattr(self, 'os_manager'):
-            from openstack.manager import OpenStackManager 
-            setattr(self, 'os_manager', OpenStackManager())
-        self.os_manager.save_user(self)
-
         if not self.id:
             self.set_password(self.password)    
         super(User, self).save(*args, **kwds)   
-
-    def delete(self, *args, **kwds):
-        if not hasattr(self, 'os_manager'):
-            from openstack.manager import OpenStackManager
-            setattr(self, 'os_manager', OpenStackManager())
-        self.os_manager.delete_user(self)
-
-        super(User, self).delete(*args, **kwds)    
index ef09a11..02b0b9c 100644 (file)
@@ -1,4 +1,5 @@
 import time
+import traceback
 from datetime import datetime
 from core.models import *
 from django.db.models import F, Q
@@ -8,7 +9,21 @@ from openstack.manager import OpenStackManager
 class OpenStackObserver:
     
     def __init__(self):
-        self.manager = OpenStackManager() 
+        self.manager = OpenStackManager()
+
+    def run(self):
+        if not self.manager.enabled or not self.manager.has_openstack:
+            return
+        while True:
+            try:
+                #self.sync_roles()
+                self.sync_tenants()
+                self.sync_users()
+                #self.sync_user_tenant_roles()
+                #self.sync_slivers()
+                time.sleep(7)
+            except:
+                traceback.print_exc() 
 
     def sync_tenants(self):
         """
@@ -50,7 +65,8 @@ class OpenStackObserver:
             if tenant.name == 'admin': 
                 continue
             if tenant.name not in site_dict and tenant.name not in slice_dict:
-                print "delete " + tenant.name
+                #print "delete " + tenant.name
+                pass
                 #self.manager.driver.delete_tenant(tenant.id)
 
 
@@ -74,7 +90,7 @@ class OpenStackObserver:
             user_dict[user.kuser_id] = user
 
         # delete keystone users that don't have a user record
-        user = self.manager.driver.shell.keystone.users.findall()
+        users = self.manager.driver.shell.keystone.users.findall()
         for user in users:
             if user.name == 'admin':
                 continue
diff --git a/planetstack/openstack/siteagent.py b/planetstack/openstack/siteagent.py
deleted file mode 100644 (file)
index a57fa0b..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-import os
-import sys
-#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
-import time
-from core.models.site import Site
-from openstack.manager import OpenStackManager    
-
-class SiteAgent:
-    def run(self):
-        manager = OpenStackManager()
-        # exit if openstack is disable or unavailable
-        if manager.enabled and manager.has_openstack:
-            # fill in null tenant ids 
-            sites = Site.objects.filter(tenant_id__in=[None, ''])
-            for site in sites:
-                # calling save() on the model should force the tenant_id to be set
-                site.os_manager = manager
-                site.save() 
-                                        
-if __name__ == '__main__':
-    SiteAgent().run()
-                 
index 2ed2b28..a78cfe7 100644 (file)
@@ -6,7 +6,7 @@ import threading
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
 from planetstack.config import Config 
 from openstack.sliveragent import SliverAgent
-from openstack.siteagent import SiteAgent
+from openstack.observer import OpenStackObserver
 
 if __name__ == '__main__':
 
@@ -16,14 +16,17 @@ if __name__ == '__main__':
     url = "%s:%s" % (config.api_host, config.api_port)
     args = [__file__, 'runserver', url] 
 
-    # run site agent once on startup
-    SiteAgent().run()    
     
     # start the sliver agent thread
     sliver_agent = SliverAgent()
     sliver_agent_thread = threading.Thread(target=sliver_agent.run)
     sliver_agent_thread.start()
 
+    # start the openstack observer
+    observer = OpenStackObserver()
+    observer_thread = threading.Thread(target=observer.run)
+    observer_thread.start()
+
     # start the server
     server = ManagementUtility(args)
     server.execute()