示例#1
0
def create_image(module, ec2):
    """
    Creates new AMI

    module : AnsibleModule object
    ec2: authenticated ec2 connection object
    """

    instance_id = module.params.get('instance_id')
    name = module.params.get('name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))
    description = module.params.get('description')
    no_reboot = module.params.get('no_reboot')
    tags =  module.params.get('tags')

    try:
        params = {'instance_id': instance_id,
                  'name': name,
                  'description': description,
                  'no_reboot': no_reboot}

        image_id = ec2.create_image(**params)
    except boto.exception.BotoServerError, e:
        module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
示例#2
0
def create_ami(instance_id, name, description):

    params = {
        'instance_id': instance_id,
        'name': name,
        'description': description,
        'no_reboot': True
    }

    image_id = ec2.create_image(**params)

    for _ in xrange(AMI_TIMEOUT):
        try:
            img = ec2.get_image(image_id)
            if img.state == 'available':
                break
            else:
                time.sleep(1)
        except EC2ResponseError as e:
            if e.error_code == 'InvalidAMIID.NotFound':
                time.sleep(1)
            else:
                raise Exception("Unexpected error code: {}".format(
                    e.error_code))
            time.sleep(1)
    else:
        raise Exception("Timeout waiting for AMI to finish")

    return image_id
示例#3
0
def create_ami(instance_id, name, description):

    params = {'instance_id': instance_id,
              'name': name,
              'description': description,
              'no_reboot': True}

    image_id = ec2.create_image(**params)
    for _ in xrange(AMI_TIMEOUT):
        try:
            img = ec2.get_image(image_id)
            if img.state == 'available':
                img.add_tag("environment", args.environment)
                img.add_tag("deployment", args.deployment)
                img.add_tag("play", args.play)
                img.add_tag("configuration_ref", args.configuration_version)
                img.add_tag("configuration_secure_ref", args.configuration_secure_version)
                img.add_tag("configuration_secure_repo", args.configuration_secure_repo)
                img.add_tag("build_id", args.jenkins_build)
                for repo,ref in git_refs.items():
                    key = "vars:{}".format(repo)
                    img.add_tag(key, ref)
            else:
                time.sleep(1)
        except EC2ResponseError as e:
            if e.error_code == 'InvalidAMIID.NotFound':
                time.sleep(1)
            else:
                raise Exception("Unexpected error code: {}".format(
                    e.error_code))
            time.sleep(1)
    else:
        raise Exception("Timeout waiting for AMI to finish")

    return image_id
示例#4
0
def create_image(module, ec2):
    """
    Creates new AMI

    module : AnsibleModule object
    ec2: authenticated ec2 connection object
    """

    instance_id = module.params.get('instance_id')
    name = module.params.get('name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))
    description = module.params.get('description')
    no_reboot = module.params.get('no_reboot')

    try:
        params = {
            'instance_id': instance_id,
            'name': name,
            'description': description,
            'no_reboot': no_reboot
        }

        image_id = ec2.create_image(**params)
    except boto.exception.BotoServerError, e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))
示例#5
0
def create_ami(instance_id, name, description):

    params = {'instance_id': instance_id,
              'name': name,
              'description': description,
              'no_reboot': True}

    image_id = ec2.create_image(**params)

    for _ in xrange(AMI_TIMEOUT):
        try:
            img = ec2.get_image(image_id)
            if img.state == 'available':
                break
            else:
                time.sleep(1)
        except EC2ResponseError as e:
            if e.error_code == 'InvalidAMIID.NotFound':
                time.sleep(1)
            else:
                raise Exception("Unexpected error code: {}".format(
                    e.error_code))
            time.sleep(1)
    else:
        raise Exception("Timeout waiting for AMI to finish")

    return image_id
