-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_command.py
executable file
·158 lines (104 loc) · 4.13 KB
/
run_command.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/env python
import sys
import time
import pprint
import boto3
from datetime import datetime
from botocore.exceptions import ClientError
class run_command():
def __init__(self, region):
# This code starts a run of a command on a "NAVO" EC2 instance.
pp = pprint.PrettyPrinter(indent=4)
# Connect to Region
region_client = boto3.client('ec2', region_name=region)
ssm = boto3.client('ssm', region_name=region)
# Find an instance that can run "command"
try:
response = region_client.describe_instances(
Filters = [
{
'Name': 'tag:Name',
'Values': ['NAVO']
}
])
except:
print('EC2 describe_instances() failed.')
sys.exit(0)
starttime = datetime.now()
print('Start time: ' + str(starttime) + ' [run_command]')
instance_id = -1
reservations = response['Reservations']
for reservation in reservations:
instances = reservation['Instances']
for instance in instances:
instance_id = instance['InstanceId']
state = instance['State']['Name']
if state == 'terminated':
continue
if instance_id == -1:
print('\n' + region + ': No "NAVO" instances. [run_command]')
sys.exit(0)
else:
print('\nInstance ID: ' + instance_id)
# Send a command to (in this case) one EC2 instance
command = 'ls -l'
timeout = 1800
start_time = time.time()
try:
response = ssm.send_command(
InstanceIds=[instance_id],
DocumentName="AWS-RunShellScript",
Parameters={
'commands': [command],
'executionTimeout': [str(timeout)]
} )
print('\n' + region + ': Command "' + command + '" sent to instance ' + instance_id)
command_id = response['Command']['CommandId']
except ClientError as e:
print('\nSSM send_command() failed:')
print('\n-------------------------------------------------------')
pp.pprint(e)
print('-------------------------------------------------------\n')
sys.exit(0)
# Poll until command finishes. command fires up
# a bunch of scripts and these will continue running
# for several minutes.
while True:
time.sleep(30)
try:
list = ssm.list_commands(CommandId=command_id)
except:
print('SSM list_commands() failed.')
sys.exit(0)
status = list['Commands'][0]['Status']
if status == 'Pending' or status == 'InProgress':
continue
elif status == 'Failed':
print('\n' + region + ': Failed (or timeout)\n')
break
elif status == 'Success':
elapsed_time = time.time() - start_time
print('\n' + region + ': Success. Elapsed time: ' + str(elapsed_time))
print('---------------------------------------')
pp.pprint(list)
print('---------------------------------------')
break
else:
print('\n' + region + ': Final status: ' + status)
print('---------------------------------------')
pp.pprint(list)
print('---------------------------------------')
elapsed_time = time.time() - start_time
print('\n' + region + ': Elapsed time: ' + str(elapsed_time))
break
endtime = datetime.now()
print('End time: ' + str(endtime) + ' [run_command]\n')
def main():
if len(sys.argv) < 2:
print('Usage: run_command.py <region_name>\n')
sys.exit(0)
else:
run_command(sys.argv[1])
sys.exit(0)
if __name__ == "__main__":
main()