Skip to content

Commit 487dc16

Browse files
author
vagrant
committed
Merge branch 'develop' of https://github.com/rightlag/timely
2 parents 9b8621c + d7eedb8 commit 487dc16

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

timely/timely.py

Lines changed: 28 additions & 1 deletion
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
@@ -38,6 +39,8 @@ def all(self, instance_ids=None):
3839
IDs
3940
"""
4041
data = {}
42+
Time = collections.namedtuple('Time',
43+
['weekday', 'start_time', 'end_time'])
4144
instances = self.conn.get_only_instances(instance_ids=instance_ids)
4245
for instance in instances:
4346
times = instance.tags.get('times')
@@ -55,7 +58,9 @@ def all(self, instance_ids=None):
5558
end_time = end_time.strftime('%H:%M')
5659
weekday = (self.weekdays[i + 1]
5760
if self.iso else self.weekdays[i])
58-
data[instance.id].append((weekday, start_time, end_time,))
61+
data[instance.id].append(
62+
Time(weekday, start_time, end_time)
63+
)
5964
return data
6065

6166
def set(self, instance_ids=None, weekdays=None, start_time=None,
@@ -130,6 +135,28 @@ def set(self, instance_ids=None, weekdays=None, start_time=None,
130135
except self.conn.ResponseError, e:
131136
raise e
132137

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

0 commit comments

Comments
 (0)