Skip to content

Commit 7be8b4c

Browse files
committed
Merge pull request #851 from aanand/swarm-names
Handle Swarm-style prefixed container names
2 parents 37ed743 + cbd3ca0 commit 7be8b4c

4 files changed

Lines changed: 58 additions & 17 deletions

File tree

fig/container.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ def from_ps(cls, client, dictionary, **kwargs):
2222
new_dictionary = {
2323
'Id': dictionary['Id'],
2424
'Image': dictionary['Image'],
25+
'Name': '/' + get_container_name(dictionary),
2526
}
26-
for name in dictionary.get('Names', []):
27-
if len(name.split('/')) == 2:
28-
new_dictionary['Name'] = name
2927
return cls(client, new_dictionary, **kwargs)
3028

3129
@classmethod
@@ -170,3 +168,14 @@ def __eq__(self, other):
170168
if type(self) != type(other):
171169
return False
172170
return self.id == other.id
171+
172+
173+
def get_container_name(container):
174+
if not container.get('Name') and not container.get('Names'):
175+
return None
176+
# inspect
177+
if 'Name' in container:
178+
return container['Name']
179+
# ps
180+
shortest_name = min(container['Names'], key=lambda n: len(n.split('/')))
181+
return shortest_name.split('/')[-1]

fig/service.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from docker.errors import APIError
1111

12-
from .container import Container
12+
from .container import Container, get_container_name
1313
from .progress_stream import stream_output, StreamOutputError
1414

1515
log = logging.getLogger(__name__)
@@ -538,18 +538,6 @@ def parse_name(name):
538538
return ServiceName(project, service_name, int(suffix))
539539

540540

541-
def get_container_name(container):
542-
if not container.get('Name') and not container.get('Names'):
543-
return None
544-
# inspect
545-
if 'Name' in container:
546-
return container['Name']
547-
# ps
548-
for name in container['Names']:
549-
if len(name.split('/')) == 2:
550-
return name[1:]
551-
552-
553541
def parse_restart_spec(restart_config):
554542
if not restart_config:
555543
return None

tests/unit/container_test.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def setUp(self):
2020
"Ports": None,
2121
"SizeRw": 0,
2222
"SizeRootFs": 0,
23-
"Names": ["/figtest_db_1"],
23+
"Names": ["/figtest_db_1", "/figtest_web_1/db"],
2424
"NetworkSettings": {
2525
"Ports": {},
2626
},
@@ -36,6 +36,18 @@ def test_from_ps(self):
3636
"Name": "/figtest_db_1",
3737
})
3838

39+
def test_from_ps_prefixed(self):
40+
self.container_dict['Names'] = ['/swarm-host-1' + n for n in self.container_dict['Names']]
41+
42+
container = Container.from_ps(None,
43+
self.container_dict,
44+
has_been_inspected=True)
45+
self.assertEqual(container.dictionary, {
46+
"Id": "abc",
47+
"Image":"busybox:latest",
48+
"Name": "/figtest_db_1",
49+
})
50+
3951
def test_environment(self):
4052
container = Container(None, {
4153
'Id': 'abc',

tests/unit/service_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
parse_volume_spec,
1818
build_volume_binding,
1919
APIError,
20+
get_container_name,
2021
parse_repository_tag,
2122
)
2223

@@ -49,6 +50,37 @@ def test_config_validation(self):
4950
self.assertRaises(ConfigError, lambda: Service(name='foo', port=['8000']))
5051
Service(name='foo', ports=['8000'])
5152

53+
def test_get_container_name(self):
54+
self.assertIsNone(get_container_name({}))
55+
self.assertEqual(get_container_name({'Name': 'myproject_db_1'}), 'myproject_db_1')
56+
self.assertEqual(get_container_name({'Names': ['/myproject_db_1', '/myproject_web_1/db']}), 'myproject_db_1')
57+
self.assertEqual(get_container_name({'Names': ['/swarm-host-1/myproject_db_1', '/swarm-host-1/myproject_web_1/db']}), 'myproject_db_1')
58+
59+
def test_containers(self):
60+
service = Service('db', client=self.mock_client, project='myproject')
61+
62+
self.mock_client.containers.return_value = []
63+
self.assertEqual(service.containers(), [])
64+
65+
self.mock_client.containers.return_value = [
66+
{'Image': 'busybox', 'Id': 'OUT_1', 'Names': ['/myproject', '/foo/bar']},
67+
{'Image': 'busybox', 'Id': 'OUT_2', 'Names': ['/myproject_db']},
68+
{'Image': 'busybox', 'Id': 'OUT_3', 'Names': ['/db_1']},
69+
{'Image': 'busybox', 'Id': 'IN_1', 'Names': ['/myproject_db_1', '/myproject_web_1/db']},
70+
]
71+
self.assertEqual([c.id for c in service.containers()], ['IN_1'])
72+
73+
def test_containers_prefixed(self):
74+
service = Service('db', client=self.mock_client, project='myproject')
75+
76+
self.mock_client.containers.return_value = [
77+
{'Image': 'busybox', 'Id': 'OUT_1', 'Names': ['/swarm-host-1/myproject', '/swarm-host-1/foo/bar']},
78+
{'Image': 'busybox', 'Id': 'OUT_2', 'Names': ['/swarm-host-1/myproject_db']},
79+
{'Image': 'busybox', 'Id': 'OUT_3', 'Names': ['/swarm-host-1/db_1']},
80+
{'Image': 'busybox', 'Id': 'IN_1', 'Names': ['/swarm-host-1/myproject_db_1', '/swarm-host-1/myproject_web_1/db']},
81+
]
82+
self.assertEqual([c.id for c in service.containers()], ['IN_1'])
83+
5284
def test_get_volumes_from_container(self):
5385
container_id = 'aabbccddee'
5486
service = Service(

0 commit comments

Comments
 (0)