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 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 52 | def refresh_images(self): |
| 53 | # collect local images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 54 | from plstackapi.core.models import Image |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 55 | images = Image.objects.all() |
| 56 | images_dict = {} |
| 57 | for image in images: |
| 58 | images_dict[image.name] = image |
| 59 | |
| 60 | # collect glance images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 61 | glance_images = self.client.glance.get_images() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 62 | glance_images_dict = {} |
| 63 | for glance_image in glance_images: |
| 64 | glance_images_dict[glance_image['name']] = glance_image |
| 65 | |
| 66 | # add new images |
| 67 | new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) |
| 68 | for name in new_image_names: |
| 69 | image = Image(image_id=glance_images_dict[name]['id'], |
| 70 | name=glance_images_dict[name]['name'], |
| 71 | disk_format=glance_images_dict[name]['disk_format'], |
| 72 | container_format=glance_images_dict[name]['container_format']) |
| 73 | image.save() |
| 74 | |
| 75 | # remove old images |
| 76 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 77 | Image.objects.filter(name__in=old_image_names).delete() |