Пример #1
0
    def network_list(self, kwargs=None):
        """List of docker networks


        :returns: None
        :rtype: NoneType


        """
        try:
            scode, networks = Rest.get('Network')
        except docker.errors.APIError as e:
            Console.error(e.explanation)
            return

        if len(networks) == 0:
            Console.info("No network exist")
            return

        n = 1
        e = {}
        data = []
        for network in networks:
            d = {}
            d['Ip'] = network['Ip']
            d['Id'] = network['Id']
            d['Name'] = network['Name']
            d['Containers'] = network['Containers']
            e[n] = d
            n = n + 1
        Console.ok(str(Printer.dict_table(e, order=['Ip', 'Id', 'Name', 'Containers'])))
Пример #2
0
    def host_list(self):
        """List of docker containers



        :returns: None
        :rtype: NoneType


        """
        try:
            scode, hosts = Rest.get('Host')
        except Exception as e:
            Console.error(e.message)
            return
        if len(hosts) == 0:
            print("No hosts exist")
            return

        n = 1
        e = {}
        for host in hosts:
            d = {}
            d['Ip'] = str(host['Ip'])
            d['Name'] = str(host['Name'])
            d['Port'] = str(host['Port'])
            d['Swarmmode'] = str(host['Swarmmode'])
            e[n] = d
            n = n + 1
        Console.ok(str(Printer.dict_table(e, order=['Ip', 'Name', 'Port', 'Swarmmode'])))
Пример #3
0
    def container_list(self, kwargs=None):
        """List of docker containers



        :returns: None
        :rtype: NoneType


        """
        try:
            scode, containers = Rest.get('Container')
        except docker.errors.APIError as e:
            Console.error(e.explanation)
            return
        if len(containers) == 0:
            print("No containers exist")
            return

        n = 1
        e = {}
        for container in containers:
            d = {}
            d['Ip'] = container['Ip']
            d['Id'] = container['Id']
            d['Name'] = container['Name']
            d['Image'] = container['Config']['Image']
            d['Status'] = container['State']['Status']
            d['StartedAt'] = container['State']['StartedAt']
            e[n] = d
            n = n + 1
        Console.ok(str(Printer.dict_table(e, order=['Ip', 'Id', 'Name', 'Image', 'Status', 'StartedAt'])))
