Skip to content

Commit 0601df8

Browse files
author
chouseknecht
committed
Fix network comparison. Fix handling of links. Updated doc strings. Added more examples.
1 parent d9c751b commit 0601df8

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

cloud/docker/docker_container.py

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@
259259
- Each network is a dict with keys C(name), C(ipv4_address), C(ipv6_address), C(links), C(aliases).
260260
- For each network C(name) is required, all other keys are optional.
261261
- If included, C(links) or C(aliases) are lists.
262-
- For more information see U(https://docs.docker.com/engine/userguide/networking/dockernetworks/).
262+
- For examples of the data structure and usage see EXAMPLES below.
263263
- To remove a container from one or more networks, use the C(purge_networks) option.
264264
default: null
265265
required: false
@@ -303,7 +303,8 @@
303303
required: false
304304
purge_networks:
305305
description:
306-
- Remove the container from all networks not included in C(networks) parameter.
306+
- Remove the container from ALL networks not included in C(networks) parameter.
307+
- Any default networks such as I(bridge), if not found in C(networks), will be removed as well.
307308
default: false
308309
required: false
309310
version_added: "2.2"
@@ -357,24 +358,24 @@
357358
required: false
358359
state:
359360
description:
360-
- '"absent" - A container matching the specified name will be stopped and removed. Use force_kill to kill the container
361+
- 'I(absent) - A container matching the specified name will be stopped and removed. Use force_kill to kill the container
361362
rather than stopping it. Use keep_volumes to retain volumes associated with the removed container.'
362-
- '"present" - Asserts the existence of a container matching the name and any provided configuration parameters. If no
363+
- 'I(present)" - Asserts the existence of a container matching the name and any provided configuration parameters. If no
363364
container matches the name, a container will be created. If a container matches the name but the provided configuration
364365
does not match, the container will be updated, if it can be. If it cannot be updated, it will be removed and re-created
365366
with the requested config. Image version will be taken into account when comparing configuration. To ignore image
366367
version use the ignore_image option. Use the recreate option to force the re-creation of the matching container. Use
367368
force_kill to kill the container rather than stopping it. Use keep_volumes to retain volumes associated with a removed
368369
container.'
369-
- '"started" - Asserts there is a running container matching the name and any provided configuration. If no container
370+
- 'I(started) - Asserts there is a running container matching the name and any provided configuration. If no container
370371
matches the name, a container will be created and started. If a container matching the name is found but the
371372
configuration does not match, the container will be updated, if it can be. If it cannot be updated, it will be removed
372373
and a new container will be created with the requested configuration and started. Image version will be taken into
373374
account when comparing configuration. To ignore image version use the ignore_image option. Use recreate to always
374375
re-create a matching container, even if it is running. Use restart to force a matching container to be stopped and
375376
restarted. Use force_kill to kill a container rather than stopping it. Use keep_volumes to retain volumes associated
376377
with a removed container.'
377-
- '"stopped" - a container matching the specified name will be stopped. Use force_kill to kill a container rather than
378+
- 'I(stopped) - a container matching the specified name will be stopped. Use force_kill to kill a container rather than
378379
stopping it.'
379380
required: false
380381
default: started
@@ -525,6 +526,28 @@
525526
syslog-facility: daemon
526527
syslog-tag: myservice
527528
529+
- name: Create db container and connect to network
530+
docker_container:
531+
name: db_test
532+
image: "postgres:latest"
533+
networks:
534+
- name: "{{ docker_network_name }}"
535+
debug: "{{ playbook_debug }}"
536+
register: output
537+
538+
- name: Start container, connect to network and link
539+
docker_container:
540+
name: sleeper
541+
image: ubuntu:14.04
542+
networks:
543+
- name: TestingNet
544+
ipv4_address: "172.1.1.100"
545+
aliases:
546+
- sleepyzz
547+
links:
548+
- db_test:db
549+
- name: TestingNet2
550+
528551
- name: Start a container with a command
529552
docker_container:
530553
name: sleepy
@@ -538,6 +561,8 @@
538561
networks:
539562
- name: TestingNet
540563
ipv4_address: 172.1.1.18
564+
links:
565+
- sleeper
541566
- name: TestingNet2
542567
ipv4_address: 172.1.10.20
543568
@@ -711,7 +736,7 @@ def __init__(self, client):
711736
self.publish_all_ports = True
712737
self.published_ports = None
713738

714-
self.links = self._parse_links()
739+
self.links = self._parse_links(self.links)
715740

716741
if self.volumes:
717742
self.volumes = self._expand_host_paths()
@@ -732,6 +757,8 @@ def __init__(self, client):
732757
network['id'] = self._get_network_id(network['name'])
733758
if not network['id']:
734759
self.fail("Parameter error: network named %s could not be found. Does it exist?" % network['name'])
760+
if network.get('links'):
761+
network['links'] = self._parse_links(network['links'])
735762

736763
def fail(self, msg):
737764
self.client.module.fail_json(msg=msg)
@@ -940,21 +967,22 @@ def _parse_exposed_ports(self):
940967
exposed.append(port_with_proto)
941968
return exposed
942969

943-
def _parse_links(self):
970+
@staticmethod
971+
def _parse_links(links):
944972
'''
945973
Turn links into a dictionary
946974
'''
947-
if self.links is None:
975+
if links is None:
948976
return None
949977

950-
links = {}
951-
for link in self.links:
978+
result = {}
979+
for link in links:
952980
parsed_link = link.split(':', 1)
953981
if len(parsed_link) == 2:
954-
links[parsed_link[0]] = parsed_link[1]
982+
result[parsed_link[0]] = parsed_link[1]
955983
else:
956-
links[parsed_link[0]] = parsed_link[0]
957-
return links
984+
result[parsed_link[0]] = parsed_link[0]
985+
return result
958986

959987
def _parse_ulimits(self):
960988
'''
@@ -1272,11 +1300,19 @@ def has_network_differences(self):
12721300
diff = True
12731301
if network.get('ipv6_address') and network['ipv6_address'] != connected_networks[network['name']].get('GlobalIPv6Address'):
12741302
diff = True
1275-
if network.get('aliases') and network['aliases'] != connected_networks[network['name']].get('Aliases'):
1276-
self.log('network aliases different')
1303+
if network.get('aliases') and not connected_networks[network['name']].get('Aliases'):
12771304
diff = True
1278-
if network.get('links') and network['links'] != connected_networks[network['name']].get('Links'):
1305+
if network.get('aliases') and connected_networks[network['name']].get('Aliases'):
1306+
if set(network.get('aliases')) != set(connected_networks[network['name']].get('Aliases')):
1307+
diff = True
1308+
if network.get('links') and not connected_networks[network['name']].get('Links'):
12791309
diff = True
1310+
if network.get('links') and connected_networks[network['name']].get('Links'):
1311+
expected_links = []
1312+
for link, alias in network['links'].iteritems():
1313+
expected_links.append("%s:%s" % (link, alias))
1314+
if set(expected_links) != set(connected_networks[network['name']].get('Links', [])):
1315+
diff = True
12801316
if diff:
12811317
different = True
12821318
differences.append(dict(
@@ -1289,7 +1325,6 @@ def has_network_differences(self):
12891325
links=connected_networks[network['name']].get('Links')
12901326
)
12911327
))
1292-
self.log(differences, pretty_print=True)
12931328
return different, differences
12941329

12951330
def has_extra_networks(self):
@@ -1632,6 +1667,7 @@ def _add_networks(self, container, differences):
16321667
if not self.check_mode:
16331668
try:
16341669
self.log("Connecting conainer to network %s" % diff['parameter']['id'])
1670+
self.log(params, pretty_print=True)
16351671
self.client.connect_container_to_network(container.Id, diff['parameter']['id'], **params)
16361672
except Exception, exc:
16371673
self.fail("Error connecting container to network %s - %s" % (diff['parameter']['name'], str(exc)))

0 commit comments

Comments
 (0)