-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.py
47 lines (37 loc) · 1.36 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
import os
from utils import pricing_util
from utils.aws_spot_instance import AWSSpotInstance
from utils.aws_spot_exception import SpotConstraintException
import aws_spot_bot.user_config as uconf
def launch_instances(qty):
"""Launches QTY instances and returns the instance objects."""
best_az = pricing_util.get_best_az()
launched_instances = []
print "Best availability zone:", best_az.name
for idx in range(qty):
print '>> Launching instance #%s' % idx
si = AWSSpotInstance(best_az.region, best_az.name, uconf.INSTANCE_TYPES[0], uconf.AMI_ID, uconf.BID)
si.request_instance()
try:
si.get_ip()
except SpotConstraintException, e:
print(">> ", e.message)
si.cancel_spot_request()
continue
launched_instances.append(si)
return launched_instances
if __name__ == '__main__':
instances = launch_instances(uconf.QTY_INSTANCES)
for si in instances:
if uconf.WAIT_FOR_HTTP:
si.wait_for_http()
if uconf.WAIT_FOR_SSH:
si.wait_for_ssh()
if uconf.OPEN_IN_BROWSER:
si.open_in_browser()
if uconf.OPEN_SSH:
si.open_ssh_term()
if uconf.ADD_TO_ANSIBLE_HOSTS:
si.add_to_ansible_hosts()
if uconf.RUN_ANSIBLE:
os.system('cd ansible && ansible-playbook -s play.yml')