示例#6
0
def create_ami(instance_id, name, description):

    params = {
        'instance_id': instance_id,
        'name': name,
        'description': description,
        'no_reboot': True
    }

    AWS_API_WAIT_TIME = 1
    image_id = ec2.create_image(**params)
    print("Checking if image is ready.")
    for _ in xrange(AMI_TIMEOUT):
        try:
            img = ec2.get_image(image_id)
            if img.state == 'available':
                print("Tagging image.")
                img.add_tag("environment", args.environment)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("deployment", args.deployment)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("cluster", args.play)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("play", args.play)
                time.sleep(AWS_API_WAIT_TIME)
                conf_tag = "{} {}".format(
                    "http://github.com/edx/configuration",
                    args.configuration_version)
                img.add_tag("version:configuration", conf_tag)
                time.sleep(AWS_API_WAIT_TIME)
                conf_secure_tag = "{} {}".format(
                    args.configuration_secure_repo,
                    args.configuration_secure_version)
                img.add_tag("version:configuration_secure", conf_secure_tag)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("cache_id", args.cache_id)
                time.sleep(AWS_API_WAIT_TIME)

                # Get versions from the instance.
                tags = ec2.get_all_tags(filters={'resource-id': instance_id})
                for tag in tags:
                    if tag.name.startswith('version:'):
                        img.add_tag(tag.name, tag.value)
                        time.sleep(AWS_API_WAIT_TIME)
                break
            else:
                time.sleep(1)
        except EC2ResponseError as e:
            if e.error_code == 'InvalidAMIID.NotFound':
                time.sleep(1)
            else:
                raise Exception("Unexpected error code: {}".format(
                    e.error_code))
            time.sleep(1)
    else:
        raise Exception("Timeout waiting for AMI to finish")

    return image_id
示例#7
0
def create_ami(instance_id, name, description):

    params = {'instance_id': instance_id,
              'name': name,
              'description': description,
              'no_reboot': False}

    AWS_API_WAIT_TIME = 1
    image_id = ec2.create_image(**params)
    print("Checking if image is ready.")
    for _ in xrange(AMI_TIMEOUT):
        try:
            img = ec2.get_image(image_id)
            if img.state == 'available':
                print("Tagging image.")
                img.add_tag("environment", args.environment)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("deployment", args.deployment)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("cluster", args.play)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("play", args.play)
                time.sleep(AWS_API_WAIT_TIME)
                conf_tag = "{} {}".format("http://github.com/edx/configuration", args.configuration_version)
                img.add_tag("version:configuration", conf_tag)
                time.sleep(AWS_API_WAIT_TIME)
                conf_secure_tag = "{} {}".format(args.configuration_secure_repo, args.configuration_secure_version)
                img.add_tag("version:configuration_secure", conf_secure_tag)
                time.sleep(AWS_API_WAIT_TIME)
                conf_internal_tag = "{} {}".format(args.configuration_internal_repo, args.configuration_internal_version)
                img.add_tag("version:configuration_internal", conf_internal_tag)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("cache_id", args.cache_id)
                time.sleep(AWS_API_WAIT_TIME)

                # Get versions from the instance.
                tags = ec2.get_all_tags(filters={'resource-id': instance_id})
                for tag in tags:
                    if tag.name.startswith('version:'):
                        img.add_tag(tag.name, tag.value)
                        time.sleep(AWS_API_WAIT_TIME)
                break
            else:
                time.sleep(1)
        except EC2ResponseError as e:
            if e.error_code == 'InvalidAMIID.NotFound':
                time.sleep(1)
            else:
                raise Exception("Unexpected error code: {}".format(
                    e.error_code))
            time.sleep(1)
    else:
        raise Exception("Timeout waiting for AMI to finish")

    return image_id
示例#8
0
def create_image(module, ec2):
    """
    Creates new AMI

    module : AnsibleModule object
    ec2: authenticated ec2 connection object
    """

    instance_id = module.params.get('instance_id')
    name = module.params.get('name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))
    description = module.params.get('description')
    no_reboot = module.params.get('no_reboot')
    device_mapping = module.params.get('device_mapping')
    tags = module.params.get('tags')
    launch_permissions = module.params.get('launch_permissions')

    try:
        params = {
            'instance_id': instance_id,
            'name': name,
            'description': description,
            'no_reboot': no_reboot
        }

        if device_mapping:
            bdm = BlockDeviceMapping()
            for device in device_mapping:
                if 'device_name' not in device:
                    module.fail_json(msg='Device name must be set for volume')
                device_name = device['device_name']
                del device['device_name']
                bd = BlockDeviceType(**device)
                bdm[device_name] = bd
            params['block_device_mapping'] = bdm

        image_id = ec2.create_image(**params)
    except boto.exception.BotoServerError, e:
        if e.error_code == 'InvalidAMIName.Duplicate':
            images = ec2.get_all_images()
            for img in images:
                if img.name == name:
                    module.exit_json(msg="AMI name already present",
                                     image_id=img.id,
                                     state=img.state,
                                     changed=False)
                    sys.exit(0)
            else:
                module.fail_json(
                    msg="Error in retrieving duplicate AMI details")
        else:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))
