service.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. from docker.client import APIError
  4. import logging
  5. import re
  6. import os
  7. import sys
  8. from .container import Container
  9. log = logging.getLogger(__name__)
  10. class BuildError(Exception):
  11. pass
  12. class Service(object):
  13. def __init__(self, name, client=None, project='default', links=[], **options):
  14. if not re.match('^[a-zA-Z0-9]+$', name):
  15. raise ValueError('Invalid name: %s' % name)
  16. if not re.match('^[a-zA-Z0-9]+$', project):
  17. raise ValueError('Invalid project: %s' % project)
  18. if 'image' in options and 'build' in options:
  19. raise ValueError('Service %s has both an image and build path specified. A service can either be built to image or use an existing image, not both.' % name)
  20. self.name = name
  21. self.client = client
  22. self.project = project
  23. self.links = links or []
  24. self.options = options
  25. def containers(self, stopped=False, one_off=False):
  26. l = []
  27. for container in self.client.containers(all=stopped):
  28. name = get_container_name(container)
  29. if not name or not is_valid_name(name, one_off):
  30. continue
  31. project, name, number = parse_name(name)
  32. if project == self.project and name == self.name:
  33. l.append(Container.from_ps(self.client, container))
  34. return l
  35. def start(self, **options):
  36. for c in self.containers(stopped=True):
  37. if not c.is_running:
  38. self.start_container(c, **options)
  39. def stop(self, **options):
  40. for c in self.containers():
  41. c.stop(**options)
  42. def kill(self, **options):
  43. for c in self.containers():
  44. c.kill(**options)
  45. def remove_stopped(self, **options):
  46. for c in self.containers(stopped=True):
  47. if not c.is_running:
  48. c.remove(**options)
  49. def create_container(self, one_off=False, **override_options):
  50. """
  51. Create a container for this service. If the image doesn't exist, attempt to pull
  52. it.
  53. """
  54. container_options = self._get_container_options(override_options, one_off=one_off)
  55. try:
  56. return Container.create(self.client, **container_options)
  57. except APIError as e:
  58. if e.response.status_code == 404 and e.explanation and 'No such image' in e.explanation:
  59. log.info('Pulling image %s...' % container_options['image'])
  60. self.client.pull(container_options['image'])
  61. return Container.create(self.client, **container_options)
  62. raise
  63. def start_container(self, container=None, **override_options):
  64. if container is None:
  65. container = self.create_container(**override_options)
  66. options = self.options.copy()
  67. options.update(override_options)
  68. port_bindings = {}
  69. if options.get('ports', None) is not None:
  70. for port in options['ports']:
  71. port = str(port)
  72. if ':' in port:
  73. internal_port, external_port = port.split(':', 1)
  74. port_bindings[int(internal_port)] = int(external_port)
  75. else:
  76. port_bindings[int(port)] = None
  77. volume_bindings = {}
  78. if options.get('volumes', None) is not None:
  79. for volume in options['volumes']:
  80. external_dir, internal_dir = volume.split(':')
  81. volume_bindings[os.path.abspath(external_dir)] = internal_dir
  82. container.start(
  83. links=self._get_links(),
  84. port_bindings=port_bindings,
  85. binds=volume_bindings,
  86. )
  87. return container
  88. def next_container_name(self, one_off=False):
  89. bits = [self.project, self.name]
  90. if one_off:
  91. bits.append('run')
  92. return '_'.join(bits + [str(self.next_container_number(one_off=one_off))])
  93. def next_container_number(self, one_off=False):
  94. numbers = [parse_name(c.name)[2] for c in self.containers(stopped=True, one_off=one_off)]
  95. if len(numbers) == 0:
  96. return 1
  97. else:
  98. return max(numbers) + 1
  99. def _get_links(self):
  100. links = {}
  101. for service in self.links:
  102. for container in service.containers():
  103. links[container.name] = container.name
  104. return links
  105. def _get_container_options(self, override_options, one_off=False):
  106. keys = ['image', 'command', 'hostname', 'user', 'detach', 'stdin_open', 'tty', 'mem_limit', 'ports', 'environment', 'dns', 'volumes', 'volumes_from']
  107. container_options = dict((k, self.options[k]) for k in keys if k in self.options)
  108. container_options.update(override_options)
  109. container_options['name'] = self.next_container_name(one_off)
  110. if 'ports' in container_options:
  111. container_options['ports'] = [str(p).split(':')[0] for p in container_options['ports']]
  112. if 'volumes' in container_options:
  113. container_options['volumes'] = dict((v.split(':')[1], {}) for v in container_options['volumes'])
  114. if self.can_be_built():
  115. if len(self.client.images(name=self._build_tag_name())) == 0:
  116. self.build()
  117. container_options['image'] = self._build_tag_name()
  118. return container_options
  119. def build(self):
  120. log.info('Building %s...' % self.name)
  121. build_output = self.client.build(
  122. self.options['build'],
  123. tag=self._build_tag_name(),
  124. stream=True
  125. )
  126. image_id = None
  127. for line in build_output:
  128. if line:
  129. match = re.search(r'Successfully built ([0-9a-f]+)', line)
  130. if match:
  131. image_id = match.group(1)
  132. sys.stdout.write(line)
  133. if image_id is None:
  134. raise BuildError()
  135. return image_id
  136. def can_be_built(self):
  137. return 'build' in self.options
  138. def _build_tag_name(self):
  139. """
  140. The tag to give to images built for this service.
  141. """
  142. return '%s_%s' % (self.project, self.name)
  143. NAME_RE = re.compile(r'^([^_]+)_([^_]+)_(run_)?(\d+)$')
  144. def is_valid_name(name, one_off=False):
  145. match = NAME_RE.match(name)
  146. if match is None:
  147. return False
  148. if one_off:
  149. return match.group(3) == 'run_'
  150. else:
  151. return match.group(3) is None
  152. def parse_name(name, one_off=False):
  153. match = NAME_RE.match(name)
  154. (project, service_name, _, suffix) = match.groups()
  155. return (project, service_name, int(suffix))
  156. def get_container_name(container):
  157. if not container.get('Name') and not container.get('Names'):
  158. return None
  159. # inspect
  160. if 'Name' in container:
  161. return container['Name']
  162. # ps
  163. for name in container['Names']:
  164. if len(name.split('/')) == 2:
  165. return name[1:]