key is now just an attribute of the user model. Added 'creator' field to sliver model
Tony Mack [Mon, 10 Jun 2013 13:57:45 +0000 (09:57 -0400)]
planetstack/core/admin.py
planetstack/core/models/plcorebase.py
planetstack/core/models/sliver.py
planetstack/core/models/user.py
planetstack/core/serializers.py
planetstack/openstack/client.py
planetstack/openstack/driver.py
planetstack/openstack/manager.py

index d54387f..1dbe0d7 100644 (file)
@@ -32,7 +32,7 @@ class ReadonlyTabularInline(admin.TabularInline):
 
 class SliverInline(admin.TabularInline):
     model = Sliver
-    fields = ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']
+    fields = ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'node', 'deploymentNetwork']
     extra = 0
     #readonly_fields = ['ip', 'instance_name', 'image']
     readonly_fields = ['ip', 'instance_name']
@@ -333,6 +333,7 @@ class SliceAdmin(OSModelAdmin):
             auth = request.session.get('auth', {})
             auth['tenant'] = obj.name       # meed to connect using slice's tenant
             inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
+            inline.model.creator = request.user
             yield inline.get_formset(request, obj)
 
     def get_queryset(self, request):
@@ -469,6 +470,7 @@ class SliverAdmin(PlanetStackBaseAdmin):
         auth = request.session.get('auth', {})
         auth['tenant'] = obj.slice.name
         obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
+        obj.creator = request.user
         obj.save()
 
     def delete_model(self, request, obj):
@@ -486,7 +488,7 @@ class UserCreationForm(forms.ModelForm):
 
     class Meta:
         model = User
-        fields = ('email', 'firstname', 'lastname', 'phone', 'key', 'site')
+        fields = ('email', 'firstname', 'lastname', 'phone', 'public_key', 'site')
 
     def clean_password2(self):
         # Check that the two password entries match