示例#9
0
def create_ami(instance_id, name, description):

    params = {
        'instance_id': instance_id,
        'name': name,
        'description': description,
        'no_reboot': True
    }

    AWS_API_WAIT_TIME = 1
    image_id = ec2.create_image(**params)
    print("Checking if image is ready.")
    for _ in xrange(AMI_TIMEOUT):
        try:
            img = ec2.get_image(image_id)
            if img.state == 'available':
                print("Tagging image.")
                img.add_tag("environment", args.environment)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("deployment", args.deployment)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("play", args.play)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("configuration_ref", args.configuration_version)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("configuration_secure_ref",
                            args.configuration_secure_version)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("configuration_secure_repo",
                            args.configuration_secure_repo)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("cache_id", args.cache_id)
                time.sleep(AWS_API_WAIT_TIME)
                for repo, ref in git_refs.items():
                    key = "vars:{}".format(repo)
                    img.add_tag(key, ref)
                    time.sleep(AWS_API_WAIT_TIME)
                break
            else:
                time.sleep(1)
        except EC2ResponseError as e:
            if e.error_code == 'InvalidAMIID.NotFound':
                time.sleep(1)
            else:
                raise Exception("Unexpected error code: {}".format(
                    e.error_code))
            time.sleep(1)
    else:
        raise Exception("Timeout waiting for AMI to finish")

    return image_id
示例#10
0
def create_image(module, ec2):
    """
    Creates new AMI

    module : AnsibleModule object
    ec2: authenticated ec2 connection object
    """

    instance_id = module.params.get('instance_id')
    name = module.params.get('name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))
    description = module.params.get('description')
    no_reboot = module.params.get('no_reboot')
    device_mapping = module.params.get('device_mapping')
    tags =  module.params.get('tags')
    launch_permissions = module.params.get('launch_permissions')

    try:
        params = {'instance_id': instance_id,
                  'name': name,
                  'description': description,
                  'no_reboot': no_reboot}

        if device_mapping:
            bdm = BlockDeviceMapping()
            for device in device_mapping:
                if 'device_name' not in device:
                    module.fail_json(msg = 'Device name must be set for volume')
                device_name = device['device_name']
                del device['device_name']
                bd = BlockDeviceType(**device)
                bdm[device_name] = bd
            params['block_device_mapping'] = bdm

        image_id = ec2.create_image(**params)
    except boto.exception.BotoServerError, e:
        if e.error_code == 'InvalidAMIName.Duplicate':
            images = ec2.get_all_images()
            for img in images:
                if img.name == name:
                    module.exit_json(msg="AMI name already present", image_id=img.id, state=img.state, changed=False)
                    sys.exit(0)
            else:
                module.fail_json(msg="Error in retrieving duplicate AMI details")
        else:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))
示例#11
0
def create_ami(instance_id, name, description):

    params = {"instance_id": instance_id, "name": name, "description": description, "no_reboot": True}

    AWS_API_WAIT_TIME = 1
    image_id = ec2.create_image(**params)
    print ("Checking if image is ready.")
    for _ in xrange(AMI_TIMEOUT):
        try:
            img = ec2.get_image(image_id)
            if img.state == "available":
                print ("Tagging image.")
                img.add_tag("environment", args.environment)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("deployment", args.deployment)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("play", args.play)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("configuration_ref", args.configuration_version)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("configuration_secure_ref", args.configuration_secure_version)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("configuration_secure_repo", args.configuration_secure_repo)
                time.sleep(AWS_API_WAIT_TIME)
                img.add_tag("cache_id", args.cache_id)
                time.sleep(AWS_API_WAIT_TIME)
                for repo, ref in git_refs.items():
                    key = "vars:{}".format(repo)
                    img.add_tag(key, ref)
                    time.sleep(AWS_API_WAIT_TIME)
                break
            else:
                time.sleep(1)
        except EC2ResponseError as e:
            if e.error_code == "InvalidAMIID.NotFound":
                time.sleep(1)
            else:
                raise Exception("Unexpected error code: {}".format(e.error_code))
            time.sleep(1)
    else:
        raise Exception("Timeout waiting for AMI to finish")

    return image_id
