From 0eb7d308615bae1ad4be1ca5112ac7b6b6cbfbaf Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Mon, 9 Dec 2013 11:41:05 +0000 Subject: [PATCH] yay --- .gitignore | 3 +++ plum/__init__.py | 3 +++ plum/service.py | 30 +++++++++++++++++++++++++ plum/tests/__init__.py | 0 plum/tests/service_test.py | 39 ++++++++++++++++++++++++++++++++ setup.py | 46 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 .gitignore create mode 100644 plum/__init__.py create mode 100644 plum/service.py create mode 100644 plum/tests/__init__.py create mode 100644 plum/tests/service_test.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..21a256dfb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.egg-info +*.pyc +/dist diff --git a/plum/__init__.py b/plum/__init__.py new file mode 100644 index 000000000..404eba85d --- /dev/null +++ b/plum/__init__.py @@ -0,0 +1,3 @@ +from .service import Service + +__version__ = '1.0.0' diff --git a/plum/service.py b/plum/service.py new file mode 100644 index 000000000..e9777be76 --- /dev/null +++ b/plum/service.py @@ -0,0 +1,30 @@ +class Service(object): + def __init__(self, client, image, command): + self.client = client + self.image = image + self.command = command + + @property + def containers(self): + return self.client.containers() + + def start(self): + if len(self.containers) == 0: + self.start_container() + + def stop(self): + self.scale(0) + + def scale(self, num): + while len(self.containers) < num: + self.start_container() + + while len(self.containers) > num: + self.stop_container() + + def start_container(self): + container = self.client.create_container(self.image, self.command) + self.client.start(container['Id']) + + def stop_container(self): + self.client.kill(self.containers[0]['Id']) diff --git a/plum/tests/__init__.py b/plum/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plum/tests/service_test.py b/plum/tests/service_test.py new file mode 100644 index 000000000..6075965e9 --- /dev/null +++ b/plum/tests/service_test.py @@ -0,0 +1,39 @@ +from unittest import TestCase +from docker import Client +from plum import Service + + +class ServiceTestCase(TestCase): + def setUp(self): + self.client = Client('http://127.0.0.1:4243') + self.client.pull('ubuntu') + + for c in self.client.containers(): + self.client.kill(c['Id']) + + self.service = Service( + client=self.client, + image="ubuntu", + command=["/bin/sleep", "300"], + ) + + def test_up_scale_down(self): + self.assertEqual(len(self.service.containers), 0) + + self.service.start() + self.assertEqual(len(self.service.containers), 1) + + self.service.start() + self.assertEqual(len(self.service.containers), 1) + + self.service.scale(2) + self.assertEqual(len(self.service.containers), 2) + + self.service.scale(1) + self.assertEqual(len(self.service.containers), 1) + + self.service.stop() + self.assertEqual(len(self.service.containers), 0) + + self.service.stop() + self.assertEqual(len(self.service.containers), 0) diff --git a/setup.py b/setup.py new file mode 100644 index 000000000..b4b80f125 --- /dev/null +++ b/setup.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from setuptools import setup +import re +import os +import codecs + + +# Borrowed from +# https://github.com/jezdez/django_compressor/blob/develop/setup.py +def read(*parts): + return codecs.open(os.path.join(os.path.dirname(__file__), *parts)).read() + + +def find_version(*file_paths): + version_file = read(*file_paths) + version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", + version_file, re.M) + if version_match: + return version_match.group(1) + raise RuntimeError("Unable to find version string.") + + +setup( + name='plum', + version=find_version("plum", "__init__.py"), + description='', + url='https://github.com/orchardup.plum', + author='Orchard Laboratories Ltd.', + author_email='hello@orchardup.com', + packages=['plum'], + package_data={}, + include_package_data=True, + install_requires=[ + 'docopt==0.6.1', + 'docker-py==0.2.2', + 'requests==2.0.1', + 'texttable==0.8.1', + ], + dependency_links=[], + entry_points=""" + [console_scripts] + plum=plum:main + """, +)