/
manage_spot_fleet.py
117 lines (99 loc) · 2.98 KB
/
manage_spot_fleet.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
# Sample Python AWS API demonstration script
# --Ashay Chitnis, 12/07/2015
import sys
from datetime import datetime
from boto3.session import Session
def sys_exit(val):
print('Exiting...')
sys.exit(val)
class DemoEc2(object):
"""
"""
def __init__(self, client):
"""
:param client:
"""
self.client = client
self.dry_run = False
def create_aws_spot_fleet(self):
"""
:return: resp
"""
# pre-configured dictionary of desired instance configuration,
# one can automate it to receive inputs from CLI or a Web interface
request_config = {
"TargetCapacity": 3,
"SpotPrice": "0.012",
"IamFleetRole": "arn:aws:iam::547078464708:role/ck_spot_fleet_role",
"LaunchSpecifications": [
{
"ImageId": "ami-e7539d8c",
"InstanceType": "t1.micro",
"SubnetId": "subnet-e9cfd8d3"
}
]
}
resp = self.client.request_spot_fleet(
DryRun=self.dry_run,
SpotFleetRequestConfig=request_config,
)
return resp
def history_aws_spot_fleet(self, spot_fleet_id=''):
"""
:return: resp
"""
resp = self.client.describe_spot_fleet_request_history(
DryRun=self.dry_run,
SpotFleetRequestId=spot_fleet_id,
StartTime=datetime(2015, 7, 12),
)
return resp
def describe_aws_spot_fleet(self, spot_fleet_id=''):
"""
:return: resp
"""
resp = self.client.describe_spot_fleet_instances(
DryRun=self.dry_run,
SpotFleetRequestId=spot_fleet_id,
)
return resp
def terminate_aws_spot_fleet(self, spot_fleet_id=''):
"""
:return: resp
"""
resp = self.client.cancel_spot_fleet_requests(
DryRun=self.dry_run,
SpotFleetRequestIds=[
spot_fleet_id,
],
TerminateInstances=True
)
return resp
def main():
if len(sys.argv) != 3:
sys_exit(1)
accepted_param = ['start', 'history', 'show', 'stop']
valid_param = False
for arg in sys.argv:
if arg in accepted_param:
valid_param = True
break
if not valid_param:
sys_exit(1)
default_profile='aater-flux7'
spot_fleet_id = sys.argv[2]
session = Session(profile_name=default_profile)
client = session.client('ec2')
resource = DemoEc2(client)
ret = False
if sys.argv[1] == 'start':
ret = resource.create_aws_spot_fleet()
elif sys.argv[1] == 'show':
ret = resource.describe_aws_spot_fleet(spot_fleet_id)
elif sys.argv[1] == 'history':
ret = resource.history_aws_spot_fleet(spot_fleet_id)
elif sys.argv[1] == 'stop':
ret = resource.terminate_aws_spot_fleet(spot_fleet_id)
print ret
if __name__ == '__main__':
main()