Пример #4
0
    def images_list(self, kwargs=None):
        """List of docker images
        
        
        :returns: None
        :rtype: NoneType


        """

        try:
            scode, images = Rest.get('Image')
        except docker.errors.APIError as e:
            Console.error(e.explanation)
            return

        if len(images) == 0:
            Console.info("No images exist")
            return

        n = 1
        e = {}
        for image in images:
            d = {}
            d['Ip'] = image['Ip']
            d['Id'] = image['Id']
            if image['RepoTags'] == None:
                d['Repository'] = image['RepoDigests'][0]
            else:
                d['Repository'] = image['RepoTags'][0]
            # d['Size'] = image['Size']
            d['Size(GB)'] = round(image['Size'] / float(1 << 30), 2)  # Converting the size to GB
            e[n] = d
            n = n + 1
        Console.ok(str(Printer.dict_table(e, order=['Ip', 'Id', 'Repository', 'Size(GB)'])))
    def node_refresh(self):
        """Refresh of swarm nodes



        :returns: None
        :rtype: NoneType


        """
        filter = {}
        filter['Swarmmode'] = 'Manager'
        scode, hosts = Rest.get('Host', filter)
        filter = {}
        n = 1
        e = {}
        data = []
        for host in hosts:
            os.environ["DOCKER_HOST"] = host['Ip'] + ":" + str(host['Port'])
            filter = {}
            filter['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
            self.client = docker.from_env()
            try:
                nodes = self.client.nodes.list()
            except docker.errors.APIError as e:
                Console.error(e.explanation)
                return
            if len(nodes) == 0:
                Console.info("No nodes exist for manager" +
                             os.environ["DOCKER_HOST"].split(':'))
                continue

            n = 1
            e = {}
            data = []
            for node in nodes:
                d = {}
                node_dict = node.__dict__['attrs']
                d['Id'] = node_dict['ID']
                data.append(node_dict)
                d['Role'] = node_dict['Spec']['Role']
                d['Status'] = node_dict['Status']['State']
                if d['Role'] == 'manager':
                    d['Ip'] = node_dict['ManagerStatus']['Addr'].split(':')[0]
                    d['Manager Ip'] = ''
                else:
                    d['Ip'] = node_dict['Status']['Addr']
                    d['Manager Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
                d['Host Name'] = node_dict['Description']['Hostname']
                e[n] = d
                n = n + 1
        Console.ok(
            str(
                Printer.dict_table(e,
                                   order=[
                                       'Ip', 'Host Name', 'Id', 'Role',
                                       'Status', 'Manager Ip'
                                   ])))
        Rest.delete('Node')
        Rest.post('Node', data)
Пример #6
0
    def host_create(self, addr, hostName=None):
        """Creates docker host


        :param str addr: Address for docker
        :param str hostName: Name of docker host
        :returns: None
        :rtype: NoneType


        """
        try:
            host = {}
            host['Name'] = hostName
            host['Ip'] = addr.split(':')[0]
            host['Port'] = int(addr.split(':')[1])
            host['Swarmmode'] = ''
            host['SwarmmanagerIp'] = ''
            host['Swarmhost'] = False
            filter = {}
            filter['Ip'] = addr.split(':')[0]
            try:
                scode, hosts = Rest.get('Host')
            except Exception as e:
                Console.error(e.message)
                return
            if len(hosts) != 0:
                Console.ok('Host ' + hostName + ' is Added and is the default docker host')
                return
            r = Rest.post('Host', host, filter)
            Console.ok('Host ' + hostName + ' is Added and is the default host')
        except Exception as e:
            Console.error(e.message)
            return
    def create(self, kwargs=None):
        """Creates docker Swarm

        :param str addr: Address of Swarm Manager
        :param str name: Name of the Swarm
        :returns: None
        :rtype: NoneType


        """
        rcode = self.client.swarm.init(**kwargs)
        Console.ok("Swarm is created")
        filter = {}
        filter['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
        scode, hosts = Rest.get('Host', filter)
        d = {}
        for host in hosts:
            d['Ip'] = host['Ip']
            d['Name'] = host['Name']
            d['Port'] = host['Port']
            d['Swarmmode'] = 'Manager'
            d['SwarmmanagerIp'] = ''
            d['Swarmhost'] = True
        Rest.post('Host', d, filter)
        self.node_refresh()
    def images_refresh(self, kwargs=None):
        """List of docker images


        :returns: None
        :rtype: NoneType


        """
        filter = {}
        scode, hosts = Rest.get('Host', filter)
        filter = {}
        n = 1
        e = {}
        data = []
        for host in hosts:
            os.environ["DOCKER_HOST"] = host['Ip'] + ":" + str(host['Port'])
            filter = {}
            filter['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
            if host['Swarmmode'] == 'Worker':
                Rest.delete('Image', filter)
                continue
            self.client = docker.from_env()
            try:
                images = self.client.images.list(**kwargs)
            except docker.errors.APIError as e:
                Console.error(e.explanation)
                return

            if len(images) == 0:
                Console.info("No images exist on host " + host['Ip'])
                continue

            for imagem in images:
                image = imagem.__dict__['attrs']
                image['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
                data.append(image)
                d = {}
                d['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
                d['Id'] = image['Id']
                if image['RepoTags'] == None:
                    d['Repository'] = image['RepoDigests'][0]
                else:
                    d['Repository'] = image['RepoTags'][0]
                # d['Size'] = image['Size']
                d['Size(GB)'] = round(image['Size'] / float(1 << 30), 2)
                e[n] = d
                n = n + 1
            Rest.delete('Image', filter)
        Rest.post('Image', data)
        Console.ok(
            str(
                Printer.dict_table(
                    e, order=['Ip', 'Id', 'Repository', 'Size(GB)'])))
    def service_refresh(self, kwargs=None):
        """List of docker images


        :returns: None
        :rtype: NoneType


        """
        filter = {}
        filter['Swarmmode'] = 'Manager'
        scode, hosts = Rest.get('Host', filter)
        filter = {}
        n = 1
        e = {}
        data = []
        for host in hosts:
            os.environ["DOCKER_HOST"] = host['Ip'] + ":" + str(host['Port'])
            filter = {}
            filter['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
            self.client = docker.from_env()
            try:
                services = self.client.services.list(**kwargs)
            except docker.errors.APIError as e:
                Console.error(e.explanation)
                return

            if len(services) == 0:
                Console.info("No service exist on host" + host['Ip'])
                continue

            for servicem in services:
                d = {}
                service = servicem.__dict__['attrs']
                service['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
                data.append(service)
                d['Ip'] = service['Ip']
                d['Id'] = service['ID']
                d['Name'] = service['Spec']['Name']
                d['Image'] = service['Spec']['TaskTemplate']['ContainerSpec'][
                    'Image']
                d['Replicas'] = service['Spec']['Mode']['Replicated'][
                    'Replicas']
                e[n] = d
                n = n + 1
        Rest.delete('Service')
        if len(data) == 0:
            Console.info("No service exist ")
            return
        Rest.post('Service', data)
        Console.ok(
            str(
                Printer.dict_table(
                    e, order=['Ip', 'Id', 'Name', 'Image', 'Replicas'])))
    def container_refresh(self, kwargs=None):
        """List of docker containers



        :returns: None
        :rtype: NoneType


        """
        filter = {}
        scode, hosts = Rest.get('Host', filter)
        filter = {}
        n = 1
        e = {}
        data = []
        for host in hosts:
            os.environ["DOCKER_HOST"] = host['Ip'] + ":" + str(host['Port'])
            self.client = docker.from_env()
            filter['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
            try:
                containers = self.client.containers.list(all, **kwargs)
            except docker.errors.APIError as e:
                Console.error(e.explanation)
                return
            if len(containers) == 0:
                print("No containers exist" + str(host['Ip']))
                continue

            for containerm in containers:
                container = containerm.__dict__['attrs']
                container['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
                data.append(container)
                d = {}
                d['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
                d['Id'] = container['Id']
                d['Name'] = container['Name']
                d['Image'] = container['Config']['Image']
                d['Status'] = container['State']['Status']
                d['StartedAt'] = container['State']['StartedAt']
                e[n] = d
                n = n + 1
            Rest.delete('Container', filter)
        Rest.post('Container', data)
        Console.ok(
            str(
                Printer.dict_table(
                    e,
                    order=['Ip', 'Id', 'Name', 'Image', 'Status',
                           'StartedAt'])))
    def network_refresh(self, kwargs=None):
        """List of docker networks


        :returns: None
        :rtype: NoneType


        """
        filter = {}
        scode, hosts = Rest.get('Host', filter)
        filter = {}
        n = 1
        e = {}
        data = []
        for host in hosts:
            os.environ["DOCKER_HOST"] = host['Ip'] + ":" + str(host['Port'])
            filter = {}
            filter['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
            if host['Swarmmode'] == 'Worker':
                Rest.delete('Network', filter)
                continue
            self.client = docker.from_env()
            try:
                networks = self.client.networks.list(**kwargs)
            except docker.errors.APIError as e:
                Console.error(e.explanation)
                return

            if len(networks) == 0:
                Console.info("No network exist" + host['Ip'])
                continue

            for networkm in networks:
                network = networkm.__dict__['attrs']
                network['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
                data.append(network)
                d = {}
                d['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
                d['Id'] = network['Id']
                d['Name'] = network['Name']
                d['Containers'] = network['Containers']
                e[n] = d
                n = n + 1
            r = Rest.delete('Network', filter)
        r = Rest.post('Network', data)
        Console.ok(
            str(Printer.dict_table(e, order=['Ip', 'Id', 'Name',
                                             'Containers'])))
    def node_list(self, kwargs=None):
        """List of docker swarm nodes 



        :returns: None
        :rtype: NoneType


        """
        scode, nodes = Rest.get('Node')
        if len(nodes) == 0:
            Console.info("No nodes exist")
            return
        n = 1
        e = {}
        data = []
        for node in nodes:
            d = {}
            node_dict = node
            d['Id'] = node_dict['ID']
            data.append(node_dict)
            d['Role'] = node_dict['Spec']['Role']
            d['Status'] = node_dict['Status']['State']
            if d['Role'] == 'manager':
                d['Ip'] = node_dict['ManagerStatus']['Addr'].split(':')[0]
                d['Manager Ip'] = ''
            else:
                d['Ip'] = node_dict['Status']['Addr']
                d['Manager Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
            d['Host Name'] = node_dict['Description']['Hostname']

            e[n] = d
            n = n + 1
        Console.ok(
            str(
                Printer.dict_table(e,
                                   order=[
                                       'Ip', 'Host Name', 'Id', 'Role',
                                       'Status', 'Manager Ip'
                                   ])))
    def join(self, addr, type, kwargs=None):
        """Creates docker Swarm
        :param str addr: Address of Swarm Manager
        :returns: None
        :rtype: NoneType


        """
        man_list = []
        man_list.append(addr)
        savehost = os.environ["DOCKER_HOST"]
        os.environ["DOCKER_HOST"] = addr.split(':')[0] + ":4243"

        self.client = docker.from_env()
        if type not in ['Manager', 'Worker']:
            Console.error('Valid values are Manager or Worker')
            return

        token = self.client.swarm.attrs['JoinTokens']
        os.environ["DOCKER_HOST"] = savehost
        self.client = docker.from_env()
        rcode = self.client.swarm.join(remote_addrs=man_list,
                                       join_token=token[type],
                                       listen_addr="0.0.0.0:2377",
                                       **kwargs)
        filter = {}
        filter['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
        scode, hosts = Rest.get('Host', filter)
        d = {}
        for host in hosts:
            d['Ip'] = host['Ip']
            d['Name'] = host['Name']
            d['Port'] = host['Port']
            d['Swarmmode'] = type
            d['SwarmmanagerIp'] = addr.split(':')[0]
            d['Swarmhost'] = True
        Rest.post('Host', d, filter)
        self.node_refresh()
        Console.ok("Node Joined Swarm")
    def service_list(self, kwargs=None):
        """List of docker images


        :returns: None
        :rtype: NoneType


        """
        try:
            scode, services = Rest.get('Service')
        # services = self.client.services.list(**kwargs)
        except docker.errors.APIError as e:
            Console.error(e.explanation)
            return

        if len(services) == 0:
            Console.info("No Services exist")
            return

        n = 1
        e = {}
        for service in services:
            d = {}
            d['Ip'] = service['Ip']
            d['Id'] = service['ID']
            d['Name'] = service['Spec']['Name']
            d['Image'] = service['Spec']['TaskTemplate']['ContainerSpec'][
                'Image']
            d['Replicas'] = service['Spec']['Mode']['Replicated']['Replicas']
            # need to see if status needs to be added.
            e[n] = d
            n = n + 1
        Console.ok(
            str(
                Printer.dict_table(
                    e, order=['Ip', 'Id', 'Name', 'Image', 'Replicas'])))
    def leave(self, kwargs=None):
        """Creates docker Swarm

        :returns: None
        :rtype: NoneType


        """

        rcode = self.client.swarm.leave(True, **kwargs)
        filter = {}
        filter['Ip'] = os.environ["DOCKER_HOST"].split(':')[0]
        scode, hosts = Rest.get('Host', filter)
        d = {}
        for host in hosts:
            d['Ip'] = host['Ip']
            d['Name'] = host['Name']
            d['Port'] = host['Port']
            d['Swarmmode'] = ''
            d['SwarmmanagerIp'] = ''
            d['Swarmhost'] = False
        Rest.post('Host', d, filter)
        self.node_refresh()
        Console.ok("Node left Swarm")