def test_set_attached():
    with env(), util.tempdir() as tempdir:
        os.environ[constants.DCOS_DIR_ENV] = tempdir

        cluster_path = add_cluster_dir("a", tempdir)
        # no attached_cluster
        assert cluster.set_attached(cluster_path) is None
        assert config.get_attached_cluster_path() == cluster_path

        cluster_path2 = add_cluster_dir("b", tempdir)
        # attach cluster already attached
        assert cluster.set_attached(cluster_path2) is None
        assert config.get_attached_cluster_path() == cluster_path2

        # attach cluster through environment
        os.environ[constants.DCOS_CLUSTER] = "a"
        assert config.get_attached_cluster_path() == cluster_path

        # attach back to old cluster through environment
        os.environ[constants.DCOS_CLUSTER] = "b"
        assert config.get_attached_cluster_path() == cluster_path2
Exemple #2
0
def _attach(name):
    """
    :param name: name of cluster
    :type name: str
    :rtype: None
    """

    c = cluster.get_cluster(name)
    if c is not None:
        return cluster.set_attached(c.get_cluster_path())
    else:
        raise DCOSException("Cluster [{}] does not exist".format(name))
def test_setup_cluster_config(mock_get):
    with env(), util.tempdir() as tempdir:
        os.environ[constants.DCOS_DIR_ENV] = tempdir
        with cluster.setup_directory() as setup_temp:

            cluster.set_attached(setup_temp)

            cluster_id = "fake"
            mock_resp = mock.Mock()
            mock_resp.json.return_value = {
                "CLUSTER_ID": cluster_id,
                "cluster": cluster_id
            }
            mock_get.return_value = mock_resp
            path = cluster.setup_cluster_config("fake_url", setup_temp, False)
            expected_path = os.path.join(
                tempdir, constants.DCOS_CLUSTERS_SUBDIR + "/" + cluster_id)
            assert path == expected_path
            assert os.path.exists(path)
            assert os.path.exists(os.path.join(path, "dcos.toml"))

        assert not os.path.exists(setup_temp)
Exemple #4
0
def _attach(name):
    """
    :param name: name of cluster
    :type name: str
    :rtype: None
    """

    c = cluster.get_cluster(name)
    if not c:
        raise DCOSException("Cluster [{}] does not exist".format(name))

    if c.get_status() == cluster.STATUS_UNCONFIGURED:
        return setup(c.get_url(), provider=c.get_provider().get('id'))

    return cluster.set_attached(c.get_cluster_path())
Exemple #5
0
def setup(dcos_url,
          insecure=False, no_check=False, ca_certs=None,
          password_str=None, password_env=None, password_file=None,
          provider=None, username=None, key_path=None):
    """
    Setup the CLI to talk to your DC/OS cluster.

    :param dcos_url: master ip of cluster
    :type dcos_url: str
    :param insecure: whether or not to verify ssl certs
    :type insecure: bool
    :param no_check: whether or not to verify downloaded ca cert
    :type no_check: bool
    :param ca_certs: path to root CA to verify requests
    :type ca_certs: str
    :param password_str: password
    :type password_str: str
    :param password_env: name of environment variable with password
    :type password_env: str
    :param password_file: path to file with password
    :type password_file: bool
    :param provider: name of provider to authentication with
    :type provider: str
    :param username: username
    :type username: str
    :param key_path: path to file with private key
    :type param: str
    :returns: process status
    :rtype: int
    """

    with cluster.setup_directory() as temp_path:

        # set cluster as attached
        cluster.set_attached(temp_path)

        # authenticate
        config.set_val("core.dcos_url", dcos_url)
        # get validated dcos_url
        dcos_url = config.get_config_val("core.dcos_url")

        # configure ssl settings
        stored_cert = False
        if insecure:
            config.set_val("core.ssl_verify", "false")
        elif ca_certs:
            config.set_val("core.ssl_verify", ca_certs)
        else:
            cert = cluster.get_cluster_cert(dcos_url)
            # if we don't have a cert don't try to verify one
            if cert is False:
                config.set_val("core.ssl_verify", "false")
            else:
                stored_cert = _store_cluster_cert(cert, no_check)

        try:
            login(dcos_url,
                  password_str, password_env, password_file,
                  provider, username, key_path)
        except DCOSAuthenticationException:
            msg = ("Authentication failed. "
                   "Please run `dcos cluster setup <dcos_url>`")
            raise DCOSException(msg)

        # configure cluster directory
        cluster.setup_cluster_config(dcos_url, temp_path, stored_cert)

    return 0
Exemple #6
0
def setup(dcos_url,
          insecure=False, no_check=False, ca_certs=None,
          password_str=None, password_env=None, password_file=None,
          provider=None, username=None, key_path=None):
    """
    Setup the CLI to talk to your DC/OS cluster.

    :param dcos_url: master ip of cluster
    :type dcos_url: str
    :param insecure: whether or not to verify ssl certs
    :type insecure: bool
    :param no_check: whether or not to verify downloaded ca cert
    :type no_check: bool
    :param ca_certs: path to root CA to verify requests
    :type ca_certs: str
    :param password_str: password
    :type password_str: str
    :param password_env: name of environment variable with password
    :type password_env: str
    :param password_file: path to file with password
    :type password_file: bool
    :param provider: name of provider to authentication with
    :type provider: str
    :param username: username
    :type username: str
    :param key_path: path to file with private key
    :type param: str
    :returns: process status
    :rtype: int
    """

    with cluster.setup_directory() as temp_path:

        # set cluster as attached
        cluster.set_attached(temp_path)

        # Make sure to ignore any environment variable for the DCOS URL.
        # There is already a mandatory command argument for this.
        env_warning = ("Ignoring '{}' environment variable.\n")
        if "DCOS_URL" in os.environ:
            emitter.publish(DefaultError(env_warning.format('DCOS_URL')))
            del os.environ["DCOS_URL"]
        if "DCOS_DCOS_URL" in os.environ:
            emitter.publish(DefaultError(env_warning.format('DCOS_DCOS_URL')))
            del os.environ["DCOS_DCOS_URL"]

        # authenticate
        config.set_val("core.dcos_url", dcos_url)
        # get validated dcos_url
        dcos_url = config.get_config_val("core.dcos_url")

        # configure ssl settings
        stored_cert = False
        if insecure:
            config.set_val("core.ssl_verify", "false")
        elif ca_certs:
            config.set_val("core.ssl_verify", ca_certs)
        elif _needs_cluster_cert(dcos_url):
            cert = cluster.get_cluster_cert(dcos_url)
            if cert:
                stored_cert = _store_cluster_cert(cert, no_check)
            else:
                config.set_val("core.ssl_verify", "false")
        else:
            config.set_val("core.ssl_verify", "false")

        try:
            login(dcos_url,
                  password_str, password_env, password_file,
                  provider, username, key_path)
        except DCOSAuthenticationException:
            msg = ("Authentication failed. "
                   "Please run `dcos cluster setup <dcos_url>`")
            raise DCOSException(msg)

        # configure cluster directory
        cluster.setup_cluster_config(dcos_url, temp_path, stored_cert)

    return 0