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 | |
| 24 | deployment = DeploymentNetwork.objects.filter(name='VICCI') |
| 25 | login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] |
| 26 | sites = Site.objects.filter(login_base__in=login_bases) |
| 27 | nodes_per_site = len(nodes)/len(sites) |
| 28 | |
| 29 | def chunks(l, n): |
| 30 | return [l[i:i+n] for i in range(0, len(l), n)] |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 31 | |
| 32 | # collect nova nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 33 | compute_nodes = self.client.nova.hypervisors.list() |
| 34 | |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 35 | compute_nodes_dict = {} |
| 36 | for compute_node in compute_nodes: |
| 37 | compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node |
| 38 | |
| 39 | # add new nodes: |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 40 | counter = 1 |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 41 | new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys()) |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 42 | |
| 43 | def chunks(l, n): |
| 44 | return [l[i:i+n] for i in range(0, len(l), n)] |
| 45 | |
| 46 | node_chunks = chunks(compute_nodes, nodes_per_site) |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 47 | for name in new_node_names: |
| 48 | node = Node(name=compute_nodes_dict[name].hypervisor_hostname) |
| 49 | node.save() |
| 50 | |
| 51 | # remove old nodes |
| 52 | old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) |
| 53 | Node.objects.filter(name__in=old_node_names).delete() |
| 54 | |
| 55 | def refresh_flavors(self): |
| 56 | # collect local flavors |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 57 | from plstackapi.core.models import Flavor |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 58 | flavors = Flavor.objects.all() |
| 59 | flavors_dict = {} |
| 60 | for flavor in flavors: |
| 61 | flavors_dict[flavor.name] = flavor |
| 62 | |
| 63 | # collect nova falvors |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 64 | nova_flavors = self.client.nova.flavors.list() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 65 | nova_flavors_dict = {} |
| 66 | for nova_flavor in nova_flavors: |
| 67 | nova_flavors_dict[nova_flavor.name] = nova_flavor |
| 68 | |
| 69 | # add new flavors |
| 70 | new_flavor_names = set(nova_flavors_dict.keys()).difference(flavors_dict.keys()) |
| 71 | for name in new_flavor_names: |
| 72 | |
| 73 | flavor = Flavor(flavor_id=nova_flavors_dict[name].id, |
| 74 | name=nova_flavors_dict[name].name, |
| 75 | memory_mb=nova_flavors_dict[name].ram, |
| 76 | disk_gb=nova_flavors_dict[name].disk, |
| 77 | vcpus=nova_flavors_dict[name].vcpus) |
| 78 | flavor.save() |
| 79 | |
| 80 | # remove old flavors |
| 81 | old_flavor_names = set(flavors_dict.keys()).difference(nova_flavors_dict.keys()) |
| 82 | Flavor.objects.filter(name__in=old_flavor_names).delete() |
| 83 | |
| 84 | def refresh_images(self): |
| 85 | # collect local images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 86 | from plstackapi.core.models import Image |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 87 | images = Image.objects.all() |
| 88 | images_dict = {} |
| 89 | for image in images: |
| 90 | images_dict[image.name] = image |
| 91 | |
| 92 | # collect glance images |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 93 | glance_images = self.client.glance.get_images() |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 94 | glance_images_dict = {} |
| 95 | for glance_image in glance_images: |
| 96 | glance_images_dict[glance_image['name']] = glance_image |
| 97 | |
| 98 | # add new images |
| 99 | new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) |
| 100 | for name in new_image_names: |
| 101 | image = Image(image_id=glance_images_dict[name]['id'], |
| 102 | name=glance_images_dict[name]['name'], |
| 103 | disk_format=glance_images_dict[name]['disk_format'], |
| 104 | container_format=glance_images_dict[name]['container_format']) |
| 105 | image.save() |
| 106 | |
| 107 | # remove old images |
| 108 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 109 | Image.objects.filter(name__in=old_image_names).delete() |