Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 1 | from plstackapi.planetstack import settings |
| 2 | from django.core import management |
| 3 | management.setup_environ(settings) |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 4 | from plstackapi.openstack.client import OpenStackClient |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 5 | |
| 6 | |
| 7 | class Manager: |
| 8 | |
| 9 | def __init__(self): |
| 10 | |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 11 | self.client = OpenStackClient() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 12 | |
| 13 | def refresh_nodes(self): |
| 14 | # collect local nodes |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 15 | from plstackapi.core.models import Node |
| 16 | from plstackapi.core.models import DeploymentNetwork |
| 17 | from plstackapi.core.models import Site |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 18 | nodes = Node.objects.all() |
| 19 | nodes_dict = {} |
| 20 | for node in nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 21 | if 'viccidev10' not in node.name: |
| 22 | nodes_dict[node.name] = node |
| 23 | |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 24 | deployment = DeploymentNetwork.objects.filter(name='VICCI')[0] |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 25 | login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] |
| 26 | sites = Site.objects.filter(login_base__in=login_bases) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 27 | # collect nova nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 28 | compute_nodes = self.client.nova.hypervisors.list() |
| 29 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 30 | compute_nodes_dict = {} |
| 31 | for compute_node in compute_nodes: |
| 32 | compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node |
| 33 | |
| 34 | # add new nodes: |
| 35 | new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys()) |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 36 | i = 0 |
| 37 | max = len(sites) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 38 | for name in new_node_names: |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 39 | if i == max: |
| 40 | i = 0 |
| 41 | site = sites[i] |
| 42 | node = Node(name=compute_nodes_dict[name].hypervisor_hostname, |
| 43 | site=site, |
| 44 | deploymentNetwork=deployment) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 45 | node.save() |
Tony Mack | 51f113d | 2013-04-13 02:02:22 -0400 | [diff] [blame] | 46 | i+=1 |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 47 | |
| 48 | # remove old nodes |
| 49 | old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) |
| 50 | Node.objects.filter(name__in=old_node_names).delete() |
| 51 | |
| 52 | def refresh_flavors(self): |
| 53 | # collect local flavors |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 54 | from plstackapi.core.models import Flavor |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 55 | flavors = Flavor.objects.all() |
| 56 | flavors_dict = {} |
| 57 | for flavor in flavors: |
| 58 | flavors_dict[flavor.name] = flavor |
| 59 | |
| 60 | # collect nova falvors |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 61 | nova_flavors = self.client.nova.flavors.list() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 62 | nova_flavors_dict = {} |
| 63 | for nova_flavor in nova_flavors: |
| 64 | nova_flavors_dict[nova_flavor.name] = nova_flavor |
| 65 | |
| 66 | # add new flavors |
| 67 | new_flavor_names = set(nova_flavors_dict.keys()).difference(flavors_dict.keys()) |
| 68 | for name in new_flavor_names: |
| 69 | |
| 70 | flavor = Flavor(flavor_id=nova_flavors_dict[name].id, |
| 71 | name=nova_flavors_dict[name].name, |
| 72 | memory_mb=nova_flavors_dict[name].ram, |
| 73 | disk_gb=nova_flavors_dict[name].disk, |
| 74 | vcpus=nova_flavors_dict[name].vcpus) |
| 75 | flavor.save() |
| 76 | |
| 77 | # remove old flavors |
| 78 | old_flavor_names = set(flavors_dict.keys()).difference(nova_flavors_dict.keys()) |
| 79 | Flavor.objects.filter(name__in=old_flavor_names).delete() |
| 80 | |
| 81 | def refresh_images(self): |
| 82 | # collect local images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 83 | from plstackapi.core.models import Image |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 84 | images = Image.objects.all() |
| 85 | images_dict = {} |
| 86 | for image in images: |
| 87 | images_dict[image.name] = image |
| 88 | |
| 89 | # collect glance images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 90 | glance_images = self.client.glance.get_images() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 91 | glance_images_dict = {} |
| 92 | for glance_image in glance_images: |
| 93 | glance_images_dict[glance_image['name']] = glance_image |
| 94 | |
| 95 | # add new images |
| 96 | new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) |
| 97 | for name in new_image_names: |
| 98 | image = Image(image_id=glance_images_dict[name]['id'], |
| 99 | name=glance_images_dict[name]['name'], |
| 100 | disk_format=glance_images_dict[name]['disk_format'], |
| 101 | container_format=glance_images_dict[name]['container_format']) |
| 102 | image.save() |
| 103 | |
| 104 | # remove old images |
| 105 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 106 | Image.objects.filter(name__in=old_image_names).delete() |