Merge pull request #5397 from sethktaylor11/4937-Labels

Added a label option to 'docker-compose run' and test.
This commit is contained in:
Joffrey F 2017-11-29 11:42:24 -08:00 committed by GitHub
commit 58dcfac853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 3 deletions

View File

@ -24,6 +24,7 @@ from ..bundle import MissingDigests
from ..bundle import serialize_bundle from ..bundle import serialize_bundle
from ..config import ConfigurationError from ..config import ConfigurationError
from ..config import parse_environment from ..config import parse_environment
from ..config import parse_labels
from ..config import resolve_build_args from ..config import resolve_build_args
from ..config.environment import Environment from ..config.environment import Environment
from ..config.serialize import serialize_config from ..config.serialize import serialize_config
@ -720,7 +721,9 @@ class TopLevelCommand(object):
running. If you do not want to start linked services, use running. If you do not want to start linked services, use
`docker-compose run --no-deps SERVICE COMMAND [ARGS...]`. `docker-compose run --no-deps SERVICE COMMAND [ARGS...]`.
Usage: run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...] Usage:
run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...]
SERVICE [COMMAND] [ARGS...]
Options: Options:
-d Detached mode: Run container in the background, print -d Detached mode: Run container in the background, print
@ -728,6 +731,7 @@ class TopLevelCommand(object):
--name NAME Assign a name to the container --name NAME Assign a name to the container
--entrypoint CMD Override the entrypoint of the image. --entrypoint CMD Override the entrypoint of the image.
-e KEY=VAL Set an environment variable (can be used multiple times) -e KEY=VAL Set an environment variable (can be used multiple times)
-l, --label KEY=VAL Add or override a label (can be used multiple times)
-u, --user="" Run as specified username or uid -u, --user="" Run as specified username or uid
--no-deps Don't start linked services. --no-deps Don't start linked services.
--rm Remove container after run. Ignored in detached mode. --rm Remove container after run. Ignored in detached mode.
@ -1122,6 +1126,9 @@ def build_container_options(options, detach, command):
parse_environment(options['-e']) parse_environment(options['-e'])
) )
if options['--label']:
container_options['labels'] = parse_labels(options['--label'])
if options['--entrypoint']: if options['--entrypoint']:
container_options['entrypoint'] = options.get('--entrypoint') container_options['entrypoint'] = options.get('--entrypoint')

View File

@ -8,5 +8,7 @@ from .config import DOCKER_CONFIG_KEYS
from .config import find from .config import find
from .config import load from .config import load
from .config import merge_environment from .config import merge_environment
from .config import merge_labels
from .config import parse_environment from .config import parse_environment
from .config import parse_labels
from .config import resolve_build_args from .config import resolve_build_args

View File

@ -1076,6 +1076,12 @@ def merge_environment(base, override):
return env return env
def merge_labels(base, override):
labels = parse_labels(base)
labels.update(parse_labels(override))
return labels
def split_kv(kvpair): def split_kv(kvpair):
if '=' in kvpair: if '=' in kvpair:
return kvpair.split('=', 1) return kvpair.split('=', 1)

View File

@ -25,6 +25,7 @@ from . import const
from . import progress_stream from . import progress_stream
from .config import DOCKER_CONFIG_KEYS from .config import DOCKER_CONFIG_KEYS
from .config import merge_environment from .config import merge_environment
from .config import merge_labels
from .config.errors import DependencyError from .config.errors import DependencyError
from .config.types import ServicePort from .config.types import ServicePort
from .config.types import VolumeSpec from .config.types import VolumeSpec
@ -778,6 +779,10 @@ class Service(object):
self.options.get('environment'), self.options.get('environment'),
override_options.get('environment')) override_options.get('environment'))
container_options['labels'] = merge_labels(
self.options.get('labels'),
override_options.get('labels'))
binds, affinity = merge_volume_bindings( binds, affinity = merge_volume_bindings(
container_options.get('volumes') or [], container_options.get('volumes') or [],
self.options.get('tmpfs') or [], self.options.get('tmpfs') or [],

View File

@ -403,14 +403,14 @@ _docker_compose_run() {
__docker_compose_nospace __docker_compose_nospace
return return
;; ;;
--entrypoint|--name|--user|-u|--volume|-v|--workdir|-w) --entrypoint|--label|-l|--name|--user|-u|--volume|-v|--workdir|-w)
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-d --entrypoint -e --help --name --no-deps --publish -p --rm --service-ports -T --user -u --volume -v --workdir -w" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-d --entrypoint -e --help --label -l --name --no-deps --publish -p --rm --service-ports -T --user -u --volume -v --workdir -w" -- "$cur" ) )
;; ;;
*) *)
__docker_compose_services_all __docker_compose_services_all

View File

@ -1830,6 +1830,17 @@ class CLITestCase(DockerClientTestCase):
assert 'FOO=bar' in environment assert 'FOO=bar' in environment
assert 'BAR=baz' not in environment assert 'BAR=baz' not in environment
def test_run_label_flag(self):
self.base_dir = 'tests/fixtures/run-labels'
name = 'service'
self.dispatch(['run', '-l', 'default', '--label', 'foo=baz', name, '/bin/true'])
service = self.project.get_service(name)
container, = service.containers(stopped=True, one_off=OneOffFilter.only)
labels = container.labels
assert labels['default'] == ''
assert labels['foo'] == 'baz'
assert labels['hello'] == 'world'
def test_rm(self): def test_rm(self):
service = self.project.get_service('simple') service = self.project.get_service('simple')
service.create_container() service.create_container()

View File

@ -0,0 +1,7 @@
service:
image: busybox:latest
command: top
labels:
foo: bar
hello: world

View File

@ -114,6 +114,7 @@ class CLITestCase(unittest.TestCase):
'SERVICE': 'service', 'SERVICE': 'service',
'COMMAND': None, 'COMMAND': None,
'-e': [], '-e': [],
'--label': [],
'--user': None, '--user': None,
'--no-deps': None, '--no-deps': None,
'-d': False, '-d': False,
@ -150,6 +151,7 @@ class CLITestCase(unittest.TestCase):
'SERVICE': 'service', 'SERVICE': 'service',
'COMMAND': None, 'COMMAND': None,
'-e': [], '-e': [],
'--label': [],
'--user': None, '--user': None,
'--no-deps': None, '--no-deps': None,
'-d': True, '-d': True,
@ -173,6 +175,7 @@ class CLITestCase(unittest.TestCase):
'SERVICE': 'service', 'SERVICE': 'service',
'COMMAND': None, 'COMMAND': None,
'-e': [], '-e': [],
'--label': [],
'--user': None, '--user': None,
'--no-deps': None, '--no-deps': None,
'-d': True, '-d': True,
@ -205,6 +208,7 @@ class CLITestCase(unittest.TestCase):
'SERVICE': 'service', 'SERVICE': 'service',
'COMMAND': None, 'COMMAND': None,
'-e': [], '-e': [],
'--label': [],
'--user': None, '--user': None,
'--no-deps': None, '--no-deps': None,
'-d': True, '-d': True,