示例#1
0
def generic_shepherd(self, preconfigured):
    """This task takes care of having the required templates spinned into required number of
    appliances. For each template group, it keeps the last template's appliances spinned up in
    required quantity. If new template comes out of the door, it automatically kills the older
    running template's appliances and spins up new ones. Sorts the groups by the fulfillment."""
    for gs in sorted(
            GroupShepherd.objects.all(),
            key=lambda g: g.get_fulfillment_percentage(preconfigured)):
        prov_filter = {'provider__user_groups': gs.user_group}
        group_versions = Template.get_versions(
            template_group=gs.template_group,
            ready=True,
            usable=True,
            preconfigured=preconfigured,
            **prov_filter)
        group_dates = Template.get_dates(template_group=gs.template_group,
                                         ready=True,
                                         usable=True,
                                         preconfigured=preconfigured,
                                         **prov_filter)
        if group_versions:
            # Downstream - by version (downstream releases)
            version = group_versions[0]
            # Find the latest date (one version can have new build)
            dates = Template.get_dates(template_group=gs.template_group,
                                       ready=True,
                                       usable=True,
                                       version=group_versions[0],
                                       preconfigured=preconfigured,
                                       **prov_filter)
            if not dates:
                # No template yet?
                continue
            date = dates[0]
            filter_keep = {"version": version, "date": date}
            filters_kill = []
            for kill_date in dates[1:]:
                filters_kill.append({"version": version, "date": kill_date})
            for kill_version in group_versions[1:]:
                filters_kill.append({"version": kill_version})
        elif group_dates:
            # Upstream - by date (upstream nightlies)
            filter_keep = {"date": group_dates[0]}
            filters_kill = [{"date": v} for v in group_dates[1:]]
        else:
            continue  # Ignore this group, no templates detected yet

        filter_keep.update(prov_filter)
        for filt in filters_kill:
            filt.update(prov_filter)
        # Keeping current appliances
        # Retrieve list of all templates for given group
        # I know joins might be a bit better solution but I'll leave that for later.
        possible_templates = list(
            Template.objects.filter(usable=True,
                                    ready=True,
                                    template_group=gs.template_group,
                                    preconfigured=preconfigured,
                                    **filter_keep).all())
        # If it can be deployed, it must exist
        possible_templates_for_provision = [
            tpl for tpl in possible_templates if tpl.exists
        ]
        appliances = []
        for template in possible_templates:
            appliances.extend(
                Appliance.objects.filter(template=template,
                                         appliance_pool=None,
                                         marked_for_deletion=False))
        # If we then want to delete some templates, better kill the eldest. status_changed
        # says which one was provisioned when, because nothing else then touches that field.
        appliances.sort(key=lambda appliance: appliance.status_changed)
        pool_size = gs.template_pool_size if preconfigured else gs.unconfigured_template_pool_size
        if len(appliances) < pool_size and possible_templates_for_provision:
            # There must be some templates in order to run the provisioning
            # Provision ONE appliance at time for each group, that way it is possible to maintain
            # reasonable balancing
            with transaction.atomic():
                # Now look for templates that are on non-busy providers
                tpl_free = [
                    t for t in possible_templates_for_provision
                    if not t.provider.disabled and t.provider.free
                ]
                if tpl_free:
                    chosen_template = sorted(
                        tpl_free, key=lambda t: t.provider.appliance_load)[0]
                    new_appliance_name = gen_appliance_name(chosen_template.id)
                    appliance = Appliance(template=chosen_template,
                                          name=new_appliance_name)
                    appliance.save()
                    self.logger.info("Adding an appliance to shepherd: %s/%s",
                                     appliance.id, appliance.name)
                    clone_template_to_appliance.delay(appliance.id, None)
        elif len(appliances) > pool_size:
            # Too many appliances, kill the surplus
            # Only kill those that are visible only for one group. This is necessary so the groups
            # don't "fight"
            for appliance in appliances[:len(appliances) - pool_size]:
                if appliance.is_visible_only_in_group(gs.user_group):
                    self.logger.info(
                        "Killing an extra appliance {}/{} in shepherd".format(
                            appliance.id, appliance.name))
                    Appliance.kill(appliance)

        # Killing old appliances
        for filter_kill in filters_kill:
            for template in Template.objects.filter(
                    ready=True,
                    usable=True,
                    template_group=gs.template_group,
                    preconfigured=preconfigured,
                    **filter_kill):

                for a in Appliance.objects.filter(template=template,
                                                  appliance_pool=None,
                                                  marked_for_deletion=False):
                    self.logger.info(
                        "Killing appliance {}/{} in shepherd because it is obsolete now"
                        .format(a.id, a.name))
                    Appliance.kill(a)