docker_api.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 get_internal_ip(container_name_or_id=None):
  64. if not container_name_or_id:
  65. return None
  66. client = get_docker_client()
  67. if not client:
  68. return None
  69. try:
  70. container = client.containers.get(container_name_or_id)
  71. import pdb; pdb.set_trace()
  72. except (NotFound, APIError):
  73. return None
  74. '''
  75. '''
  76. def run_container(image=None, name=None, ports=[], volumes=None, net=None):
  77. if not name:
  78. return False
  79. client = get_docker_client()
  80. if not client:
  81. return False
  82. try:
  83. client.containers.run(image, None, name=name, detach=True, ports=ports, volumes=volumes, network=net)
  84. return True
  85. except (ContainerError, ImageNotFound, APIError):
  86. return False
  87. '''
  88. '''
  89. def start_container(id=None):
  90. if not id:
  91. return None
  92. client = get_docker_client()
  93. if not client:
  94. return None
  95. try:
  96. container = client.containers.get(id)
  97. container.start()
  98. time.sleep(1)
  99. container.reload()
  100. return {
  101. 'id': container.id,
  102. 'short_id': container.short_id,
  103. 'name': container.name,
  104. 'image': container.image,
  105. 'labels': container.labels,
  106. 'status': container.status
  107. }
  108. except (NotFound, APIError):
  109. return None
  110. '''
  111. '''
  112. def restart_container(id=None):
  113. if not id:
  114. return None
  115. client = get_docker_client()
  116. if not client:
  117. return None
  118. try:
  119. container = client.containers.get(id)
  120. container.restart()
  121. time.sleep(1)
  122. container.reload()
  123. return {
  124. 'id': container.id,
  125. 'short_id': container.short_id,
  126. 'name': container.name,
  127. 'image': container.image,
  128. 'labels': container.labels,
  129. 'status': container.status
  130. }
  131. except (NotFound, APIError):
  132. return None
  133. '''
  134. '''
  135. def stop_container(id=None):
  136. if not id:
  137. return None
  138. client = get_docker_client()
  139. if not client:
  140. return None
  141. try:
  142. container = client.containers.get(id)
  143. container.stop()
  144. time.sleep(1)
  145. container.reload()
  146. return {
  147. 'id': container.id,
  148. 'short_id': container.short_id,
  149. 'name': container.name,
  150. 'image': container.image,
  151. 'labels': container.labels,
  152. 'status': container.status
  153. }
  154. except (NotFound, APIError):
  155. return None
  156. '''
  157. '''
  158. def execute_command(container_id_or_name=None, command=None):
  159. if not container_id_or_name or not command:
  160. return None
  161. client = get_docker_client()
  162. if not client:
  163. return None
  164. try:
  165. container = client.containers.get(container_id_or_name)
  166. (exit_code, unused_output) = container.exec_run(command)
  167. return (False, True)[exit_code == 0]
  168. except (NotFound, APIError):
  169. return None
  170. '''
  171. '''
  172. def copy_in(container_id_or_name=None, path=None, tarfile=None):
  173. if not container_id_or_name or not path or not tarfile:
  174. return False
  175. client = get_docker_client()
  176. if not client:
  177. return False
  178. try:
  179. file = open(tarfile, 'r')
  180. container = client.containers.get(container_id_or_name)
  181. result = container.put_archive(path, file)
  182. file.close()
  183. return result
  184. except (NotFound, APIError, IOError):
  185. return False