Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker-compose up --scale, scale in config file #4742

Merged
merged 4 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions compose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ..config.environment import Environment
from ..config.serialize import serialize_config
from ..config.types import VolumeSpec
from ..const import COMPOSEFILE_V2_2 as V2_2
from ..const import IS_WINDOWS_PLATFORM
from ..errors import StreamParseError
from ..progress_stream import StreamOutputError
Expand Down Expand Up @@ -763,6 +764,9 @@ def scale(self, options):

$ docker-compose scale web=2 worker=3

This command is deprecated. Use the up command with the `--scale` flag
instead.

Usage: scale [options] [SERVICE=NUM...]

Options:
Expand All @@ -771,15 +775,18 @@ def scale(self, options):
"""
timeout = timeout_from_opts(options)

for s in options['SERVICE=NUM']:
if '=' not in s:
raise UserError('Arguments to scale should be in the form service=num')
service_name, num = s.split('=', 1)
try:
num = int(num)
except ValueError:
raise UserError('Number of containers for service "%s" is not a '
'number' % service_name)
if self.project.config_version == V2_2:
raise UserError(
'The scale command is incompatible with the v2.2 format. '
'Use the up command with the --scale flag instead.'
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we relax this restriction and only error if the config file contains scale: fields in the config?

I think that will remove the need to store config_version on the project as well.

else:
log.warn(
'The scale command is deprecated. '
'Use the up command with the --scale flag instead.'
)

for service_name, num in parse_scale_args(options['SERVICE=NUM']).items():
self.project.get_service(service_name).scale(num, timeout=timeout)

def start(self, options):
Expand Down Expand Up @@ -875,7 +882,7 @@ def up(self, options):
If you want to force Compose to stop and recreate all containers, use the
`--force-recreate` flag.

Usage: up [options] [SERVICE...]
Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would just be part of options like the other options?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have to declare it separately because it's repeatable. Same goes for build for example:
Usage: build [options] [--build-arg key=val...] [SERVICE...]

or run:
Usage: run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, cool, I didn't know that.


Options:
-d Detached mode: Run containers in the background,
Expand All @@ -898,7 +905,9 @@ def up(self, options):
--remove-orphans Remove containers for services not
defined in the Compose file
--exit-code-from SERVICE Return the exit code of the selected service container.
Requires --abort-on-container-exit.
Implies --abort-on-container-exit.
--scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the `scale`
setting in the Compose file if present.
"""
start_deps = not options['--no-deps']
exit_value_from = exitval_from_opts(options, self.project)
Expand All @@ -919,7 +928,9 @@ def up(self, options):
do_build=build_action_from_opts(options),
timeout=timeout,
detached=detached,
remove_orphans=remove_orphans)
remove_orphans=remove_orphans,
scale_override=parse_scale_args(options['--scale']),
)

if detached:
return
Expand Down Expand Up @@ -1238,3 +1249,19 @@ def call_docker(args):
log.debug(" ".join(map(pipes.quote, args)))

return subprocess.call(args)


def parse_scale_args(options):
res = {}
for s in options:
if '=' not in s:
raise UserError('Arguments to scale should be in the form service=num')
service_name, num = s.split('=', 1)
try:
num = int(num)
except ValueError:
raise UserError(
'Number of containers for service "%s" is not a number' % service_name
)
res[service_name] = num
return res
1 change: 1 addition & 0 deletions compose/config/config_schema_v2.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
"privileged": {"type": "boolean"},
"read_only": {"type": "boolean"},
"restart": {"type": "string"},
"scale": {"type": "integer"},
"security_opt": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"shm_size": {"type": ["number", "string"]},
"sysctls": {"$ref": "#/definitions/list_or_dict"},
Expand Down
4 changes: 0 additions & 4 deletions compose/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,6 @@ def parallel_remove(containers, options):
parallel_operation(stopped_containers, 'remove', options, 'Removing')


def parallel_start(containers, options):
parallel_operation(containers, 'start', options, 'Starting')


def parallel_pause(containers, options):
parallel_operation(containers, 'pause', options, 'Pausing')

Expand Down
25 changes: 17 additions & 8 deletions compose/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ class Project(object):
"""
A collection of services.
"""
def __init__(self, name, services, client, networks=None, volumes=None):
def __init__(self, name, services, client, networks=None, volumes=None, config_version=None):
self.name = name
self.services = services
self.client = client
self.volumes = volumes or ProjectVolumes({})
self.networks = networks or ProjectNetworks({}, False)
self.config_version = config_version

def labels(self, one_off=OneOffFilter.exclude):
labels = ['{0}={1}'.format(LABEL_PROJECT, self.name)]
Expand All @@ -82,7 +83,7 @@ def from_config(cls, name, config_data, client):
networks,
use_networking)
volumes = ProjectVolumes.from_config(name, config_data, client)
project = cls(name, [], client, project_networks, volumes)
project = cls(name, [], client, project_networks, volumes, config_data.version)

for service_dict in config_data.services:
service_dict = dict(service_dict)
Expand Down Expand Up @@ -380,13 +381,17 @@ def up(self,
do_build=BuildAction.none,
timeout=None,
detached=False,
remove_orphans=False):
remove_orphans=False,
scale_override=None):

warn_for_swarm_mode(self.client)

self.initialize()
self.find_orphan_containers(remove_orphans)

if scale_override is None:
scale_override = {}

services = self.get_services_without_duplicate(
service_names,
include_deps=start_deps)
Expand All @@ -399,7 +404,8 @@ def do(service):
return service.execute_convergence_plan(
plans[service.name],
timeout=timeout,
detached=detached
detached=detached,
scale_override=scale_override.get(service.name)
)

def get_deps(service):
Expand Down Expand Up @@ -589,10 +595,13 @@ def get_secrets(service, service_secrets, secret_defs):
continue

if secret.uid or secret.gid or secret.mode:
log.warn("Service \"{service}\" uses secret \"{secret}\" with uid, "
"gid, or mode. These fields are not supported by this "
"implementation of the Compose file".format(
service=service, secret=secret.source))
log.warn(
"Service \"{service}\" uses secret \"{secret}\" with uid, "
"gid, or mode. These fields are not supported by this "
"implementation of the Compose file".format(
service=service, secret=secret.source
)
)

secrets.append({'secret': secret, 'file': secret_def.get('file')})

Expand Down
Loading