Aanand Prasad 12 年之前
当前提交
0eb7d30861
共有 6 个文件被更改,包括 121 次插入0 次删除
  1. 3 0
      .gitignore
  2. 3 0
      plum/__init__.py
  3. 30 0
      plum/service.py
  4. 0 0
      plum/tests/__init__.py
  5. 39 0
      plum/tests/service_test.py
  6. 46 0
      setup.py

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+*.egg-info
+*.pyc
+/dist

+ 3 - 0
plum/__init__.py

@@ -0,0 +1,3 @@
+from .service import Service
+
+__version__ = '1.0.0'

+ 30 - 0
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'])

+ 0 - 0
plum/tests/__init__.py


+ 39 - 0
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)

+ 46 - 0
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='[email protected]',
+    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
+    """,
+)