project.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. links.append((project.get_service(service_name), link_name))
  53. del service_dict['links']
  54. project.services.append(Service(client=client, project=name, links=links, **service_dict))
  55. return project
  56. @classmethod
  57. def from_config(cls, name, config, client):
  58. dicts = []
  59. for service_name, service in list(config.items()):
  60. if not isinstance(service, dict):
  61. 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.')
  62. service['name'] = service_name
  63. dicts.append(service)
  64. return cls.from_dicts(name, dicts, client)
  65. def get_service(self, name):
  66. """
  67. Retrieve a service by name. Raises NoSuchService
  68. if the named service does not exist.
  69. """
  70. for service in self.services:
  71. if service.name == name:
  72. return service
  73. raise NoSuchService(name)
  74. def get_services(self, service_names=None):
  75. """
  76. Returns a list of this project's services filtered
  77. by the provided list of names, or all services if
  78. service_names is None or [].
  79. Preserves the original order of self.services.
  80. Raises NoSuchService if any of the named services
  81. do not exist.
  82. """
  83. if service_names is None or len(service_names) == 0:
  84. return self.services
  85. else:
  86. unsorted = [self.get_service(name) for name in service_names]
  87. return [s for s in self.services if s in unsorted]
  88. def recreate_containers(self, service_names=None):
  89. """
  90. For each service, create or recreate their containers.
  91. Returns a tuple with two lists. The first is a list of
  92. (service, old_container) tuples; the second is a list
  93. of (service, new_container) tuples.
  94. """
  95. old = []
  96. new = []
  97. for service in self.get_services(service_names):
  98. (s_old, s_new) = service.recreate_containers()
  99. old += [(service, container) for container in s_old]
  100. new += [(service, container) for container in s_new]
  101. return (old, new)
  102. def start(self, service_names=None, **options):
  103. for service in self.get_services(service_names):
  104. service.start(**options)
  105. def stop(self, service_names=None, **options):
  106. for service in reversed(self.get_services(service_names)):
  107. service.stop(**options)
  108. def kill(self, service_names=None, **options):
  109. for service in reversed(self.get_services(service_names)):
  110. service.kill(**options)
  111. def build(self, service_names=None, **options):
  112. for service in self.get_services(service_names):
  113. if service.can_be_built():
  114. service.build(**options)
  115. else:
  116. log.info('%s uses an image, skipping' % service.name)
  117. def up(self, service_names=None):
  118. (old, new) = self.recreate_containers(service_names=service_names)
  119. for (service, container) in new:
  120. service.start_container(container)
  121. for (service, container) in old:
  122. container.remove()
  123. return new
  124. def remove_stopped(self, service_names=None, **options):
  125. for service in self.get_services(service_names):
  126. service.remove_stopped(**options)
  127. def containers(self, service_names=None, *args, **kwargs):
  128. l = []
  129. for service in self.get_services(service_names):
  130. for container in service.containers(*args, **kwargs):
  131. l.append(container)
  132. return l
  133. class NoSuchService(Exception):
  134. def __init__(self, name):
  135. self.name = name
  136. self.msg = "No such service: %s" % self.name
  137. def __str__(self):
  138. return self.msg
  139. class ConfigurationError(Exception):
  140. def __init__(self, msg):
  141. self.msg = msg
  142. def __str__(self):
  143. return self.msg
  144. class DependencyError(ConfigurationError):
  145. pass