Esempio n. 1
0
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()
Esempio n. 2
0
    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
Esempio n. 3
0
    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
Esempio n. 4
0
    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
Esempio n. 5
0
    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)))
Esempio n. 6
0
 def select_by_user(user):
     if user.is_admin:
         qs = ReservedResource.objects.all()
     else:
         instance_ids = [s.id for s in Instance.select_by_user(user)]
         qs = ReservedResource.objects.filter(id__in=instance_ids)
     return qs
Esempio n. 7
0
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
Esempio n. 8
0
 def select_by_user(user):
     if user.is_admin:
         qs = Port.objects.all()
     else:
         instances = Instance.select_by_user(user)
         instance_ids = [instance.id for instance in instances]
         networks = Network.select_by_user(user)
         network_ids = [network.id for network in networks]
         qs = Port.objects.filter(Q(instance__in=instance_ids) | Q(network__in=network_ids))
     return qs
Esempio n. 9
0
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
Esempio n. 10
0
 def get_queryset(self):
     """
     Filter projects by current user.
     """
     user = self.request.user
     qs = Instance.shared_with_user(user)
     if 'archived' not in self.request.query_params:
         qs = qs.filter(only_current_instances())
     # logger.info("DEBUG- User %s querying for instances, available IDs are:%s" % (user, qs.values_list('id',flat=True)))
     qs = qs.select_related("created_by")\
         .select_related('created_by_identity')\
         .select_related('source')\
         .select_related('project')
     return qs
    def get_queryset(self):
        request_user = self.request.user
        if request_user.is_staff or request_user.is_superuser:
            instances_qs = Instance.objects.all()
        elif request_user.is_authenticated():
            instances_qs = Instance.shared_with_user(request_user)
        else:
            raise exceptions.NotAuthenticated()
        query_params = self.request.query_params
        query = self.get_filter_query(query_params)

        queryset = instances_qs.select_related('source').prefetch_related(
            'source__providermachine__application_version__application',
        ).filter(query)
        return queryset
Esempio n. 12
0
    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
Esempio n. 13
0
 def get_queryset(self):
     user = self.request.user
     qs = Instance.for_user(user)
     if 'archived' in self.request.query_params:
         return qs
     return qs.filter(only_current())
 def get_queryset(self):
     user = self.request.user
     qs = Instance.shared_with_user(user)
     if 'archived' in self.request.query_params:
         return qs
     return qs.filter(only_current())