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) |
| 4 | from plstackapi.openstack.shell import OpenStackShell |
| 5 | |
| 6 | |
| 7 | class Manager: |
| 8 | |
| 9 | def __init__(self): |
| 10 | |
| 11 | self.shell = OpenStackShell() |
| 12 | |
| 13 | def refresh_nodes(self): |
| 14 | # collect local nodes |
Tony Mack | 0e17ee2 | 2013-04-06 20:04:02 -0400 | [diff] [blame] | 15 | from plstackapi.planetstack.models import Node |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 16 | nodes = Node.objects.all() |
| 17 | nodes_dict = {} |
| 18 | for node in nodes: |
| 19 | nodes_dict[node.name] = node |
| 20 | |
| 21 | # collect nova nodes: |
| 22 | compute_nodes = self.shell.nova.hypervisors.list() |
| 23 | compute_nodes_dict = {} |
| 24 | for compute_node in compute_nodes: |
| 25 | compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node |
| 26 | |
| 27 | # add new nodes: |
| 28 | new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys()) |
| 29 | for name in new_node_names: |
| 30 | node = Node(name=compute_nodes_dict[name].hypervisor_hostname) |
| 31 | node.save() |
| 32 | |
| 33 | # remove old nodes |
| 34 | old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) |
| 35 | Node.objects.filter(name__in=old_node_names).delete() |
| 36 | |
| 37 | def refresh_flavors(self): |
| 38 | # collect local flavors |
Tony Mack | 0e17ee2 | 2013-04-06 20:04:02 -0400 | [diff] [blame] | 39 | from plstackapi.planetstack.models import Flavor |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 40 | flavors = Flavor.objects.all() |
| 41 | flavors_dict = {} |
| 42 | for flavor in flavors: |
| 43 | flavors_dict[flavor.name] = flavor |
| 44 | |
| 45 | # collect nova falvors |
| 46 | nova_flavors = self.shell.nova.flavors.list() |
| 47 | nova_flavors_dict = {} |
| 48 | for nova_flavor in nova_flavors: |
| 49 | nova_flavors_dict[nova_flavor.name] = nova_flavor |
| 50 | |
| 51 | # add new flavors |
| 52 | new_flavor_names = set(nova_flavors_dict.keys()).difference(flavors_dict.keys()) |
| 53 | for name in new_flavor_names: |
| 54 | |
| 55 | flavor = Flavor(flavor_id=nova_flavors_dict[name].id, |
| 56 | name=nova_flavors_dict[name].name, |
| 57 | memory_mb=nova_flavors_dict[name].ram, |
| 58 | disk_gb=nova_flavors_dict[name].disk, |
| 59 | vcpus=nova_flavors_dict[name].vcpus) |
| 60 | flavor.save() |
| 61 | |
| 62 | # remove old flavors |
| 63 | old_flavor_names = set(flavors_dict.keys()).difference(nova_flavors_dict.keys()) |
| 64 | Flavor.objects.filter(name__in=old_flavor_names).delete() |
| 65 | |
| 66 | def refresh_images(self): |
| 67 | # collect local images |
Tony Mack | 0e17ee2 | 2013-04-06 20:04:02 -0400 | [diff] [blame] | 68 | from plstackapi.planetstack.models import Image |
Tony Mack | 735493a | 2013-04-04 23:54:28 -0400 | [diff] [blame] | 69 | images = Image.objects.all() |
| 70 | images_dict = {} |
| 71 | for image in images: |
| 72 | images_dict[image.name] = image |
| 73 | |
| 74 | # collect glance images |
| 75 | glance_images = self.shell.glance.get_images() |
| 76 | glance_images_dict = {} |
| 77 | for glance_image in glance_images: |
| 78 | glance_images_dict[glance_image['name']] = glance_image |
| 79 | |
| 80 | # add new images |
| 81 | new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) |
| 82 | for name in new_image_names: |
| 83 | image = Image(image_id=glance_images_dict[name]['id'], |
| 84 | name=glance_images_dict[name]['name'], |
| 85 | disk_format=glance_images_dict[name]['disk_format'], |
| 86 | container_format=glance_images_dict[name]['container_format']) |
| 87 | image.save() |
| 88 | |
| 89 | # remove old images |
| 90 | old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) |
| 91 | Image.objects.filter(name__in=old_image_names).delete() |
| 92 | |
| 93 | |