Skip to content
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
15 changes: 15 additions & 0 deletions docker/models/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def update(self, **kwargs):
spec = self.attrs['Spec']['TaskTemplate']['ContainerSpec']
kwargs['image'] = spec['Image']

if kwargs.get('force_update') is True:
task_template = self.attrs['Spec']['TaskTemplate']
current_value = int(task_template.get('ForceUpdate', 0))
kwargs['force_update'] = current_value + 1

create_kwargs = _get_create_service_kwargs('update', kwargs)

return self.client.api.update_service(
Expand Down Expand Up @@ -124,6 +129,16 @@ def scale(self, replicas):
service_mode,
fetch_current_spec=True)

def force_update(self):
"""
Force update the service even if no changes require it.

Returns:
``True``if successful.
"""

return self.update(force_update=True, fetch_current_spec=True)


class ServiceCollection(Collection):
"""Services on the Docker server."""
Expand Down
41 changes: 39 additions & 2 deletions tests/integration/models_services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def test_scale_method_global_service(self):
assert spec.get('Command') == ['sleep', '300']

@helpers.requires_api_version('1.25')
def test_restart_service(self):
def test_force_update_service(self):
client = docker.from_env(version=TEST_API_VERSION)
service = client.services.create(
# create arguments
Expand All @@ -286,7 +286,7 @@ def test_restart_service(self):
command="sleep 300"
)
initial_version = service.version
service.update(
assert service.update(
# create argument
name=service.name,
# task template argument
Expand All @@ -296,3 +296,40 @@ def test_restart_service(self):
)
service.reload()
assert service.version > initial_version

@helpers.requires_api_version('1.25')
def test_force_update_service_using_bool(self):
client = docker.from_env(version=TEST_API_VERSION)
service = client.services.create(
# create arguments
name=helpers.random_name(),
# ContainerSpec arguments
image="alpine",
command="sleep 300"
)
initial_version = service.version
assert service.update(
# create argument
name=service.name,
# task template argument
force_update=True,
# ContainerSpec argument
command="sleep 600"
)
service.reload()
assert service.version > initial_version

@helpers.requires_api_version('1.25')
def test_force_update_service_using_shorthand_method(self):
client = docker.from_env(version=TEST_API_VERSION)
service = client.services.create(
# create arguments
name=helpers.random_name(),
# ContainerSpec arguments
image="alpine",
command="sleep 300"
)
initial_version = service.version
assert service.force_update()
service.reload()
assert service.version > initial_version