# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.conf import settings from docker import DockerClient from docker.errors import DockerException, NotFound, APIError, ContainerError, ImageNotFound import time ''' ''' def get_docker_client(): try: return DockerClient(base_url='unix:/%s' % settings.DOCKER_SOCK_DIR) except DockerException: return None ''' ''' def get_all_images(): client = get_docker_client() images = [] if not client: return images for image in client.images.list(): images.append({ 'id': image.id, 'short_id': image.short_id, 'tags': image.tags }) return images ''' ''' def get_all_containers(): client = get_docker_client() containers = [] if not client: return containers for container in client.containers.list(all=True): containers.append({ 'id': container.id, 'short_id': container.short_id, 'name': container.name, 'image': container.image, 'labels': container.labels, 'status': container.status, 'attrs': container.attrs }) return containers ''' ''' def run_container(image=None, name=None, ports=[], volumes=None, net=None): if not name: return False client = get_docker_client() if not client: return False try: client.containers.run(image, None, name=name, detach=True, ports=ports, volumes=volumes, network=net) return True except (ContainerError, ImageNotFound, APIError): return False ''' ''' def start_container(id=None): if not id: return None client = get_docker_client() if not client: return None try: container = client.containers.get(id) container.start() time.sleep(1) container.reload() return { 'id': container.id, 'short_id': container.short_id, 'name': container.name, 'image': container.image, 'labels': container.labels, 'status': container.status } except (NotFound, APIError): return None ''' ''' def restart_container(id=None): if not id: return None client = get_docker_client() if not client: return None try: container = client.containers.get(id) container.restart() time.sleep(1) container.reload() return { 'id': container.id, 'short_id': container.short_id, 'name': container.name, 'image': container.image, 'labels': container.labels, 'status': container.status } except (NotFound, APIError): return None ''' ''' def stop_container(id=None): if not id: return None client = get_docker_client() if not client: return None try: container = client.containers.get(id) container.stop() time.sleep(1) container.reload() return { 'id': container.id, 'short_id': container.short_id, 'name': container.name, 'image': container.image, 'labels': container.labels, 'status': container.status } except (NotFound, APIError): return None ''' ''' def execute_command(container_id_or_name=None, command=None): if not container_id_or_name or not command: return None client = get_docker_client() if not client: return None try: container = client.containers.get(container_id_or_name) (exit_code, unused_output) = container.exec_run(command) return (False, True)[exit_code == 0] except (NotFound, APIError): return None ''' ''' def copy_in(container_id_or_name=None, path=None, tarfile=None): if not container_id_or_name or not path or not tarfile: return False client = get_docker_client() if not client: return False try: file = open(tarfile, 'r') container = client.containers.get(container_id_or_name) result = container.put_archive(path, file) file.close() return result except (NotFound, APIError, IOError): return False