def save_instance_distribution(self, noAct=False, reset=False): print "save_instance_distribution, reset=", reset if (not self._instance_distribution): # Must be a instance that was just created, and has not instance_distribution # field. return all_slice_instances = self.instances.all() for site_name in self._instance_distribution.keys(): desired_allocation = self._instance_distribution[site_name] # make a list of the instances for this site instances = [] for instance in all_slice_instances: if instance.node.site_deployment.site.name == site_name: instances.append(instance) # delete extra instances while (reset and len(instances) > 0) or (len(instances) > desired_allocation): instance = instances.pop() if (not noAct): print "deleting instance", instance instance.delete() else: print "would delete instance", instance # add more instances if (len(instances) < desired_allocation): site = Site.objects.get(name=site_name) nodes = self.get_node_allocation([site]) if (not nodes): raise APIException(detail="no nodes in site %s" % site_name) while (len(instances) < desired_allocation): # pick the least allocated node nodes = sorted(nodes, key=attrgetter("instanceCount")) node = nodes[0] instance = Instance( name=node.name, slice=self, node=node, image=self.default_image, flavor=self.default_flavor, creator=self.creator, deployment=node.site_deployment.deployment) instance.caller = self.caller instances.append(instance) if (not noAct): print "added instance", instance instance.save() else: print "would add instance", instance node.instanceCount = node.instanceCount + 1
def save_site_allocation(self, noAct = False, reset=False): print "save_site_allocation, reset=",reset if (not self._site_allocation): # Must be a instance that was just created, and has not site_allocation # field. return all_slice_instances = self.instances.all() for site_name in self._site_allocation.keys(): desired_allocation = self._site_allocation[site_name] # make a list of the instances for this site instances = [] for instance in all_slice_instances: if instance.node.site_deployment.site.name == site_name: instances.append(instance) # delete extra instances while (reset and len(instances)>0) or (len(instances) > desired_allocation): instance = instances.pop() if (not noAct): print "deleting instance", instance instance.delete() else: print "would delete instance", instance # add more instances if (len(instances) < desired_allocation): site = Site.objects.get(name = site_name) nodes = self.get_node_allocation([site]) if (not nodes): raise APIException(detail="no nodes in site %s" % site_name) while (len(instances) < desired_allocation): # pick the least allocated node nodes = sorted(nodes, key=attrgetter("instanceCount")) node = nodes[0] instance = Instance(name=node.name, slice=self, node=node, image = self.default_image, flavor = self.default_flavor, creator = self.creator, deployment = node.site_deployment.deployment) instance.caller = self.caller instances.append(instance) if (not noAct): print "added instance", instance instance.save() else: print "would add instance", instance node.instanceCount = node.instanceCount + 1
def create(self, name = None, index = None): xos_args = self.get_xos_args(name=name, index=index) instance = Instance(**xos_args) instance.caller = self.user instance.no_sync = True instance.save() self.deferred_sync.append(instance) self.info("Created Instance '%s' on node '%s' using flavor '%s' and image '%s'" % (str(instance), str(instance.node), str(instance.flavor), str(instance.image)))