From 4895203c531a0d617c55c6ff0db58f04af696066 Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Fri, 12 Apr 2013 11:49:34 -0400 Subject: [PATCH] fix bugs --- plstackapi/core/api/subnets.py | 17 +++++---- plstackapi/core/models/slice.py | 2 +- plstackapi/openstack/driver.py | 67 +++++++++++++++------------------ plstackapi/openstack/manager.py | 39 +++++++++++++------ 4 files changed, 68 insertions(+), 57 deletions(-) diff --git a/plstackapi/core/api/subnets.py b/plstackapi/core/api/subnets.py index 6ef051e..ad1a877 100644 --- a/plstackapi/core/api/subnets.py +++ b/plstackapi/core/api/subnets.py @@ -28,14 +28,15 @@ def add_subnet(auth, fields): if slices: fields['slice'] = slices[0] subnet = Subnet(**fields) # create quantum subnet - subnet = driver.create_subnet(network_name=subnet.slice.name, - cidr_ip = subnet.cidr, - ip_version=subnet.ip_version, - start = subnet.start, - end = subnet.end, - dns_nameservers = ['8.8.8.8', '8.8.4.4']) - - subnet.subnet_id=subnet.id + quantum_subnet = 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'] + ## set dns servers + #driver.update_subnet(subnet.id, {'dns_nameservers': ['8.8.8.8', '8.8.4.4']}) # add subnet as interface to slice's router driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id) diff --git a/plstackapi/core/models/slice.py b/plstackapi/core/models/slice.py index 63941c7..4182e2f 100644 --- a/plstackapi/core/models/slice.py +++ b/plstackapi/core/models/slice.py @@ -12,7 +12,7 @@ from plstackapi.openstack.driver import OpenStackDriver class Slice(PlCoreBase): tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") - name = models.CharField(help_text="The Name of the Slice", max_length=80) + name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80) enabled = models.BooleanField(default=True, help_text="Status for this Slice") SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None')) instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES) diff --git a/plstackapi/openstack/driver.py b/plstackapi/openstack/driver.py index 8817990..fe93fbc 100644 --- a/plstackapi/openstack/driver.py +++ b/plstackapi/openstack/driver.py @@ -96,34 +96,16 @@ class OpenStackDriver: self.shell.quantum.delete_router(router['id']) def add_router_interface(self, router_id, subnet_id): - router = None - subnet = None - for r in self.shell.quantum.list_routers(): - if r['id'] == router_id: - router = r - break - for s in self.shell.quantum.list_subnets(): - if s['id'] == subnet_id: - subnet = s - break - + router = self.shell.quantum.show_router(router_id)['router'] + subnet = self.shell.quantum.show_subnet(subnet_id)['subnet'] if router and subnet: - self.shell.quantum.router_add_interface(router, subnet) + self.shell.quantum.add_interface_router(router_id, {'subnet_id': subnet_id}) def delete_router_interface(self, router_id, subnet_id): - router = None - subnet = None - for r in self.shell.quantum.list_routers(): - if r['id'] == router_id: - router = r - break - for s in self.shell.quantum.list_subnets(): - if s['id'] == subnet_id: - subnet = s - break - + router = self.shell.quantum.show_router(router_id) + subnet = self.shell.quantum.show_subnet(subnet_id) if router and subnet: - self.shell.quantum.router_remove_interface(router, subnet) + self.shell.quantum.remove_interface_router(router_id, {'subnet_id': subnet_id}) def create_network(self, name): nets = self.shell.quantum.list_networks(name=name)['networks'] @@ -142,24 +124,35 @@ class OpenStackDriver: self.delete_subnet(subnet_id) self.shell.quantum.delete_network(net['id']) - def create_subnet(self, network_name, cidr_ip, ip_version, start, end): - nets = self.shell.quantum.list_networks(name=network_name) - if not nets: - raise Exception, "No such network: %s" % network_name - nets = nets[0] - - subnets = self.shell.quantum.list_subnets(name=self.name) - allocation_pools = [{'start': start, 'end': end}] - subnet = self.shell.quantum.create_subnet(network_id=net['id'], - ip_version=ip_version, - cidr=cidr_ip, - dns_nameservers=['8.8.8.8', '8.8.8.4'], - allocation_pools=allocation_pools) + def create_subnet(self, name, network_id, cidr_ip, ip_version, start, end): + #nets = self.shell.quantum.list_networks(name=network_name)['networks'] + #if not nets: + # raise Exception, "No such network: %s" % network_name + #net = nets[0] + + subnet = None + subnets = self.shell.quantum.list_subnets()['subnets'] + for snet in subnets: + if snet['cidr'] == cidr_ip and snet['network_id'] == network_id: + subnet = snet + + if not subnet: + allocation_pools = [{'start': start, 'end': end}] + subnet = {'subnet': {'name': name, + 'network_id': network_id, + 'ip_version': ip_version, + 'cidr': cidr_ip, + 'dns_nameservers': ['8.8.8.8', '8.8.8.4'], + 'allocation_pools': allocation_pools}} + subnet = self.shell.quantum.create_subnet(subnet)['subnet'] # TODO: Add route to external network # e.g. # route add -net 10.0.3.0/24 dev br-ex gw 10.100.0.5 return subnet + def update_subnet(self, id, fields): + return self.shell.quantum.update_subnet(id, fields) + def delete_subnet(self, id): return self.shell.quantum.delete_subnet(id=id) diff --git a/plstackapi/openstack/manager.py b/plstackapi/openstack/manager.py index 50a36e2..eb76ff7 100644 --- a/plstackapi/openstack/manager.py +++ b/plstackapi/openstack/manager.py @@ -1,32 +1,51 @@ from plstackapi.planetstack import settings from django.core import management management.setup_environ(settings) -from plstackapi.openstack.shell import OpenStackShell +from plstackapi.openstack.client import OpenStackClient class Manager: def __init__(self): - self.shell = OpenStackShell() + self.client = OpenStackClient() def refresh_nodes(self): # collect local nodes - from plstackapi.planetstack.models import Node + from plstackapi.core.models import Node + from plstackapi.core.models import DeploymentNetwork + from plstackapi.core.models import Site nodes = Node.objects.all() nodes_dict = {} for node in nodes: - nodes_dict[node.name] = node + if 'viccidev10' not in node.name: + nodes_dict[node.name] = node + + deployment = DeploymentNetwork.objects.filter(name='VICCI') + login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] + sites = Site.objects.filter(login_base__in=login_bases) + nodes_per_site = len(nodes)/len(sites) + + def chunks(l, n): + return [l[i:i+n] for i in range(0, len(l), n)] # collect nova nodes: - compute_nodes = self.shell.nova.hypervisors.list() + compute_nodes = self.client.nova.hypervisors.list() + compute_nodes_dict = {} for compute_node in compute_nodes: compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node # add new nodes: + counter = 1 new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys()) + + def chunks(l, n): + return [l[i:i+n] for i in range(0, len(l), n)] + + node_chunks = chunks(compute_nodes, nodes_per_site) for name in new_node_names: + for node = Node(name=compute_nodes_dict[name].hypervisor_hostname) node.save() @@ -36,14 +55,14 @@ class Manager: def refresh_flavors(self): # collect local flavors - from plstackapi.planetstack.models import Flavor + from plstackapi.core.models import Flavor flavors = Flavor.objects.all() flavors_dict = {} for flavor in flavors: flavors_dict[flavor.name] = flavor # collect nova falvors - nova_flavors = self.shell.nova.flavors.list() + nova_flavors = self.client.nova.flavors.list() nova_flavors_dict = {} for nova_flavor in nova_flavors: nova_flavors_dict[nova_flavor.name] = nova_flavor @@ -65,14 +84,14 @@ class Manager: def refresh_images(self): # collect local images - from plstackapi.planetstack.models import Image + from plstackapi.core.models import Image images = Image.objects.all() images_dict = {} for image in images: images_dict[image.name] = image # collect glance images - glance_images = self.shell.glance.get_images() + glance_images = self.client.glance.get_images() glance_images_dict = {} for glance_image in glance_images: glance_images_dict[glance_image['name']] = glance_image @@ -89,5 +108,3 @@ class Manager: # remove old images old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) Image.objects.filter(name__in=old_image_names).delete() - - -- 2.43.0