-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2manager.py
125 lines (102 loc) · 3.8 KB
/
ec2manager.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
import sys
import base64
import time
from pprint import pprint
import boto3
from modules.cloudinit_templates import ec2_userdata_tpl
TAGNAME = "Project"
TAGVALUE = "vmmanager"
class EC2Manager:
def __init__(self):
self.__ec2_client = boto3.client('ec2')
self.__ec2_resource = boto3.resource('ec2')
def printprices(self):
price_history = self.__ec2_client.describe_spot_price_history(
InstanceTypes=["c6g.medium"],
ProductDescriptions=["Linux/UNIX"],
)
pprint(price_history["SpotPriceHistory"])
# TODO: add spot instance support!
def createinstances(self, key_name):
# TODO: get these arguments properly:
# - for every region these will be different, except maybe instance_type,
# but not every instance is available everywhere...
# cmd: aws ec2 describe-images --filters='[{"Name":"architecture","Values":["arm64"]}]' --owners="903794441882"
# ami-0245697ee3e07e755 (64-bit (x86)) / ami-0886e2450125a1f08 (64-bit (Arm))
image_id = "ami-0886e2450125a1f08"
# cmd: aws ec2 describe-instance-types
instance_type = "c6g.medium"
# cmd: aws ec2 describe-key-pairs
# keyname comes from this ^^
# cmd: aws ec2 describe-security-groups
# NOTE: make sure tcp port 22 in is allowed in the group!
securitygroup = "default"
print("[+] Generating cloudinit script")
sshkey_raw = open(".sshkey").read()
userdata_raw = ec2_userdata_tpl.substitute({
"SSH_KEY": sshkey_raw
})
user_data = base64.b64encode(userdata_raw.encode("utf-8")).decode("utf-8")
instance_params = {
"ImageId": image_id,
"InstanceType": instance_type,
"KeyName": key_name,
"UserData": user_data,
"SecurityGroups": [securitygroup],
"TagSpecifications": [
{
"ResourceType": "instance",
"Tags": [
{
"Key": TAGNAME,
"Value": TAGVALUE,
}
]
}
]
}
print("[+] Spawning instance {}".format(instance_type))
instances = self.__ec2_resource.create_instances(
**instance_params,
MinCount=1,
MaxCount=1
)
for instance in instances:
print("Created instance: {}".format(instance))
def __get_instances(self):
reservations = self.__ec2_client.describe_instances(Filters=[
{
"Name": "tag:{}".format(TAGNAME),
"Values": [TAGVALUE]
},
])
for reservation in reservations["Reservations"]:
for instance in reservation["Instances"]:
yield instance
def describeinstances(self):
for instance in self.__get_instances():
state = instance["State"]["Name"]
tags = instance.get("Tags", None)
# if state not in ("terminated", "shutting-down"):
# pprint(instance)
print("{}: {}".format(instance["InstanceId"], state, tags))
def destroyinstances(self):
todelete = []
for instance in self.__get_instances():
iid = instance["InstanceId"]
todelete.append(iid)
resp = self.__ec2_client.terminate_instances(
InstanceIds=todelete,
)
if __name__ == "__main__":
keyname = sys.argv[1]
func = sys.argv[2]
ec2mgr = EC2Manager()
if func == "create":
ec2mgr.createinstances(keyname)
elif func == "list":
ec2mgr.describeinstances()
elif func == "destroy":
ec2mgr.destroyinstances()
else:
print("Invalid func: {}".format(func))