docker_api.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. '''
  7. '''
  8. def get_docker_client():
  9. try:
  10. return DockerClient(base_url='unix:/%s' % settings.DOCKER_SOCK_DIR)
  11. except DockerException:
  12. return None
  13. '''
  14. '''
  15. def get_all_images():
  16. client = get_docker_client()
  17. images = []
  18. if not client:
  19. return images
  20. for image in client.images.list():
  21. images.append({
  22. 'id': image.id,
  23. 'short_id': image.short_id,
  24. 'tags': image.tags
  25. })
  26. return images
  27. '''
  28. '''
  29. def get_all_containers():
  30. client = get_docker_client()
  31. containers = []
  32. if not client:
  33. return containers
  34. for container in client.containers.list(all=True):
  35. containers.append({
  36. 'id': container.id,
  37. 'short_id': container.short_id,
  38. 'name': container.name,
  39. 'image': container.image,
  40. 'labels': container.labels,
  41. 'status': container.status
  42. })
  43. return containers
  44. '''
  45. '''
  46. def start_container(id=None):
  47. if not id:
  48. return None
  49. client = get_docker_client()
  50. if not client:
  51. return None
  52. try:
  53. container = client.containers.get(id)
  54. container.start()
  55. container.reload()
  56. return {
  57. 'id': container.id,
  58. 'short_id': container.short_id,
  59. 'name': container.name,
  60. 'image': container.image,
  61. 'labels': container.labels,
  62. 'status': container.status
  63. }
  64. except NotFound:
  65. return None
  66. except APIError:
  67. return None
  68. '''
  69. '''
  70. def restart_container(id=None):
  71. if not id:
  72. return None
  73. client = get_docker_client()
  74. if not client:
  75. return None
  76. try:
  77. container = client.containers.get(id)
  78. container.restart()
  79. container.reload()
  80. return {
  81. 'id': container.id,
  82. 'short_id': container.short_id,
  83. 'name': container.name,
  84. 'image': container.image,
  85. 'labels': container.labels,
  86. 'status': container.status
  87. }
  88. except NotFound:
  89. return None
  90. except APIError:
  91. return None
  92. '''
  93. '''
  94. def stop_container(id=None):
  95. if not id:
  96. return None
  97. client = get_docker_client()
  98. if not client:
  99. return None
  100. try:
  101. container = client.containers.get(id)
  102. container.stop()
  103. container.reload()
  104. return {
  105. 'id': container.id,
  106. 'short_id': container.short_id,
  107. 'name': container.name,
  108. 'image': container.image,
  109. 'labels': container.labels,
  110. 'status': container.status
  111. }
  112. except NotFound:
  113. return None
  114. except APIError:
  115. return None