123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- from django.conf import settings
- from docker import DockerClient
- from docker.errors import DockerException, NotFound, APIError
- '''
- '''
- def get_docker_client():
- try:
- return DockerClient(base_url='unix:/%s' % settings.DOCKER_SOCK_DIR)
- except DockerException:
- return None
- '''
- '''
- def get_all_containers():
- client = get_docker_client()
- containers = []
- if not client:
- return containers
- for container in client.containers.list():
- containers.append({
- 'id': container.id,
- 'short_id': container.short_id,
- 'name': container.name,
- 'image': container.image,
- 'labels': container.labels,
- 'status': container.status
- })
- return containers
- '''
- '''
- 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
- })
- '''
- '''
- def start_container(id=None):
- if not id:
- return False
- client = get_docker_client()
- if not client:
- return False
- try:
- container = client.containers.get(id)
- container.start()
- except NotFound:
- return False
- except APIError:
- return
-
- '''
- '''
- def stop_container(id=None):
- if not id:
- return False
- client = get_docker_client()
- if not client:
- return False
- try:
- container = client.containers.get(id)
- container.stop()
- except NotFound:
- return False
- except APIError:
- return
|