import os
import sys
import json
import docker
import argparse
import jsonschema

class DckrMgr(object):
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('-c', dest='commands', action='append_const', const='c', help='Create container')
        parser.add_argument('-s', dest='commands', action='append_const', const='s', help='Start container')
        parser.add_argument('-t', dest='commands', action='append_const', const='t', help='Stop container')
        parser.add_argument('-r', dest='commands', action='append_const', const='r', help='Remove container')
        parser.add_argument('-p', dest='commands', action='append_const', const='p', help='Pull container')
        parser.add_argument('-b', dest='commands', action='append_const', const='b', help='Backup volumes')
        args = parser.parse_args()

        cli = docker.Client('unix://var/run/docker.sock')

        self.p_cwd = os.getcwd()
        self.p_cnf = os.path.join(self.p_cwd, 'dckrcnf.json')

        try:
            f_cnf = open(self.p_cnf, 'r')
            f_sch = open('../dckrcnf.schema.json', 'r')
        except OSError:
            print('Couldn\'t open dckrcnf.json')
            exit(1)

        self.j_cnf = json.load(f_cnf)
        j_sch = json.load(f_sch)

        try:
            jsonschema.validate(self.j_cnf, j_sch)
        except jsonschema.exceptions.ValidationError as detail:
            print(detail.message)
            exit(1)

        for command in args.commands:
            if getattr(self, command)() != 0:
                exit(1)

        exit(0)

    def c(self):
        print('C')
        return 0

    def s(self):
        print('S')
        return 0

    def t(self):
        print('T')
        return 0

    def r(self):
        print('R')
        return 0

    def p(self):
        print('P')
        return 0

    def b(self):
        print('B')
        return 0

if __name__ == '__main__':
    DckrMgr()

@@ -862,16 +862,21 @@
         
+self.
 cli = do
@@ -1609,32 +1609,1472 @@
    def c(self):%0A
+        environment = %7B%7D%0A%0A        for variable in self.j_cnf%5B'environment'%5D:%0A            environment%5Bvariable%5B'name'%5D%5D = variable%5B'value'%5D%0A%0A        volumes = %5B%5D%0A        binds = %7B%7D%0A%0A        for volume in self.j_cnf%5B'volume'%5D:%0A            volumes.append(volume%5B'container_path'%5D)%0A%0A            binds%5Bos.path.join(self.p_cwd, volume%5B'host_path'%5D)%5D = %7B%0A                'bind': volume%5B'container_path'%5D,%0A                'mode': volume%5B'mode'%5D%0A            %7D%0A%0A        ports = %5B%5D%0A        port_bindings = %7B%7D%0A%0A        for port in self.j_cnf%5B'port'%5D:%0A            ports.append(port%5B'container_port'%5D)%0A%0A            if 'address' in  port:%0A                port_bindings%5Bport%5B'container_port'%5D%5D = (port%5B'address'%5D, port%5B'host_port'%5D)%0A            else:%0A                port_bindings%5Bport%5B'container_port'%5D%5D = port%5B'host_port'%5D%0A%0A        links = %7B%7D%0A%0A        for link in self.j_cnf%5B'link'%5D:%0A            links%5Blink%5B'name'%5D%5D = link%5B'alias'%5D%0A%0A        host_config = docker.utils.create_host_config(%0A            binds = binds,%0A            port_bindings = port_bindings,%0A            links = links%0A        )%0A%0A        self.cli.create_container(%0A            name = self.j_cnf%5B'name'%5D,%0A            image = self.j_cnf%5B'image'%5D%5B'name'%5D + ':' + self.j_cnf%5B'image'%5D%5B'version'%5D,%0A            hostname = self.j_cnf.get('hostname'),%0A            environment = environment,%0A            volumes = volumes,%0A            ports = ports,%0A            host_config = host_config%0A        )%0A%0A
         print('C

implement container creation with most important options