-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2-power-on-off.py
executable file
·51 lines (39 loc) · 1.74 KB
/
ec2-power-on-off.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
#!/usr/bin/python3
'''Power On, or Power Off EC2 instances based on time of day and tags'''
import sys
from subprocess import call
def get_tag(arr, tagname='Name'):
'''Just get a single tag'''
for tag in arr:
if tag['Key'] == tagname: return tag['Value']
def instance_details(inst):
'''Display an instances details'''
#print(inst)
dispinst = {}
dispinst['PrivateIpAddress'] = inst['PrivateIpAddress']
dispinst['KeyName'] = inst['KeyName']
dispinst['InstanceId'] = inst['InstanceId']
dispinst['Name'] = get_tag(inst['Tags'], 'Name')
dispinst['POWER-ON'] = get_tag(inst['Tags'], 'CS-UTILITY-POWER-ON')
dispinst['POWER-OFF'] = get_tag(inst['Tags'], 'CS-UTILITY-POWER-OFF')
#dispinst['State'] = inst['State']['Name']
#print("{:15}\t{}\t{}".format(inst['PrivateIpAddress'], inst['KeyName'], get_tag(inst['Tags'], 'Name')))
return(dispinst)
def describe_ec2(filter_text):
'''List AWS EC2 instances'''
import boto3
ec2 = boto3.client('ec2')
response = ec2.describe_instances(Filters=[
{'Name': 'tag:Name', 'Values': [filter_text]},
{'Name': 'tag:CS-UTILITY-POWER', 'Values': ['true']},
{'Name': 'instance-state-name', 'Values': ['running']}
])
results = [instance_details(item) for res in response['Reservations']
for item in res['Instances']]
ssh_ec2(results)
#ec2.start_instances()
def ssh_ec2(instances):
'''Display SSH command for list of instances'''
_ = [print("ssh {:15}\t{:18}\t{}".format(server['PrivateIpAddress'], server['InstanceId'], server['Name'])) for server in sorted(instances, key=lambda k: k['Name'])]
if __name__ == '__main__':
results = [describe_ec2(text) for text in sys.argv[1:]]