docker_api.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # -*- coding: utf-8 -*-
  2. from openerp.tools import config
  3. from openerp.exceptions import Warning
  4. from docker import DockerClient
  5. from docker.errors import DockerException, NotFound, APIError, ContainerError, ImageNotFound
  6. import time
  7. try:
  8. client = DockerClient(base_url='unix:/%s' % config['docker_sock'])
  9. except DockerException:
  10. raise Warning('Socket not found')
  11. def get_all_images():
  12. images = []
  13. for image in client.images.list():
  14. images.append({
  15. 'id': image.id,
  16. 'short_id': image.short_id,
  17. 'tags': image.tags
  18. })
  19. return images
  20. def get_all_containers():
  21. exclusion_list = map(lambda x: x.strip(), config['docker_exclusion'] or [])
  22. containers = []
  23. for container in client.containers.list(all=True):
  24. if container.name in exclusion_list:
  25. continue
  26. containers.append({
  27. 'id': container.id,
  28. 'short_id': container.short_id,
  29. 'name': container.name,
  30. 'image': container.image,
  31. 'labels': container.labels,
  32. 'status': container.status,
  33. 'attrs': container.attrs
  34. })
  35. return containers
  36. def get_all_external_ports():
  37. port_dict = map(lambda x: x.get('attrs').get('NetworkSettings').get('Ports'), get_all_containers())
  38. ports = []
  39. for m in port_dict:
  40. if not m:
  41. continue
  42. for _, v in m.iteritems():
  43. if not v:
  44. continue
  45. ports.extend(map(lambda x: x['HostPort'], v))
  46. return ports
  47. def get_network(network_name_or_id):
  48. try:
  49. network = client.networks.get(network_name_or_id)
  50. return {
  51. 'id': network.id,
  52. 'short_id': network.short_id,
  53. 'name': network.name,
  54. 'attrs': network.attrs
  55. }
  56. except (NotFound, APIError):
  57. return None
  58. def get_internal_ip(container_name_or_id):
  59. network = get_network(config['docker_network'] or 'bridge')
  60. if not network:
  61. return None
  62. try:
  63. container = client.containers.get(container_name_or_id)
  64. internal_ip = container.attrs.get('NetworkSettings').get('Networks').get(network.get('name')).get('IPAddress')
  65. return str(internal_ip)
  66. except (NotFound, APIError):
  67. return None
  68. def run_container(image, name, ports=[], volumes=None, network=None, memory_limit=None, memory_swap_limit=None):
  69. try:
  70. client.containers.run(
  71. image,
  72. None,
  73. name=name,
  74. detach=True,
  75. mem_limit=memory_limit,
  76. memswap_limit=memory_swap_limit,
  77. network=network,
  78. ports=ports,
  79. volumes=volumes
  80. )
  81. return True
  82. except (ContainerError, ImageNotFound, APIError):
  83. return False
  84. def start_container(container_name_or_id):
  85. try:
  86. container = client.containers.get(container_name_or_id)
  87. container.start()
  88. time.sleep(0.3)
  89. container.reload()
  90. return {
  91. 'id': container.id,
  92. 'short_id': container.short_id,
  93. 'name': container.name,
  94. 'image': container.image,
  95. 'labels': container.labels,
  96. 'status': container.status
  97. }
  98. except (NotFound, APIError):
  99. return False
  100. def restart_container(container_name_or_id):
  101. try:
  102. container = client.containers.get(container_name_or_id)
  103. container.restart()
  104. time.sleep(0.3)
  105. container.reload()
  106. return {
  107. 'id': container.id,
  108. 'short_id': container.short_id,
  109. 'name': container.name,
  110. 'image': container.image,
  111. 'labels': container.labels,
  112. 'status': container.status
  113. }
  114. except (NotFound, APIError):
  115. return False
  116. def stop_container(container_name_or_id):
  117. try:
  118. container = client.containers.get(container_name_or_id)
  119. container.stop()
  120. time.sleep(0.3)
  121. container.reload()
  122. return {
  123. 'id': container.id,
  124. 'short_id': container.short_id,
  125. 'name': container.name,
  126. 'image': container.image,
  127. 'labels': container.labels,
  128. 'status': container.status
  129. }
  130. except (NotFound, APIError):
  131. return False
  132. def run_command(container_name_or_id, command):
  133. try:
  134. container = client.containers.get(container_name_or_id)
  135. (exit_code, _) = container.exec_run(command)
  136. return (False, True)[exit_code == 0]
  137. except (NotFound, APIError):
  138. return False
  139. def copy_in(container_name_or_id, path, tarfile):
  140. try:
  141. filestream = open(tarfile, 'r')
  142. container = client.containers.get(container_name_or_id)
  143. result = container.put_archive(path, filestream)
  144. filestream.close()
  145. return result
  146. except (NotFound, APIError, IOError):
  147. return False
  148. def get_status(container_name_or_id):
  149. try:
  150. container = client.containers.get(container_name_or_id)
  151. return (False, True)[container.status == 'running']
  152. except (NotFound, APIError):
  153. return False