service.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 container_options.get('ports', []):
  36. port_bindings[port] = None
  37. self.client.start(
  38. container['Id'],
  39. links=self._get_links(),
  40. port_bindings=port_bindings,
  41. )
  42. return container
  43. def stop_container(self):
  44. container_id = self.containers[-1]['Id']
  45. self.client.kill(container_id)
  46. self.client.remove_container(container_id)
  47. def next_container_number(self):
  48. numbers = [parse_name(get_container_name(c))[1] for c in self.containers]
  49. if len(numbers) == 0:
  50. return 1
  51. else:
  52. return max(numbers) + 1
  53. def get_names(self):
  54. return [get_container_name(c) for c in self.containers]
  55. def inspect(self):
  56. return [self.client.inspect_container(c['Id']) for c in self.containers]
  57. def _get_links(self):
  58. links = {}
  59. for service in self.links:
  60. for name in service.get_names():
  61. links[name] = name
  62. return links
  63. def _get_container_options(self, override_options):
  64. keys = ['image', 'command', 'hostname', 'user', 'detach', 'stdin_open', 'tty', 'mem_limit', 'ports', 'environment', 'dns', 'volumes', 'volumes_from']
  65. container_options = dict((k, self.options[k]) for k in keys if k in self.options)
  66. container_options.update(override_options)
  67. number = self.next_container_number()
  68. container_options['name'] = make_name(self.name, number)
  69. if 'build' in self.options:
  70. log.info('Building %s from %s...' % (self.name, self.options['build']))
  71. container_options['image'] = self.client.build(self.options['build'])[0]
  72. return container_options
  73. def make_name(prefix, number):
  74. return '%s_%s' % (prefix, number)
  75. def parse_name(name):
  76. match = re.match('^(.+)_(\d+)$', name)
  77. if match is None:
  78. raise ValueError("Invalid name: %s" % name)
  79. (service_name, suffix) = match.groups()
  80. return (service_name, int(suffix))
  81. def get_container_name(container):
  82. for name in container['Names']:
  83. if len(name.split('/')) == 2:
  84. return name[1:]