· 7 years ago · Dec 06, 2018, 04:30 AM
1from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping
2self.dev_sda1 = BlockDeviceType()
3self.bdm = BlockDeviceMapping()
4self.dev_sda1.size = 100
5self.bdm['/dev/xvda'] = self.dev_sda1
6
7class connect:
8 def __init__(self, access=None, secret=None):
9 self.region='us-east-2'
10 self.ac = access
11 self.se = secret
12 self.myaddress='52.14.29.127'
13 self.key='dec15a'
14 self.MAX_SPOT_BID='1.0'
15 self.security_groups = ['default']
16
17 import boto
18 import boto.ec2
19 self.ec2_conn = boto.ec2.connect_to_region(self.region, aws_access_key_id=self.ac, aws_secret_access_key=self.se)
20
21 newtest = """
22 #!/bin/bash -ex
23 yum install -y docker mysql git python-pip
24 sudo amazon-linux-extras install -y docker
25 pip install aws-ec2-assign-elastic-ip
26 aws-ec2-assign-elastic-ip --access-key {}, --secret-key {} --valid-ips {}
27 service docker start
28 docker run -d -p 8887:8888 -v /tmp:/tmp shantanuo/notebook
29
30 """
31 self.mytest=newtest.format(self.ac,self.se, self.myaddress).encode()
32
33 self.bug=b"""
34 from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping
35 self.dev_sda1 = BlockDeviceType()
36 self.bdm = BlockDeviceMapping()
37 self.dev_sda1.size = 100
38 self.bdm['/dev/xvda'] = self.dev_sda1
39 #reservation = image.run(..., , ...)
40 """
41 def startEc2Spot(self, ami, instance_type):
42 aid = {'image_id': ami, 'instance_type': instance_type, 'user_data': self.mytest , 'key_name': self.key,
43 'price': self.MAX_SPOT_BID, 'security_groups': self.security_groups}
44 daid=dict(aid)
45 rid=self.ec2_conn.request_spot_instances(**daid)
46 import time
47 time.sleep(300)
48 job_sir_id = rid[0].id # spot instance request = sir, job_ is the relevant aws item for this job
49 reqs = self.ec2_conn.get_all_spot_instance_requests()
50 for sir in reqs:
51 if sir.id == job_sir_id:
52 job_instance_id = sir.instance_id
53 print ("job instance id: " + str(job_instance_id))
54
55 if self.myaddress:
56 self.ec2_conn.associate_address(job_instance_id, self.myaddress)
57 print ('ssh -i ' + self.key+ '.pem ec2-user@'+self.myaddress)
58 else:
59 address = self.ec2_conn.allocate_address()
60 self.ec2_conn.associate_address(job_instance_id, address.public_ip)
61 print ('ssh -i ' + self.key+ '.pem ec2-user@'+str(address.public_ip))