project.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. # Get all services that are dependant on another.
  8. dependent_services = [s for s in services if s.get('links')]
  9. flatten_links = sum([s['links'] for s in dependent_services], [])
  10. # Get all services that are not linked to and don't link to others.
  11. non_dependent_sevices = [s for s in services if s['name'] not in flatten_links and not s.get('links')]
  12. sorted_services = []
  13. # Topological sort.
  14. while dependent_services:
  15. n = dependent_services.pop()
  16. # Check if a service is dependent on itself, if so raise an error.
  17. if n['name'] in n.get('links', []):
  18. raise DependencyError('A service can not link to itself: %s' % n['name'])
  19. sorted_services.append(n)
  20. for l in n['links']:
  21. # Get the linked service.
  22. linked_service = next(s for s in services if l == s['name'])
  23. # Check that there isn't a circular import between services.
  24. if n['name'] in linked_service.get('links', []):
  25. raise DependencyError('Circular import between %s and %s' % (n['name'], linked_service['name']))
  26. # Check the linked service has no links and is not already in the
  27. # sorted service list.
  28. if not linked_service.get('links') and linked_service not in sorted_services:
  29. sorted_services.insert(0, linked_service)
  30. return non_dependent_sevices + sorted_services
  31. class Project(object):
  32. """
  33. A collection of services.
  34. """
  35. def __init__(self, name, services, client):
  36. self.name = name
  37. self.services = services
  38. self.client = client
  39. @classmethod
  40. def from_dicts(cls, name, service_dicts, client):
  41. """
  42. Construct a ServiceCollection from a list of dicts representing services.
  43. """
  44. project = cls(name, [], client)
  45. for service_dict in sort_service_dicts(service_dicts):
  46. # Reference links by object
  47. links = []
  48. if 'links' in service_dict:
  49. for service_name in service_dict.get('links', []):
  50. links.append(project.get_service(service_name))
  51. del service_dict['links']
  52. project.services.append(Service(client=client, project=name, links=links, **service_dict))
  53. return project
  54. @classmethod
  55. def from_config(cls, name, config, client):
  56. dicts = []
  57. for service_name, service in list(config.items()):
  58. service['name'] = service_name
  59. dicts.append(service)
  60. return cls.from_dicts(name, dicts, client)
  61. def get_service(self, name):
  62. """
  63. Retrieve a service by name. Raises NoSuchService
  64. if the named service does not exist.
  65. """
  66. for service in self.services:
  67. if service.name == name:
  68. return service
  69. raise NoSuchService(name)
  70. def get_services(self, service_names=None):
  71. """
  72. Returns a list of this project's services filtered
  73. by the provided list of names, or all services if
  74. service_names is None or [].
  75. Preserves the original order of self.services.
  76. Raises NoSuchService if any of the named services
  77. do not exist.
  78. """
  79. if service_names is None or len(service_names) == 0:
  80. return self.services
  81. else:
  82. unsorted = [self.get_service(name) for name in service_names]
  83. return [s for s in self.services if s in unsorted]
  84. def recreate_containers(self, service_names=None):
  85. """
  86. For each service, create or recreate their containers.
  87. Returns a tuple with two lists. The first is a list of
  88. (service, old_container) tuples; the second is a list
  89. of (service, new_container) tuples.
  90. """
  91. old = []
  92. new = []
  93. for service in self.get_services(service_names):
  94. (s_old, s_new) = service.recreate_containers()
  95. old += [(service, container) for container in s_old]
  96. new += [(service, container) for container in s_new]
  97. return (old, new)
  98. def start(self, service_names=None, **options):
  99. for service in self.get_services(service_names):
  100. service.start(**options)
  101. def stop(self, service_names=None, **options):
  102. for service in reversed(self.get_services(service_names)):
  103. service.stop(**options)
  104. def kill(self, service_names=None, **options):
  105. for service in reversed(self.get_services(service_names)):
  106. service.kill(**options)
  107. def build(self, service_names=None, **options):
  108. for service in self.get_services(service_names):
  109. if service.can_be_built():
  110. service.build(**options)
  111. else:
  112. log.info('%s uses an image, skipping' % service.name)
  113. def remove_stopped(self, service_names=None, **options):
  114. for service in self.get_services(service_names):
  115. service.remove_stopped(**options)
  116. def containers(self, service_names=None, *args, **kwargs):
  117. l = []
  118. for service in self.get_services(service_names):
  119. for container in service.containers(*args, **kwargs):
  120. l.append(container)
  121. return l
  122. class NoSuchService(Exception):
  123. def __init__(self, name):
  124. self.name = name
  125. self.msg = "No such service: %s" % self.name
  126. def __str__(self):
  127. return self.msg
  128. class DependencyError(Exception):
  129. def __init__(self, msg):
  130. self.msg = msg
  131. def __str__(self):
  132. return self.msg