Skip to content

Commit 357fef9

Browse files
acdoussanAdam Doussan
andauthored
better handling for default networks (#42)
* better handling for default networks * fix for issues/43 * check for none explictly rather than allowing truthy/falsy conversion * update to read volume data from mounts, rather than host config binds Co-authored-by: Adam Doussan <[email protected]>
1 parent 0c4ff4f commit 357fef9

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

autocompose.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ def main():
2727
cfile, c_networks, c_volumes = generate(cname)
2828

2929
struct.update(cfile)
30-
networks.update(c_networks)
31-
volumes.update(c_volumes)
30+
31+
if c_networks is None:
32+
networks = None
33+
else:
34+
networks.update(c_networks)
35+
36+
if c_volumes is None:
37+
volumes = None
38+
else:
39+
volumes.update(c_volumes)
3240

3341
render(struct, args, networks, volumes)
3442

@@ -38,7 +46,15 @@ def render(struct, args, networks, volumes):
3846
if args.version == 1:
3947
pyaml.p(OrderedDict(struct))
4048
else:
41-
pyaml.p(OrderedDict({'version': '"3"', 'services': struct, 'networks': networks, 'volumes': volumes}))
49+
ans = {'version': '"3"', 'services': struct}
50+
51+
if networks is not None:
52+
ans['networks'] = networks
53+
54+
if volumes is not None:
55+
ans['volumes'] = volumes
56+
57+
pyaml.p(OrderedDict(ans))
4258

4359

4460
def is_date_or_time(s: str):
@@ -73,6 +89,8 @@ def generate(cname):
7389
cfile[cattrs['Name'][1:]] = {}
7490
ct = cfile[cattrs['Name'][1:]]
7591

92+
default_networks = ['bridge', 'host', 'none']
93+
7694
values = {
7795
'cap_add': cattrs['HostConfig']['CapAdd'],
7896
'cap_drop': cattrs['HostConfig']['CapDrop'],
@@ -89,10 +107,10 @@ def generate(cname):
89107
#'log_driver': cattrs['HostConfig']['LogConfig']['Type'],
90108
#'log_opt': cattrs['HostConfig']['LogConfig']['Config'],
91109
'logging': {'driver': cattrs['HostConfig']['LogConfig']['Type'], 'options': cattrs['HostConfig']['LogConfig']['Config']},
92-
'networks': {x for x in cattrs['NetworkSettings']['Networks'].keys() if x != 'bridge'},
110+
'networks': {x for x in cattrs['NetworkSettings']['Networks'].keys() if x not in default_networks},
93111
'security_opt': cattrs['HostConfig']['SecurityOpt'],
94112
'ulimits': cattrs['HostConfig']['Ulimits'],
95-
'volumes': cattrs['HostConfig']['Binds'],
113+
'volumes': [f'{m["Name"]}:{m["Destination"]}' for m in cattrs['Mounts'] if m['Type'] == 'volume'],
96114
'volume_driver': cattrs['HostConfig']['VolumeDriver'],
97115
'volumes_from': cattrs['HostConfig']['VolumesFrom'],
98116
'entrypoint': cattrs['Config']['Entrypoint'],
@@ -112,10 +130,13 @@ def generate(cname):
112130
# Populate devices key if device values are present
113131
if cattrs['HostConfig']['Devices']:
114132
values['devices'] = [x['PathOnHost']+':'+x['PathInContainer'] for x in cattrs['HostConfig']['Devices']]
115-
133+
116134
networks = {}
117135
if values['networks'] == set():
118136
del values['networks']
137+
assumed_default_network = list(cattrs['NetworkSettings']['Networks'].keys())[0]
138+
values['network_mode'] = assumed_default_network
139+
networks = None
119140
else:
120141
networklist = c.networks.list()
121142
for network in networklist:
@@ -124,9 +145,12 @@ def generate(cname):
124145
'name': network.attrs['Name']}
125146

126147
volumes = {}
127-
for volume in values['volumes']:
128-
volume_name = volume.split(':')[0]
129-
volumes[volume_name] = {'external': True}
148+
if values['volumes'] is not None:
149+
for volume in values['volumes']:
150+
volume_name = volume.split(':')[0]
151+
volumes[volume_name] = {'external': True}
152+
else:
153+
volumes = None
130154

131155
# Check for command and add it if present.
132156
if cattrs['Config']['Cmd'] is not None:

0 commit comments

Comments
 (0)