Пример #1
0
def create_image(name):
    """
    Create an image from an instance.
    """
    ec2 = connect.ec2()

    # Clean up any existing images.
    # Things usually mess up if there
    # is conflicting/existing stuff.
    delete_image(name)

    # Try to use an existing base if specified.
    logger.info("Looking for an existing base instance...")
    base_instance = None
    base_instances = ec2.get_all_instances(filters={"tag-key": "Name", "tag-value": name})
    for reservation in base_instances:
        if reservation.instances:
            base_instance = reservation.instances[0]
            if base_instance.update() != "ready":
                continue
            else:
                logger.info("Existing base instance found.")
                break

    if base_instance is None:
        raise Exception("No base instance named {0} was found. Please create one first.".format(name))

    try:
        # Create the AMI and get its ID.
        logger.info("Creating image...")
        ami_id = base_instance.create_image(name, description="Base image {0}".format(name))

        # Wait until instance is ready.
        image = ec2.get_all_images([ami_id])[0]
        wait_until_ready(image)
        logger.info("Created image with id {0}".format(ami_id))

        # Clean up the image infrastructure.
        delete_image_instance(name)

        logger.info("AMI creation complete. ({0})".format(ami_id))

        return ami_id

    except EC2ResponseError as e:
        logger.error("Error creating the image, undoing...")

        # Try to undo all the changes.
        delete_image(name)

        # Re-raise the error.
        raise e
Пример #2
0
def get_image(name):
    """
    Tries to get an existing image.
    """
    logger.info("Looking for an existing image...")
    ec2 = connect.ec2()
    images = ec2.get_all_images(filters={"name": name})
    if images:
        ami_id = images[0].id

        logger.info("Existing image found. ({0})".format(ami_id))
        return ami_id
    return None
Пример #3
0
def delete_image(name):
    """
    Deregisters the AMI and deletes its snapshot.
    """
    ec2 = connect.ec2()
    images = ec2.get_all_images(filters={"name": name})
    for image in images:
        image_id = image.id
        logger.info("Deleting image with id {0}".format(image_id))
        try:
            try:
                ec2.deregister_image(image_id, delete_snapshot=True)

            # If the snapshot doesn't exist, just try deregistering the image.
            except AttributeError as e:
                ec2.deregister_image(image_id)

        except EC2ResponseError as e:
            logger.warning("Could not deregister the image. It may already be deregistered.")