diff --git a/plum/service.py b/plum/service.py index 2d832182a..6b928564d 100644 --- a/plum/service.py +++ b/plum/service.py @@ -14,7 +14,7 @@ class Service(object): @property def containers(self): - return self.client.containers() + return [c for c in self.client.containers() if parse_name(get_container_name(c))[0] == self.name] def start(self): if len(self.containers) == 0: @@ -31,8 +31,37 @@ class Service(object): self.stop_container() def start_container(self): - container = self.client.create_container(self.image, self.command) + number = self.next_container_number() + name = make_name(self.name, number) + container = self.client.create_container(self.image, self.command, name=name) self.client.start(container['Id']) def stop_container(self): self.client.kill(self.containers[0]['Id']) + + def next_container_number(self): + numbers = [parse_name(get_container_name(c))[1] for c in self.containers] + + if len(numbers) == 0: + return 1 + else: + return max(numbers) + 1 + + +def make_name(prefix, number): + return '%s_%s' % (prefix, number) + + +def parse_name(name): + match = re.match('^(.+)_(\d+)$', name) + + if match is None: + raise ValueError("Invalid name: %s" % name) + + (service_name, suffix) = match.groups() + + return (service_name, int(suffix)) + + +def get_container_name(container): + return container['Names'][0][1:] diff --git a/plum/tests/service_test.py b/plum/tests/service_test.py index 5870c30da..e8db26ff0 100644 --- a/plum/tests/service_test.py +++ b/plum/tests/service_test.py @@ -39,6 +39,22 @@ class NameTestCase(ServiceTestCase): Service('_____') +class ContainersTestCase(ServiceTestCase): + def test_containers(self): + foo = self.create_service('foo') + bar = self.create_service('bar') + + foo.start() + + self.assertEqual(len(foo.containers), 1) + self.assertEqual(len(bar.containers), 0) + + bar.scale(2) + + self.assertEqual(len(foo.containers), 1) + self.assertEqual(len(bar.containers), 2) + + class ScalingTestCase(ServiceTestCase): def setUp(self): super(ServiceTestCase, self).setUp()