<gh_stars>1-10
"""
This module is the snapshot command of bowl.

Created on 15 March 2014
@author: <NAME>
"""
import docker
import os
import uuid

from bowl.cli_opts import hosts
from bowl.cli_opts import list
from docker import Client

class Object(object):
    pass

class snapshot(object):
    """
    This class is responsible for the snapshot command of the cli.
    """
    @classmethod
    def main(self, args):
        # !! TODO needs to implement login if using that


        directory = args.metadata_path
        directory = os.path.expanduser(directory)
        if not os.path.exists(directory):
            os.makedirs(directory)

        list_args = Object()
        list_args.metadata_path = args.metadata_path
        list_args.z = True
        list_a = list.list.main(list_args)

        host_args = Object()
        host_args.metadata_path = args.metadata_path
        host_args.z = True
        host_a = hosts.hosts.main(host_args)

        commit = str(uuid.uuid4())
        snapshot = {}
        found = 0
        for container in list_a:
            if args.CONTAINER in container:
                 host = container.split(",")[1]
                 cont = container.split(",")[0]
                 commit_name = "bowl-snapshot-"+commit
                 try:
                     for h in host_a:
                         if host in h:
                             c = Client(**kwargs_from_env())
                             #c = docker.Client(base_url='tcp://'+h,
                             #                  version='1.12',
                             #                  timeout=2)
                     c.commit(cont, repository=commit_name)
                     snapshot[cont] = host+":"+commit_name
                 except:
                     if not args.z:
                         print "unable to connect to "+host
                     return False
                 found = 1

        if found:
            try:
                with open(os.path.join(directory, "snapshots"), 'a') as f:
                    for container in snapshot:
                        f.write("{" +
                                "'container_id': '"+container+"'," +
                                " 'snapshot_id': '"+snapshot[container].split(":")[1]+"'," +
                                " 'host': '"+snapshot[container].split(":")[0]+"'" +
                                "}\n")
                        if not args.z:
                            print "snapshotted",
                            print container + " on",
                            print snapshot[container].split(":")[0],
                            print "to repository;",
                            print snapshot[container].split(":")[1]
            except:
                if not args.z:
                    print "unable to snapshot container"
                return False
        else:
            if not args.z:
                print args.CONTAINER, "is not a running container"
            return False
        return True

"""
This module is the snapshot command of bowl.

Created on 15 March 2014
@author: <NAME>
"""
import docker
import os
import uuid

from bowl.cli_opts import hosts
from bowl.cli_opts import list
from docker import Client

class Object(object):
    pass

class snapshot(object):
    """
    This class is responsible for the snapshot command of the cli.
    """
    @classmethod
    def main(self, args):
        # !! TODO needs to implement login if using that


        directory = args.metadata_path
        directory = os.path.expanduser(directory)
        if not os.path.exists(directory):
            os.makedirs(directory)

        list_args = Object()
        list_args.metadata_path = args.metadata_path
        list_args.z = True
        list_a = list.list.main(list_args)

        host_args = Object()
        host_args.metadata_path = args.metadata_path
        host_args.z = True
        host_a = hosts.hosts.main(host_args)

        commit = str(uuid.uuid4())
        snapshot = {}
        found = 0
        for container in list_a:
            if args.CONTAINER in container:
                 host = container.split(",")[1]
                 cont = container.split(",")[0]
                 commit_name = "bowl-snapshot-"+commit
                 try:
                     for h in host_a:
                         if host in h:
                             c = Client(**kwargs_from_env())
                             #c = docker.Client(base_url='tcp://'+h,
                             #                  version='1.12',
                             #                  timeout=2)
                     c.commit(cont, repository=commit_name)
                     snapshot[cont] = host+":"+commit_name
                 except:
                     if not args.z:
                         print "unable to connect to "+host
                     return False
                 found = 1

        if found:
            try:
                with open(os.path.join(directory, "snapshots"), 'a') as f:
                    for container in snapshot:
                        f.write("{" +
                                "'container_id': '"+container+"'," +
                                " 'snapshot_id': '"+snapshot[container].split(":")[1]+"'," +
                                " 'host': '"+snapshot[container].split(":")[0]+"'" +
                                "}\n")
                        if not args.z:
                            print "snapshotted",
                            print container + " on",
                            print snapshot[container].split(":")[0],
                            print "to repository;",
                            print snapshot[container].split(":")[1]
            except:
                if not args.z:
                    print "unable to snapshot container"
                return False
        else:
            if not args.z:
                print args.CONTAINER, "is not a running container"
            return False
        return True
This module is the snapshot command of bowl.  Created on 15 March 2014 @author: <NAME> This class is responsible for the snapshot command of the cli. # !! TODO needs to implement login if using that #c = docker.Client(base_url='tcp://'+h, #                  version='1.12', #                  timeout=2)