docker_api.py 4.3 KB

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