docker_api.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.conf import settings
  4. from docker import DockerClient
  5. from docker.errors import DockerException, NotFound, APIError, ContainerError, ImageNotFound
  6. import time
  7. import tarfile as tar
  8. from io import BytesIO
  9. '''
  10. '''
  11. def get_docker_client():
  12. try:
  13. return DockerClient(base_url='unix:/%s' % settings.DOCKER_SOCK_DIR)
  14. except DockerException:
  15. return None
  16. '''
  17. '''
  18. def get_all_images():
  19. client = get_docker_client()
  20. images = []
  21. if not client:
  22. return images
  23. for image in client.images.list():
  24. images.append({
  25. 'id': image.id,
  26. 'short_id': image.short_id,
  27. 'tags': image.tags
  28. })
  29. return images
  30. '''
  31. '''
  32. def get_all_containers():
  33. client = get_docker_client()
  34. containers = []
  35. if not client:
  36. return containers
  37. for container in client.containers.list(all=True):
  38. containers.append({
  39. 'id': container.id,
  40. 'short_id': container.short_id,
  41. 'name': container.name,
  42. 'image': container.image,
  43. 'labels': container.labels,
  44. 'status': container.status,
  45. 'attrs': container.attrs
  46. })
  47. return containers
  48. '''
  49. '''
  50. def run_container(image=None, name=None, ports=[], volumes=None, net=None):
  51. if not name:
  52. return False
  53. client = get_docker_client()
  54. if not client:
  55. return False
  56. try:
  57. client.containers.run(image, None, name=name, detach=True, ports=ports, volumes=volumes, network=net)
  58. return True
  59. except (ContainerError, ImageNotFound, APIError):
  60. return False
  61. '''
  62. '''
  63. def start_container(id=None):
  64. if not id:
  65. return None
  66. client = get_docker_client()
  67. if not client:
  68. return None
  69. try:
  70. container = client.containers.get(id)
  71. container.start()
  72. time.sleep(1)
  73. container.reload()
  74. return {
  75. 'id': container.id,
  76. 'short_id': container.short_id,
  77. 'name': container.name,
  78. 'image': container.image,
  79. 'labels': container.labels,
  80. 'status': container.status
  81. }
  82. except (NotFound, APIError):
  83. return None
  84. '''
  85. '''
  86. def restart_container(id=None):
  87. if not id:
  88. return None
  89. client = get_docker_client()
  90. if not client:
  91. return None
  92. try:
  93. container = client.containers.get(id)
  94. container.restart()
  95. time.sleep(1)
  96. container.reload()
  97. return {
  98. 'id': container.id,
  99. 'short_id': container.short_id,
  100. 'name': container.name,
  101. 'image': container.image,
  102. 'labels': container.labels,
  103. 'status': container.status
  104. }
  105. except (NotFound, APIError):
  106. return None
  107. '''
  108. '''
  109. def stop_container(id=None):
  110. if not id:
  111. return None
  112. client = get_docker_client()
  113. if not client:
  114. return None
  115. try:
  116. container = client.containers.get(id)
  117. container.stop()
  118. time.sleep(1)
  119. container.reload()
  120. return {
  121. 'id': container.id,
  122. 'short_id': container.short_id,
  123. 'name': container.name,
  124. 'image': container.image,
  125. 'labels': container.labels,
  126. 'status': container.status
  127. }
  128. except (NotFound, APIError):
  129. return None
  130. '''
  131. '''
  132. def execute_command(container_id_or_name=None, command=None):
  133. if not container_id_or_name or not command:
  134. return None
  135. client = get_docker_client()
  136. if not client:
  137. return None
  138. try:
  139. container = client.containers.get(container_id_or_name)
  140. (exit_code, unused_output) = container.exec_run(command)
  141. return (False, True)[exit_code == 0]
  142. except (NotFound, APIError):
  143. return None
  144. '''
  145. '''
  146. def copy_in(container_id_or_name=None, path=None, tarfile=None):
  147. if not container_id_or_name or not path or not tarfile:
  148. return False
  149. client = get_docker_client()
  150. if not client:
  151. return False
  152. try:
  153. file = open(tarfile, 'r')
  154. container = client.containers.get(container_id_or_name)
  155. result = container.put_archive(path, file)
  156. file.close()
  157. return result
  158. except (NotFound, APIError, IOError):
  159. return False