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,28 +3,33 @@
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):
c = Client(base_url='unix://var/run/docker.sock')
try:
cid = [x['Id'] for x in c.containers() if args.cname in x['Names'][0]][0] cid = [x['Id'] for x in c.containers() if args.cname in x['Names'][0]][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) 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'],
@@ -61,14 +66,14 @@ values = {
'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']]
@@ -82,16 +87,19 @@ try:
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',
] ]
} }
) )