示例#12
0
def create_ami(instance_id, name, description):

    params = {"instance_id": instance_id, "name": name, "description": description, "no_reboot": True}

    image_id = ec2.create_image(**params)

    for _ in xrange(AMI_TIMEOUT):
        try:
            img = ec2.get_image(image_id)
            if img.state == "available":
                break
            else:
                time.sleep(1)
        except boto.exception.EC2ResponseError as e:
            if e.error_code == "InvalidAMIID.NotFound":
                time.sleep(1)
            else:
                raise Exception("Unexpected error code: {}".format(e.error_code))
            time.sleep(1)
    else:
        raise Exception("Timeout waiting for AMI to finish")

    return image_id
示例#13
0
def create_image(module, ec2):
    """
    Creates new AMI

    module : AnsibleModule object
    ec2: authenticated ec2 connection object
    """

    instance_id = module.params.get('instance_id')
    name = module.params.get('name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))
    description = module.params.get('description')
    architecture = module.params.get('architecture')
    kernel_id = module.params.get('kernel_id')
    root_device_name = module.params.get('root_device_name')
    virtualization_type = module.params.get('virtualization_type')
    no_reboot = module.params.get('no_reboot')
    device_mapping = module.params.get('device_mapping')
    tags = module.params.get('tags')
    launch_permissions = module.params.get('launch_permissions')

    try:
        params = {'name': name, 'description': description}

        images = ec2.get_all_images(filters={'name': name})

        if images and images[0]:
            # ensure that launch_permissions are up to date
            update_image(module, ec2, images[0].id)

        bdm = None
        if device_mapping:
            bdm = BlockDeviceMapping()
            for device in device_mapping:
                if 'device_name' not in device:
                    module.fail_json(msg='Device name must be set for volume')
                device_name = device['device_name']
                del device['device_name']
                bd = BlockDeviceType(**device)
                bdm[device_name] = bd

        if instance_id:
            params['instance_id'] = instance_id
            params['no_reboot'] = no_reboot
            if bdm:
                params['block_device_mapping'] = bdm
            image_id = ec2.create_image(**params)
        else:
            params['architecture'] = architecture
            params['virtualization_type'] = virtualization_type
            if kernel_id:
                params['kernel_id'] = kernel_id
            if root_device_name:
                params['root_device_name'] = root_device_name
            if bdm:
                params['block_device_map'] = bdm
            image_id = ec2.register_image(**params)
    except boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    # Wait until the image is recognized. EC2 API has eventual consistency,
    # such that a successful CreateImage API call doesn't guarantee the success
    # of subsequent DescribeImages API call using the new image id returned.
    for i in range(wait_timeout):
        try:
            img = ec2.get_image(image_id)

            if img.state == 'available':
                break
            elif img.state == 'failed':
                module.fail_json(
                    msg=
                    "AMI creation failed, please see the AWS console for more details"
                )
        except boto.exception.EC2ResponseError as e:
            if ('InvalidAMIID.NotFound' not in e.error_code
                    and 'InvalidAMIID.Unavailable'
                    not in e.error_code) and wait and i == wait_timeout - 1:
                module.fail_json(
                    msg=
                    "Error while trying to find the new image. Using wait=yes and/or a longer "
                    "wait_timeout may help. %s: %s" %
                    (e.error_code, e.error_message))
        finally:
            time.sleep(1)

    if img.state != 'available':
        module.fail_json(
            msg=
            "Error while trying to find the new image. Using wait=yes and/or a longer wait_timeout may help."
        )

    if tags:
        try:
            ec2.create_tags(image_id, tags)
        except boto.exception.EC2ResponseError as e:
            module.fail_json(msg="Image tagging failed => %s: %s" %
                             (e.error_code, e.error_message))
    if launch_permissions:
        try:
            img = ec2.get_image(image_id)
            img.set_launch_permissions(**launch_permissions)
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message),
                             image_id=image_id)

    module.exit_json(msg="AMI creation operation complete",
                     changed=True,
                     **get_ami_info(img))
