Esempio n. 1
0
def bosh_login(*, user=None, pswd=None, cacert=None, env=None):
    """
    Login to the bosh CLI. All parameters default to config values if not specified.
    :param user: Bosh username.
    :param pswd: Bosh password.
    :param cacert: Bosh client cert.
    :param env: Bosh environment to login to.
    :return: int, str, str; Returncode, stdout, stderr.
    """
    cfg = Config()['bosh']
    certfile = Certfile(cacert=cacert)

    creds = cfg.get('credentials')
    if not creds:
        assert user and pswd, "Must specify username and password!"
    else:
        user = user or creds['user']
        pswd = pswd or creds['pswd']

    cmd = [cfg['cmd'], '-e', env or cfg['env']]
    stdin = [user, pswd]
    if certfile:
        cmd.extend(['--ca-cert=' + certfile.name])
    cmd.append('login')
    return run_cmd(cmd, stdin)
Esempio n. 2
0
def cf_login(*, user=None, pswd=None, api=None, skip_ssl_validation=None):
    """
    Login to the CF CLI. All parameters default to config values if not specified.
    :param user: str; Cf username.
    :param pswd: str; Cf password.
    :param api: str; Cf api url.
    :param skip_ssl_validation: bool; Whether cf ssl validation should be skipped, useful if certs are not installed.
    :return: int, str, str; Returncode, stdout, stderr.
    """
    cfg = Config()['cf']
    creds = cfg.get('credentials')
    if not creds:
        assert user and pswd and api, "Must specify username, password, and API address!"
    else:
        user = user or creds['user']
        pswd = pswd or creds['pswd']
        if skip_ssl_validation is None:
            skip_ssl_validation = creds.get('skip-ssl-validation') or False
    pswd = '"{}"'.format(pswd.replace('"', r'\"'))
    api = api or creds['api']
    cmd = [cfg['cmd'], 'login']
    if skip_ssl_validation:
        cmd.append('--skip-ssl-validation')
    cmd.extend(['-a', api, '-u', user, '-p', pswd])
    return run_cmd(cmd, stdin='\n')
Esempio n. 3
0
def bosh_cli(args, stdin=None, env=None, dep=None, suppress_output=False):
    """
    Call the bosh CLI.
    :param args: Union[List[str], str]; Arguments for the bosh CLI. This will allow chaining additional commands
    with '&&', '|', etc.
    :param env: str; The bosh environment to use. Defaults to config value.
    :param dep: str; The bosh deployment to use. Defaults to configured cf deployment value.
    :param stdin: Optional[Union[str, List[Union[str, List[str]]]]]; Input to pipe to the program.
    :param suppress_output: bool; If true, no extra debug output will be printed when an error occurs.
    :return: int, str, str; Returncode, stdout, stderr.
    """
    boshcfg = Config()['bosh']
    certfile = Certfile()
    cmd = [
        boshcfg['cmd'], '-e', env or boshcfg['env'], '-d', dep
        or boshcfg['cf-dep']
    ]
    if certfile:
        cmd.extend(['--ca-cert=' + certfile.name])
    if isinstance(args, list):
        cmd.extend(args)
    else:
        cmd.append(args)
    res = run_cmd(cmd, stdin=stdin, suppress_output=suppress_output)
    return res
Esempio n. 4
0
def cf_target(org, space):
    """
    Target a specific organization and space using the cloud foundry CLI. This should be run before anything which calls
    out to cloud foundry. This will fail if cloud foundry is not logged in.
    :param org: The organization to target.
    :param space: The space within the organization to target.
    :return: The returncode of the cloud foundry CLI.
    """
    cfg = Config()
    rcode, _, _ = run_cmd([cfg['cf']['cmd'], 'target', '-o', org, '-s', space])
    return rcode
Esempio n. 5
0
def assert_cmd(*args, **kwargs):
    rcode, stdout, stderr = run_cmd(*args, **kwargs)
    assert rcode == 0
    return stdout, stderr