Added scale method to the Service model.

Signed-off-by: Felipe Ruhland <felipe.ruhland@gmail.com>
This commit is contained in:
Felipe Ruhland 2017-12-21 23:13:18 -02:00
parent 190d95c61c
commit edb9e3c2ae
2 changed files with 39 additions and 1 deletions

View File

@ -1,6 +1,6 @@
import copy
from docker.errors import create_unexpected_kwargs_error
from docker.types import TaskTemplate, ContainerSpec
from docker.types import TaskTemplate, ContainerSpec, ServiceMode
from .resource import Model, Collection
@ -105,6 +105,22 @@ class Service(Model):
)
return self.client.api.service_logs(self.id, is_tty=is_tty, **kwargs)
def scale(self, replicas):
"""
Scale service container.
Args:
replicas (int): The number of containers that should be running.
Returns:
``True``if successful.
"""
service_mode = ServiceMode('replicated', replicas)
return self.client.api.update_service(self.id, self.version,
service_mode,
fetch_current_spec=True)
class ServiceCollection(Collection):
"""Services on the Docker server."""

View File

@ -203,6 +203,28 @@ class ServiceTest(unittest.TestCase):
spec = service.attrs['Spec']['TaskTemplate']['ContainerSpec']
assert spec.get('Command') == ['sleep', '300']
def test_scale_method_service(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"
)
tasks = []
while len(tasks) == 0:
tasks = service.tasks()
assert len(tasks) == 1
service.scale(2)
while len(tasks) == 1:
tasks = service.tasks()
assert len(tasks) >= 2
# check that the container spec is not overridden with None
service.reload()
spec = service.attrs['Spec']['TaskTemplate']['ContainerSpec']
assert spec.get('Command') == ['sleep', '300']
@helpers.requires_api_version('1.25')
def test_restart_service(self):
client = docker.from_env(version=TEST_API_VERSION)