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)
|
|
|
|
|
2013-12-19 17:55:12 +01:00
|
|
|
class Project(object):
|
|
|
|
"""
|
|
|
|
A collection of services.
|
|
|
|
"""
|
|
|
|
def __init__(self, name, services, client):
|
|
|
|
self.name = name
|
|
|
|
self.services = services
|
|
|
|
self.client = client
|
|
|
|
|
2013-12-09 15:10:23 +01:00
|
|
|
@classmethod
|
2013-12-19 17:55:12 +01:00
|
|
|
def from_dicts(cls, name, service_dicts, client):
|
2013-12-09 15:10:23 +01:00
|
|
|
"""
|
|
|
|
Construct a ServiceCollection from a list of dicts representing services.
|
|
|
|
"""
|
2013-12-19 17:55:12 +01:00
|
|
|
project = cls(name, [], client)
|
2013-12-09 15:10:23 +01:00
|
|
|
for service_dict in sort_service_dicts(service_dicts):
|
|
|
|
# Reference links by object
|
|
|
|
links = []
|
|
|
|
if 'links' in service_dict:
|
2013-12-19 17:56:58 +01:00
|
|
|
for service_name in service_dict.get('links', []):
|
|
|
|
links.append(project.get_service(service_name))
|
2013-12-09 15:10:23 +01:00
|
|
|
del service_dict['links']
|
2013-12-19 17:55:12 +01:00
|
|
|
project.services.append(Service(client=client, project=name, links=links, **service_dict))
|
|
|
|
return project
|
2013-12-09 15:10:23 +01:00
|
|
|
|
2013-12-11 15:25:32 +01:00
|
|
|
@classmethod
|
2013-12-19 17:55:12 +01:00
|
|
|
def from_config(cls, name, config, client):
|
2013-12-11 15:25:32 +01:00
|
|
|
dicts = []
|
2013-12-19 17:56:58 +01:00
|
|
|
for service_name, service in config.items():
|
|
|
|
service['name'] = service_name
|
2013-12-11 15:25:32 +01:00
|
|
|
dicts.append(service)
|
2013-12-19 17:55:12 +01:00
|
|
|
return cls.from_dicts(name, dicts, client)
|
2013-12-11 15:25:32 +01:00
|
|
|
|
2013-12-19 17:55:12 +01:00
|
|
|
def get_service(self, name):
|
2013-12-20 19:30:23 +01:00
|
|
|
"""
|
|
|
|
Retrieve a service by name. Raises NoSuchService
|
|
|
|
if the named service does not exist.
|
|
|
|
"""
|
2013-12-19 17:55:12 +01:00
|
|
|
for service in self.services:
|
2013-12-09 15:10:23 +01:00
|
|
|
if service.name == name:
|
|
|
|
return service
|
|
|
|
|
2013-12-20 19:30:23 +01:00
|
|
|
raise NoSuchService(name)
|
|
|
|
|
|
|
|
def get_services(self, service_names=None):
|
|
|
|
"""
|
|
|
|
Returns a list of this project's services filtered
|
|
|
|
by the provided list of names, or all services if
|
|
|
|
service_names is None or [].
|
|
|
|
|
|
|
|
Preserves the original order of self.services.
|
|
|
|
|
|
|
|
Raises NoSuchService if any of the named services
|
|
|
|
do not exist.
|
|
|
|
"""
|
|
|
|
if service_names is None or len(service_names) == 0:
|
|
|
|
return self.services
|
|
|
|
else:
|
|
|
|
unsorted = [self.get_service(name) for name in service_names]
|
|
|
|
return [s for s in self.services if s in unsorted]
|
|
|
|
|
|
|
|
def create_containers(self, service_names=None):
|
2013-12-20 17:22:54 +01:00
|
|
|
"""
|
2013-12-31 14:02:08 +01:00
|
|
|
For each service, creates a container if there are none.
|
2013-12-20 17:22:54 +01:00
|
|
|
"""
|
2013-12-20 19:30:23 +01:00
|
|
|
for service in self.get_services(service_names):
|
2013-12-31 14:02:08 +01:00
|
|
|
if len(service.containers(stopped=True)) == 0:
|
|
|
|
service.create_container()
|
2013-12-20 17:22:54 +01:00
|
|
|
|
2013-12-20 19:30:23 +01:00
|
|
|
def start(self, service_names=None, **options):
|
|
|
|
for service in self.get_services(service_names):
|
2013-12-20 17:22:54 +01:00
|
|
|
service.start(**options)
|
2013-12-09 18:48:15 +01:00
|
|
|
|
2013-12-20 19:30:23 +01:00
|
|
|
def stop(self, service_names=None, **options):
|
|
|
|
for service in self.get_services(service_names):
|
2013-12-20 17:22:54 +01:00
|
|
|
service.stop(**options)
|
2013-12-09 18:48:15 +01:00
|
|
|
|
2013-12-20 19:30:23 +01:00
|
|
|
def kill(self, service_names=None, **options):
|
|
|
|
for service in self.get_services(service_names):
|
2013-12-20 17:53:07 +01:00
|
|
|
service.kill(**options)
|
|
|
|
|
2013-12-20 19:30:23 +01:00
|
|
|
def remove_stopped(self, service_names=None, **options):
|
|
|
|
for service in self.get_services(service_names):
|
2013-12-20 17:53:07 +01:00
|
|
|
service.remove_stopped(**options)
|
|
|
|
|
2013-12-20 19:30:23 +01:00
|
|
|
def containers(self, service_names=None, *args, **kwargs):
|
2013-12-19 14:02:04 +01:00
|
|
|
l = []
|
2013-12-20 19:30:23 +01:00
|
|
|
for service in self.get_services(service_names):
|
2013-12-19 14:02:04 +01:00
|
|
|
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
|
|
|
|
2013-12-20 19:30:23 +01:00
|
|
|
class NoSuchService(Exception):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
self.msg = "No such service: %s" % self.name
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.msg
|