docker_api.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if container.name in settings.DOCKER_FILTERS:
  37. continue
  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 get_all_external_ports():
  51. ports = []
  52. for m in map(lambda x: x['attrs']['NetworkSettings']['Ports'], get_all_containers()):
  53. if not m:
  54. continue
  55. for _, v in m.iteritems():
  56. if not v:
  57. continue
  58. port = map(lambda x: x['HostPort'], v)
  59. ports.extend(port)
  60. return ports
  61. '''
  62. '''
  63. def run_container(image=None, name=None, ports=[], volumes=None, net=None):
  64. if not name:
  65. return False
  66. client = get_docker_client()
  67. if not client:
  68. return False
  69. try:
  70. client.containers.run(image, None, name=name, detach=True, ports=ports, volumes=volumes, network=net)
  71. return True
  72. except (ContainerError, ImageNotFound, APIError):
  73. return False
  74. '''
  75. '''
  76. def start_container(id=None):
  77. if not id:
  78. return None
  79. client = get_docker_client()
  80. if not client:
  81. return None
  82. try:
  83. container = client.containers.get(id)
  84. container.start()
  85. time.sleep(1)
  86. container.reload()
  87. return {
  88. 'id': container.id,
  89. 'short_id': container.short_id,
  90. 'name': container.name,
  91. 'image': container.image,
  92. 'labels': container.labels,
  93. 'status': container.status
  94. }
  95. except (NotFound, APIError):
  96. return None
  97. '''
  98. '''
  99. def restart_container(id=None):
  100. if not id:
  101. return None
  102. client = get_docker_client()
  103. if not client:
  104. return None
  105. try:
  106. container = client.containers.get(id)
  107. container.restart()
  108. time.sleep(1)
  109. container.reload()
  110. return {
  111. 'id': container.id,
  112. 'short_id': container.short_id,
  113. 'name': container.name,
  114. 'image': container.image,
  115. 'labels': container.labels,
  116. 'status': container.status
  117. }
  118. except (NotFound, APIError):
  119. return None
  120. '''
  121. '''
  122. def stop_container(id=None):
  123. if not id:
  124. return None
  125. client = get_docker_client()
  126. if not client:
  127. return None
  128. try:
  129. container = client.containers.get(id)
  130. container.stop()
  131. time.sleep(1)
  132. container.reload()
  133. return {
  134. 'id': container.id,
  135. 'short_id': container.short_id,
  136. 'name': container.name,
  137. 'image': container.image,
  138. 'labels': container.labels,
  139. 'status': container.status
  140. }
  141. except (NotFound, APIError):
  142. return None
  143. '''
  144. '''
  145. def execute_command(container_id_or_name=None, command=None):
  146. if not container_id_or_name or not command:
  147. return None
  148. client = get_docker_client()
  149. if not client:
  150. return None
  151. try:
  152. container = client.containers.get(container_id_or_name)
  153. (exit_code, unused_output) = container.exec_run(command)
  154. return (False, True)[exit_code == 0]
  155. except (NotFound, APIError):
  156. return None
  157. '''
  158. '''
  159. def copy_in(container_id_or_name=None, path=None, tarfile=None):
  160. if not container_id_or_name or not path or not tarfile:
  161. return False
  162. client = get_docker_client()
  163. if not client:
  164. return False
  165. try:
  166. file = open(tarfile, 'r')
  167. container = client.containers.get(container_id_or_name)
  168. result = container.put_archive(path, file)
  169. file.close()
  170. return result
  171. except (NotFound, APIError, IOError):
  172. return False