docker_api.py 5.4 KB

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