from plstackapi.planetstack import settings from django.core import management management.setup_environ(settings) from plstackapi.openstack.client import OpenStackClient class Manager: def __init__(self): self.client = OpenStackClient() def refresh_nodes(self): # collect local nodes from plstackapi.core.models import Node from plstackapi.core.models import DeploymentNetwork from plstackapi.core.models import Site nodes = Node.objects.all() nodes_dict = {} for node in nodes: if 'viccidev10' not in node.name: nodes_dict[node.name] = node deployment = DeploymentNetwork.objects.filter(name='VICCI')[0] login_bases = ['princeton', 'stanford', 'gt', 'uw', 'mpisws'] sites = Site.objects.filter(login_base__in=login_bases) # collect nova nodes: compute_nodes = self.client.nova.hypervisors.list() compute_nodes_dict = {} for compute_node in compute_nodes: compute_nodes_dict[compute_node.hypervisor_hostname] = compute_node # add new nodes: new_node_names = set(compute_nodes_dict.keys()).difference(nodes_dict.keys()) i = 0 max = len(sites) for name in new_node_names: if i == max: i = 0 site = sites[i] node = Node(name=compute_nodes_dict[name].hypervisor_hostname, site=site, deploymentNetwork=deployment) node.save() i+=1 # remove old nodes old_node_names = set(nodes_dict.keys()).difference(compute_nodes_dict.keys()) Node.objects.filter(name__in=old_node_names).delete() def refresh_images(self): # collect local images from plstackapi.core.models import Image images = Image.objects.all() images_dict = {} for image in images: images_dict[image.name] = image # collect glance images glance_images = self.client.glance.get_images() glance_images_dict = {} for glance_image in glance_images: glance_images_dict[glance_image['name']] = glance_image # add new images new_image_names = set(glance_images_dict.keys()).difference(images_dict.keys()) for name in new_image_names: image = Image(image_id=glance_images_dict[name]['id'], name=glance_images_dict[name]['name'], disk_format=glance_images_dict[name]['disk_format'], container_format=glance_images_dict[name]['container_format']) image.save() # remove old images old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys()) Image.objects.filter(name__in=old_image_names).delete()