Beispiel #1
0
def active_ami_for_edp(env, dep, play):
    """
    Given an environment, deployment, and play, find the base AMI id used for the active deployment.

    Arguments:
        env (str): Environment to check (stage, prod, loadtest, etc.)
        dep (str): Deployment to check (edx, edge, mckinsey, etc.)
        play (str): Play to check (edxapp, discovery, ecommerce, etc.)
    Returns:
        str: Base AMI id of current active deployment for the EDP.
    Raises:
        MultipleImagesFoundException: If multiple AMI IDs are found within the EDP's ELB.
        ImageNotFoundException: If no AMI IDs are found for the EDP.
    """
    LOG.info("Looking up AMI for {}-{}-{}...".format(env, dep, play))
    edp = EDP(env, dep, play)
    ec2_conn = boto.connect_ec2()
    asg_conn = boto.connect_autoscale()
    all_elbs = get_all_load_balancers()
    LOG.info("Found {} load balancers.".format(len(all_elbs)))

    edp_filter = {
        "tag:environment": env,
        "tag:deployment": dep,
        "tag:play": play,
    }
    reservations = ec2_conn.get_all_reservations(filters=edp_filter)
    LOG.info("{} reservations found for EDP {}-{}-{}".format(
        len(reservations), env, dep, play))
    amis = set()
    instances_by_id = {}
    for reservation in reservations:
        for instance in reservation.instances:
            # Need to build up instances_by_id for code below
            instances_by_id[instance.id] = instance

    asgs = asg_conn.get_all_groups(names=asgs_for_edp(edp))
    for asg in asgs:
        for asg_inst in asg.instances:
            instance = instances_by_id[asg_inst.instance_id]
            asg_enabled = len(asg.suspended_processes) == 0
            if instance.state == 'running' and asg_enabled:
                amis.add(instance.image_id)
                LOG.info("AMI found in ASG {} for {}-{}-{}: {}".format(
                    asg.name, env, dep, play, instance.image_id))
            else:
                LOG.info("Instance {} state: {} - asg {} enabled: {}".format(
                    instance.id, instance.state, asg.name, asg_enabled))

    if len(amis) > 1:
        msg = "Multiple AMIs found for {}-{}-{}, should have only one.".format(
            env, dep, play)
        raise MultipleImagesFoundException(msg)

    if not amis:
        msg = "No AMIs found for {}-{}-{}.".format(env, dep, play)
        raise ImageNotFoundException(msg)

    return amis.pop()
Beispiel #2
0
def tags_for_ami(ami_id):
    """
    Look up the tags for an AMI.

    Arguments:
        ami_id (str): An AMI Id.
    Returns:
        dict: The tags for this AMI.
    Raises:
        ImageNotFoundException: No image found with this ami ID.
        MissingTagException: AMI is missing one or more of the expected tags.
    """
    LOG.debug("Looking up edp for {}".format(ami_id))
    ec2 = boto.connect_ec2()

    try:
        ami = ec2.get_all_images(ami_id)[0]
    except IndexError:
        raise ImageNotFoundException("ami: {} not found".format(ami_id))
    except EC2ResponseError as error:
        raise ImageNotFoundException(str(error))

    return ami.tags
Beispiel #3
0
def active_ami_for_edp(env, dep, play):
    """
    Given an environment, deployment, and play, find the base AMI id used for the active deployment.

    Arguments:
        env (str): Environment to check (stage, prod, loadtest, etc.)
        dep (str): Deployment to check (edx, edge, mckinsey, etc.)
        play (str): Play to check (edxapp, discovery, ecommerce, etc.)
    Returns:
        str: Base AMI id of current active deployment for the EDP.
    Raises:
        MultipleImagesFoundException: If multiple AMI IDs are found within the EDP's ELB.
        ImageNotFoundException: If no AMI IDs are found for the EDP.
    """
    LOG.info("Looking up AMI for {}-{}-{}...".format(env, dep, play))
    ec2_conn = boto.connect_ec2()
    all_elbs = get_all_load_balancers()
    LOG.info("Found {} load balancers.".format(len(all_elbs)))

    edp_filter = {
        "tag:environment": env,
        "tag:deployment": dep,
        "tag:play": play,
    }
    reservations = ec2_conn.get_all_reservations(filters=edp_filter)
    LOG.info("{} reservations found for EDP {}-{}-{}".format(
        len(reservations), env, dep, play))
    amis = set()
    for reservation in reservations:
        for instance in reservation.instances:
            elbs = _instance_elbs(instance.id, all_elbs)
            if instance.state == 'running' and len(elbs) > 0:
                amis.add(instance.image_id)
                LOG.info("AMI found for {}-{}-{}: {}".format(
                    env, dep, play, instance.image_id))
            else:
                LOG.info("Instance {} state: {} - elbs in: {}".format(
                    instance.id, instance.state, len(elbs)))

    if len(amis) > 1:
        msg = "Multiple AMIs found for {}-{}-{}, should have only one.".format(
            env, dep, play)
        raise MultipleImagesFoundException(msg)

    if len(amis) == 0:
        msg = "No AMIs found for {}-{}-{}.".format(env, dep, play)
        raise ImageNotFoundException(msg)

    return amis.pop()