def find_or_make_instance_for_s_tag(self, s_tag): instance = self.find_instance_for_s_tag(self.volt.s_tag) if instance: return instance flavors = Flavor.objects.filter(name="m1.small") if not flavors: raise XOSConfigurationError("No m1.small flavor") slice = self.provider_service.slices.all()[0] if slice.default_isolation == "container_vm": (node, parent) = ContainerVmScheduler(slice).pick() else: (node, parent) = LeastLoadedNodeScheduler( slice, label=self.get_vsg_service().node_label).pick() instance = Instance(slice=slice, node=node, image=self.image, creator=self.creator, deployment=node.site_deployment.deployment, flavor=flavors[0], isolation=slice.default_isolation, parent=parent) self.save_instance(instance) return instance
def test_nat_net(self): slice3Name = self.make_slice_name() slice3 = Slice(name=slice3Name, omf_friendly=True, site=self.testSite, creator=self.testUser) slice3 = self.save_and_wait_for_enacted(slice3, nonempty_fields=["tenant_id"]) network3 = Network(name=slice3Name + "-nat", template=self.get_network_template("private-nat"), owner=slice3) # note that router_id will not be filled in for nat-net, since nat-net has no routers network3 = self.save_and_wait_for_enacted( network3, nonempty_fields=["network_id", "subnet_id", "subnet"]) network3_slice3 = NetworkSlice(network=network3, slice=slice3) network3_slice3.save() # does not need to be enacted instance3_1 = Instance(image=self.testImage, creator=self.testUser, slice=slice3, node=self.testNode, deploymentNetwork=self.testDeployment) instance3_1 = self.save_and_wait_for_enacted( instance3_1, nonempty_fields=["instance_id", "ip"]) ports = self.wait_for_ports(instance3_1, count=2) self.verify_network_names(ports, [slice3.name, "nat-net"])
def test_slice2(self): slice2Name = self.make_slice_name() slice2 = Slice(name=slice2Name, omf_friendly=True, site=self.testSite, creator=self.testUser) slice2 = self.save_and_wait_for_enacted(slice2, nonempty_fields=["tenant_id"]) network2 = Network(name=slice2Name + "-pvt", template=self.get_network_template("private"), owner=slice2) network2 = self.save_and_wait_for_enacted( network2, nonempty_fields=["network_id", "subnet_id", "router_id", "subnet"]) network2_slice2 = NetworkSlice(network=network2, slice=slice2) network2_slice2.save() # does not need to be enacted instance2_1 = Instance(image=self.testImage, creator=self.testUser, slice=slice2, node=self.testNode, deploymentNetwork=self.testDeployment) instance2_1 = self.save_and_wait_for_enacted( instance2_1, nonempty_fields=["instance_id", "ip"]) ports = self.wait_for_ports(instance2_1, count=2) self.verify_network_names(ports, [slice2.name, network2.name]) self.slice2 = slice2 self.network2 = network2
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 service has no slices") new_instance_created = False instance = None if self.get_attribute("use_same_instance_for_multiple_tenants", default=False): # Find if any existing instances can be used for this tenant slices = self.provider_service.slices.all() instance = self.pick_least_loaded_instance_in_slice(slices) if not instance: slice = self.provider_service.slices.all()[0] flavors = Flavor.objects.filter( name=slice.default_flavor) #MCORD flavor = slice.default_flavor if not flavor: flavors = Flavor.objects.filter(name="m1.small") if not flavors: raise XOSConfigurationError("No m1.small flavor") flavor = flavors[0] # default_flavor = slice.default_flavor #MCORD if slice.default_isolation == "container_vm": (node, parent) = ContainerVmScheduler(slice).pick() else: (node, parent) = LeastLoadedNodeScheduler(slice).pick() instance = Instance(slice=slice, node=node, image=self.image, creator=self.creator, deployment=node.site_deployment.deployment, flavor=flavor, isolation=slice.default_isolation, parent=parent) self.save_instance(instance) new_instance_created = True try: self.instance = instance super(TenantWithContainer, self).save() except: if new_instance_created: instance.delete() raise
def make_instance(self): slice = self.provider_service.slices.all()[0] flavors = Flavor.objects.filter(name=slice.default_flavor) if not flavors: raise XOSConfigurationError("No default flavor") default_flavor = slice.default_flavor slice = self.provider_service.slices.all()[0] if slice.default_isolation == "container_vm": (node, parent) = ContainerVmScheduler(slice).pick() else: (node, parent) = LeastLoadedNodeScheduler(slice).pick() instance = Instance(slice=slice, node=node, image=self.image, creator=self.creator, deployment=node.site_deployment.deployment, flavor=flavors[0], isolation=slice.default_isolation, parent=parent) self.save_instance(instance) return instance
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 test_slice1(self): slice1Name = self.make_slice_name() slice1 = Slice(name=slice1Name, omf_friendly=True, site=self.testSite, creator=self.testUser) slice1 = self.save_and_wait_for_enacted(slice1, nonempty_fields=["tenant_id"]) instance1 = Instance(image=self.testImage, creator=self.testUser, slice=slice1, node=self.testNode, deploymentNetwork=self.testDeployment) instance1 = self.save_and_wait_for_enacted( instance1, nonempty_fields=["instance_id", "ip"]) # instance1 should have only one port, its private network ports = self.wait_for_ports(instance1, count=1) self.verify_network_names(ports, [slice1.name]) network1 = Network(name=slice1Name + "-pvt", template=self.get_network_template("private"), owner=slice1) network1 = self.save_and_wait_for_enacted( network1, nonempty_fields=["network_id", "subnet_id", "router_id", "subnet"]) network1_slice1 = NetworkSlice(network=network1, slice=slice1) network1_slice1.save() # does not need to be enacted instance1_2 = Instance(image=self.testImage, creator=self.testUser, slice=slice1, node=self.testNode, deploymentNetwork=self.testDeployment) instance1_2 = self.save_and_wait_for_enacted( instance1_2, nonempty_fields=["instance_id", "ip"]) ports = self.wait_for_ports(instance1_2, count=2) self.verify_network_names(ports, [slice1.name, network1.name]) self.slice1 = slice1 self.network1 = network1
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 run_instance1(self): slice1Name = self.make_slice_name() slice1 = Slice(name = slice1Name, omf_friendly=True, site=self.testSite, creator=self.testUser) slice1=self.save_and_wait_for_enacted(slice1, nonempty_fields=["tenant_id"]) instance1 = Instance(image = self.testImage, creator=self.testUser, slice=slice1, node=self.testNode, deploymentNetwork=self.testDeployment) instance1=self.save_and_wait_for_enacted(instance1, nonempty_fields=["instance_id", "ip"])
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 test_shared_private_net(self): # connect network2 to slice1 self.network2.permittedSlices.add(self.slice1) network2_slice1 = NetworkSlice(network=self.network2, slice=self.slice1) network2_slice1.save() instance1_3 = Instance(image=self.testImage, creator=self.testUser, slice=self.slice1, node=self.testNode, deploymentNetwork=self.testDeployment) instance1_3 = self.save_and_wait_for_enacted( instance1_3, nonempty_fields=["instance_id", "ip"]) ports = self.wait_for_ports(instance1_3, count=3) self.verify_network_names( ports, [self.slice1.name, self.network1.name, self.network2.name])
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 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