subnets and slivers work when openstack is disable or unavailable
diff --git a/plstackapi/core/admin.py b/plstackapi/core/admin.py
index a562911..039ac17 100644
--- a/plstackapi/core/admin.py
+++ b/plstackapi/core/admin.py
@@ -233,17 +233,17 @@
list_display = ('slice','cidr', 'start', 'end', 'ip_version')
def save_model(self, request, obj, form, change):
- # update openstack connection to use this subnet's slice/tenant
- client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
- obj.driver = OpenStackDriver(client=client)
- obj.caller = request.user
+ # update openstack connection to use this site/tenant
+ auth = request.session.get('auth', {})
+ auth['tenant'] = obj.slice.name
+ obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
obj.save()
def delete_model(self, request, obj):
- # update openstack connection to use this subnet's slice/tenant
- client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
- obj.driver = OpenStackDriver(client=client)
- obj.caller = request.user
+ # update openstack connection to use this site/tenant
+ auth = request.session.get('auth', {})
+ auth['tenant'] = obj.slice.name
+ obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
obj.delete()
class ImageAdmin(admin.ModelAdmin):
@@ -272,19 +272,18 @@
list_display = ['ip', 'instance_name', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']
def save_model(self, request, obj, form, change):
- # update openstack connection to use this sliver's slice/tenant
- client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
- obj.driver = OpenStackDriver(client=client)
- obj.caller = request.user
+ # update openstack connection to use this site/tenant
+ auth = request.session.get('auth', {})
+ auth['tenant'] = obj.slice.name
+ obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
obj.save()
def delete_model(self, request, obj):
- # update openstack connection to use this sliver's slice/tenant
- client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
- obj.driver = OpenStackDriver(client=client)
- obj.caller = request.user
+ # update openstack connection to use this site/tenant
+ auth = request.session.get('auth', {})
+ auth['tenant'] = obj.slice.name
+ obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
obj.delete()
-
class UserCreationForm(forms.ModelForm):
"""A form for creating new users. Includes all the required
diff --git a/plstackapi/core/models/sliver.py b/plstackapi/core/models/sliver.py
index a4bf2c0..9a3ca81 100644
--- a/plstackapi/core/models/sliver.py
+++ b/plstackapi/core/models/sliver.py
@@ -29,18 +29,9 @@
if not self.slice.subnet.exists():
raise exceptions.ValidationError, "Slice %s has no subnet" % self.slice.name
- if not self.instance_id:
- instance = self.driver.spawn_instance(name=self.name,
- key_name = self.key.name,
- image_id = self.image.image_id,
- hostname = self.node.name )
- self.instance_id = instance.id
- self.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
-
+ self.os_manager.save_sliver(self)
super(Sliver, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- if self.instance_id:
- self.driver.destroy_instance(self.instance_id)
-
+ self.os_manager.delete_sliver(self)
super(Sliver, self).delete(*args, **kwds)
diff --git a/plstackapi/core/models/subnet.py b/plstackapi/core/models/subnet.py
index c9ea1ef..be8c938 100644
--- a/plstackapi/core/models/subnet.py
+++ b/plstackapi/core/models/subnet.py
@@ -17,25 +17,9 @@
def __unicode__(self): return u'%s' % (self.slice.name)
def save(self, *args, **kwds):
- if not self.subnet_id:
- quantum_subnet = self.driver.create_subnet(name= self.slice.name,
- network_id=self.slice.network_id,
- cidr_ip = self.cidr,
- ip_version=self.ip_version,
- start = self.start,
- end = self.end)
- self.subnet_id = quantum_subnet['id']
- # add subnet as interface to slice's router
- self.driver.add_router_interface(self.slice.router_id, self.subnet_id)
- #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
- #commands.getstatusoutput(add_route)
-
+ self.os_manager.save_subnet(self)
super(Subnet, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- if self.subnet_id:
- self.driver.delete_router_interface(self.slice.router_id, self.subnet_id)
- self.driver.delete_subnet(self.subnet_id)
- #del_route = 'route del -net %s' % self.cidr
- #commands.getstatusoutput(del_route)
+ self.os_manager.delete_subnet(self)
super(Subnet, self).delete(*args, **kwds)
diff --git a/plstackapi/openstack/manager.py b/plstackapi/openstack/manager.py
index c0af4ef..e48acc0 100644
--- a/plstackapi/openstack/manager.py
+++ b/plstackapi/openstack/manager.py
@@ -8,12 +8,13 @@
from plstackapi.core.models import *
has_openstack = True
except:
- has_openstack = False
+ has_openpstack = False
+
+manager_enabled = Config().api_nova_enabled
def require_enabled(callable):
- enabled = Config().api_nova_enabled
def wrapper(*args, **kwds):
- if enabled and has_openstack:
+ if manager_enabled and has_openstack:
return callable(*args, **kwds)
else:
return None
@@ -23,10 +24,12 @@
class OpenStackManager:
def __init__(self, auth={}, caller=None):
- self.client = None
if auth:
self.client = OpenStackClient(**auth)
-
+ else:
+ self.client = OpenStackClient()
+ self.has_openstack = has_openstack
+ self.enabled = manager_enabled
self.driver = OpenStackDriver(client=self.client)
self.caller=caller
@@ -130,6 +133,46 @@
self.driver.delete_network(slice.network_id)
self.driver.delete_tenant(slice.tenant_id)
+ @require_enabled
+ def save_subnet(self, subnet):
+ if not subnet.subnet_id:
+ quantum_subnet = self.driver.create_subnet(name= subnet.slice.name,
+ network_id=subnet.slice.network_id,
+ cidr_ip = subnet.cidr,
+ ip_version=subnet.ip_version,
+ start = subnet.start,
+ end = subnet.end)
+ subnet.subnet_id = quantum_subnet['id']
+ # add subnet as interface to slice's router
+ self.driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id)
+ #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
+ #commands.getstatusoutput(add_route)
+
+
+ @require_enabled
+ def delete_subnet(self, subnet):
+ if subnet.subnet_id:
+ self.driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id)
+ self.driver.delete_subnet(subnet.subnet_id)
+ #del_route = 'route del -net %s' % self.cidr
+ #commands.getstatusoutput(del_route)
+
+ @require_enabled
+ def save_sliver(self, sliver):
+ if not sliver.instance_id:
+ instance = self.driver.spawn_instance(name=sliver.name,
+ key_name = sliver.key.name,
+ image_id = sliver.image.image_id,
+ hostname = sliver.node.name )
+ sliver.instance_id = instance.id
+ sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
+
+ @require_enabled
+ def delete_sliver(self, sliver):
+ if sliver.instance_id:
+ self.driver.destroy_instance(sliver.instance_id)
+
+
def refresh_nodes(self):
# collect local nodes
nodes = Node.objects.all()
diff --git a/plstackapi/openstack/sliveragent.py b/plstackapi/openstack/sliveragent.py
index 732a5f1..f07f603 100644
--- a/plstackapi/openstack/sliveragent.py
+++ b/plstackapi/openstack/sliveragent.py
@@ -1,22 +1,28 @@
import os
+import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "plstackapi.planetstack.settings")
import time
from plstackapi.core.models.sliver import Sliver
-from plstackapi.openstack.client import OpenStackClient
+from plstackapi.openstack.manager import OpenStackManager
class SliverAgent:
def run(self):
- client = OpenStackClient()
- while True:
+ manager = OpenStackManager()
+ # exit if openstack is disable or unavailable
+ if not manager.enabled or not manager.has_openstack:
+ sys.exit()
+
+ while True :
# fill in null ip addresses
slivers = Sliver.objects.filter(ip=None)
for sliver in slivers:
# update connection
- client.connect(username=client.keystone.username,
- password=client.keystone.password,
+ manager.client.connect(username=manager.client.keystone.username,
+ password=manager.client.keystone.password,
tenant=sliver.slice.name)
- servers = client.nova.servers.findall(id=sliver.instance_id)
+ sliver.os_manager = manager
+ servers = manager.client.nova.servers.findall(id=sliver.instance_id)
if not servers:
continue
server = servers[0]