def create_hosts(self, host_definition=None, count=None, backfill=False):
        """
        Creates host objects on this Stack. If no arguments are given, then
        all hosts available based on the Stack's blueprint host definitions
        will be created. If args are given, then only the `count` for the
        given `host_definition` will be created.

        @param host_definition (BlueprintHostDefinition object); the host
            definition to use for creating new hosts. If None, all host
            definitions for the stack's blueprint will be used.
        @param count (int); the number of hosts to create. If None, all
            hosts will be created.
        @param backfill (bool); If True, then hosts will be created with
            hostnames that fill in any gaps if necessary. If False, then
            hostnames will start at the end of the host list. This is only
            used when `host_definition` and `count` arguments are provided.
        """

        created_hosts = []

        if host_definition is None:
            host_definitions = self.blueprint.host_definitions.all()
        else:
            host_definitions = [host_definition]

        for hostdef in host_definitions:
            hosts = self.hosts.all()

            if count is None:
                start, end = 0, hostdef.count
                indexes = range(start, end)
            elif not hosts:
                start, end = 0, count
                indexes = range(start, end)
            else:
                if backfill:
                    hosts = hosts.order_by('index')

                    # The set of existing host indexes
                    host_indexes = set([h.index for h in hosts])

                    # The last index available
                    last_index = sorted(host_indexes)[-1]

                    # The set of expected indexes based on the last known
                    # index
                    expected_indexes = set(range(last_index + 1))

                    # Any gaps any the expected indexes?
                    gaps = host_indexes - expected_indexes

                    indexes = []
                    if gaps:
                        indexes = list(gaps)

                    count -= len(indexes)
                    start = sorted(host_indexes)[-1] + 1
                    end = start + count
                    indexes += range(start, end)
                else:
                    start = hosts.order_by('-index')[0].index + 1
                    end = start + count
                    indexes = xrange(start, end)

            # all components defined in the host definition
            components = hostdef.formula_components.all()

            # iterate over the host definition count and create individual
            # host records on the stack
            for i in indexes:
                hostname = hostdef.hostname_template.format(
                    namespace=self.namespace,
                    index=i
                )

                kwargs = dict(
                    index=i,
                    cloud_image=hostdef.cloud_image,
                    blueprint_host_definition=hostdef,
                    instance_size=hostdef.size,
                    hostname=hostname,
                    sir_price=hostdef.spot_price,
                    state=Host.PENDING
                )

                if hostdef.cloud_image.account.vpc_enabled:
                    kwargs['subnet_id'] = hostdef.subnet_id
                else:
                    kwargs['availability_zone'] = hostdef.zone

                host = self.hosts.create(**kwargs)

                account = host.cloud_image.account

                # Add in the cloud account default security groups as
                # defined by an admin.
                account_groups = set(list(
                    account.security_groups.filter(
                        is_default=True
                    )
                ))

                host.security_groups.add(*account_groups)

                if account.create_security_groups:
                    # Add in the security group provided by this host definition,
                    # but only if this functionality is enabled on the account
                    security_group = SecurityGroup.objects.get(
                        stack=self,
                        blueprint_host_definition=hostdef
                    )
                    host.security_groups.add(security_group)

                # add formula components
                host.formula_components.add(*components)

                for volumedef in hostdef.volumes.all():
                    self.volumes.create(
                        host=host,
                        snapshot=volumedef.snapshot,
                        hostname=hostname,
                        device=volumedef.device,
                        mount_point=volumedef.mount_point
                    )

                created_hosts.append(host)

        return created_hosts
dataset/ETHPy150Open stackdio/stackdio/stackdio/api/stacks/models.py/Stack.create_hosts/swapped operands of `-`