示例#14
0
def prepare_ami(region, zone, nodename, es, client, image=None, dag=False, progress=None):
    """
    Prepare client AMI
    """
    if image is None:
        image = "ethereum/client-%s" % client

    global completed
    if progress:
        completed += 5
        progress.update(completed)

    # Get our Instance ID
    inspect = json.loads(machine("inspect %s" % nodename))
    instance_id = inspect['Driver']['InstanceId']
    if progress:
        completed += 5
        progress.update(completed)

    # Pull base image
    pull_on(nodename, image)
    if progress:
        completed += 10
        progress.update(completed)

    # Create docker container w/ logstash-forwarder
    # Build logstash-forwarder directly, docker-compose doesn't seem to
    # like getting called concurrently
    # with lcd('logstash-forwarder'):
    #     compose_on(nodename, "build")
    build_on(nodename, "logstash-forwarder", "forwarder")
    if progress:
        completed += 20
        progress.update(completed)

    # Run logstash-forwarder, using run_on so we can pass --add-host
    # with lcd('logstash-forwarder'):
    #     compose_on(nodename, "up -d")  # ElasticSearch IP
    run_on(
        nodename,
        "forwarder",
        ("-d "
         "-v /var/log/syslog:/var/log/syslog "
         "--add-host logs.ethdev.com:%s "
         "--restart always" % es),
        name="forwarder")
    if progress:
        completed += 5
        progress.update(completed)

    # Generate DAG
    if client == 'cpp':
        ssh_on(nodename, "sudo mkdir /opt/dag")  # see generate_dag()
    elif client == 'go':
        ssh_on(nodename, "sudo mkdir /opt/dag")  # see generate_dag()
    if dag and client != 'python':
        generate_dag(nodename, client, image)
        # FIXME For some reason, 'docker run' exits with 0
        # but never returns, so somewhere between futures,
        # Fabric and docker, there's an unhandled timeout
        # while generating DAG caches and getting no output...
        # We poll for 'Exited' in 'docker ps -a' and run with
        # -d in generate_dag() for now...
        dag_done = False
        logging.info("Generating DAG on %s..." % nodename)
        while dag_done is False:
            time.sleep(5)
            ps = docker_on(nodename, "ps -a")
            if "Exited" in ps:
                logging.info("DAG done on %s" % nodename)
                dag_done = True
    if progress:
        completed += 20
        progress.update(completed)

    # Stop the instance
    machine("stop %s" % nodename)
    if progress:
        completed += 5
        progress.update(completed)

    # Create EC2 connection with boto
    ec2 = boto.ec2.connect_to_region(region)

    # Cleanup old AMIs
    images = ec2.get_all_images(filters={'tag:Name': "prepared-%s" % client})
    for image in images:
        image.deregister(delete_snapshot=True)
        logger.info("Deleted AMI %s" % image.id)

    # Create new AMI
    ami_id = ec2.create_image(instance_id, "prepared-%s" % client, description="Prepared %s AMI" % client)

    # Tag new AMI
    image = ec2.get_all_images(image_ids=ami_id)[0]
    image.add_tag("Name", "prepared-%s" % client)
    if progress:
        completed += 10
        progress.update(completed)

    # Wait until the image is ready
    logger.info("Waiting for AMI to be available")
    while image.state == 'pending':
        sys.stdout.write('.')
        sys.stdout.flush()
        time.sleep(5)
        image.update()
    if image.state == 'available':
        return ami_id
    else:
        raise ValueError("Created AMI returned non-available state", image.state)
