mirror of
https://github.com/Red5d/docker-autocompose
synced 2026-01-05 16:18:02 +00:00
Update script to use new module version, output compose file version 3, and be able to generate a combined file for multiple containers.
This commit is contained in:
118
autocompose.py
118
autocompose.py
@@ -1,81 +1,95 @@
|
|||||||
#! /usr/bin/env python
|
#! /usr/bin/env python
|
||||||
|
|
||||||
import pyaml, argparse, sys
|
import sys, argparse, pyaml, docker
|
||||||
from docker import Client
|
from collections import OrderedDict
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description='Generate docker-compose yaml definition from running container.')
|
parser = argparse.ArgumentParser(description='Generate docker-compose yaml definition from running container.')
|
||||||
parser.add_argument('cname', type=str, help='The name of the container to process.')
|
parser.add_argument('-v', '--version', type=int, default=3, help='Compose file version (1 or 3)')
|
||||||
|
parser.add_argument('cnames', nargs='*', type=str, help='The name of the container to process.')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
generate(args)
|
struct = {}
|
||||||
|
for cname in args.cnames:
|
||||||
|
struct.update(generate(cname))
|
||||||
|
|
||||||
|
render(struct, args)
|
||||||
|
|
||||||
|
|
||||||
def generate(args):
|
def render(struct, args):
|
||||||
c = Client(base_url='unix://var/run/docker.sock')
|
# Render yaml file
|
||||||
|
if args.version == 1:
|
||||||
|
pyaml.p(OrderedDict(struct))
|
||||||
|
else:
|
||||||
|
pyaml.p(OrderedDict({'version': '"3"', 'services': struct}))
|
||||||
|
|
||||||
|
|
||||||
|
def generate(cname):
|
||||||
|
c = docker.from_env()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cid = [x['Id'] for x in c.containers() if args.cname in x['Names'][0]][0]
|
cid = [x.short_id for x in c.containers.list() if cname == x.name or x.short_id in cname][0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
print("That container is not running.")
|
print("That container is not running.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
cinspect = c.inspect_container(cid)
|
cattrs = c.containers.get(cid).attrs
|
||||||
|
|
||||||
|
|
||||||
# Build yaml dict structure
|
# Build yaml dict structure
|
||||||
|
|
||||||
cfile = {}
|
cfile = {}
|
||||||
cfile[args.cname] = {}
|
cfile[cattrs['Name'][1:]] = {}
|
||||||
ct = cfile[args.cname]
|
ct = cfile[cattrs['Name'][1:]]
|
||||||
|
|
||||||
values = {
|
values = {
|
||||||
'cap_add': cinspect['HostConfig']['CapAdd'],
|
'cap_add': cattrs['HostConfig']['CapAdd'],
|
||||||
'cap_drop': cinspect['HostConfig']['CapDrop'],
|
'cap_drop': cattrs['HostConfig']['CapDrop'],
|
||||||
'cgroup_parent': cinspect['HostConfig']['CgroupParent'],
|
'cgroup_parent': cattrs['HostConfig']['CgroupParent'],
|
||||||
'container_name': args.cname,
|
'container_name': cattrs['Name'][1:],
|
||||||
'devices': cinspect['HostConfig']['Devices'],
|
'devices': cattrs['HostConfig']['Devices'],
|
||||||
'dns': cinspect['HostConfig']['Dns'],
|
'dns': cattrs['HostConfig']['Dns'],
|
||||||
'dns_search': cinspect['HostConfig']['DnsSearch'],
|
'dns_search': cattrs['HostConfig']['DnsSearch'],
|
||||||
'environment': cinspect['Config']['Env'],
|
'environment': cattrs['Config']['Env'],
|
||||||
'extra_hosts': cinspect['HostConfig']['ExtraHosts'],
|
'extra_hosts': cattrs['HostConfig']['ExtraHosts'],
|
||||||
'image': cinspect['Config']['Image'],
|
'image': cattrs['Config']['Image'],
|
||||||
'labels': cinspect['Config']['Labels'],
|
'labels': cattrs['Config']['Labels'],
|
||||||
'links': cinspect['HostConfig']['Links'],
|
'links': cattrs['HostConfig']['Links'],
|
||||||
'log_driver': cinspect['HostConfig']['LogConfig']['Type'],
|
#'log_driver': cattrs['HostConfig']['LogConfig']['Type'],
|
||||||
'log_opt': cinspect['HostConfig']['LogConfig']['Config'],
|
#'log_opt': cattrs['HostConfig']['LogConfig']['Config'],
|
||||||
'net': cinspect['HostConfig']['NetworkMode'],
|
'logging': {'driver': cattrs['HostConfig']['LogConfig']['Type'], 'options': cattrs['HostConfig']['LogConfig']['Config']},
|
||||||
'security_opt': cinspect['HostConfig']['SecurityOpt'],
|
'net': cattrs['HostConfig']['NetworkMode'],
|
||||||
'ulimits': cinspect['HostConfig']['Ulimits'],
|
'security_opt': cattrs['HostConfig']['SecurityOpt'],
|
||||||
'volumes': cinspect['HostConfig']['Binds'],
|
'ulimits': cattrs['HostConfig']['Ulimits'],
|
||||||
'volume_driver': cinspect['HostConfig']['VolumeDriver'],
|
'volumes': cattrs['HostConfig']['Binds'],
|
||||||
'volumes_from': cinspect['HostConfig']['VolumesFrom'],
|
'volume_driver': cattrs['HostConfig']['VolumeDriver'],
|
||||||
'cpu_shares': cinspect['HostConfig']['CpuShares'],
|
'volumes_from': cattrs['HostConfig']['VolumesFrom'],
|
||||||
'cpuset': cinspect['HostConfig']['CpusetCpus']+','+cinspect['HostConfig']['CpusetMems'],
|
'cpu_shares': cattrs['HostConfig']['CpuShares'],
|
||||||
'entrypoint': cinspect['Config']['Entrypoint'],
|
'cpuset': cattrs['HostConfig']['CpusetCpus']+','+cattrs['HostConfig']['CpusetMems'],
|
||||||
'user': cinspect['Config']['User'],
|
'entrypoint': cattrs['Config']['Entrypoint'],
|
||||||
'working_dir': cinspect['Config']['WorkingDir'],
|
'user': cattrs['Config']['User'],
|
||||||
'domainname': cinspect['Config']['Domainname'],
|
'working_dir': cattrs['Config']['WorkingDir'],
|
||||||
'hostname': cinspect['Config']['Hostname'],
|
'domainname': cattrs['Config']['Domainname'],
|
||||||
'ipc': cinspect['HostConfig']['IpcMode'],
|
'hostname': cattrs['Config']['Hostname'],
|
||||||
'mac_address': cinspect['NetworkSettings']['MacAddress'],
|
'ipc': cattrs['HostConfig']['IpcMode'],
|
||||||
'mem_limit': cinspect['HostConfig']['Memory'],
|
'mac_address': cattrs['NetworkSettings']['MacAddress'],
|
||||||
'memswap_limit': cinspect['HostConfig']['MemorySwap'],
|
'mem_limit': cattrs['HostConfig']['Memory'],
|
||||||
'privileged': cinspect['HostConfig']['Privileged'],
|
'memswap_limit': cattrs['HostConfig']['MemorySwap'],
|
||||||
'restart': cinspect['HostConfig']['RestartPolicy']['Name'],
|
'privileged': cattrs['HostConfig']['Privileged'],
|
||||||
'read_only': cinspect['HostConfig']['ReadonlyRootfs'],
|
'restart': cattrs['HostConfig']['RestartPolicy']['Name'],
|
||||||
'stdin_open': cinspect['Config']['OpenStdin'],
|
'read_only': cattrs['HostConfig']['ReadonlyRootfs'],
|
||||||
'tty': cinspect['Config']['Tty']
|
'stdin_open': cattrs['Config']['OpenStdin'],
|
||||||
|
'tty': cattrs['Config']['Tty']
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check for command and add it if present.
|
# Check for command and add it if present.
|
||||||
if cinspect['Config']['Cmd'] != None:
|
if cattrs['Config']['Cmd'] != None:
|
||||||
values['command'] = " ".join(cinspect['Config']['Cmd']),
|
values['command'] = " ".join(cattrs['Config']['Cmd']),
|
||||||
|
|
||||||
# Check for exposed/bound ports and add them if needed.
|
# Check for exposed/bound ports and add them if needed.
|
||||||
try:
|
try:
|
||||||
expose_value = list(cinspect['Config']['ExposedPorts'].keys())
|
expose_value = list(cattrs['Config']['ExposedPorts'].keys())
|
||||||
ports_value = [cinspect['HostConfig']['PortBindings'][key][0]['HostIp']+':'+cinspect['HostConfig']['PortBindings'][key][0]['HostPort']+':'+key for key in cinspect['HostConfig']['PortBindings']]
|
ports_value = [cattrs['HostConfig']['PortBindings'][key][0]['HostIp']+':'+cattrs['HostConfig']['PortBindings'][key][0]['HostPort']+':'+key for key in cattrs['HostConfig']['PortBindings']]
|
||||||
|
|
||||||
# If bound ports found, don't use the 'expose' value.
|
# If bound ports found, don't use the 'expose' value.
|
||||||
if (ports_value != None) and (ports_value != "") and (ports_value != []) and (ports_value != 'null') and (ports_value != {}) and (ports_value != "default") and (ports_value != 0) and (ports_value != ",") and (ports_value != "no"):
|
if (ports_value != None) and (ports_value != "") and (ports_value != []) and (ports_value != 'null') and (ports_value != {}) and (ports_value != "default") and (ports_value != 0) and (ports_value != ",") and (ports_value != "no"):
|
||||||
@@ -97,8 +111,8 @@ def generate(args):
|
|||||||
if (value != None) and (value != "") and (value != []) and (value != 'null') and (value != {}) and (value != "default") and (value != 0) and (value != ",") and (value != "no"):
|
if (value != None) and (value != "") and (value != []) and (value != 'null') and (value != {}) and (value != "default") and (value != 0) and (value != ",") and (value != "no"):
|
||||||
ct[key] = value
|
ct[key] = value
|
||||||
|
|
||||||
# Render yaml file
|
return cfile
|
||||||
pyaml.p(cfile)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user