docker_api.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 start_container(id=None):
  48. if not id:
  49. return None
  50. client = get_docker_client()
  51. if not client:
  52. return None
  53. try:
  54. container = client.containers.get(id)
  55. container.start()
  56. time.sleep(1)
  57. container.reload()
  58. return {
  59. 'id': container.id,
  60. 'short_id': container.short_id,
  61. 'name': container.name,
  62. 'image': container.image,
  63. 'labels': container.labels,
  64. 'status': container.status
  65. }
  66. except NotFound:
  67. return None
  68. except APIError:
  69. return None
  70. '''
  71. '''
  72. def restart_container(id=None):
  73. if not id:
  74. return None
  75. client = get_docker_client()
  76. if not client:
  77. return None
  78. try:
  79. container = client.containers.get(id)
  80. container.restart()
  81. time.sleep(1)
  82. container.reload()
  83. return {
  84. 'id': container.id,
  85. 'short_id': container.short_id,
  86. 'name': container.name,
  87. 'image': container.image,
  88. 'labels': container.labels,
  89. 'status': container.status
  90. }
  91. except NotFound:
  92. return None
  93. except APIError:
  94. return None
  95. '''
  96. '''
  97. def stop_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.stop()
  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:
  117. return None
  118. except APIError:
  119. return None