Skip to content

Commit 7326fe3

Browse files
committed
add setup.py #1
1 parent 4842907 commit 7326fe3

File tree

8 files changed

+98
-27
lines changed

8 files changed

+98
-27
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Ben Koziol ([email protected])
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boto
2+
setuptools

setup.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from setuptools import setup, find_packages
2+
3+
4+
setup(
5+
name='aws',
6+
version='0.01a',
7+
packages=find_packages(where='./src'),
8+
package_dir={'': 'src'},
9+
url='https://github.com/bekozi/simple-aws',
10+
license='MIT',
11+
author='bekozi',
12+
author_email='[email protected]',
13+
description='A simple management interface to Amazon Web Services using boto.'
14+
)
File renamed without changes.

src/aws.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import boto.ec2
66
import time
77
from boto.ec2.address import Address
8+
from exc import InstanceNameNotAvailable
89

910

1011
class AwsManager(object):
@@ -16,6 +17,10 @@ class AwsManager(object):
1617
"""
1718

1819
def __init__(self, conf_path=None, filtered=True, only_running=True, section_title='simple-aws'):
20+
if conf_path is None:
21+
msg = 'Path to configuration file required.'
22+
raise ValueError(msg)
23+
1924
self.conf_path = conf_path or os.path.expanduser(os.getenv('SIMPLEAWS_CONF_PATH'))
2025
self.filtered = filtered
2126
self.only_running = only_running
@@ -30,14 +35,27 @@ def conn(self):
3035
self._conn = self._get_aws_connection_()
3136
return self._conn
3237

33-
def get_instances(self):
38+
def get_instances(self, key='id'):
39+
40+
def _get_key_(instance):
41+
if key == 'id':
42+
ret = instance.id
43+
elif key == 'name':
44+
try:
45+
ret = instance.tags['Name']
46+
except KeyError:
47+
ret = instance.id
48+
else:
49+
raise NotImplementedError(key)
50+
return ret
51+
3452
reservations = self.conn.get_all_reservations()
3553
instances = [i for r in reservations for i in r.instances]
3654
instances = {i.id: i for i in instances}
3755
if self.only_running:
38-
instances = {i.id: i for i in instances.values() if i.update() == 'running'}
56+
instances = {_get_key_(i): i for i in instances.values() if i.update() == 'running'}
3957
if self.filtered:
40-
instances = {i.id: i for i in instances.values() if self._filter_(i)}
58+
instances = {_get_key_(i): i for i in instances.values() if self._filter_(i)}
4159
return instances
4260

4361
def get_instance_by_name(self, name):
@@ -46,7 +64,7 @@ def get_instance_by_name(self, name):
4664
if instance.tags['Name'] == name:
4765
ret = instance
4866
if ret is None:
49-
raise ValueError('Name not found.')
67+
raise InstanceNameNotAvailable('Name not found.')
5068
else:
5169
return ret
5270

@@ -104,10 +122,12 @@ def start_instance_by_name(self, name, wait=True):
104122
self.only_running = prev_only_running
105123

106124
@staticmethod
107-
def wait_for_status(target, status_target, sleep=1):
125+
def wait_for_status(target, status_target, sleep=1, pad=None):
108126
time.sleep(sleep)
109127
while target.update() != status_target:
110128
time.sleep(sleep)
129+
if pad is not None:
130+
time.sleep(pad)
111131

112132
def _filter_(self, instance):
113133
if instance.key_name == self._cfg['key_name']:

src/exc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class SimpleAwsException(Exception):
2+
pass
3+
4+
5+
class InstanceNameNotAvailable(SimpleAwsException):
6+
pass

src/test/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__author__ = 'ben.koziol'

src/test_aws.py renamed to src/test/test_aws.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,83 @@
88

99

1010
class TestAwsManager(unittest.TestCase):
11+
PREFIX = '_test_simple_aws_'
1112

1213
@classmethod
1314
def tearDownClass(cls):
1415
m = AwsManager(CONF_PATH)
15-
assert(m.get_instances() == {})
16+
instances = m.get_instances(key='name')
17+
for key in instances.iterkeys():
18+
assert not key.startswith(cls.PREFIX)
1619

17-
def test_constructor(self):
20+
@property
21+
def iname(self):
22+
return self.get_instance_name('foo')
23+
24+
def get_instance_name(self, suffix):
25+
return '{0}{1}'.format(self.PREFIX, suffix)
26+
27+
def test_init(self):
1828
filtered = [True, False]
1929
only_running = [True, False]
2030
conf_path = [None, CONF_PATH]
2131
for f, a, c in itertools.product(filtered, only_running, conf_path):
2232
try:
2333
m = AwsManager(c, filtered=f, only_running=a)
2434
self.assertIsInstance(m._cfg, dict)
25-
except TypeError:
35+
except ValueError:
2636
if c is None:
2737
pass
2838
else:
2939
raise
3040

3141
def test_get_instance_by_name(self):
3242
m = AwsManager(CONF_PATH)
33-
i1 = m.launch_new_instance('_foo_name_test', wait=True)
43+
i1 = m.launch_new_instance(self.iname, wait=True)
3444
try:
35-
instance = m.get_instance_by_name('_foo_name_test')
36-
self.assertEqual(instance.tags['Name'], '_foo_name_test')
45+
instance = m.get_instance_by_name(self.iname)
46+
self.assertEqual(instance.tags['Name'], self.iname)
3747
finally:
3848
i1.terminate()
3949

4050
def test_start_instance_by_name(self):
4151
m = AwsManager(CONF_PATH)
42-
i1 = m.launch_new_instance('_foo_name_test', wait=True)
52+
i1 = m.launch_new_instance(self.iname, wait=True)
4353
try:
4454
i1.stop()
45-
AwsManager.wait_for_status(i1, 'stopped')
46-
instance = m.start_instance_by_name('_foo_name_test')
55+
AwsManager.wait_for_status(i1, 'stopped', pad=20)
56+
instance = m.start_instance_by_name(self.iname)
4757
self.assertEqual(instance.update(), 'running')
4858
finally:
4959
i1.terminate()
5060

5161
def test_launch_instance_with_wait(self):
5262
m = AwsManager(CONF_PATH)
5363
# print('launching 1')
54-
i1 = m.launch_new_instance('_foo_test_1')
55-
self.assertEqual(i1.tags['Name'], '_foo_test_1')
64+
name1 = self.get_instance_name('foo_test_1')
65+
i1 = m.launch_new_instance(name1)
5666
# print('launching 2')
57-
i2 = m.launch_new_instance('_foo_test_2')
67+
i2 = m.launch_new_instance(self.get_instance_name('foo_test_2'))
5868
# print('instances launched')
5969
try:
60-
instances = m.get_instances()
61-
self.assertEqual(len(instances), 2)
62-
self.assertEqual(set([i.update() for i in instances.values()]), set(['running']))
70+
self.assertEqual(i1.update(), 'running')
71+
self.assertEqual(i2.update(), 'running')
6372
finally:
6473
i1.terminate()
6574
i2.terminate()
66-
self.assertEqual(m.get_instances(), {})
6775

6876
def test_launch_instance_without_wait(self):
6977
m = AwsManager(CONF_PATH)
70-
i1 = m.launch_new_instance('_foo_test_1', wait=False)
78+
i1 = m.launch_new_instance(self.iname, wait=False)
7179
try:
7280
self.assertNotEqual(i1.update(), 'running')
7381
finally:
7482
i1.terminate()
75-
self.assertEqual(m.get_instances(), {})
7683

7784
def test_launch_new_instance_elastic_ip(self):
7885
elastic_ip = '54.68.61.218'
7986
m = AwsManager(CONF_PATH)
80-
name = '_foo_test_elastic_ip'
87+
name = self.get_instance_name('foo_test_elastic_ip')
8188
i1 = m.launch_new_instance(name, elastic_ip=elastic_ip)
8289
try:
8390
self.assertEqual(i1.ip_address, elastic_ip)
@@ -86,11 +93,11 @@ def test_launch_new_instance_elastic_ip(self):
8693

8794
def test_name_must_be_unique(self):
8895
m = AwsManager(CONF_PATH)
89-
self.assertEqual(m.get_instances(), {})
96+
name = self.get_instance_name('foo_unique')
9097
try:
91-
i1 = m.launch_new_instance('_foo_test_4', wait=True)
98+
i1 = m.launch_new_instance(name, wait=True)
9299
with self.assertRaises(ValueError):
93-
i2 = m.launch_new_instance('_foo_test_4', wait=True)
100+
i2 = m.launch_new_instance(name, wait=True)
94101
finally:
95102
try:
96103
i1.terminate()

0 commit comments

Comments
 (0)