Skip to content

Commit 3808a3e

Browse files
committed
Merge pull request #2 from rightlag/develop
Develop
2 parents 9b8621c + 68e45cd commit 3808a3e

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

tests/test_timely.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
import unittest
44

5+
from time import sleep
56
from timely import Timely
67

78

@@ -47,5 +48,48 @@ def test_exception_if_start_time_is_greater_than_equal_to_end_time(self):
4748
self.timely.set(weekdays=['*'], start_time='9:00 AM',
4849
end_time='9:00 AM')
4950

51+
def test_unset_method(self):
52+
self.timely.set(weekdays=['*'], start_time='9:00 AM',
53+
end_time='5:00 PM')
54+
try:
55+
instance = self.conn.get_only_instances()[0]
56+
times = self.timely.all()[instance.id]
57+
# First - set times for all days of the week
58+
self.assertEqual(len(times), 7)
59+
# Second - unset times for all days of the week
60+
self.timely.unset(weekdays=['*'])
61+
times = self.timely.all()[instance.id]
62+
self.assertEqual(len(times), 0)
63+
except IndexError:
64+
pass
65+
66+
def test_check_method_stops_instance_if_should_not_be_running(self):
67+
try:
68+
instance = self.conn.get_only_instances()[0]
69+
if instance.state == 'stopped':
70+
# Start the instance to ensure it is running
71+
instance.start()
72+
weekday = self.timely.weekdays[self.now.weekday()]
73+
# Automatically sets `start_time` and `end_time` to `None`
74+
self.timely.set(weekdays=[weekday])
75+
# Ensure that the instance is being stopped
76+
self.timely.check()
77+
stopped = False
78+
instance = None
79+
while not stopped:
80+
try:
81+
# Need to remake a connection to AWS to get the updated
82+
# instance status
83+
instance = self.conn.get_only_instances()[0]
84+
if instance.state == 'stopped':
85+
stopped = True
86+
else:
87+
sleep(1)
88+
except IndexError:
89+
pass
90+
self.assertEqual(instance.state, 'stopped')
91+
except IndexError:
92+
pass
93+
5094
def tearDown(self):
5195
del self.timely

timely/timely.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import boto.ec2
2+
import collections
23
import sys
34

45
from datetime import datetime
@@ -30,6 +31,12 @@ def __init__(self, region='us-east-1', iso=False, verbose=False):
3031
def use_verbose(self):
3132
self.verbose = True
3233

34+
def use_iso(self):
35+
self.iso = True
36+
37+
def set_region(self, region):
38+
self.conn = boto.ec2.connect_to_region(region)
39+
3340
def all(self, instance_ids=None):
3441
"""Read weekday run times for all or specific EC2 instances.
3542
@@ -38,6 +45,8 @@ def all(self, instance_ids=None):
3845
IDs
3946
"""
4047
data = {}
48+
Time = collections.namedtuple('Time',
49+
['weekday', 'start_time', 'end_time'])
4150
instances = self.conn.get_only_instances(instance_ids=instance_ids)
4251
for instance in instances:
4352
times = instance.tags.get('times')
@@ -55,7 +64,9 @@ def all(self, instance_ids=None):
5564
end_time = end_time.strftime('%H:%M')
5665
weekday = (self.weekdays[i + 1]
5766
if self.iso else self.weekdays[i])
58-
data[instance.id].append((weekday, start_time, end_time,))
67+
data[instance.id].append(
68+
Time(weekday, start_time, end_time)
69+
)
5970
return data
6071

6172
def set(self, instance_ids=None, weekdays=None, start_time=None,
@@ -75,7 +86,7 @@ def set(self, instance_ids=None, weekdays=None, start_time=None,
7586
start_time = datetime.strptime(start_time, '%I:%M %p')
7687
end_time = datetime.strptime(end_time, '%I:%M %p')
7788
if start_time >= end_time:
78-
raise ValueError('start time can\'t be greater than end time')
89+
raise ValueError('Start time can\'t be greater than end time')
7990
start_time = start_time.strftime('%H:%M')
8091
end_time = end_time.strftime('%H:%M')
8192
updated = '{0}-{1}'.format(start_time, end_time)
@@ -130,6 +141,28 @@ def set(self, instance_ids=None, weekdays=None, start_time=None,
130141
except self.conn.ResponseError, e:
131142
raise e
132143

144+
def unset(self, instance_ids=None, weekdays=None):
145+
"""Unset instance times for specific weekdays or all weekdays."""
146+
# integer representation of `weekdays`
147+
if weekdays == ['*']:
148+
# All 7 days
149+
weekdays = range(len(self.weekdays))
150+
else:
151+
weekdays = [self.weekdays.index(weekday) for weekday in weekdays]
152+
instances = self.conn.get_only_instances(instance_ids=instance_ids)
153+
for instance in instances:
154+
times = instance.tags.get('times')
155+
if times:
156+
times = times.split(';')
157+
for weekday in weekdays:
158+
times[weekday] = None
159+
times = ';'.join([str(time) for time in times])
160+
try:
161+
# Overwrite existing `times` tag with new value
162+
instance.add_tag('times', times)
163+
except self.conn.ResponseError, e:
164+
raise e
165+
133166
def check(self, instance_ids=None):
134167
"""Check the state of instances and either start or stop them
135168
based on the current time restrictions set for the current day.

0 commit comments

Comments
 (0)