Esempio n. 1
0
def test_is_fourfront_env():

    assert is_fourfront_env('fourfront-cgap') is False
    assert is_fourfront_env('cgap-prod') is False
    assert is_fourfront_env('fourfront-blue') is True

    assert is_fourfront_env(None) is False
Esempio n. 2
0
def snapshot_rds(connection, **kwargs):
    check = CheckResult(connection, 'snapshot_rds')
    if Stage.is_stage_prod() is False:
        check.summary = check.description = 'This check only runs on Foursight prod'
        return check
    rds_name = 'fourfront-production' if (env_utils.is_fourfront_env(connection.ff_env) and env_utils.is_stg_or_prd_env(connection.ff_env)) else connection.ff_env
    # snapshot ID can only have letters, numbers, and hyphens
    snap_time = datetime.datetime.strptime(kwargs['uuid'], "%Y-%m-%dT%H:%M:%S.%f").strftime("%Y-%m-%dT%H-%M-%S")
    snapshot_name = 'foursight-snapshot-%s-%s' % (rds_name, snap_time)
    client = boto3.client('rds', region_name=beanstalk_utils.REGION)
    res = client.create_db_snapshot(
             DBSnapshotIdentifier=snapshot_name,
             DBInstanceIdentifier=rds_name
    )
    if not res.get('DBSnapshot'):
        check.status = 'FAIL'
        check.summary = check.description = 'Something went wrong during snapshot creation'
        check.full_output = res
    else:
        check.status = 'PASS'
        # there is a datetime in the response that must be str formatted
        res['DBSnapshot']['InstanceCreateTime'] = str(res['DBSnapshot']['InstanceCreateTime'])
        check.full_output = res
        check.summary = 'Snapshot successfully created'
        check.description = 'Snapshot succesfully created with name: %s' % snapshot_name
    return check
Esempio n. 3
0
 def mocked_standard_mirror_env(envname):
     assert envname.endswith(
         "-prd-env"
     ), "mocked_standard_mirror_env does nto handle %r." % envname
     if env_utils.is_fourfront_env(envname):
         return "fourfront-stg-env"
     elif env_utils.is_cgap_env(envname):
         return None
     else:
         raise AssertionError(
             "mocked_standard_mirror_env does not handle %r." % envname)
Esempio n. 4
0
 def _deploy_indexer(e, version):
     if is_fourfront_env(e):
         description = try_to_describe_indexer_env(FF_ENV_INDEXER)
     else:
         description = try_to_describe_indexer_env(CGAP_ENV_INDEXER)
     if description is not None:
         check.status = 'ERROR'
         check.summary = 'Tried to spin up indexer env for %s when one already exists for this portal' % e
         return False
     else:
         return EBDeployer.deploy_indexer(e, version)
Esempio n. 5
0
def test_compute_prd_env_for_env():

    computed_ff_prd = compute_prd_env_for_env('fourfront-mastertest')
    assert is_fourfront_env(computed_ff_prd)
    assert is_stg_or_prd_env(computed_ff_prd)

    computed_cgap_prd = compute_prd_env_for_env('fourfront-cgapwolf')
    assert is_cgap_env(computed_cgap_prd)
    assert is_stg_or_prd_env(computed_cgap_prd)

    with pytest.raises(NotBeanstalkEnvironment):
        compute_prd_env_for_env('fourfront-production-blue')
