Exemple #1
0
def get_build_env_vars(stage, config):
    ''' Get env vars to be injected to the build script. '''
    # The stage for which the build script is being run is passed
    # via an environment variable STAGE.
    # This could be useful for creating specific builds for
    # different environments.
    env_vars = merge(os.environ, {'STAGE': stage})

    # If either remote env injection is not enabled,
    # or remote_env_path is not provided skip it.
    if not config['remote_env_injection']:
        return env_vars

    try:
        # Remote environment variables are sent to the build script too
        # if remote_env_injection is enabled.
        remote_env_path = config['stages'][stage]['remote_env_path']
        remote_vars = load_remote_env_vars(remote_env_path)

        print(remote_vars)
        return merge(remote_vars, env_vars)
    except IOError as e:
        warn('Warining: Failed to fetch remote env file from `{}`. {}'.format(
            remote_env_path, str(e)))
        return env_vars
Exemple #2
0
def setup_remote(quiet=True):
    ''' Setup remote environment before we can proceed with the deployment process. '''
    base_dir = get_deploy_dir()
    release_dir = get_release_dir()
    current_path = get_current_path()
    build_history_path = get_builds_file()
    preset = get_config()['deployment']['preset']
    did_setup = False
    stage = shell.get_stage()

    # If the release directory does not exist, create it.
    if not fs.exists(release_dir):
        remote_info('Setting up {} server for {} deployment'.format(
            stage, preset))
        remote_info('Creating the releases directory {}'.format(
            cyan(release_dir)))
        fs.mkdir(release_dir, nested=True)

        # Add build history file.
        remote_info('Creating new build meta file {}'.format(
            cyan(build_history_path)))
        save_history(merge(INITIAL_BUILD_HISTORY, {'preset': preset}))

        # Setup a default web page for web deployment.
        if preset == presets.WEB:
            setup_default_html(base_dir)

        did_setup = True

    if not did_setup and not quiet:
        remote_info('Remote already setup for deployment')

    return (release_dir, current_path)
Exemple #3
0
def get_build_env_vars(stage, config):
    ''' Get env vars to be injected to the build script. '''
    # The stage for which the build script is being run is passed
    # via an environment variable STAGE.
    # This could be useful for creating specific builds for
    # different environments.
    env_vars = merge(os.environ, {'STAGE': stage})

    # If remote env injection is not enabled skip it.
    if not config['remote_env_injection']:
        return env_vars

    # Remote environment variables are sent to the build script too
    # if remote_env_injection is enabled.
    remote_env_path = config['stages'][stage]['remote_env_path']
    remote_vars = load_remote_env_vars(remote_env_path)

    return merge(remote_vars, env_vars)
Exemple #4
0
def test_build_with_loaded_env_vars(get_config_m, remote_env_m, capfd):
    '''
    Test build() with env vars injected from remote path and vault.

    Precedence:
        - Env vars injected from OS
        - Env vars injected by boss (eg: STAGE=stage)
        - Env vars injected from remote (remote_env_path if remote_env_injection = True)
        - Env vars injected from vault
    '''

    build_script = '''
    echo STAGE = $STAGE
    echo FOO = $FOO
    echo BAR = $BAR
    echo BAT = $BAT
    echo BAZ = $BAZ
    '''

    (_, script_path) = mkstemp()
    fs.write(script_path, build_script)
    test_config = merge(
        DEFAULT_CONFIG, {
            'remote_env_injection': True,
            'stages': {
                'stage1': {
                    'remote_env_path': 'remote/env/path',
                }
            },
            'scripts': {
                'build': 'sh ' + script_path
            }
        })
    get_config_m.return_value = test_config
    remote_env_m.return_value = {
        'BAR': 'bar-from-remote',
        'BAT': 'bat-from-remote'
    }
    # Injected from host os
    os.environ['FOO'] = 'foo-from-host'

    # Vault's env vars are injected into the Host's env
    os.environ['BAT'] = 'bat-from-vault'
    os.environ['BAZ'] = 'baz-from-vault'

    buildman.build('stage1', test_config)

    out, _ = capfd.readouterr()

    # Assert all the environment varialbes have been injected
    # in the build script
    assert 'STAGE = stage1' in out
    assert 'FOO = foo-from-host' in out
    assert 'BAR = bar-from-remote' in out
    assert 'BAT = bat-from-vault' in out
    assert 'BAZ = baz-from-vault' in out
Exemple #5
0
def test_merge_v1():
    '''
    Test for boss.util.merge()
    Assert that second dictionary overrides conflicting keys during merge
    '''
    dict1 = {'key1': 'value1', 'key2': {'key3': 'value3', 'key4': 'value4'}}

    dict2 = {'key1': 'valueA', 'key2': {'keyB': 'valueB'}}

    expectedmerge = {
        'key1': 'valueA',
        'key2': {
            'keyB': 'valueB',
            'key3': 'value3',
            'key4': 'value4'
        },
    }

    merged = merge(dict1, dict2)

    assert merged == expectedmerge
Exemple #6
0
def test_merge_v0():
    '''
    Test for boss.util.merge()
    Assert expected merge outcome when no conflicting keys are present.
    '''
    dict1 = {'key1': 'value1', 'key2': {'key3': 'value3', 'key4': 'value4'}}

    dict2 = {'keyA': 'valueA', 'key2': {'keyB': 'valueB'}}

    expectedmerge = {
        'key1': 'value1',
        'key2': {
            'keyB': 'valueB',
            'key3': 'value3',
            'key4': 'value4'
        },
        'keyA': 'valueA'
    }

    merged = merge(dict1, dict2)

    assert merged == expectedmerge