示例#15
0
def create_image(module, ec2):
    """
    Creates new AMI

    module : AnsibleModule object
    ec2: authenticated ec2 connection object
    """

    instance_id = module.params.get("instance_id")
    name = module.params.get("name")
    wait = module.params.get("wait")
    wait_timeout = int(module.params.get("wait_timeout"))
    description = module.params.get("description")
    no_reboot = module.params.get("no_reboot")
    device_mapping = module.params.get("device_mapping")
    tags = module.params.get("tags")
    launch_permissions = module.params.get("launch_permissions")

    try:
        params = {"instance_id": instance_id, "name": name, "description": description, "no_reboot": no_reboot}

        images = ec2.get_all_images(filters={"name": name})

        if images and images[0]:
            module.exit_json(
                msg="AMI name already present", image_id=images[0].id, state=images[0].state, changed=False
            )

        if device_mapping:
            bdm = BlockDeviceMapping()
            for device in device_mapping:
                if "device_name" not in device:
                    module.fail_json(msg="Device name must be set for volume")
                device_name = device["device_name"]
                del device["device_name"]
                bd = BlockDeviceType(**device)
                bdm[device_name] = bd
            params["block_device_mapping"] = bdm

        image_id = ec2.create_image(**params)
    except boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    # Wait until the image is recognized. EC2 API has eventual consistency,
    # such that a successful CreateImage API call doesn't guarantee the success
    # of subsequent DescribeImages API call using the new image id returned.
    for i in range(wait_timeout):
        try:
            img = ec2.get_image(image_id)

            if img.state == "available":
                break
            elif img.state == "failed":
                module.fail_json(msg="AMI creation failed, please see the AWS console for more details")
        except boto.exception.EC2ResponseError as e:
            if (
                ("InvalidAMIID.NotFound" not in e.error_code and "InvalidAMIID.Unavailable" not in e.error_code)
                and wait
                and i == wait_timeout - 1
            ):
                module.fail_json(
                    msg="Error while trying to find the new image. Using wait=yes and/or a longer wait_timeout may help. %s: %s"
                    % (e.error_code, e.error_message)
                )
        finally:
            time.sleep(1)

    if img.state != "available":
        module.fail_json(
            msg="Error while trying to find the new image. Using wait=yes and/or a longer wait_timeout may help."
        )

    if tags:
        try:
            ec2.create_tags(image_id, tags)
        except boto.exception.EC2ResponseError as e:
            module.fail_json(msg="Image tagging failed => %s: %s" % (e.error_code, e.error_message))
    if launch_permissions:
        try:
            img = ec2.get_image(image_id)
            img.set_launch_permissions(**launch_permissions)
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message), image_id=image_id)

    module.exit_json(msg="AMI creation operation complete", changed=True, **get_ami_info(img))
示例#16
0
文件: ec2_ami.py 项目: ernstp/ansible
def create_image(module, ec2):
    """
    Creates new AMI

    module : AnsibleModule object
    ec2: authenticated ec2 connection object
    """

    instance_id = module.params.get('instance_id')
    name = module.params.get('name')
    wait = module.params.get('wait')
    wait_timeout = int(module.params.get('wait_timeout'))
    description = module.params.get('description')
    architecture = module.params.get('architecture')
    kernel_id = module.params.get('kernel_id')
    root_device_name = module.params.get('root_device_name')
    virtualization_type = module.params.get('virtualization_type')
    no_reboot = module.params.get('no_reboot')
    device_mapping = module.params.get('device_mapping')
    tags = module.params.get('tags')
    launch_permissions = module.params.get('launch_permissions')

    try:
        params = {'name': name,
                  'description': description}

        images = ec2.get_all_images(filters={'name': name})

        if images and images[0]:
            # ensure that launch_permissions are up to date
            update_image(module, ec2, images[0].id)

        bdm = None
        if device_mapping:
            bdm = BlockDeviceMapping()
            for device in device_mapping:
                if 'device_name' not in device:
                    module.fail_json(msg='Device name must be set for volume')
                device_name = device['device_name']
                del device['device_name']
                bd = BlockDeviceType(**device)
                bdm[device_name] = bd

        if instance_id:
            params['instance_id'] = instance_id
            params['no_reboot'] = no_reboot
            if bdm:
                params['block_device_mapping'] = bdm
            image_id = ec2.create_image(**params)
        else:
            params['architecture'] = architecture
            params['virtualization_type'] = virtualization_type
            if kernel_id:
                params['kernel_id'] = kernel_id
            if root_device_name:
                params['root_device_name'] = root_device_name
            if bdm:
                params['block_device_map'] = bdm
            image_id = ec2.register_image(**params)
    except boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    # Wait until the image is recognized. EC2 API has eventual consistency,
    # such that a successful CreateImage API call doesn't guarantee the success
    # of subsequent DescribeImages API call using the new image id returned.
    for i in range(wait_timeout):
        try:
            img = ec2.get_image(image_id)

            if img.state == 'available':
                break
            elif img.state == 'failed':
                module.fail_json(msg="AMI creation failed, please see the AWS console for more details")
        except boto.exception.EC2ResponseError as e:
            if ('InvalidAMIID.NotFound' not in e.error_code and 'InvalidAMIID.Unavailable' not in e.error_code) and wait and i == wait_timeout - 1:
                module.fail_json(msg="Error while trying to find the new image. Using wait=yes and/or a longer "
                                     "wait_timeout may help. %s: %s" % (e.error_code, e.error_message))
        finally:
            time.sleep(1)

    if img.state != 'available':
        module.fail_json(msg="Error while trying to find the new image. Using wait=yes and/or a longer wait_timeout may help.")

    if tags:
        try:
            ec2.create_tags(image_id, tags)
        except boto.exception.EC2ResponseError as e:
            module.fail_json(msg="Image tagging failed => %s: %s" % (e.error_code, e.error_message))
    if launch_permissions:
        try:
            img = ec2.get_image(image_id)
            img.set_launch_permissions(**launch_permissions)
        except boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message), image_id=image_id)

    module.exit_json(msg="AMI creation operation complete", changed=True, **get_ami_info(img))
