service.py 5.9 KB

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