mirror of
https://github.com/docker/compose.git
synced 2025-07-26 23:24:05 +02:00
Handle Swarm-style prefixed names in Container.from_ps()
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
edb6b24b8f
commit
cbd3ca07c4
@ -22,10 +22,8 @@ class Container(object):
|
|||||||
new_dictionary = {
|
new_dictionary = {
|
||||||
'Id': dictionary['Id'],
|
'Id': dictionary['Id'],
|
||||||
'Image': dictionary['Image'],
|
'Image': dictionary['Image'],
|
||||||
|
'Name': '/' + get_container_name(dictionary),
|
||||||
}
|
}
|
||||||
for name in dictionary.get('Names', []):
|
|
||||||
if len(name.split('/')) == 2:
|
|
||||||
new_dictionary['Name'] = name
|
|
||||||
return cls(client, new_dictionary, **kwargs)
|
return cls(client, new_dictionary, **kwargs)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -170,3 +168,14 @@ class Container(object):
|
|||||||
if type(self) != type(other):
|
if type(self) != type(other):
|
||||||
return False
|
return False
|
||||||
return self.id == other.id
|
return self.id == other.id
|
||||||
|
|
||||||
|
|
||||||
|
def get_container_name(container):
|
||||||
|
if not container.get('Name') and not container.get('Names'):
|
||||||
|
return None
|
||||||
|
# inspect
|
||||||
|
if 'Name' in container:
|
||||||
|
return container['Name']
|
||||||
|
# ps
|
||||||
|
shortest_name = min(container['Names'], key=lambda n: len(n.split('/')))
|
||||||
|
return shortest_name.split('/')[-1]
|
||||||
|
@ -9,7 +9,7 @@ import sys
|
|||||||
|
|
||||||
from docker.errors import APIError
|
from docker.errors import APIError
|
||||||
|
|
||||||
from .container import Container
|
from .container import Container, get_container_name
|
||||||
from .progress_stream import stream_output, StreamOutputError
|
from .progress_stream import stream_output, StreamOutputError
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -538,17 +538,6 @@ def parse_name(name):
|
|||||||
return ServiceName(project, service_name, int(suffix))
|
return ServiceName(project, service_name, int(suffix))
|
||||||
|
|
||||||
|
|
||||||
def get_container_name(container):
|
|
||||||
if not container.get('Name') and not container.get('Names'):
|
|
||||||
return None
|
|
||||||
# inspect
|
|
||||||
if 'Name' in container:
|
|
||||||
return container['Name']
|
|
||||||
# ps
|
|
||||||
shortest_name = min(container['Names'], key=lambda n: len(n.split('/')))
|
|
||||||
return shortest_name.split('/')[-1]
|
|
||||||
|
|
||||||
|
|
||||||
def parse_restart_spec(restart_config):
|
def parse_restart_spec(restart_config):
|
||||||
if not restart_config:
|
if not restart_config:
|
||||||
return None
|
return None
|
||||||
|
@ -20,7 +20,7 @@ class ContainerTest(unittest.TestCase):
|
|||||||
"Ports": None,
|
"Ports": None,
|
||||||
"SizeRw": 0,
|
"SizeRw": 0,
|
||||||
"SizeRootFs": 0,
|
"SizeRootFs": 0,
|
||||||
"Names": ["/figtest_db_1"],
|
"Names": ["/figtest_db_1", "/figtest_web_1/db"],
|
||||||
"NetworkSettings": {
|
"NetworkSettings": {
|
||||||
"Ports": {},
|
"Ports": {},
|
||||||
},
|
},
|
||||||
@ -36,6 +36,18 @@ class ContainerTest(unittest.TestCase):
|
|||||||
"Name": "/figtest_db_1",
|
"Name": "/figtest_db_1",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def test_from_ps_prefixed(self):
|
||||||
|
self.container_dict['Names'] = ['/swarm-host-1' + n for n in self.container_dict['Names']]
|
||||||
|
|
||||||
|
container = Container.from_ps(None,
|
||||||
|
self.container_dict,
|
||||||
|
has_been_inspected=True)
|
||||||
|
self.assertEqual(container.dictionary, {
|
||||||
|
"Id": "abc",
|
||||||
|
"Image":"busybox:latest",
|
||||||
|
"Name": "/figtest_db_1",
|
||||||
|
})
|
||||||
|
|
||||||
def test_environment(self):
|
def test_environment(self):
|
||||||
container = Container(None, {
|
container = Container(None, {
|
||||||
'Id': 'abc',
|
'Id': 'abc',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user