|
@@ -2,8 +2,10 @@
|
|
|
from __future__ import unicode_literals
|
|
|
from django.conf import settings
|
|
|
from docker import DockerClient
|
|
|
-from docker.errors import DockerException, NotFound, APIError
|
|
|
+from docker.errors import DockerException, NotFound, APIError, ContainerError, ImageNotFound
|
|
|
import time
|
|
|
+import tarfile as tar
|
|
|
+from io import BytesIO
|
|
|
|
|
|
'''
|
|
|
'''
|
|
@@ -49,7 +51,8 @@ def get_all_containers():
|
|
|
'name': container.name,
|
|
|
'image': container.image,
|
|
|
'labels': container.labels,
|
|
|
- 'status': container.status
|
|
|
+ 'status': container.status,
|
|
|
+ 'attrs': container.attrs
|
|
|
})
|
|
|
|
|
|
return containers
|
|
@@ -68,9 +71,7 @@ def run_container(image=None, name=None, ports=[], volumes=None, net=None):
|
|
|
try:
|
|
|
client.containers.run(image, None, name=name, detach=True, ports=ports, volumes=volumes, network=net)
|
|
|
return True
|
|
|
- except NotFound:
|
|
|
- return False
|
|
|
- except APIError:
|
|
|
+ except (ContainerError, ImageNotFound, APIError):
|
|
|
return False
|
|
|
|
|
|
'''
|
|
@@ -100,9 +101,7 @@ def start_container(id=None):
|
|
|
'labels': container.labels,
|
|
|
'status': container.status
|
|
|
}
|
|
|
- except NotFound:
|
|
|
- return None
|
|
|
- except APIError:
|
|
|
+ except (NotFound, APIError):
|
|
|
return None
|
|
|
|
|
|
'''
|
|
@@ -132,9 +131,7 @@ def restart_container(id=None):
|
|
|
'labels': container.labels,
|
|
|
'status': container.status
|
|
|
}
|
|
|
- except NotFound:
|
|
|
- return None
|
|
|
- except APIError:
|
|
|
+ except (NotFound, APIError):
|
|
|
return None
|
|
|
|
|
|
'''
|
|
@@ -164,9 +161,7 @@ def stop_container(id=None):
|
|
|
'labels': container.labels,
|
|
|
'status': container.status
|
|
|
}
|
|
|
- except NotFound:
|
|
|
- return None
|
|
|
- except APIError:
|
|
|
+ except (NotFound, APIError):
|
|
|
return None
|
|
|
|
|
|
'''
|
|
@@ -182,10 +177,27 @@ def execute_command(container_id_or_name=None, command=None):
|
|
|
|
|
|
try:
|
|
|
container = client.containers.get(container_id_or_name)
|
|
|
- container.exec_run(command)
|
|
|
-
|
|
|
- return True
|
|
|
- except NotFound:
|
|
|
- return None
|
|
|
- except APIError:
|
|
|
+ (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
|