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
from docker import Client
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.')
args = parser.parse_args()
def main():
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.')
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]
except IndexError:
except IndexError:
print("That container is not running.")
sys.exit(1)
cinspect = c.inspect_container(cid)
cinspect = c.inspect_container(cid)
# Build yaml dict structure
# Build yaml dict structure
cfile = {}
cfile[args.cname] = {}
ct = cfile[args.cname]
cfile = {}
cfile[args.cname] = {}
ct = cfile[args.cname]
values = {
values = {
'cap_add': cinspect['HostConfig']['CapAdd'],
'cap_drop': cinspect['HostConfig']['CapDrop'],
'cgroup_parent': cinspect['HostConfig']['CgroupParent'],
@@ -61,14 +66,14 @@ values = {
'read_only': cinspect['HostConfig']['ReadonlyRootfs'],
'stdin_open': cinspect['Config']['OpenStdin'],
'tty': cinspect['Config']['Tty']
}
}
# Check for command and add it if present.
if cinspect['Config']['Cmd'] != None:
# Check for command and add it if present.
if cinspect['Config']['Cmd'] != None:
values['command'] = " ".join(cinspect['Config']['Cmd']),
# Check for exposed/bound ports and add them if needed.
try:
# Check for exposed/bound ports and add them if needed.
try:
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']]
@@ -82,16 +87,19 @@ try:
else:
values['expose'] = expose_value
except KeyError:
except KeyError:
# No ports exposed/bound. Continue without them.
ports = None
# Iterate through values to finish building yaml dict.
for key in values:
# Iterate through values to finish building yaml dict.
for key in values:
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"):
ct[key] = value
# Render yaml file
pyaml.p(cfile)
# Render yaml file
pyaml.p(cfile)
if __name__ == "__main__":
main()

View File

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