Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better handling for default networks #42

Merged
merged 4 commits into from
Aug 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions autocompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ def main():
cfile, c_networks, c_volumes = generate(cname)

struct.update(cfile)
networks.update(c_networks)
volumes.update(c_volumes)

if c_networks is None:
networks = None
else:
networks.update(c_networks)

if c_volumes is None:
volumes = None
else:
volumes.update(c_volumes)

render(struct, args, networks, volumes)

Expand All @@ -38,7 +46,15 @@ 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}

if networks is not None:
ans['networks'] = networks

if volumes is not None:
ans['volumes'] = volumes

pyaml.p(OrderedDict(ans))


def is_date_or_time(s: str):
Expand Down Expand Up @@ -73,6 +89,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'],
Expand All @@ -89,10 +107,10 @@ 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'],
'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'],
Expand All @@ -112,10 +130,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:
Expand All @@ -124,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'] is not None:
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:
Expand Down