docker_api.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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
  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. })
  44. return containers
  45. '''
  46. '''
  47. def run_container(image=None, name=None, ports=[], volumes=None, net=None):
  48. if not name:
  49. return False
  50. client = get_docker_client()
  51. if not client:
  52. return False
  53. try:
  54. client.containers.run(image, None, name=name, detach=True, ports=ports, volumes=volumes, network=net)
  55. return True
  56. except NotFound:
  57. return False
  58. except APIError:
  59. return False
  60. '''
  61. '''
  62. def start_container(id=None):
  63. if not id:
  64. return None
  65. client = get_docker_client()
  66. if not client:
  67. return None
  68. try:
  69. container = client.containers.get(id)
  70. container.start()
  71. time.sleep(1)
  72. container.reload()
  73. return {
  74. 'id': container.id,
  75. 'short_id': container.short_id,
  76. 'name': container.name,
  77. 'image': container.image,
  78. 'labels': container.labels,
  79. 'status': container.status
  80. }
  81. except NotFound:
  82. return None
  83. except APIError:
  84. return None
  85. '''
  86. '''
  87. def restart_container(id=None):
  88. if not id:
  89. return None
  90. client = get_docker_client()
  91. if not client:
  92. return None
  93. try:
  94. container = client.containers.get(id)
  95. container.restart()
  96. time.sleep(1)
  97. container.reload()
  98. return {
  99. 'id': container.id,
  100. 'short_id': container.short_id,
  101. 'name': container.name,
  102. 'image': container.image,
  103. 'labels': container.labels,
  104. 'status': container.status
  105. }
  106. except NotFound:
  107. return None
  108. except APIError:
  109. return None
  110. '''
  111. '''
  112. def stop_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.stop()
  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:
  132. return None
  133. except APIError:
  134. return None
  135. '''
  136. '''
  137. def execute_command(container_id_or_name=None, command=None):
  138. if not container_id_or_name or not command:
  139. return None
  140. client = get_docker_client()
  141. if not client:
  142. return None
  143. try:
  144. container = client.containers.get(container_id_or_name)
  145. container.exec_run(command)
  146. return True
  147. except NotFound:
  148. return None
  149. except APIError:
  150. return None