|
@@ -0,0 +1,90 @@
|
|
|
|
+# -*- 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
|