From 9e56a7e9db380d3206129c157283bba3dcada9b8 Mon Sep 17 00:00:00 2001 From: Nicolas Delaby Date: Fri, 2 Oct 2015 21:30:33 +0200 Subject: [PATCH 1/2] Expose container_id in mine.get_docker Because sometimes we should be able to perform additional introspection. --- salt/modules/mine.py | 29 +++++++++++++----- tests/unit/modules/mine_test.py | 54 +++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 10 deletions(-) diff --git a/salt/modules/mine.py b/salt/modules/mine.py index 0ac2f4dcb0..28f6e634b4 100644 --- a/salt/modules/mine.py +++ b/salt/modules/mine.py @@ -294,13 +294,17 @@ def flush(): return _mine_send(load, __opts__) -def get_docker(interfaces=None, cidrs=None): +def get_docker(interfaces=None, cidrs=None, with_container_id=False): ''' Get all mine data for 'docker.get_containers' and run an aggregation routine. The "interfaces" parameter allows for specifying which network interfaces to select ip addresses from. The "cidrs" parameter allows for specifying a list of cidrs which the ip address must match. + with_container_id + Boolean, to expose container_id in the list of results + + CLI Example: .. code-block:: bash @@ -358,6 +362,7 @@ def get_docker(interfaces=None, cidrs=None): # Process each container for container in six.itervalues(containers): + container_id = container['Info']['Id'] if container['Image'] not in proxy_lists: proxy_lists[container['Image']] = {} for dock_port in container['Ports']: @@ -366,12 +371,22 @@ def get_docker(interfaces=None, cidrs=None): # If port is 0.0.0.0, then we must get the docker host IP if ip_address == '0.0.0.0': for ip_ in host_ips: - proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], []).append( - '{0}:{1}'.format(ip_, dock_port['PublicPort'])) - proxy_lists[container['Image']]['ipv4'][dock_port['PrivatePort']] = list(set(proxy_lists[container['Image']]['ipv4'][dock_port['PrivatePort']])) + containers = proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], []) + container_network_footprint = '{0}:{1}'.format(ip_, dock_port['PublicPort']) + if with_container_id: + value = (container_network_footprint, container_id) + else: + value = container_network_footprint + if value not in containers: + containers.append(value) elif ip_address: - proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], []).append( - '{0}:{1}'.format(dock_port['IP'], dock_port['PublicPort'])) - proxy_lists[container['Image']]['ipv4'][dock_port['PrivatePort']] = list(set(proxy_lists[container['Image']]['ipv4'][dock_port['PrivatePort']])) + containers = proxy_lists[container['Image']].setdefault('ipv4', {}).setdefault(dock_port['PrivatePort'], []) + container_network_footprint = '{0}:{1}'.format(dock_port['IP'], dock_port['PublicPort']) + if with_container_id: + value = (container_network_footprint, container_id) + else: + value = container_network_footprint + if value not in containers: + containers.append(value) return proxy_lists diff --git a/tests/unit/modules/mine_test.py b/tests/unit/modules/mine_test.py index f8e6d885d6..987c56274b 100644 --- a/tests/unit/modules/mine_test.py +++ b/tests/unit/modules/mine_test.py @@ -190,14 +190,62 @@ class MineTestCase(TestCase): 'PrivatePort': 80, 'PublicPort': 80, 'Type': 'tcp'}], - 'Image': 'image:latest' + 'Image': 'image:latest', + 'Info': {'Id': 'abcdefhjhi1234567899'}, }, }} with patch.object(mine, 'get', return_value=ps_response): self.assertEqual(mine.get_docker(), {'image:latest': { - 'ipv4': {80: ['192.168.0.1:80', - '172.17.42.1:80']}}}) + 'ipv4': {80: [ + '172.17.42.1:80', + '192.168.0.1:80', + ]}}}) + + def test_get_docker_with_container_id(self): + ''' + Test for Get all mine data for 'dockerng.ps' and run an + aggregation. + ''' + ps_response = { + 'localhost': { + 'host': { + 'interfaces': { + 'docker0': { + 'hwaddr': '88:99:00:00:99:99', + 'inet': [{'address': '172.17.42.1', + 'broadcast': None, + 'label': 'docker0', + 'netmask': '255.255.0.0'}], + 'inet6': [{'address': 'ffff::eeee:aaaa:bbbb:8888', + 'prefixlen': '64'}], + 'up': True}, + 'eth0': {'hwaddr': '88:99:00:99:99:99', + 'inet': [{'address': '192.168.0.1', + 'broadcast': '192.168.0.255', + 'label': 'eth0', + 'netmask': '255.255.255.0'}], + 'inet6': [{'address': + 'ffff::aaaa:aaaa:bbbb:8888', + 'prefixlen': '64'}], + 'up': True}, + }}, + 'abcdefhjhi1234567899': { # container Id + 'Ports': [{'IP': '0.0.0.0', # we bind on every interfaces + 'PrivatePort': 80, + 'PublicPort': 80, + 'Type': 'tcp'}], + 'Image': 'image:latest', + 'Info': {'Id': 'abcdefhjhi1234567899'}, + }, + }} + with patch.object(mine, 'get', return_value=ps_response): + self.assertEqual(mine.get_docker(with_container_id=True), + {'image:latest': { + 'ipv4': {80: [ + ('172.17.42.1:80', 'abcdefhjhi1234567899'), + ('192.168.0.1:80', 'abcdefhjhi1234567899'), + ]}}}) if __name__ == '__main__': From 7293ded2f6713bf2792ac09479424f8bbe7f8ed0 Mon Sep 17 00:00:00 2001 From: Nicolas Delaby Date: Fri, 2 Oct 2015 21:34:51 +0200 Subject: [PATCH 2/2] fixup! Expose container_id in mine.get_docker --- salt/modules/mine.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/salt/modules/mine.py b/salt/modules/mine.py index 28f6e634b4..e189bc725e 100644 --- a/salt/modules/mine.py +++ b/salt/modules/mine.py @@ -304,6 +304,8 @@ def get_docker(interfaces=None, cidrs=None, with_container_id=False): with_container_id Boolean, to expose container_id in the list of results + .. versionadded:: 2015.8.2 + CLI Example: