removing refrences to flavor
[plstackapi.git] / plstackapi / openstack / manager.py
1 from plstackapi.planetstack import settings
2 from django.core import management
3 management.setup_environ(settings)
4 from plstackapi.openstack.client import OpenStackClient
5
6
7 class Manager:
8
9     def __init__(self):
10         
11         self.client = OpenStackClient()
12
13     def refresh_nodes(self):
14         # collect local nodes
15         from plstackapi.core.models import Node
16         from plstackapi.core.models import DeploymentNetwork
17         from plstackapi.core.models import Site
18         nodes = Node.objects.all()
19         nodes_dict = {}
20         for node in nodes:
21             if 'viccidev10' not in node.name:
22                 nodes_dict[node.name] = node 
23         
24         deployment = DeploymentNetwork.objects.filter(name='VICCI')[0]
25         login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws']
26         sites = Site.objects.filter(login_base__in=login_bases)
27         # collect nova nodes:
28         compute_nodes = self.client.nova.hypervisors.list()
29
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())
36         i = 0
37         max = len(sites)
38         for name in new_node_names:
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)
45             node.save()
46             i+=1
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_images(self):
53         # collect local images
54         from plstackapi.core.models import Image
55         images = Image.objects.all()
56         images_dict = {}    
57         for image in images:
58             images_dict[image.name] = image
59
60         # collect glance images
61         glance_images = self.client.glance.get_images()
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()