project.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. import logging
  4. from .service import Service
  5. log = logging.getLogger(__name__)
  6. def sort_service_dicts(services):
  7. # Topological sort (Cormen/Tarjan algorithm).
  8. unmarked = services[:]
  9. temporary_marked = set()
  10. sorted_services = []
  11. get_service_names = lambda links: [link.split(':')[0] for link in links]
  12. def visit(n):
  13. if n['name'] in temporary_marked:
  14. if n['name'] in get_service_names(n.get('links', [])):
  15. raise DependencyError('A service can not link to itself: %s' % n['name'])
  16. else:
  17. raise DependencyError('Circular import between %s' % ' and '.join(temporary_marked))
  18. if n in unmarked:
  19. temporary_marked.add(n['name'])
  20. dependents = [m for m in services if n['name'] in get_service_names(m.get('links', []))]
  21. for m in dependents:
  22. visit(m)
  23. temporary_marked.remove(n['name'])
  24. unmarked.remove(n)
  25. sorted_services.insert(0, n)
  26. while unmarked:
  27. visit(unmarked[-1])
  28. return sorted_services
  29. class Project(object):
  30. """
  31. A collection of services.
  32. """
  33. def __init__(self, name, services, client):
  34. self.name = name
  35. self.services = services
  36. self.client = client
  37. @classmethod
  38. def from_dicts(cls, name, service_dicts, client):
  39. """
  40. Construct a ServiceCollection from a list of dicts representing services.
  41. """
  42. project = cls(name, [], client)
  43. for service_dict in sort_service_dicts(service_dicts):
  44. # Reference links by object
  45. links = []
  46. if 'links' in service_dict:
  47. for link in service_dict.get('links', []):
  48. if ':' in link:
  49. service_name, link_name = link.split(':', 1)
  50. else:
  51. service_name, link_name = link, None
  52. try:
  53. links.append((project.get_service(service_name), link_name))
  54. except NoSuchService:
  55. raise ConfigurationError('Service "%s" has a link to service "%s" which does not exist.' % (service_dict['name'], service_name))
  56. del service_dict['links']
  57. project.services.append(Service(client=client, project=name, links=links, **service_dict))
  58. return project
  59. @classmethod
  60. def from_config(cls, name, config, client):
  61. dicts = []
  62. for service_name, service in list(config.items()):
  63. if not isinstance(service, dict):
  64. raise ConfigurationError('Service "%s" doesn\'t have any configuration options. All top level keys in your fig.yml must map to a dictionary of configuration options.')
  65. service['name'] = service_name
  66. dicts.append(service)
  67. return cls.from_dicts(name, dicts, client)
  68. def get_service(self, name):
  69. """
  70. Retrieve a service by name. Raises NoSuchService
  71. if the named service does not exist.
  72. """
  73. for service in self.services:
  74. if service.name == name:
  75. return service
  76. raise NoSuchService(name)
  77. def get_services(self, service_names=None):
  78. """
  79. Returns a list of this project's services filtered
  80. by the provided list of names, or all services if
  81. service_names is None or [].
  82. Preserves the original order of self.services.
  83. Raises NoSuchService if any of the named services
  84. do not exist.
  85. """
  86. if service_names is None or len(service_names) == 0:
  87. return filter(lambda s: s.options['auto_start'], self.services)
  88. else:
  89. unsorted = [self.get_service(name) for name in service_names]
  90. return [s for s in self.services if s in unsorted]
  91. def start(self, service_names=None, **options):
  92. for service in self.get_services(service_names):
  93. service.start(**options)
  94. def stop(self, service_names=None, **options):
  95. for service in reversed(self.get_services(service_names)):
  96. service.stop(**options)
  97. def kill(self, service_names=None, **options):
  98. for service in reversed(self.get_services(service_names)):
  99. service.kill(**options)
  100. def build(self, service_names=None, **options):
  101. for service in self.get_services(service_names):
  102. if service.can_be_built():
  103. service.build(**options)
  104. else:
  105. log.info('%s uses an image, skipping' % service.name)
  106. def up(self, service_names=None):
  107. new_containers = []
  108. for service in self.get_services(service_names):
  109. for (_, new) in service.recreate_containers():
  110. new_containers.append(new)
  111. return new_containers
  112. def remove_stopped(self, service_names=None, **options):
  113. for service in self.get_services(service_names):
  114. service.remove_stopped(**options)
  115. def containers(self, service_names=None, *args, **kwargs):
  116. l = []
  117. for service in self.get_services(service_names):
  118. for container in service.containers(*args, **kwargs):
  119. l.append(container)
  120. return l
  121. class NoSuchService(Exception):
  122. def __init__(self, name):
  123. self.name = name
  124. self.msg = "No such service: %s" % self.name
  125. def __str__(self):
  126. return self.msg
  127. class ConfigurationError(Exception):
  128. def __init__(self, msg):
  129. self.msg = msg
  130. def __str__(self):
  131. return self.msg
  132. class DependencyError(ConfigurationError):
  133. pass