def adjust_scale(self, slice_hint, scale, max_per_node=None, exclusive_slices=[]): # late import to get around order-of-imports constraint in __init__.py from core.models import Instance slices = [x for x in self.slices.all() if slice_hint in x.name] for slice in slices: while slice.instances.all().count() > scale: s = slice.instances.all()[0] # print "drop instance", s s.delete() while slice.instances.all().count() < scale: node = self.pick_node(slice, max_per_node, exclusive_slices) if not node: # no more available nodes break image = slice.default_image if not image: raise XOSConfigurationError( "No default_image for slice %s" % slice.name) flavor = slice.default_flavor if not flavor: raise XOSConfigurationError( "No default_flavor for slice %s" % slice.name) s = Instance(slice=slice, node=node, creator=slice.creator, image=image, flavor=flavor, deployment=node.site_deployment.deployment) s.save()
def manage_container(self): from core.models import Instance, Flavor if self.deleted: return if (self.instance is not None) and (self.instance.image != self.image): self.instance.delete() self.instance = None if self.instance is None: if not self.provider_service.slices.count(): raise XOSConfigurationError("The VCPE service has no slices") flavors = Flavor.objects.filter(name="m1.small") if not flavors: raise XOSConfigurationError("No m1.small flavor") node =self.pick_node() instance = Instance(slice = self.provider_service.slices.all()[0], node = node, image = self.image, creator = self.creator, deployment = node.site_deployment.deployment, flavor = flavors[0]) instance.save() try: self.instance = instance super(TenantWithContainer, self).save() except: instance.delete() raise
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)))
def handle_container_on_metal(instance): from core.models import Instance, Flavor, Port, Image print "MODEL POLICY: instance", instance, "handle container_on_metal" if instance.deleted: return if (instance.isolation in ["container"]) and (instance.slice.network not in ["host", "bridged"]): # Our current docker-on-metal network strategy requires that there be some # VM on the server that connects to the networks, so that # the containers can piggyback off of that configuration. if not Instance.objects.filter(slice=instance.slice, node=instance.node, isolation="vm").exists(): flavors = Flavor.objects.filter(name="m1.small") if not flavors: raise XOSConfigurationError("No m1.small flavor") images = Image.objects.filter(kind="vm") companion_instance = Instance( slice=instance.slice, node=instance.node, image=images[0], creator=instance.creator, deployment=instance.node.site_deployment.deployment, flavor=flavors[0]) companion_instance.save() print "MODEL POLICY: instance", instance, "created companion", companion_instance # Add the ports for the container for network in instance.slice.networks.all(): # hmmm... The NAT ports never become ready, because sync_ports never # instantiates them. Need to think about this. print "MODEL POLICY: instance", instance, "handling network", network if (network.name.endswith("-nat")): continue if not Port.objects.filter(network=network, instance=instance).exists(): port = Port(network=network, instance=instance) port.save() print "MODEL POLICY: instance", instance, "created port", port
def add_instance(auth, fields): user = authenticate(username=auth.get('username'), password=auth.get('password')) images = _get_images(fields.get('image')) slices = _get_slices(fields.get('slice')) deployment_networks = _get_deployment_networks(fields.get('deploymentNetwork')) nodes = _get_nodes(fields.get('node')) if images: fields['image'] = images[0] if slices: fields['slice'] = slices[0] if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0] if nodes: fields['node'] = nodes[0] instance = Instance(**fields) auth['tenant'] = instance.slice.name instance.os_manager = OpenStackManager(auth=auth, caller = user) instance.save() return instance
def new_record(request): if not request.user.is_authenticated: return HttpResponse(status=403) must_be = ["author", 'target', 'howmuch', 'year', 'month', 'day'] for must in must_be: if must not in request.POST: return HttpResponse("there is no parameter {}".format(must), status=500) try: author_id = int(request.POST["author"]) target_id = int(request.POST["target"]) how_much = int(request.POST["howmuch"]) year = int(request.POST["year"]) month = int(request.POST["month"]) day = int(request.POST["day"]) except ValueError: return HttpResponse("not valid value of parameter", status=500) try: own = Owner.objects.get(id=author_id) tar = Target.objects.get(id=target_id) when = date(year, month, day) except Owner.DoesNotExist: return HttpResponse("invalid owner", status=500) except Target.DoesNotExist: return HttpResponse("invalid target", status=500) except TypeError: return HttpResponse("wrong date", status=500) has = Instance.objects.filter(owner=own, target=tar, when=when, how_much=how_much)[:1] if len(has) > 0: return HttpResponse(u"Запись уже есть в базе данных", status=500) record = Instance(owner=own, target=tar, when=when, how_much=how_much) tar.last_used = datetime.now() tar.save() record.save() return HttpResponse("OK")
def add_instance(auth, fields): user = authenticate(username=auth.get('username'), password=auth.get('password')) images = _get_images(fields.get('image')) slices = _get_slices(fields.get('slice')) deployment_networks = _get_deployment_networks( fields.get('deploymentNetwork')) nodes = _get_nodes(fields.get('node')) if images: fields['image'] = images[0] if slices: fields['slice'] = slices[0] if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0] if nodes: fields['node'] = nodes[0] instance = Instance(**fields) auth['tenant'] = instance.slice.name instance.os_manager = OpenStackManager(auth=auth, caller=user) instance.save() return instance
def handle_container_on_metal(instance): from core.models import Instance, Flavor, Port, Image print "MODEL POLICY: instance", instance, "handle container_on_metal" if instance.deleted: return if (instance.isolation in ["container"]) and (instance.slice.network not in ["host", "bridged"]): # Our current docker-on-metal network strategy requires that there be some # VM on the server that connects to the networks, so that # the containers can piggyback off of that configuration. if not Instance.objects.filter(slice=instance.slice, node=instance.node, isolation="vm").exists(): flavors = Flavor.objects.filter(name="m1.small") if not flavors: raise XOSConfigurationError("No m1.small flavor") images = Image.objects.filter(kind="vm") companion_instance = Instance(slice = instance.slice, node = instance.node, image = images[0], creator = instance.creator, deployment = instance.node.site_deployment.deployment, flavor = flavors[0]) companion_instance.save() print "MODEL POLICY: instance", instance, "created companion", companion_instance # Add the ports for the container for network in instance.slice.networks.all(): # hmmm... The NAT ports never become ready, because sync_ports never # instantiates them. Need to think about this. print "MODEL POLICY: instance", instance, "handling network", network if (network.name.endswith("-nat")): continue if not Port.objects.filter(network=network, instance=instance).exists(): port = Port(network = network, instance=instance) port.save() print "MODEL POLICY: instance", instance, "created port", port
def make_new_instance(self): from core.models import Instance, Flavor flavors = Flavor.objects.filter(name="m1.small") if not flavors: raise XOSConfigurationError("No m1.small flavor") (node, parent) = LeastLoadedNodeScheduler(self.slice).pick() instance = Instance(slice=self.slice, node=node, image=self.image, creator=self.slice.creator, deployment=node.site_deployment.deployment, flavor=flavors[0], isolation="vm", parent=parent) instance.save() # We rely on a special naming convention to identify the VMs that will # hole containers. instance.name = "%s-outer-%s" % (instance.slice.name, instance.id) instance.save() return instance