-
Notifications
You must be signed in to change notification settings - Fork 22
/
spot_launcher.py
executable file
·80 lines (67 loc) · 2.34 KB
/
spot_launcher.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
#!/usr/bin/env python
"""Launches a test spot instance"""
import click
import boto3
import base64
from sensible.loginit import logger
log = logger(__name__)
#Tell Boto3 To Enable Debug Logging
#boto3.set_stream_logger(name='botocore')
@click.group()
def cli():
"""Spot Launcher"""
def user_data_cmds(duration):
"""Initial cmds to run, takes duration for halt cmd"""
cmds = """
#cloud-config
runcmd:
- echo "halt" | at now + {duration} min
""".format(duration=duration)
return cmds
@cli.command("launch")
@click.option('--instance', default="r4.large", help='Instance Type')
@click.option('--duration', default="55", help='Duration')
@click.option('--keyname', default="pragai", help='Key Name')
@click.option('--profile', default="arn:aws:iam::561744971673:instance-profile/admin",
help='IamInstanceProfile')
@click.option('--securitygroup', default="sg-61706e07", help='Key Name')
@click.option('--ami', default="ami-6df1e514", help='Key Name')
def request_spot_instance(duration, instance, keyname,
profile, securitygroup, ami):
"""Request spot instance"""
#import pdb;pdb.set_trace()
user_data = user_data_cmds(duration)
LaunchSpecifications = {
"ImageId": ami,
"InstanceType": instance,
"KeyName": keyname,
"IamInstanceProfile": {
"Arn": profile
},
"UserData": base64.b64encode(user_data.encode("ascii")).\
decode('ascii'),
"BlockDeviceMappings": [
{
"DeviceName": "/dev/xvda",
"Ebs": {
"DeleteOnTermination": True,
"VolumeType": "gp2",
"VolumeSize": 8,
}
}
],
"SecurityGroupIds": [securitygroup]
}
run_args = {
'SpotPrice' : "0.8",
'Type' : "one-time",
'InstanceCount' : 1,
'LaunchSpecification' : LaunchSpecifications
}
msg_user_data = "SPOT REQUEST DATA: %s" % run_args
log.info(msg_user_data)
client = boto3.client('ec2', "us-west-2")
reservation = client.request_spot_instances(**run_args)
return reservation
if __name__ == '__main__':
cli()