ec2_launch: Allow to deregister the AMI after launching image

If all you want is to test an image or product and to achieve this
you need to generate several images a day, you will end up with
lots of AMI's and snapshots that have no use in the end of the day.

This commit adds the new boolean manifest option "deregister_ami"
that, if True, deletes the recently created AMI and snapshot. So, the
final result will be only the running instance, nothing else.
This commit is contained in:
Jonh Wendell 2015-04-06 14:23:36 -03:00
parent 50d61c735d
commit a5cd6e077d
3 changed files with 25 additions and 0 deletions

View file

@ -9,3 +9,5 @@ def resolve_tasks(taskset, manifest):
taskset.add(tasks.LaunchEC2Instance)
if 'print_public_ip' in manifest.plugins['ec2_launch']:
taskset.add(tasks.PrintPublicIPAddress)
if manifest.plugins['ec2_launch'].get('deregister_ami', False):
taskset.add(tasks.DeregisterAMI)

View file

@ -16,3 +16,5 @@ properties:
instance_type: {type: string}
print_public_ip: {type: string}
tags: {type: object}
deregister_ami: {type: boolean}
additionalProperties: false

View file

@ -62,3 +62,24 @@ class PrintPublicIPAddress(Task):
f.write('')
f.close()
class DeregisterAMI(Task):
description = 'Deregistering AMI'
phase = phases.image_registration
predecessors = [LaunchEC2Instance]
@classmethod
def run(cls, info):
ec2 = info._ec2
logger = logging.getLogger(__name__)
def instance_running():
ec2['instance'].update()
return ec2['instance'].state == 'running'
if waituntil(instance_running, timeout=120, interval=5):
info._ec2['connection'].deregister_image(info._ec2['image'])
info._ec2['snapshot'].delete()
else:
logger.error('Timeout while booting instance')