-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
67 lines (50 loc) · 2.24 KB
/
main.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
import copy
import time
import boto3
import launch
import run
from config import NUM_INSTANCE, REGION, TAG, REBOOT, CLUSTER_ID, TERMINATE, RUN, NUM_RUN, \
CREDENTIAL_PROFILE, CONFIG_DICT
def main():
""" Main function;
* Launch spot request of NUMINSTANCE
* Run Benchmark
* Download Log
* Plot data from log
"""
session = boto3.Session(profile_name=CREDENTIAL_PROFILE)
client = session.client('ec2', region_name=REGION)
if NUM_INSTANCE > 0:
spot_request_ids = launch.launch(client, NUM_INSTANCE, CONFIG_DICT)
print("CHECK SECURITY GROUP ALLOWED IP SETTINGS!!!")
# Wait for our spots to fulfill
launch.wait_for_fulfillment(client, spot_request_ids, copy.deepcopy(spot_request_ids))
spot_instance_response = client.describe_spot_instance_requests(
SpotInstanceRequestIds=spot_request_ids)
instance_ids = [result["InstanceId"] for result in
spot_instance_response["SpotInstanceRequests"]]
client.create_tags(Resources=instance_ids, Tags=TAG)
# Wait Running
launch.wait_for_running(client, instance_ids, copy.deepcopy(instance_ids))
time.sleep(15)
launch.wait_ping(client, instance_ids, copy.deepcopy(instance_ids))
if REBOOT:
print("Rebooting instances...")
session = boto3.Session(profile_name=CREDENTIAL_PROFILE)
ec2 = session.resource('ec2', region_name=REGION)
instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}, {'Name': 'tag:ClusterId', 'Values': [CLUSTER_ID]}])
instance_ids = [x.id for x in instances]
client.reboot_instances(InstanceIds=instance_ids)
launch.wait_ping(client, instance_ids, copy.deepcopy(instance_ids))
if RUN:
for i in range(NUM_RUN):
run.run_benchmark()
if TERMINATE:
instances = client.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']},
{'Name': 'tag:ClusterId', 'Values': [CLUSTER_ID]}])
instance_ids = [x.id for x in instances]
# TODO get spot_request_ids
launch.terminate(client, spot_request_ids, instance_ids)
if __name__ == "__main__":
main()