Containers have names which are used to isolate them to their service

This commit is contained in:
Aanand Prasad 2013-12-09 15:00:41 +00:00
parent 630f6fcf02
commit 4e426e77fa
2 changed files with 47 additions and 2 deletions

View File

@ -14,7 +14,7 @@ class Service(object):
@property @property
def containers(self): 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): def start(self):
if len(self.containers) == 0: if len(self.containers) == 0:
@ -31,8 +31,37 @@ class Service(object):
self.stop_container() self.stop_container()
def start_container(self): 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']) self.client.start(container['Id'])
def stop_container(self): def stop_container(self):
self.client.kill(self.containers[0]['Id']) 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:]

View File

@ -39,6 +39,22 @@ class NameTestCase(ServiceTestCase):
Service('_____') 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): class ScalingTestCase(ServiceTestCase):
def setUp(self): def setUp(self):
super(ServiceTestCase, self).setUp() super(ServiceTestCase, self).setUp()