Skip to content

Commit

Permalink
Implement --scale option on up command, allow scale config in v2.2 fo…
Browse files Browse the repository at this point in the history
…rmat

docker-compose scale modified to reuse code between up and scale

Signed-off-by: Joffrey F <[email protected]>
  • Loading branch information
shin- committed Apr 18, 2017
1 parent 1bd9083 commit 7eb8295
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 107 deletions.
36 changes: 24 additions & 12 deletions compose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,15 +771,7 @@ 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)
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 +867,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 +890,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 +913,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 +1234,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
20 changes: 14 additions & 6 deletions compose/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,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 +403,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 +594,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 7eb8295

Please sign in to comment.