Rename "service collection" to "project"

This commit is contained in:
Ben Firshman 2013-12-19 16:55:12 +00:00
parent 818728b825
commit 2d2d81d33f
5 changed files with 92 additions and 91 deletions

View File

@ -4,7 +4,7 @@ import os
import re import re
import yaml import yaml
from ..service_collection import ServiceCollection from ..project import Project
from .docopt_command import DocoptCommand from .docopt_command import DocoptCommand
from .formatter import Formatter from .formatter import Formatter
from .utils import cached_property, mkdir from .utils import cached_property, mkdir
@ -20,16 +20,12 @@ class Command(DocoptCommand):
return Client() return Client()
@cached_property @cached_property
def service_collection(self): def project(self):
config = yaml.load(open('plum.yml')) config = yaml.load(open('plum.yml'))
return ServiceCollection.from_config( return Project.from_config(self.project_name, config, self.client)
config,
client=self.client,
project=self.project
)
@cached_property @cached_property
def project(self): def project_name(self):
project = os.path.basename(os.getcwd()) project = os.path.basename(os.getcwd())
project = re.sub(r'[^a-zA-Z0-9]', '', project) project = re.sub(r'[^a-zA-Z0-9]', '', project)
if not project: if not project:

View File

@ -86,7 +86,7 @@ class TopLevelCommand(Command):
-q Only display IDs -q Only display IDs
""" """
if options['-q']: if options['-q']:
for container in self.service_collection.containers(all=True): for container in self.project.containers(all=True):
print container.id print container.id
else: else:
headers = [ headers = [
@ -96,7 +96,7 @@ class TopLevelCommand(Command):
'Ports', 'Ports',
] ]
rows = [] rows = []
for container in self.service_collection.containers(all=True): for container in self.project.containers(all=True):
rows.append([ rows.append([
container.name, container.name,
container.human_readable_command, container.human_readable_command,
@ -111,7 +111,7 @@ class TopLevelCommand(Command):
Usage: run SERVICE COMMAND [ARGS...] Usage: run SERVICE COMMAND [ARGS...]
""" """
service = self.service_collection.get(options['SERVICE']) service = self.project.get_service(options['SERVICE'])
if service is None: if service is None:
raise UserError("No such service: %s" % options['SERVICE']) raise UserError("No such service: %s" % options['SERVICE'])
container_options = { container_options = {
@ -132,13 +132,13 @@ class TopLevelCommand(Command):
Usage: start [-d] Usage: start [-d]
""" """
if options['-d']: if options['-d']:
self.service_collection.start() self.project.start()
return return
running = [] running = []
unstarted = [] unstarted = []
for s in self.service_collection: for s in self.project.services:
if len(s.containers()) == 0: if len(s.containers()) == 0:
unstarted.append((s, s.create_container())) unstarted.append((s, s.create_container()))
else: else:
@ -152,7 +152,7 @@ class TopLevelCommand(Command):
try: try:
log_printer.run() log_printer.run()
finally: finally:
self.service_collection.stop() self.project.stop()
def stop(self, options): def stop(self, options):
""" """
@ -160,7 +160,7 @@ class TopLevelCommand(Command):
Usage: stop Usage: stop
""" """
self.service_collection.stop() self.project.stop()
def logs(self, options): def logs(self, options):
""" """
@ -168,7 +168,7 @@ class TopLevelCommand(Command):
Usage: logs Usage: logs
""" """
containers = self.service_collection.containers(all=False) containers = self.project.containers(all=False)
print "Attaching to", list_containers(containers) print "Attaching to", list_containers(containers)
LogPrinter(containers, attach_params={'logs': True}).run() LogPrinter(containers, attach_params={'logs': True}).run()

View File

@ -12,47 +12,55 @@ def sort_service_dicts(services):
return 0 return 0
return sorted(services, cmp=cmp) return sorted(services, cmp=cmp)
class ServiceCollection(list): class Project(object):
"""
A collection of services.
"""
def __init__(self, name, services, client):
self.name = name
self.services = services
self.client = client
@classmethod @classmethod
def from_dicts(cls, service_dicts, client, project='default'): def from_dicts(cls, name, service_dicts, client):
""" """
Construct a ServiceCollection from a list of dicts representing services. Construct a ServiceCollection from a list of dicts representing services.
""" """
collection = ServiceCollection() project = cls(name, [], client)
for service_dict in sort_service_dicts(service_dicts): for service_dict in sort_service_dicts(service_dicts):
# Reference links by object # Reference links by object
links = [] links = []
if 'links' in service_dict: if 'links' in service_dict:
for name in service_dict.get('links', []): for name in service_dict.get('links', []):
links.append(collection.get(name)) links.append(project.get_service(name))
del service_dict['links'] del service_dict['links']
collection.append(Service(client=client, project=project, links=links, **service_dict)) project.services.append(Service(client=client, project=name, links=links, **service_dict))
return collection return project
@classmethod @classmethod
def from_config(cls, config, client, project='default'): def from_config(cls, name, config, client):
dicts = [] dicts = []
for name, service in config.items(): for name, service in config.items():
service['name'] = name service['name'] = name
dicts.append(service) dicts.append(service)
return cls.from_dicts(dicts, client, project) return cls.from_dicts(name, dicts, client)
def get(self, name): def get_service(self, name):
for service in self: for service in self.services:
if service.name == name: if service.name == name:
return service return service
def start(self): def start(self):
for service in self: for service in self.services:
service.start() service.start()
def stop(self): def stop(self):
for service in self: for service in self.services:
service.stop() service.stop()
def containers(self, *args, **kwargs): def containers(self, *args, **kwargs):
l = [] l = []
for service in self: for service in self.services:
for container in service.containers(*args, **kwargs): for container in service.containers(*args, **kwargs):
l.append(container) l.append(container)
return l return l

59
tests/project_test.py Normal file
View File

@ -0,0 +1,59 @@
from plum.project import Project
from plum.service import Service
from .testcases import DockerClientTestCase
class ProjectTest(DockerClientTestCase):
def test_from_dict(self):
project = Project.from_dicts('test', [
{
'name': 'web',
'image': 'ubuntu'
},
{
'name': 'db',
'image': 'ubuntu'
}
], self.client)
self.assertEqual(len(project.services), 2)
self.assertEqual(project.get_service('web').name, 'web')
self.assertEqual(project.get_service('web').options['image'], 'ubuntu')
self.assertEqual(project.get_service('db').name, 'db')
self.assertEqual(project.get_service('db').options['image'], 'ubuntu')
def test_from_dict_sorts_in_dependency_order(self):
project = Project.from_dicts('test', [
{
'name': 'web',
'image': 'ubuntu',
'links': ['db'],
},
{
'name': 'db',
'image': 'ubuntu'
}
], self.client)
self.assertEqual(project.services[0].name, 'db')
self.assertEqual(project.services[1].name, 'web')
def test_get_service(self):
web = self.create_service('web')
project = Project('test', [web], self.client)
self.assertEqual(project.get_service('web'), web)
def test_start_stop(self):
project = Project('test', [
self.create_service('web'),
self.create_service('db'),
], self.client)
project.start()
self.assertEqual(len(project.get_service('web').containers()), 1)
self.assertEqual(len(project.get_service('db').containers()), 1)
project.stop()
self.assertEqual(len(project.get_service('web').containers()), 0)
self.assertEqual(len(project.get_service('db').containers()), 0)

View File

@ -1,62 +0,0 @@
from plum.service import Service
from plum.service_collection import ServiceCollection
from .testcases import DockerClientTestCase
class ServiceCollectionTest(DockerClientTestCase):
def test_from_dict(self):
collection = ServiceCollection.from_dicts(None, [
{
'name': 'web',
'image': 'ubuntu'
},
{
'name': 'db',
'image': 'ubuntu'
}
])
self.assertEqual(len(collection), 2)
self.assertEqual(collection.get('web').name, 'web')
self.assertEqual(collection.get('web').options['image'], 'ubuntu')
self.assertEqual(collection.get('db').name, 'db')
self.assertEqual(collection.get('db').options['image'], 'ubuntu')
def test_from_dict_sorts_in_dependency_order(self):
collection = ServiceCollection.from_dicts(None, [
{
'name': 'web',
'image': 'ubuntu',
'links': ['db'],
},
{
'name': 'db',
'image': 'ubuntu'
}
])
self.assertEqual(collection[0].name, 'db')
self.assertEqual(collection[1].name, 'web')
def test_get(self):
web = self.create_service('web')
collection = ServiceCollection([web])
self.assertEqual(collection.get('web'), web)
def test_start_stop(self):
collection = ServiceCollection([
self.create_service('web'),
self.create_service('db'),
])
collection.start()
self.assertEqual(len(collection[0].containers()), 1)
self.assertEqual(len(collection[1].containers()), 1)
collection.stop()
self.assertEqual(len(collection[0].containers()), 0)
self.assertEqual(len(collection[1].containers()), 0)