2013-12-09 15:10:23 +01:00
|
|
|
from .service import Service
|
|
|
|
|
|
|
|
def sort_service_dicts(services):
|
|
|
|
# Sort in dependency order
|
|
|
|
def cmp(x, y):
|
|
|
|
x_deps_y = y['name'] in x.get('links', [])
|
|
|
|
y_deps_x = x['name'] in y.get('links', [])
|
|
|
|
if x_deps_y and not y_deps_x:
|
|
|
|
return 1
|
|
|
|
elif y_deps_x and not x_deps_y:
|
|
|
|
return -1
|
|
|
|
return 0
|
|
|
|
return sorted(services, cmp=cmp)
|
|
|
|
|
|
|
|
class ServiceCollection(list):
|
|
|
|
@classmethod
|
2013-12-19 16:32:24 +01:00
|
|
|
def from_dicts(cls, service_dicts, client, project='default'):
|
2013-12-09 15:10:23 +01:00
|
|
|
"""
|
|
|
|
Construct a ServiceCollection from a list of dicts representing services.
|
|
|
|
"""
|
|
|
|
collection = ServiceCollection()
|
|
|
|
for service_dict in sort_service_dicts(service_dicts):
|
|
|
|
# Reference links by object
|
|
|
|
links = []
|
|
|
|
if 'links' in service_dict:
|
|
|
|
for name in service_dict.get('links', []):
|
|
|
|
links.append(collection.get(name))
|
|
|
|
del service_dict['links']
|
2013-12-19 16:32:24 +01:00
|
|
|
collection.append(Service(client=client, project=project, links=links, **service_dict))
|
2013-12-09 15:10:23 +01:00
|
|
|
return collection
|
|
|
|
|
2013-12-11 15:25:32 +01:00
|
|
|
@classmethod
|
2013-12-19 16:32:24 +01:00
|
|
|
def from_config(cls, config, client, project='default'):
|
2013-12-11 15:25:32 +01:00
|
|
|
dicts = []
|
|
|
|
for name, service in config.items():
|
|
|
|
service['name'] = name
|
|
|
|
dicts.append(service)
|
2013-12-19 16:32:24 +01:00
|
|
|
return cls.from_dicts(dicts, client, project)
|
2013-12-11 15:25:32 +01:00
|
|
|
|
2013-12-09 15:10:23 +01:00
|
|
|
def get(self, name):
|
|
|
|
for service in self:
|
|
|
|
if service.name == name:
|
|
|
|
return service
|
|
|
|
|
2013-12-09 18:48:15 +01:00
|
|
|
def start(self):
|
2013-12-17 12:44:36 +01:00
|
|
|
for service in self:
|
|
|
|
service.start()
|
2013-12-09 18:48:15 +01:00
|
|
|
|
|
|
|
def stop(self):
|
2013-12-17 12:44:36 +01:00
|
|
|
for service in self:
|
|
|
|
service.stop()
|
2013-12-09 18:48:15 +01:00
|
|
|
|
2013-12-19 14:02:04 +01:00
|
|
|
def containers(self, *args, **kwargs):
|
|
|
|
l = []
|
|
|
|
for service in self:
|
|
|
|
for container in service.containers(*args, **kwargs):
|
|
|
|
l.append(container)
|
|
|
|
return l
|
2013-12-09 18:48:15 +01:00
|
|
|
|
2013-12-09 15:10:23 +01:00
|
|
|
|