Use enum to represent 3 possible states of the one_off filter
Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
parent
187ea4cd81
commit
5826a2147b
5 changed files with 53 additions and 34 deletions
|
|
@ -22,6 +22,7 @@ from ..const import DEFAULT_TIMEOUT
|
|||
from ..const import IS_WINDOWS_PLATFORM
|
||||
from ..progress_stream import StreamOutputError
|
||||
from ..project import NoSuchService
|
||||
from ..project import OneOffFilter
|
||||
from ..service import BuildAction
|
||||
from ..service import BuildError
|
||||
from ..service import ConvergenceStrategy
|
||||
|
|
@ -437,7 +438,7 @@ class TopLevelCommand(object):
|
|||
"""
|
||||
containers = sorted(
|
||||
self.project.containers(service_names=options['SERVICE'], stopped=True) +
|
||||
self.project.containers(service_names=options['SERVICE'], one_off=True),
|
||||
self.project.containers(service_names=options['SERVICE'], one_off=OneOffFilter.only),
|
||||
key=attrgetter('name'))
|
||||
|
||||
if options['-q']:
|
||||
|
|
@ -491,11 +492,13 @@ class TopLevelCommand(object):
|
|||
Options:
|
||||
-f, --force Don't ask to confirm removal
|
||||
-v Remove volumes associated with containers
|
||||
-a, --all Also remove one-off containers
|
||||
-a, --all Also remove one-off containers created by
|
||||
docker-compose run
|
||||
"""
|
||||
one_off = OneOffFilter.include if options.get('--all') else OneOffFilter.exclude
|
||||
|
||||
all_containers = self.project.containers(
|
||||
service_names=options['SERVICE'], stopped=True,
|
||||
one_off=(None if options.get('--all') else False)
|
||||
service_names=options['SERVICE'], stopped=True, one_off=one_off
|
||||
)
|
||||
stopped_containers = [c for c in all_containers if not c.is_running]
|
||||
|
||||
|
|
@ -506,7 +509,7 @@ class TopLevelCommand(object):
|
|||
self.project.remove_stopped(
|
||||
service_names=options['SERVICE'],
|
||||
v=options.get('-v', False),
|
||||
one_off=options.get('--all')
|
||||
one_off=one_off
|
||||
)
|
||||
else:
|
||||
print("No stopped containers")
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import logging
|
|||
import operator
|
||||
from functools import reduce
|
||||
|
||||
import enum
|
||||
from docker.errors import APIError
|
||||
|
||||
from . import parallel
|
||||
|
|
@ -35,6 +36,20 @@ from .volume import ProjectVolumes
|
|||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@enum.unique
|
||||
class OneOffFilter(enum.Enum):
|
||||
include = 0
|
||||
exclude = 1
|
||||
only = 2
|
||||
|
||||
@classmethod
|
||||
def update_labels(cls, value, labels):
|
||||
if value == cls.only:
|
||||
labels.append('{0}={1}'.format(LABEL_ONE_OFF, "True"))
|
||||
elif value == cls.exclude or value is False:
|
||||
labels.append('{0}={1}'.format(LABEL_ONE_OFF, "False"))
|
||||
|
||||
|
||||
class Project(object):
|
||||
"""
|
||||
A collection of services.
|
||||
|
|
@ -48,10 +63,8 @@ class Project(object):
|
|||
|
||||
def labels(self, one_off=False):
|
||||
labels = ['{0}={1}'.format(LABEL_PROJECT, self.name)]
|
||||
if one_off is not None:
|
||||
labels.append(
|
||||
'{0}={1}'.format(LABEL_ONE_OFF, "True" if one_off else "False")
|
||||
)
|
||||
|
||||
OneOffFilter.update_labels(one_off, labels)
|
||||
return labels
|
||||
|
||||
@classmethod
|
||||
|
|
@ -253,7 +266,7 @@ class Project(object):
|
|||
|
||||
def remove_stopped(self, service_names=None, one_off=False, **options):
|
||||
parallel.parallel_remove(self.containers(
|
||||
service_names, stopped=True, one_off=(None if one_off else False)
|
||||
service_names, stopped=True, one_off=one_off
|
||||
), options)
|
||||
|
||||
def down(self, remove_image_type, include_volumes, remove_orphans=False):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue