Exemple #1
0
def initialize():
    ''' Initialize the local project directory for boss. '''
    files_written = []
    fabfile = FABFILE_PATH
    config_file = DEFAULT_CONFIG_FILE

    # If config file doesn't exist create it.
    if not fs.exists(config_file):
        config_tmpl = fs.read(BASE_PATH + '/misc/boss.yml_template')
        config_tmpl = config_tmpl.format(
            project_name=DEFAULT_CONFIG['project_name'],
            user=DEFAULT_CONFIG['user'],
            deployment_preset=DEFAULT_CONFIG['deployment']['preset'],
            deployment_base_dir=DEFAULT_CONFIG['deployment']['base_dir']
        )
        fs.write(config_file, config_tmpl)
        files_written.append(config_file)

    # If fabfile doesn't exist create it.
    if not fs.exists(fabfile):
        fabfile_tmpl = fs.read(BASE_PATH + '/misc/fabfile.py_template')
        fs.write(fabfile, fabfile_tmpl)
        files_written.append(fabfile)

    return files_written
def initialize_config(interactive):
    '''
    Initialize a new boss.yml file.
    If the file already exists return None, else return the file.
    '''
    config_file = DEFAULT_CONFIG_FILE

    # If config already exists, return None
    if fs.exists(config_file):
        return None

    config_tmpl = fs.read(BASE_PATH + '/misc/boss.yml_template')

    if not interactive:
        tmpl_params = {
            'project_name': DEFAULT_CONFIG['project_name'],
            'user': DEFAULT_CONFIG['user'],
            'ssh_port': DEFAULT_CONFIG['port'],
            'deployment_preset': DEFAULT_CONFIG['deployment']['preset'],
            'deployment_base_dir': DEFAULT_CONFIG['deployment']['base_dir']
        }
    else:
        tmpl_params = get_initial_config_params()

    fs.write(config_file, config_tmpl.format(**tmpl_params))

    return config_file
Exemple #3
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 #4
0
def test_write():
    ''' Test fs.write() works. '''
    filename = 'somefile'
    data = 'somedata'
    m = mock_open()

    with patch('__builtin__.open', m) as mock_file:
        fs.write(filename, data)
        mock_file.assert_called_with(filename, 'w')
        m().write.assert_called_with(data)
def initialize_fabfile():
    '''
    Initialize a new fabfile.
    If the file already exists return None, else return the file.
    '''
    fabfile = FABFILE_PATH

    if not fs.exists(fabfile):
        fabfile_tmpl = fs.read(BASE_PATH + '/misc/fabfile.py_template')
        fs.write(fabfile, fabfile_tmpl)

        return fabfile
def test_get(server):
    ''' Test get() transfers remote file to the local. '''
    for uid in server.users:
        target_dir = tempfile.mkdtemp()
        source_file = os.path.join(target_dir, 'foo_src')
        target_file = os.path.join(target_dir, 'foo_dest')

        fs.write(source_file, 'Test get operation')
        assert not fs.exists(target_file)

        with server.client(uid) as client:
            sftp = client.open_sftp()
            remote.get(
                sftp,
                remote_path=source_file,
                local_path=target_file
            )
            assert fs.read(target_file) == 'Test get operation'