@@ -539,13 +541,13 @@ class UserAdmin(UserAdmin, OSModelAdmin):
     inlines = [SitePrivilegeInline, SliceMembershipInline]
     fieldsets = (
         (None, {'fields': ('email', 'password', 'site', 'is_admin', 'timezone')}),
-        ('Personal info', {'fields': ('firstname','lastname','phone', 'key')}),
+        ('Personal info', {'fields': ('firstname','lastname','phone', 'public_key')}),
         #('Important dates', {'fields': ('last_login',)}),
     )
     add_fieldsets = (
         (None, {
             'classes': ('wide',),
-            'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'key','password1', 'password2', 'is_admin')}
+            'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'public_key','password1', 'password2', 'is_admin')}
         ),
     )
     search_fields = ('email',)
index ed7c3ff..709fdc6 100644 (file)
@@ -6,6 +6,7 @@ class PlCoreBase(models.Model):
 
     created = models.DateTimeField(auto_now_add=True)
     updated = models.DateTimeField(auto_now=True)
+    enacted = models.DateTimeField(null=True, default=None)
 
     class Meta:
         abstract = True
index 03fa1d8..72ad66d 100644 (file)
@@ -3,11 +3,11 @@ from django.db import models
 from django.core import exceptions
 from core.models import PlCoreBase
 from core.models import Image
-from core.models import Key
 from core.models import Slice
 from core.models import Node
 from core.models import Site
 from core.models import Deployment
+from core.models import User
 
 # Create your models here.
 class Sliver(PlCoreBase):
@@ -16,7 +16,8 @@ class Sliver(PlCoreBase):
     instance_name = models.CharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name")
     ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
     image = models.ForeignKey(Image, related_name='slivers')
-    key = models.ForeignKey(Key, related_name='slivers')
+    #key = models.ForeignKey(Key, related_name='slivers')
+    creator = models.ForeignKey(User, related_name='slivers', blank=True, null=True)
     slice = models.ForeignKey(Slice, related_name='slivers')
     node = models.ForeignKey(Node, related_name='slivers')
     deploymentNetwork = models.ForeignKey(Deployment, verbose_name='deployment', related_name='sliver_deploymentNetwork')
index 055b6cd..d494ede 100644 (file)
@@ -63,7 +63,8 @@ class User(AbstractBaseUser):
     phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
     user_url = models.URLField(null=True, blank=True)
     site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too", null=True)
-    key = models.ForeignKey(Key, related_name='user', null=True, blank=True)
+    #key = models.ForeignKey(Key, related_name='user', null=True, blank=True)
+    public_key = models.CharField(null=True, blank=True, max_length=1024, help_text="Public key string")
 
     is_active = models.BooleanField(default=True)
     is_admin = models.BooleanField(default=True)
@@ -84,6 +85,10 @@ class User(AbstractBaseUser):
         # The user is identified by their email address
         return self.email
 
+    @property
+    def keyname(self):
+        return self.email[:self.email.find('@')]
+
     def __unicode__(self):
         return self.email
 
index 447bac4..fd0c6c6 100644 (file)
@@ -28,8 +28,9 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
                   'firstname', 
                   'lastname',
                   'email', 
-                  'password', 
-                  'phone', 
+                  'password',
+                  'phone',
+                  'public_key', 
                   'user_url',
                   'is_admin',
                   'site',
index f3abbb2..b21c341 100644 (file)
@@ -3,6 +3,8 @@ try:
     from glance import client as glance_client
     from novaclient.v1_1 import client as nova_client
     from quantumclient.v2_0 import client as quantum_client
+    from nova.db.sqlalchemy import api as nova_db_api 
+    from nova.context import get_admin_context
     has_openstack = True
 except:
     has_openstack = False
@@ -110,6 +112,23 @@ class NovaClient(Client):
     def __getattr__(self, name):
         return getattr(self.client, name)
 
+class NovaDB(Client):
+    def __init__(self, *args, **kwds):
+        Client.__init__(self, *args, **kwds)
+        if has_openstack:
+            self.ctx = get_admin_context()
+            api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
+            self.client = nova_db_api
+
+
+    @require_enabled
+    def connect(self, *args, **kwds):
+        self.__init__(*args, **kwds)
+
+    @require_enabled
+    def __getattr__(self, name):
+        return getattr(self.client, name)
+
 class QuantumClient(Client):
     def __init__(self, *args, **kwds):
         Client.__init__(self, *args, **kwds)
@@ -137,6 +156,7 @@ class OpenStackClient:
         self.keystone = KeystoneClient(*args, **kwds)
         self.glance = GlanceClient(*args, **kwds)
         self.nova = NovaClient(*args, **kwds)
+        self.nova_db = NovaDB(*args, **kwds)
         self.quantum = QuantumClient(*args, **kwds)
 
     @require_enabled
index f84fbb8..a38b0a1 100644 (file)
@@ -278,12 +278,16 @@ class OpenStackDriver:
              
         return 1
     
-    def create_keypair(self, name, key):
+    def create_keypair(self, name, public_key):
         keys = self.shell.nova.keypairs.findall(name=name)
         if keys:
             key = keys[0]
+            # update key     
+            if key.public_key != public_key:
+                self.delete_keypair(key.id)
+                key = self.shell.nova.keypairs.create(name=name, public_key=public_key)
         else:
-            key = self.shell.nova.keypairs.create(name=name, public_key=key)
+            key = self.shell.nova.keypairs.create(name=name, public_key=public_key)
         return key
 
     def delete_keypair(self, id):
index 83dd954..3c77952 100644 (file)
@@ -6,11 +6,11 @@ import random
 from netaddr import IPAddress, IPNetwork
 from planetstack import settings
 from django.core import management
+from core.models import * 
 from planetstack.config import Config
 try:
     from openstack.client import OpenStackClient
     from openstack.driver import OpenStackDriver
-    from core.models import * 
     has_openstack = True
 except:
     has_openstack = False
@@ -79,12 +79,10 @@ class OpenStackManager:
             self.driver.delete_role({'id': role.role})
 
     @require_enabled
-    def save_key(self, key):
-        if not key.nkey_id:
-            key_fields = {'name': random_string(8),
-                          'key': key.key}
-            nova_key = self.driver.create_keypair(**key_fields)
-            key.nkey_id = nova_key.id        
+    def save_key(self, key, name):
+        key_fields = {'name': name,
+                      'public_key': key}
+        nova_key = self.driver.create_keypair(**key_fields)
 
     @require_enabled
     def delete_key(self, key):
@@ -101,6 +99,10 @@ class OpenStackManager:
                            'enabled': True}
             keystone_user = self.driver.create_user(**user_fields)
             user.kuser_id = keystone_user.id
+
+        if user.public_key:
+            self.save_key(user.public_key, user.keyname)
+
         if user.site:
             self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'user')
             if user.is_admin:
@@ -243,7 +245,7 @@ class OpenStackManager:
     def save_sliver(self, sliver):
         if not sliver.instance_id:
             instance = self.driver.spawn_instance(name=sliver.name,
-                                   key_name = sliver.key.nkey_id,
+                                   key_name = sliver.creator.keyname,
                                    image_id = sliver.image.image_id,
                                    hostname = sliver.node.name )
             sliver.instance_id = instance.id
@@ -286,7 +288,7 @@ class OpenStackManager:
             site = sites[i]
             node = Node(name=compute_nodes_dict[name].hypervisor_hostname,
                         site=site,
-                        deploymentNetwork=deployment)
+                        deployment=deployment)
             node.save()
             i+=1