service.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import logging
  2. import re
  3. log = logging.getLogger(__name__)
  4. class Service(object):
  5. def __init__(self, name, client=None, links=[], **options):
  6. if not re.match('^[a-zA-Z0-9_]+$', name):
  7. raise ValueError('Invalid name: %s' % name)
  8. if 'image' in options and 'build' in options:
  9. 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.')
  10. self.name = name
  11. self.client = client
  12. self.links = links or []
  13. self.options = options
  14. @property
  15. def containers(self):
  16. return [c for c in self.client.containers(all=True) if parse_name(get_container_name(c))[0] == self.name]
  17. def start(self):
  18. if len(self.containers) == 0:
  19. return self.start_container()
  20. def stop(self):
  21. self.scale(0)
  22. def scale(self, num):
  23. while len(self.containers) < num:
  24. self.start_container()
  25. while len(self.containers) > num:
  26. self.stop_container()
  27. def create_container(self, **override_options):
  28. container_options = self._get_container_options(override_options)
  29. return self.client.create_container(**container_options)
  30. def start_container(self, container=None, **override_options):
  31. container_options = self._get_container_options(override_options)
  32. if container is None:
  33. container = self.create_container(**override_options)
  34. port_bindings = {}
  35. for port in self.options.get('ports', []):
  36. port = unicode(port)
  37. if ':' in port:
  38. internal_port, external_port = port.split(':', 1)
  39. port_bindings[int(internal_port)] = int(external_port)
  40. else:
  41. port_bindings[int(port)] = None
  42. self.client.start(
  43. container['Id'],
  44. links=self._get_links(),
  45. port_bindings=port_bindings,
  46. )
  47. return container
  48. def stop_container(self):
  49. container_id = self.containers[-1]['Id']
  50. self.client.kill(container_id)
  51. self.client.remove_container(container_id)
  52. def next_container_number(self):
  53. numbers = [parse_name(get_container_name(c))[1] for c in self.containers]
  54. if len(numbers) == 0:
  55. return 1
  56. else:
  57. return max(numbers) + 1
  58. def get_names(self):
  59. return [get_container_name(c) for c in self.containers]
  60. def inspect(self):
  61. return [self.client.inspect_container(c['Id']) for c in self.containers]
  62. def _get_links(self):
  63. links = {}
  64. for service in self.links:
  65. for name in service.get_names():
  66. links[name] = name
  67. return links
  68. def _get_container_options(self, override_options):
  69. keys = ['image', 'command', 'hostname', 'user', 'detach', 'stdin_open', 'tty', 'mem_limit', 'ports', 'environment', 'dns', 'volumes', 'volumes_from']
  70. container_options = dict((k, self.options[k]) for k in keys if k in self.options)
  71. container_options.update(override_options)
  72. number = self.next_container_number()
  73. container_options['name'] = make_name(self.name, number)
  74. if 'ports' in container_options:
  75. container_options['ports'] = [unicode(p).split(':')[0] for p in container_options['ports']]
  76. if 'build' in self.options:
  77. log.info('Building %s from %s...' % (self.name, self.options['build']))
  78. container_options['image'] = self.client.build(self.options['build'])[0]
  79. return container_options
  80. def make_name(prefix, number):
  81. return '%s_%s' % (prefix, number)
  82. def parse_name(name):
  83. match = re.match('^(.+)_(\d+)$', name)
  84. if match is None:
  85. raise ValueError("Invalid name: %s" % name)
  86. (service_name, suffix) = match.groups()
  87. return (service_name, int(suffix))
  88. def get_container_name(container):
  89. for name in container['Names']:
  90. if len(name.split('/')) == 2:
  91. return name[1:]