blob: 68d4dbb9c9bd3c40ad2c0a9ca6c82e93269cf95a [file] [log] [blame]
Tony Mack735493a2013-04-04 23:54:28 -04001from plstackapi.planetstack import settings
2from django.core import management
3management.setup_environ(settings)
Tony Mack48952032013-04-12 11:49:34 -04004from plstackapi.openstack.client import OpenStackClient
Tony Mack735493a2013-04-04 23:54:28 -04005
6
7class Manager:
8
9 def __init__(self):
10
Tony Mack48952032013-04-12 11:49:34 -040011 self.client = OpenStackClient()
Tony Mack735493a2013-04-04 23:54:28 -040012
13 def refresh_nodes(self):
14 # collect local nodes
Tony Mack48952032013-04-12 11:49:34 -040015 from plstackapi.core.models import Node
16 from plstackapi.core.models import DeploymentNetwork
17 from plstackapi.core.models import Site
Tony Mack735493a2013-04-04 23:54:28 -040018 nodes = Node.objects.all()
19 nodes_dict = {}
20 for node in nodes:
Tony Mack48952032013-04-12 11:49:34 -040021 if 'viccidev10' not in node.name:
22 nodes_dict[node.name] = node
23
Tony Mack51f113d2013-04-13 02:02:22 -040024 deployment = DeploymentNetwork.objects.filter(name='VICCI')[0]
Tony Mack48952032013-04-12 11:49:34 -040025 login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws']
26 sites = Site.objects.filter(login_base__in=login_bases)
Tony Mack735493a2013-04-04 23:54:28 -040027 # collect nova nodes:
Tony Mack48952032013-04-12 11:49:34 -040028 compute_nodes = self.client.nova.hypervisors.list()
29
Tony Mack735493a2013-04-04 23:54:28 -040030 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 Mack51f113d2013-04-13 02:02:22 -040036 i = 0
37 max = len(sites)
Tony Mack735493a2013-04-04 23:54:28 -040038 for name in new_node_names:
Tony Mack51f113d2013-04-13 02:02:22 -040039 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 Mack735493a2013-04-04 23:54:28 -040045 node.save()
Tony Mack51f113d2013-04-13 02:02:22 -040046 i+=1
Tony Mack735493a2013-04-04 23:54:28 -040047
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 Mack735493a2013-04-04 23:54:28 -040052 def refresh_images(self):
53 # collect local images
Tony Mack48952032013-04-12 11:49:34 -040054 from plstackapi.core.models import Image
Tony Mack735493a2013-04-04 23:54:28 -040055 images = Image.objects.all()
56 images_dict = {}
57 for image in images:
58 images_dict[image.name] = image
59
60 # collect glance images
Tony Mack48952032013-04-12 11:49:34 -040061 glance_images = self.client.glance.get_images()
Tony Mack735493a2013-04-04 23:54:28 -040062 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()