Fix setup file and reformat autocompose to fit setup requirements.

This commit is contained in:
Red5d
2016-01-02 23:11:51 -05:00
parent b80db6fa8f
commit 0f1a9e9308
2 changed files with 87 additions and 79 deletions

View File

@@ -3,95 +3,103 @@
import pyaml, argparse, sys import pyaml, argparse, sys
from docker import Client from docker import Client
parser = argparse.ArgumentParser(description='Generate docker-compose yaml definition from running container.') def main():
parser.add_argument('cname', type=str, help='The name of the container to process.') parser = argparse.ArgumentParser(description='Generate docker-compose yaml definition from running container.')
args = parser.parse_args() parser.add_argument('cname', type=str, help='The name of the container to process.')
args = parser.parse_args()
c = Client(base_url='unix://var/run/docker.sock') generate(args)
try: def generate(args):
cid = [x['Id'] for x in c.containers() if args.cname in x['Names'][0]][0] c = Client(base_url='unix://var/run/docker.sock')
except IndexError:
print("That container is not running.")
sys.exit(1)
cinspect = c.inspect_container(cid) try:
cid = [x['Id'] for x in c.containers() if args.cname in x['Names'][0]][0]
except IndexError:
print("That container is not running.")
sys.exit(1)
cinspect = c.inspect_container(cid)
# Build yaml dict structure # Build yaml dict structure
cfile = {} cfile = {}
cfile[args.cname] = {} cfile[args.cname] = {}
ct = cfile[args.cname] ct = cfile[args.cname]
values = { values = {
'cap_add': cinspect['HostConfig']['CapAdd'], 'cap_add': cinspect['HostConfig']['CapAdd'],
'cap_drop': cinspect['HostConfig']['CapDrop'], 'cap_drop': cinspect['HostConfig']['CapDrop'],
'cgroup_parent': cinspect['HostConfig']['CgroupParent'], 'cgroup_parent': cinspect['HostConfig']['CgroupParent'],
'container_name': args.cname, 'container_name': args.cname,
'devices': cinspect['HostConfig']['Devices'], 'devices': cinspect['HostConfig']['Devices'],
'dns': cinspect['HostConfig']['Dns'], 'dns': cinspect['HostConfig']['Dns'],
'dns_search': cinspect['HostConfig']['DnsSearch'], 'dns_search': cinspect['HostConfig']['DnsSearch'],
'environment': cinspect['Config']['Env'], 'environment': cinspect['Config']['Env'],
'extra_hosts': cinspect['HostConfig']['ExtraHosts'], 'extra_hosts': cinspect['HostConfig']['ExtraHosts'],
'image': cinspect['Config']['Image'], 'image': cinspect['Config']['Image'],
'labels': cinspect['Config']['Labels'], 'labels': cinspect['Config']['Labels'],
'links': cinspect['HostConfig']['Links'], 'links': cinspect['HostConfig']['Links'],
'log_driver': cinspect['HostConfig']['LogConfig']['Type'], 'log_driver': cinspect['HostConfig']['LogConfig']['Type'],
'log_opt': cinspect['HostConfig']['LogConfig']['Config'], 'log_opt': cinspect['HostConfig']['LogConfig']['Config'],
'net': cinspect['HostConfig']['NetworkMode'], 'net': cinspect['HostConfig']['NetworkMode'],
'security_opt': cinspect['HostConfig']['SecurityOpt'], 'security_opt': cinspect['HostConfig']['SecurityOpt'],
'ulimits': cinspect['HostConfig']['Ulimits'], 'ulimits': cinspect['HostConfig']['Ulimits'],
'volumes': cinspect['HostConfig']['Binds'], 'volumes': cinspect['HostConfig']['Binds'],
'volume_driver': cinspect['HostConfig']['VolumeDriver'], 'volume_driver': cinspect['HostConfig']['VolumeDriver'],
'volumes_from': cinspect['HostConfig']['VolumesFrom'], 'volumes_from': cinspect['HostConfig']['VolumesFrom'],
'cpu_shares': cinspect['HostConfig']['CpuShares'], 'cpu_shares': cinspect['HostConfig']['CpuShares'],
'cpuset': cinspect['HostConfig']['CpusetCpus']+','+cinspect['HostConfig']['CpusetMems'], 'cpuset': cinspect['HostConfig']['CpusetCpus']+','+cinspect['HostConfig']['CpusetMems'],
'entrypoint': cinspect['Config']['Entrypoint'], 'entrypoint': cinspect['Config']['Entrypoint'],
'user': cinspect['Config']['User'], 'user': cinspect['Config']['User'],
'working_dir': cinspect['Config']['WorkingDir'], 'working_dir': cinspect['Config']['WorkingDir'],
'domainname': cinspect['Config']['Domainname'], 'domainname': cinspect['Config']['Domainname'],
'hostname': cinspect['Config']['Hostname'], 'hostname': cinspect['Config']['Hostname'],
'ipc': cinspect['HostConfig']['IpcMode'], 'ipc': cinspect['HostConfig']['IpcMode'],
'mac_address': cinspect['NetworkSettings']['MacAddress'], 'mac_address': cinspect['NetworkSettings']['MacAddress'],
'mem_limit': cinspect['HostConfig']['Memory'], 'mem_limit': cinspect['HostConfig']['Memory'],
'memswap_limit': cinspect['HostConfig']['MemorySwap'], 'memswap_limit': cinspect['HostConfig']['MemorySwap'],
'privileged': cinspect['HostConfig']['Privileged'], 'privileged': cinspect['HostConfig']['Privileged'],
'restart': cinspect['HostConfig']['RestartPolicy']['Name'], 'restart': cinspect['HostConfig']['RestartPolicy']['Name'],
'read_only': cinspect['HostConfig']['ReadonlyRootfs'], 'read_only': cinspect['HostConfig']['ReadonlyRootfs'],
'stdin_open': cinspect['Config']['OpenStdin'], 'stdin_open': cinspect['Config']['OpenStdin'],
'tty': cinspect['Config']['Tty'] 'tty': cinspect['Config']['Tty']
} }
# Check for command and add it if present. # Check for command and add it if present.
if cinspect['Config']['Cmd'] != None: if cinspect['Config']['Cmd'] != None:
values['command'] = " ".join(cinspect['Config']['Cmd']), values['command'] = " ".join(cinspect['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(cinspect['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 = [cinspect['HostConfig']['PortBindings'][key][0]['HostIp']+':'+cinspect['HostConfig']['PortBindings'][key][0]['HostPort']+':'+key for key in cinspect['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"):
for index, port in enumerate(ports_value): for index, port in enumerate(ports_value):
if port[0] == ':': if port[0] == ':':
ports_value[index] = port[1:] ports_value[index] = port[1:]
values['ports'] = ports_value values['ports'] = ports_value
else: else:
values['expose'] = expose_value values['expose'] = expose_value
except KeyError: except KeyError:
# No ports exposed/bound. Continue without them. # No ports exposed/bound. Continue without them.
ports = None ports = None
# Iterate through values to finish building yaml dict. # Iterate through values to finish building yaml dict.
for key in values: for key in values:
value = values[key] value = values[key]
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 # Render yaml file
pyaml.p(cfile) pyaml.p(cfile)
if __name__ == "__main__":
main()

View File

@@ -3,10 +3,10 @@ setup(
name = "docker-autocompose", name = "docker-autocompose",
version = "1.0", version = "1.0",
packages = find_packages(), packages = find_packages(),
install_requires = ['pyaml, docker-py'], install_requires = ['pyaml>=15.8.2', 'docker-py>=1.6.0'],
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'autocompose = autocompose', 'autocompose = autocompose:main',
] ]
} }
) )