This commit is contained in:
Aanand Prasad 2013-12-09 11:41:05 +00:00
commit 0eb7d30861
6 changed files with 121 additions and 0 deletions

3
.gitignore vendored Normal file
View File

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

3
plum/__init__.py Normal file
View File

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

30
plum/service.py Normal file
View File

@ -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
plum/tests/__init__.py Normal file
View File

View File

@ -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
setup.py Normal file
View File

@ -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
""",
)