AMI: Implement copy_image.
This commit is contained in:
parent
2650d9981f
commit
a4fdce2e55
3 changed files with 94 additions and 14 deletions
|
|
@ -576,21 +576,37 @@ class TagBackend(object):
|
|||
|
||||
|
||||
class Ami(TaggedEC2Instance):
|
||||
def __init__(self, ami_id, instance, name, description):
|
||||
def __init__(self, ami_id, instance=None, source_ami=None, name=None, description=None):
|
||||
self.id = ami_id
|
||||
self.state = "available"
|
||||
|
||||
self.instance = instance
|
||||
self.instance_id = instance.id
|
||||
self.virtualization_type = instance.virtualization_type
|
||||
self.architecture = instance.architecture
|
||||
self.kernel_id = instance.kernel
|
||||
self.platform = instance.platform
|
||||
if instance:
|
||||
self.instance = instance
|
||||
self.instance_id = instance.id
|
||||
self.virtualization_type = instance.virtualization_type
|
||||
self.architecture = instance.architecture
|
||||
self.kernel_id = instance.kernel
|
||||
self.platform = instance.platform
|
||||
self.launch_permission_groups = set()
|
||||
self.name = name
|
||||
self.description = description
|
||||
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.launch_permission_groups = set()
|
||||
elif source_ami:
|
||||
"""
|
||||
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/CopyingAMIs.html
|
||||
"We don't copy launch permissions, user-defined tags, or Amazon S3 bucket permissions from the source AMI to the new AMI."
|
||||
~ 2014.09.29
|
||||
"""
|
||||
self.virtualization_type = source_ami.virtualization_type
|
||||
self.architecture = source_ami.architecture
|
||||
self.kernel_id = source_ami.kernel_id
|
||||
self.platform = source_ami.platform
|
||||
self.name = name if name else source_ami.name
|
||||
self.description = description if description else source_ami.description
|
||||
|
||||
self.autocreate_volume_and_snapshot()
|
||||
|
||||
def autocreate_volume_and_snapshot(self):
|
||||
# AWS auto-creates these, we should reflect the same.
|
||||
volume = ec2_backend.create_volume(15, "us-east-1a")
|
||||
self.ebs_snapshot = ec2_backend.create_snapshot(volume.id, "Auto-created snapshot for AMI %s" % self.id)
|
||||
|
|
@ -613,11 +629,18 @@ class AmiBackend(object):
|
|||
self.amis = {}
|
||||
super(AmiBackend, self).__init__()
|
||||
|
||||
def create_image(self, instance_id, name, description):
|
||||
def create_image(self, instance_id, name=None, description=None):
|
||||
# TODO: check that instance exists and pull info from it.
|
||||
ami_id = random_ami_id()
|
||||
instance = self.get_instance(instance_id)
|
||||
ami = Ami(ami_id, instance, name, description)
|
||||
ami = Ami(ami_id, instance=instance, source_ami=None, name=name, description=description)
|
||||
self.amis[ami_id] = ami
|
||||
return ami
|
||||
|
||||
def copy_image(self, source_image_id, source_region, name=None, description=None):
|
||||
source_ami = ec2_backends[source_region].describe_images(ami_ids=[source_image_id])[0]
|
||||
ami_id = random_ami_id()
|
||||
ami = Ami(ami_id, instance=None, source_ami=source_ami, name=name, description=description)
|
||||
self.amis[ami_id] = ami
|
||||
return ami
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,15 @@ class AmisResponse(BaseResponse):
|
|||
template = Template(CREATE_IMAGE_RESPONSE)
|
||||
return template.render(image=image)
|
||||
|
||||
def copy_image(self):
|
||||
source_image_id = self.querystring.get('SourceImageId')[0]
|
||||
source_region = self.querystring.get('SourceRegion')[0]
|
||||
name = self.querystring.get('Name')[0] if self.querystring.get('Name') else None
|
||||
description = self.querystring.get('Description')[0] if self.querystring.get('Description') else None
|
||||
image = ec2_backend.copy_image(source_image_id, source_region, name, description)
|
||||
template = Template(COPY_IMAGE_RESPONSE)
|
||||
return template.render(image=image)
|
||||
|
||||
def deregister_image(self):
|
||||
ami_id = self.querystring.get('ImageId')[0]
|
||||
success = ec2_backend.deregister_image(ami_id)
|
||||
|
|
@ -61,6 +70,11 @@ CREATE_IMAGE_RESPONSE = """<CreateImageResponse xmlns="http://ec2.amazonaws.com/
|
|||
<imageId>{{ image.id }}</imageId>
|
||||
</CreateImageResponse>"""
|
||||
|
||||
COPY_IMAGE_RESPONSE = """<CopyImageResponse xmlns="http://ec2.amazonaws.com/doc/2013-07-15/">
|
||||
<requestId>60bc441d-fa2c-494d-b155-5d6a3EXAMPLE</requestId>
|
||||
<imageId>{{ image.id }}</imageId>
|
||||
</CopyImageResponse>"""
|
||||
|
||||
# TODO almost all of these params should actually be templated based on the ec2 image
|
||||
DESCRIBE_IMAGES_RESPONSE = """<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
|
||||
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue