From 63844ecf2b903391884a713eb6d9de6c4d58e6e6 Mon Sep 17 00:00:00 2001 From: Adam Doussan Date: Sun, 14 Aug 2022 11:59:53 -0500 Subject: [PATCH 1/4] better handling for default networks --- autocompose.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/autocompose.py b/autocompose.py index b206f41..575e807 100644 --- a/autocompose.py +++ b/autocompose.py @@ -27,7 +27,10 @@ def main(): cfile, c_networks, c_volumes = generate(cname) struct.update(cfile) - networks.update(c_networks) + if c_networks is None: + networks = None + else: + networks.update(c_networks) volumes.update(c_volumes) render(struct, args, networks, volumes) @@ -38,7 +41,12 @@ def render(struct, args, networks, volumes): if args.version == 1: pyaml.p(OrderedDict(struct)) else: - pyaml.p(OrderedDict({'version': '"3"', 'services': struct, 'networks': networks, 'volumes': volumes})) + ans = {'version': '"3"', 'services': struct, 'volumes': volumes} + + if networks is not None: + ans['networks'] = networks + + pyaml.p(OrderedDict(ans)) def is_date_or_time(s: str): @@ -73,6 +81,8 @@ def generate(cname): cfile[cattrs['Name'][1:]] = {} ct = cfile[cattrs['Name'][1:]] + default_networks = ['bridge', 'host', 'none'] + values = { 'cap_add': cattrs['HostConfig']['CapAdd'], 'cap_drop': cattrs['HostConfig']['CapDrop'], @@ -89,7 +99,7 @@ def generate(cname): #'log_driver': cattrs['HostConfig']['LogConfig']['Type'], #'log_opt': cattrs['HostConfig']['LogConfig']['Config'], 'logging': {'driver': cattrs['HostConfig']['LogConfig']['Type'], 'options': cattrs['HostConfig']['LogConfig']['Config']}, - 'networks': {x for x in cattrs['NetworkSettings']['Networks'].keys() if x != 'bridge'}, + 'networks': {x for x in cattrs['NetworkSettings']['Networks'].keys() if x not in default_networks}, 'security_opt': cattrs['HostConfig']['SecurityOpt'], 'ulimits': cattrs['HostConfig']['Ulimits'], 'volumes': cattrs['HostConfig']['Binds'], @@ -112,10 +122,13 @@ def generate(cname): # Populate devices key if device values are present if cattrs['HostConfig']['Devices']: values['devices'] = [x['PathOnHost']+':'+x['PathInContainer'] for x in cattrs['HostConfig']['Devices']] - + networks = {} if values['networks'] == set(): del values['networks'] + assumed_default_network = list(cattrs['NetworkSettings']['Networks'].keys())[0] + values['network_mode'] = assumed_default_network + networks = None else: networklist = c.networks.list() for network in networklist: From a2871a4d90aab185ad1ea4324fba54c9acb14fa1 Mon Sep 17 00:00:00 2001 From: Adam Doussan Date: Sun, 14 Aug 2022 13:19:39 -0500 Subject: [PATCH 2/4] fix for issues/43 --- autocompose.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/autocompose.py b/autocompose.py index 575e807..3178479 100644 --- a/autocompose.py +++ b/autocompose.py @@ -27,11 +27,16 @@ def main(): cfile, c_networks, c_volumes = generate(cname) struct.update(cfile) + if c_networks is None: networks = None else: networks.update(c_networks) - volumes.update(c_volumes) + + if c_volumes is None: + volumes = None + else: + volumes.update(c_volumes) render(struct, args, networks, volumes) @@ -41,11 +46,14 @@ def render(struct, args, networks, volumes): if args.version == 1: pyaml.p(OrderedDict(struct)) else: - ans = {'version': '"3"', 'services': struct, 'volumes': volumes} + ans = {'version': '"3"', 'services': struct} if networks is not None: ans['networks'] = networks + if volumes is not None: + ans['volumes'] = volumes + pyaml.p(OrderedDict(ans)) @@ -137,9 +145,12 @@ def generate(cname): 'name': network.attrs['Name']} volumes = {} - for volume in values['volumes']: - volume_name = volume.split(':')[0] - volumes[volume_name] = {'external': True} + if values['volumes']: + for volume in values['volumes']: + volume_name = volume.split(':')[0] + volumes[volume_name] = {'external': True} + else: + volumes = None # Check for command and add it if present. if cattrs['Config']['Cmd'] is not None: From feb55433321b6efe171ee604ff9b11644f72a47d Mon Sep 17 00:00:00 2001 From: Adam Doussan Date: Sun, 14 Aug 2022 13:28:32 -0500 Subject: [PATCH 3/4] check for none explictly rather than allowing truthy/falsy conversion --- autocompose.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autocompose.py b/autocompose.py index 3178479..3ed6ba1 100644 --- a/autocompose.py +++ b/autocompose.py @@ -145,7 +145,7 @@ def generate(cname): 'name': network.attrs['Name']} volumes = {} - if values['volumes']: + if values['volumes'] is not None: for volume in values['volumes']: volume_name = volume.split(':')[0] volumes[volume_name] = {'external': True} From 435306c2e1884071a433c604320165d4dbd6e4c0 Mon Sep 17 00:00:00 2001 From: Adam Doussan Date: Sun, 14 Aug 2022 13:48:32 -0500 Subject: [PATCH 4/4] update to read volume data from mounts, rather than host config binds --- autocompose.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autocompose.py b/autocompose.py index 3ed6ba1..856c37f 100644 --- a/autocompose.py +++ b/autocompose.py @@ -110,7 +110,7 @@ def generate(cname): 'networks': {x for x in cattrs['NetworkSettings']['Networks'].keys() if x not in default_networks}, 'security_opt': cattrs['HostConfig']['SecurityOpt'], 'ulimits': cattrs['HostConfig']['Ulimits'], - 'volumes': cattrs['HostConfig']['Binds'], + 'volumes': [f'{m["Name"]}:{m["Destination"]}' for m in cattrs['Mounts'] if m['Type'] == 'volume'], 'volume_driver': cattrs['HostConfig']['VolumeDriver'], 'volumes_from': cattrs['HostConfig']['VolumesFrom'], 'entrypoint': cattrs['Config']['Entrypoint'],