Merge pull request #8005 from asterite3/up-only-attach-foreground-services

Only attach services we're going to read logs from in "up"
This commit is contained in:
Ulysses Souza 2021-01-04 13:44:12 +00:00 committed by GitHub
commit 2e273c5029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 9 deletions

View File

@ -38,6 +38,7 @@ from ..service import ConvergenceStrategy
from ..service import ImageType from ..service import ImageType
from ..service import NeedsBuildError from ..service import NeedsBuildError
from ..service import OperationFailedError from ..service import OperationFailedError
from ..utils import filter_attached_for_up
from .command import get_config_from_options from .command import get_config_from_options
from .command import get_project_dir from .command import get_project_dir
from .command import project_from_options from .command import project_from_options
@ -1071,6 +1072,7 @@ class TopLevelCommand:
renew_anonymous_volumes=options.get('--renew-anon-volumes'), renew_anonymous_volumes=options.get('--renew-anon-volumes'),
silent=options.get('--quiet-pull'), silent=options.get('--quiet-pull'),
cli=native_builder, cli=native_builder,
attach_dependencies=attach_dependencies,
) )
try: try:
@ -1401,13 +1403,11 @@ def log_printer_from_project(
def filter_attached_containers(containers, service_names, attach_dependencies=False): def filter_attached_containers(containers, service_names, attach_dependencies=False):
if attach_dependencies or not service_names: return filter_attached_for_up(
return containers containers,
service_names,
return [ attach_dependencies,
container lambda container: container.service)
for container in containers if container.service in service_names
]
@contextlib.contextmanager @contextlib.contextmanager

View File

@ -39,6 +39,7 @@ from .service import Service
from .service import ServiceIpcMode from .service import ServiceIpcMode
from .service import ServiceNetworkMode from .service import ServiceNetworkMode
from .service import ServicePidMode from .service import ServicePidMode
from .utils import filter_attached_for_up
from .utils import microseconds_from_time_nano from .utils import microseconds_from_time_nano
from .utils import truncate_string from .utils import truncate_string
from .volume import ProjectVolumes from .volume import ProjectVolumes
@ -645,6 +646,7 @@ class Project:
silent=False, silent=False,
cli=False, cli=False,
one_off=False, one_off=False,
attach_dependencies=False,
override_options=None, override_options=None,
): ):
@ -671,12 +673,17 @@ class Project:
one_off=service_names if one_off else [], one_off=service_names if one_off else [],
) )
def do(service): services_to_attach = filter_attached_for_up(
services,
service_names,
attach_dependencies,
lambda service: service.name)
def do(service):
return service.execute_convergence_plan( return service.execute_convergence_plan(
plans[service.name], plans[service.name],
timeout=timeout, timeout=timeout,
detached=detached, detached=detached or (service not in services_to_attach),
scale_override=scale_override.get(service.name), scale_override=scale_override.get(service.name),
rescale=rescale, rescale=rescale,
start=start, start=start,

View File

@ -174,3 +174,18 @@ def truncate_string(s, max_chars=35):
if len(s) > max_chars: if len(s) > max_chars:
return s[:max_chars - 2] + '...' return s[:max_chars - 2] + '...'
return s return s
def filter_attached_for_up(items, service_names, attach_dependencies=False,
item_to_service_name=lambda x: x):
"""This function contains the logic of choosing which services to
attach when doing docker-compose up. It may be used both with containers
and services, and any other entities that map to service names -
this mapping is provided by item_to_service_name."""
if attach_dependencies or not service_names:
return items
return [
item
for item in items if item_to_service_name(item) in service_names
]