Skip to content

Commit

Permalink
Merge pull request #4742 from shin-/2496-new_scale
Browse files Browse the repository at this point in the history
docker-compose up --scale, scale in config file
  • Loading branch information
shin- authored Apr 27, 2017
2 parents d9902e8 + 28b8688 commit fd699c5
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 118 deletions.
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.'
)
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...]
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

0 comments on commit fd699c5

Please sign in to comment.