-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-ec2-start-stop.py
81 lines (75 loc) · 3.04 KB
/
aws-ec2-start-stop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ec2 shutdown after business hours and start on business hours
#run as python aws-ec2-start-stop.py <region> <inst_id1>,<inst_id2>,<inst_id3>
#Status code
# 0 (pending)
# 16 (running)
# 32 (shutting-down)
# 48 (terminated)
# 64 (stopping)
# 80 (stopped)
import boto.ec2
import sys
import time as t_time
from datetime import datetime, time, date
#business hours interval
start_business_hours = time(8,00)
end_business_hours = time(21,00)
#default variables
v_region=sys.argv[1]
now = datetime.now()
now_time = now.time()
def return_list_instances(ec2):
vtag = {"tag:EC2_ECONOMIZATOR": "TRUE"}
instances = ec2.get_only_instances(filters=vtag)
return instances
def f_stop(ec2):
list_instances = return_list_instances(ec2)
for instances in list_instances:
if instances.state_code == 16:
print "The instance %s is in state %s and will be stopped" % (instances.id, instances.state)
ec2.stop_instances(instances.id)
t_time.sleep(3)
instances.update()
while instances.state_code in [64]:
print "Wainting for stop of instance %s, current state %s" % (instances.id, instances.state)
t_time.sleep(15)
instances.update()
if instances.state_code == 80:
print "The instance %s is %s" % (instances.id, instances.state)
else:
print "ERROR: The instance %s is %s" % (instances.id, instances.state)
elif instances.state_code == 80:
print "The instance %s is already stopped." % instances.id
else:
print "ERROR: Instance %s status is %s." % (instances.id, instances.state)
def f_start(ec2):
list_instances = return_list_instances(ec2)
for instances in list_instances:
if instances.state_code != 16:
print "The instance %s is in state %s and will be started" % (instances.id, instances.state)
ec2.start_instances(instances.id)
t_time.sleep(3)
instances.update()
while instances.state_code in [0]:
print "Wainting for start of instance %s, current state %s" % (instances.id, instances.state)
t_time.sleep(15)
instances.update()
if instances.state_code == 16:
print "The instance %s is %s" % (instances.id, instances.state)
else:
print "ERROR: The instance %s is %s" % (instances.id, instances.state)
elif instances.state_code == 16:
print "The instance %s is already started." % instances.id
else:
print "ERROR: Instance %s status is %s." % (instances.id, instances.state)
ec2 = boto.ec2.connect_to_region(v_region)
if datetime.weekday(date.today()) in [5,6]:
print "Not in a week day... stopping instances..."
f_stop(ec2)
else:
if now_time >= start_business_hours and now_time <= end_business_hours:
print "Business hours OK - Starting instances"
f_start(ec2)
else:
print "Business hours NOK - Stopping instances"
f_stop(ec2)