observer for sites, slices and users
diff --git a/planetstack/core/admin.py b/planetstack/core/admin.py
index 2f44f40..0c90b9a 100644
--- a/planetstack/core/admin.py
+++ b/planetstack/core/admin.py
@@ -126,24 +126,7 @@
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 @@
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 @@
# 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 @@
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')
diff --git a/planetstack/core/models/site.py b/planetstack/core/models/site.py
index db4b1ef..81bf5f3 100644
--- a/planetstack/core/models/site.py
+++ b/planetstack/core/models/site.py
@@ -19,22 +19,6 @@
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')
diff --git a/planetstack/core/models/slice.py b/planetstack/core/models/slice.py
index cc7dcce..d6908a7 100644
--- a/planetstack/core/models/slice.py
+++ b/planetstack/core/models/slice.py
@@ -31,19 +31,8 @@
# 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')
diff --git a/planetstack/core/models/user.py b/planetstack/core/models/user.py
index f00d5eb..758bcbf 100644
--- a/planetstack/core/models/user.py
+++ b/planetstack/core/models/user.py
@@ -68,6 +68,10 @@
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 @@
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)
diff --git a/planetstack/openstack/observer.py b/planetstack/openstack/observer.py
index ef09a11..02b0b9c 100644
--- a/planetstack/openstack/observer.py
+++ b/planetstack/openstack/observer.py
@@ -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 @@
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 @@
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 @@
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
index a57fa0b..0000000
--- a/planetstack/openstack/siteagent.py
+++ /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()
-
diff --git a/planetstack/plstackapi-debug-server.py b/planetstack/plstackapi-debug-server.py
index 2ed2b28..a78cfe7 100644
--- a/planetstack/plstackapi-debug-server.py
+++ b/planetstack/plstackapi-debug-server.py
@@ -6,7 +6,7 @@
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 @@
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()