Skip to content

Commit

Permalink
Add scale option. (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein authored Jan 24, 2024
1 parent eebb73a commit 32cb76b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/776-docker_compose-scale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "docker_compose_v2 - add ``scale`` option to allow to explicitly scale services (https://github.com/ansible-collections/community.docker/pull/776)."
24 changes: 24 additions & 0 deletions plugins/modules/docker_compose_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@
- Specifies a subset of services to be targeted.
type: list
elements: str
scale:
description:
- Define how to scale services when running C(docker compose up).
- Provide a dictionary of key/value pairs where the key is the name of the service
and the value is an integer count for the number of containers.
type: dict
version_added: 3.7.0
author:
- Felix Fontein (@felixfontein)
Expand Down Expand Up @@ -376,7 +383,9 @@

import traceback

from ansible.module_utils.common.validation import check_type_int
from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.six import string_types

from ansible_collections.community.docker.plugins.module_utils.common_cli import (
AnsibleModuleDockerClient,
Expand Down Expand Up @@ -405,6 +414,18 @@ def __init__(self, client):
self.remove_orphans = parameters['remove_orphans']
self.timeout = parameters['timeout']
self.services = parameters['services'] or []
self.scale = parameters['scale'] or {}

for key, value in self.scale.items():
if not isinstance(key, string_types):
self.client.fail('The key %s for `scale` is not a string' % repr(key))
try:
value = check_type_int(value)
except TypeError as exc:
self.client.fail('The value %s for `scale[%s]` is not an integer' % (repr(value), repr(key)))
if value < 0:
self.client.fail('The value %s for `scale[%s]` is negative' % (repr(value), repr(key)))
self.scale[key] = value

def run(self):
if self.state == 'present':
Expand Down Expand Up @@ -439,6 +460,8 @@ def get_up_cmd(self, dry_run, no_start=False):
args.append('--build')
elif self.build == 'never':
args.append('--no-build')
for key, value in sorted(self.scale.items()):
args.extend(['--scale', '%s=%d' % (key, value)])
if no_start:
args.append('--no-start')
if dry_run:
Expand Down Expand Up @@ -572,6 +595,7 @@ def main():
remove_orphans=dict(type='bool', default=False),
timeout=dict(type='int'),
services=dict(type='list', elements='str'),
scale=dict(type='dict'),
)
argument_spec.update(common_compose_argspec())

Expand Down

0 comments on commit 32cb76b

Please sign in to comment.