'Adjusts the number of containers to the specified number and ensures
they are running.
- creates containers until there are at least `desired_num`
- stops containers until there are at most `desired_num` running
- starts containers until there are at least `desired_num` running
- removes all stopped containers'

def scale(self, desired_num, timeout=None):

    self.show_scale_warnings(desired_num)
    running_containers = self.containers(stopped=False)
    num_running = len(running_containers)
    if (desired_num == num_running):
        log.info(u'Desired    container    number    already    achieved')
        return
    if (desired_num > num_running):
        all_containers = self.containers(stopped=True)
        if (num_running != len(all_containers)):
            stopped_containers = [c for c in all_containers if (not c.is_running)]
            divergent_containers = [c for c in stopped_containers if self._containers_have_diverged([c])]
            for c in divergent_containers:
                c.remove()
            all_containers = list((set(all_containers) - set(divergent_containers)))
        sorted_containers = sorted(all_containers, key=attrgetter(u'number'))
        self._execute_convergence_start(sorted_containers, desired_num, timeout, True, True)
    if (desired_num < num_running):
        num_to_stop = (num_running - desired_num)
        sorted_running_containers = sorted(running_containers, key=attrgetter(u'number'))
        self._downscale(sorted_running_containers[(- num_to_stop):], timeout)
