2015-03-10 23:05:12 +01:00
|
|
|
from image import Image
|
|
|
|
import logging
|
|
|
|
from contextlib import contextmanager
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def initialize_image(manifest, credentials, bootstrap_info):
|
2015-04-08 21:23:21 +02:00
|
|
|
image = AmazonMachineImage(manifest, bootstrap_info._ec2['image'],
|
|
|
|
bootstrap_info._ec2['region'], credentials)
|
2015-03-10 23:05:12 +01:00
|
|
|
return image
|
|
|
|
|
|
|
|
|
|
|
|
class AmazonMachineImage(Image):
|
|
|
|
|
2015-04-08 21:23:21 +02:00
|
|
|
def __init__(self, manifest, image_id, region, credentials):
|
2015-03-10 23:05:12 +01:00
|
|
|
super(AmazonMachineImage, self).__init__(manifest)
|
2015-04-08 21:23:21 +02:00
|
|
|
|
|
|
|
from boto.ec2 import connect_to_region as ec2_connect
|
|
|
|
self.ec2_connection = ec2_connect(region, aws_access_key_id=credentials['access-key'],
|
|
|
|
aws_secret_access_key=credentials['secret-key'])
|
|
|
|
from boto.vpc import connect_to_region as vpc_connect
|
|
|
|
self.vpc_connection = vpc_connect(region, aws_access_key_id=credentials['access-key'],
|
|
|
|
aws_secret_access_key=credentials['secret-key'])
|
|
|
|
|
|
|
|
self.ami = self.ec2_connection.get_image(image_id)
|
2015-03-10 23:05:12 +01:00
|
|
|
|
|
|
|
def destroy(self):
|
|
|
|
log.debug('Deleting AMI')
|
2015-03-25 21:07:05 +01:00
|
|
|
self.ami.deregister()
|
|
|
|
for device, block_device_type in self.ami.block_device_mapping.items():
|
2015-04-08 21:23:21 +02:00
|
|
|
self.ec2_connection.delete_snapshot(block_device_type.snapshot_id)
|
2015-03-10 23:05:12 +01:00
|
|
|
del self.ami
|
|
|
|
|
|
|
|
@contextmanager
|
2015-03-25 20:40:15 +01:00
|
|
|
def get_instance(self, instance_type):
|
2015-03-10 23:05:12 +01:00
|
|
|
from ..instances.ec2 import boot_image
|
2015-04-08 21:23:21 +02:00
|
|
|
with boot_image(self.ami, instance_type, self.ec2_connection, self.vpc_connection) as instance:
|
2015-03-10 23:05:12 +01:00
|
|
|
yield instance
|