1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- # -*- 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_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():
- 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 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 False
|