Fix for issue 3740. Make docker_service more resilient when targeting older API versions.

This commit is contained in:
chouseknecht 2016-05-25 16:28:53 -04:00 committed by Matt Clay
parent f6690828af
commit d0da544aef

View file

@ -599,25 +599,54 @@ class ContainerManager(DockerBaseClass):
for container in service.containers(stopped=True): for container in service.containers(stopped=True):
inspection = container.inspect() inspection = container.inspect()
# pare down the inspection data to the most useful bits # pare down the inspection data to the most useful bits
facts = dict() facts = dict(
facts['cmd'] = inspection['Config']['Cmd'] cmd=[],
facts['labels'] = inspection['Config']['Labels'] labels=dict(),
facts['image'] = inspection['Config']['Image'] image=None,
facts['state'] = dict( state=dict(
running=inspection['State']['Running'], running=None,
status=inspection['State']['Status'], status=None
),
networks=dict()
) )
facts['networks'] = dict() if inspection['Config'].get('Cmd', None) is not None:
for key, value in inspection['NetworkSettings']['Networks'].items(): facts['cmd'] = inspection['Config']['Cmd']
facts['networks'][key] = dict( if inspection['Config'].get('Labels', None) is not None:
aliases=inspection['NetworkSettings']['Networks'][key]['Aliases'], facts['labels'] = inspection['Config']['Labels']
globalIPv6=inspection['NetworkSettings']['Networks'][key]['GlobalIPv6Address'], if inspection['Config'].get('Image', None) is not None:
globalIPv6PrefixLen=inspection['NetworkSettings']['Networks'][key]['GlobalIPv6PrefixLen'], facts['image'] = inspection['Config']['Image']
IPAddress=inspection['NetworkSettings']['Networks'][key]['IPAddress'], if inspection['State'].get('Running', None) is not None:
IPPrefixLen=inspection['NetworkSettings']['Networks'][key]['IPPrefixLen'], facts['state']['running'] = inspection['State']['Running']
links=inspection['NetworkSettings']['Networks'][key]['Links'], if inspection['State'].get('Status', None) is not None:
macAddress=inspection['NetworkSettings']['Networks'][key]['MacAddress'], facts['state']['status'] = inspection['State']['Status']
)
if inspection.get('NetworkSettings') and inspection['NetworkSettings'].get('Networks'):
networks = inspection['NetworkSettings']['Networks']
for key in networks:
facts['networks'][key] = dict(
aliases=[],
globalIPv6=None,
globalIPv6PrefixLen=0,
IPAddress=None,
IPPrefixLen=0,
links=None,
macAddress=None,
)
if networks[key].get('Aliases', None) is not None:
facts['networks'][key]['aliases'] = networks[key]['Aliases']
if networks[key].get('GlobalIPv6Address', None) is not None:
facts['networks'][key]['globalIPv6'] = networks[key]['GlobalIPv6Address']
if networks[key].get('GlobalIPv6PrefixLen', None) is not None:
facts['networks'][key]['globalIPv6PrefixLen'] = networks[key]['GlobalIPv6PrefixLen']
if networks[key].get('IPAddress', None) is not None:
facts['networks'][key]['IPAddress'] = networks[key]['IPAddress']
if networks[key].get('IPPrefixLen', None) is not None:
facts['networks'][key]['IPPrefixLen'] = networks[key]['IPPrefixLen']
if networks[key].get('Links', None) is not None:
facts['networks'][key]['links'] = networks[key]['Links']
if networks[key].get('MacAddress', None) is not None:
facts['networks'][key]['macAddress'] = networks[key]['MacAddress']
result['ansible_facts'][service.name][container.name] = facts result['ansible_facts'][service.name][container.name] = facts
return result return result