-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_region.py
executable file
·102 lines (63 loc) · 2.88 KB
/
start_region.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/env python
import sys
import time
import pprint
import boto3
from datetime import datetime
from botocore.exceptions import ClientError
class start_region():
# This code starts instances named "NAVO" on the named region.
def __init__(self, region):
pp = pprint.PrettyPrinter(indent=4)
# Connect to Region
region_client = boto3.client ('ec2', region_name=region)
region_resource = boto3.resource('ec2', region_name=region)
# Start "NAVO"-named instances
response = region_client.describe_instances(
Filters = [
{
'Name': 'tag:Name',
'Values': ['NAVO']
}
])
reservations = response['Reservations']
if len(reservations) == 0:
print(region + ': No "NAVO" instances [start_region].')
else:
# Start all the stopped (and non-terminated) instances
# that have the Name tag "NAVO"
starttime = datetime.now()
print('\n' + region + ': Start time: ' + str(starttime) + ' [start_region]')
for reservation in reservations:
instances = reservation['Instances']
for instance in instances:
instance_id = instance['InstanceId']
instance_object = region_resource.Instance(instance_id)
state = instance['State']['Name']
if state == 'stopped':
response = region_client.start_instances(
InstanceIds=[instance_id], DryRun=False)
instance_object.wait_until_running()
print(region + ': Adding sleep(30) to avoid race condition.')
time.sleep(30)
check = region_client.describe_instances(InstanceIds=[instance_id])
state = check['Reservations'][0]['Instances'][0]['State']['Name']
print(region + ': Check state: ' + state)
print(region + ': Instance ' + instance_id + ' started.')
elif state == 'running':
print(region + ': Instance ' + instance_id + ' is already running.')
else:
continue
dns = instance_object.public_dns_name
# print('[ec2-user@' + instance['PublicDnsName'] + ']\n')
print(region + ': [ec2-user@' + dns + ']')
endtime = datetime.now()
print(region + ': End time: ' + str(endtime) + ' [start_region]')
def main():
if len(sys.argv) < 2:
print('Usage: start_region.py <region_name>\n')
sys.exit(0)
else:
start_region(sys.argv[1])
if __name__ == "__main__":
main()