示例#17
0
def prepare_ami(region,
                zone,
                nodename,
                es,
                client,
                image=None,
                dag=False,
                progress=None):
    """
    Prepare client AMI
    """
    if image is None:
        image = "ethereum/client-%s" % client

    global completed
    if progress:
        completed += 5
        progress.update(completed)

    # Get our Instance ID
    inspect = json.loads(machine("inspect %s" % nodename))
    instance_id = inspect['Driver']['InstanceId']
    if progress:
        completed += 5
        progress.update(completed)

    # Pull base image
    pull_on(nodename, image)
    if progress:
        completed += 10
        progress.update(completed)

    # Create docker container w/ logstash-forwarder
    # Build logstash-forwarder directly, docker-compose doesn't seem to
    # like getting called concurrently
    # with lcd('logstash-forwarder'):
    #     compose_on(nodename, "build")
    build_on(nodename, "logstash-forwarder", "forwarder")
    if progress:
        completed += 20
        progress.update(completed)

    # Run logstash-forwarder, using run_on so we can pass --add-host
    # with lcd('logstash-forwarder'):
    #     compose_on(nodename, "up -d")  # ElasticSearch IP
    run_on(nodename,
           "forwarder", ("-d "
                         "-v /var/log/syslog:/var/log/syslog "
                         "--add-host logs.ethdev.com:%s "
                         "--restart always" % es),
           name="forwarder")
    if progress:
        completed += 5
        progress.update(completed)

    # Generate DAG
    if client == 'cpp':
        ssh_on(nodename, "sudo mkdir /opt/dag")  # see generate_dag()
    elif client == 'go':
        ssh_on(nodename, "sudo mkdir /opt/dag")  # see generate_dag()
    if dag and client != 'python':
        generate_dag(nodename, client, image)
        # FIXME For some reason, 'docker run' exits with 0
        # but never returns, so somewhere between futures,
        # Fabric and docker, there's an unhandled timeout
        # while generating DAG caches and getting no output...
        # We poll for 'Exited' in 'docker ps -a' and run with
        # -d in generate_dag() for now...
        dag_done = False
        logging.info("Generating DAG on %s..." % nodename)
        while dag_done is False:
            time.sleep(5)
            ps = docker_on(nodename, "ps -a")
            if "Exited" in ps:
                logging.info("DAG done on %s" % nodename)
                dag_done = True
    if progress:
        completed += 20
        progress.update(completed)

    # Stop the instance
    machine("stop %s" % nodename)
    if progress:
        completed += 5
        progress.update(completed)

    # Create EC2 connection with boto
    ec2 = boto.ec2.connect_to_region(region)

    # Cleanup old AMIs
    images = ec2.get_all_images(filters={'tag:Name': "prepared-%s" % client})
    for image in images:
        image.deregister(delete_snapshot=True)
        logger.info("Deleted AMI %s" % image.id)

    # Create new AMI
    ami_id = ec2.create_image(instance_id,
                              "prepared-%s" % client,
                              description="Prepared %s AMI" % client)

    # Tag new AMI
    image = ec2.get_all_images(image_ids=ami_id)[0]
    image.add_tag("Name", "prepared-%s" % client)
    if progress:
        completed += 10
        progress.update(completed)

    # Wait until the image is ready
    logger.info("Waiting for AMI to be available")
    while image.state == 'pending':
        sys.stdout.write('.')
        sys.stdout.flush()
        time.sleep(5)
        image.update()
    if image.state == 'available':
        return ami_id
    else:
        raise ValueError("Created AMI returned non-available state",
                         image.state)