Skip to content

Commit

Permalink
yay
Browse files Browse the repository at this point in the history
  • Loading branch information
aanand committed Dec 9, 2013
0 parents commit 0eb7d30
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.egg-info
*.pyc
/dist
3 changes: 3 additions & 0 deletions plum/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .service import Service

__version__ = '1.0.0'
30 changes: 30 additions & 0 deletions plum/service.py
Original file line number Diff line number Diff line change
@@ -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'])
Empty file added plum/tests/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions plum/tests/service_test.py
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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
""",
)

0 comments on commit 0eb7d30

Please sign in to comment.