123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- # -*- 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
|