Esempio n. 6
0
def handler(event, context):
    '''
    Kick off the ff_deploy_staging step function, which will trigger a staging
    deployment. The ElasticBeanstalk environments for data and staging are
    determined using beanstalk_utils.compute_ff_prd_env, which requires ElasticBeanstalk
    IAM permissions
    '''
    # determine if we are deploying to a production env
    event['source_env'] = bs.compute_ff_prd_env()
    source_env = event['source_env']
    dest_env = None
    if is_fourfront_env(source_env) and is_stg_or_prd_env(source_env):
        # TODO: Making this cgap-compatible means making an adjustment indicated later in this file. -kmp 10-Apr-2020
        dest_env = guess_mirror_env(source_env)
    if not dest_env:
        # e.g., can get here if:
        #  - source_env is a cgap environment (downstream isn't yet ready for that)
        #  - source_env is not a production environment
        #  - source_env is a production environment with no mirror
        return {
            "type": "trigger_staging_build",
            "torb_message": "invalid event.source_env",
            "event": event
        }
    event['dest_env'] = dest_env

    start_time = datetime.now().strftime("%a_%b_%d_%Y__%H%M%S")
    run_name = "%s_deploy_for_%s" % (dest_env, start_time)
    event['start_time'] = start_time

    if event.get('run_name'):
        run_name = event.get('run_name')  # used for testing

    try:
        input = make_input(event)
    except Exception as e:
        return {
            "type": "trigger_staging_build",
            "torb_message": str(e),
        }

    # trigger the step function to run
    response = client.start_execution(
        stateMachineArn=STEP_FUNCTION_ARN,
        name=run_name,
        input=input,
    )

    # pop non-JSON serializable stuff...
    response.pop('startDate')
    return response
Esempio n. 7
0
def provision_indexer_environment(connection, **kwargs):
    """ Provisions an indexer environment for the given env. Note that only one indexer can be online
        per application (one for 4DN, one for CGAP).

        IMPORTANT: env is an EB ENV NAME ('data', 'staging' are NOT valid).
        IMPORTANT: This ecosystem will break some integrated tests in dcicutils
    """
    check = CheckResult(connection, 'provision_indexer_environment')
    env = kwargs.get('env', None)
    application_version = kwargs.get('application_version', None)

    if application_version is None:
        check.status = 'ERROR'
        check.summary = 'Did not provide application_version to deploy indexer, which is required. ' \
                        'Get this information from the EB Console.'
        return check

    def _deploy_indexer(e, version):
        if is_fourfront_env(e):
            description = try_to_describe_indexer_env(FF_ENV_INDEXER)
        else:
            description = try_to_describe_indexer_env(CGAP_ENV_INDEXER)
        if description is not None:
            check.status = 'ERROR'
            check.summary = 'Tried to spin up indexer env for %s when one already exists for this portal' % e
            return False
        else:
            return EBDeployer.deploy_indexer(e, version)

    if is_cgap_env(env) or is_fourfront_env(env):
        success = _deploy_indexer(env, application_version)
        if success:
            check.status = 'PASS'
            check.summary = 'Successfully triggered indexer-server provision for environment %s' % env
        else:
            check.status = 'ERROR'
            check.summary = 'An error occurred on deployment. Check the AWS EB Console.'
    else:
        check.status = 'FAIL'
        check.summary = 'Gave an unknown environment: %s' % env

    return check
Esempio n. 8
0
def indexer_server_status(connection, **kwargs):
    """ Checks the status of index servers (if any are up) """
    check = CheckResult(connection, 'indexer_server_status')
    check.action = 'terminate_indexer_server'
    env = kwargs.get('env')
    indexer_env = FF_ENV_INDEXER if is_fourfront_env(env) else CGAP_ENV_INDEXER
    description = try_to_describe_indexer_env(
        indexer_env)  # verify an indexer is online
    if description is None:
        check.status = 'PASS'  # could have been terminated
        check.summary = 'Did not locate corresponding indexer env: ' \
                        'given env %s does not have indexer %s' % (env, indexer_env)
        check.allow_action = False
        return check

    try:
        indexing_finished, counts = are_counts_even(env)
    except Exception as e:  # XXX: This should be handled in dcicutils -Will 5/18/2020
        check.status = 'ERROR'
        check.summary = 'Failed to get indexing status of given env: %s' % str(
            e)
        check.allow_action = False
        return check

    if indexing_finished is True:
        check.status = 'PASS'
        check.summary = 'Env %s has finished indexing and is ready to be terminated' % env
        check.action_message = 'Safe to terminate indexer env: %s since indexing is finished' % env
        check.allow_action = True  # allow us to terminate
    else:
        check.status = 'WARN'
        check.allow_action = True
        check.action_message = 'Will terminate indexer env %s when indexing is not done yet!' % env
        check.summary = 'Env %s has not finished indexing with remaining counts %s' % (
            env, ' '.join(counts))

    check.full_output = indexer_env  # full_output contains indexer_env we checked
    return check