service.py 2.9 KB

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