docker_api.py 4.7 KB

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