Example #1
0
def setup_cluster_config(dcos_url, temp_path, stored_cert):
    """
    Create a cluster directory for cluster specified in "temp_path"
    directory.

    :param dcos_url: url to DC/OS cluster
    :type dcos_url: str
    :param temp_path: path to temporary config dir
    :type temp_path: str
    :param stored_cert: whether we stored cert bundle in 'setup' dir
    :type stored_cert: bool
    :returns: path to cluster specific directory
    :rtype: str
    """

    try:
        # find cluster id
        cluster_url = dcos_url.rstrip('/') + '/metadata'
        res = http.get(cluster_url, timeout=1)
        cluster_id = res.json().get("CLUSTER_ID")

    except DCOSException as e:
        msg = ("Error trying to find cluster id: {}\n "
               "Please make sure the provided DC/OS URL is valid: {}".format(
                   e, dcos_url))
        raise DCOSException(msg)

    # create cluster id dir
    cluster_path = os.path.join(config.get_config_dir_path(),
                                constants.DCOS_CLUSTERS_SUBDIR, cluster_id)
    if os.path.exists(cluster_path):
        raise DCOSException("Cluster [{}] is already setup".format(dcos_url))

    util.ensure_dir_exists(cluster_path)

    # move contents of setup dir to new location
    for (path, dirnames, filenames) in os.walk(temp_path):
        for f in filenames:
            util.sh_copy(os.path.join(path, f), cluster_path)

    cluster = Cluster(cluster_id)
    config_path = cluster.get_config_path()
    if stored_cert:
        cert_path = os.path.join(cluster_path, "dcos_ca.crt")
        config.set_val("core.ssl_verify", cert_path, config_path=config_path)

    cluster_name = cluster_id
    try:
        url = dcos_url.rstrip('/') + '/mesos/state-summary'
        name_query = http.get(url, toml_config=cluster.get_config())
        cluster_name = name_query.json().get("cluster")

    except DCOSException:
        pass

    config.set_val("cluster.name", cluster_name, config_path=config_path)

    return cluster_path
Example #2
0
def dcos_dir_tmp_copy():
    with util.tempdir() as tempdir:
        old_dcos_dir_env = os.environ.get(constants.DCOS_DIR_ENV)
        old_dcos_dir = config.get_config_dir_path()
        os.environ[constants.DCOS_DIR_ENV] = tempdir
        copy_tree(old_dcos_dir, tempdir)

        yield tempdir

        if old_dcos_dir_env:
            os.environ[constants.DCOS_DIR_ENV] = old_dcos_dir_env
        else:
            os.environ.pop(constants.DCOS_DIR_ENV)
Example #3
0
def test_uses_deprecated_config():
    with env(), util.tempdir() as tempdir:
        os.environ.pop('DCOS_CONFIG', None)
        os.environ[constants.DCOS_DIR_ENV] = tempdir
        assert config.get_config_dir_path() == tempdir

        # create old global config toml
        global_toml = create_global_config(tempdir)
        assert config.get_global_config_path() == global_toml
        assert config.uses_deprecated_config() is True

        # create clusters subdir
        _create_clusters_dir(tempdir)
        assert config.uses_deprecated_config() is False
Example #4
0
def setup_directory():
    """
    A context manager for the temporary setup directory created as a
    placeholder before we find the cluster's CLUSTER_ID.

    :returns: path of setup directory
    :rtype: str
    """

    try:
        temp_path = os.path.join(config.get_config_dir_path(),
                                 constants.DCOS_CLUSTERS_SUBDIR, "setup")
        util.ensure_dir_exists(temp_path)

        yield temp_path
    finally:
        shutil.rmtree(temp_path, ignore_errors=True)
Example #5
0
def move_to_cluster_config():
    """Create a cluster specific config file + directory
    from a global config file. This will move users from global config
    structure (~/.dcos/dcos.toml) to the cluster specific one
    (~/.dcos/clusters/CLUSTER_ID/dcos.toml) and set that cluster as
    the "attached" cluster.

    :rtype: None
    """

    global_config = config.get_global_config()
    dcos_url = config.get_config_val("core.dcos_url", global_config)

    # if no cluster is set, do not move the cluster yet
    if dcos_url is None:
        return

    try:
        # find cluster id
        cluster_url = dcos_url.rstrip('/') + '/metadata'
        res = http.get(cluster_url)
        cluster_id = res.json().get("CLUSTER_ID")

    # don't move cluster if dcos_url is not valid
    except DCOSException as e:
        logger.error("Error trying to find cluster id: {}".format(e))
        return

    # create cluster id dir
    cluster_path = os.path.join(config.get_config_dir_path(),
                                constants.DCOS_CLUSTERS_SUBDIR, cluster_id)

    util.ensure_dir_exists(cluster_path)

    # move config file to new location
    global_config_path = config.get_global_config_path()
    util.sh_copy(global_config_path, cluster_path)

    # set cluster as attached
    util.ensure_file_exists(
        os.path.join(cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE))
def test_move_to_cluster_config(mock_get, mock_config):
    with env(), util.tempdir() as tempdir:
        os.environ[constants.DCOS_DIR_ENV] = tempdir

        create_global_config(tempdir)
        mock_config.return_value = "fake-url"

        cluster_id = "fake"
        mock_resp = mock.Mock()
        mock_resp.json.return_value = {"CLUSTER_ID": cluster_id}
        mock_get.return_value = mock_resp

        assert config.get_config_dir_path() == tempdir
        cluster.move_to_cluster_config()

        clusters_path = os.path.join(tempdir, constants.DCOS_CLUSTERS_SUBDIR)
        assert os.path.exists(clusters_path)
        cluster_path = os.path.join(clusters_path, cluster_id)
        assert os.path.exists(os.path.join(cluster_path, "dcos.toml"))
        assert os.path.exists(
            os.path.join(cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE))
Example #7
0
def dcos_tempdir(copy=False):
    """
    Context manager for getting a temporary DCOS_DIR.

    :param copy: whether or not to copy the current one
    :type copy: bool
    """

    with util.tempdir() as tempdir:
        old_dcos_dir_env = os.environ.get(constants.DCOS_DIR_ENV)
        old_dcos_dir = config.get_config_dir_path()
        os.environ[constants.DCOS_DIR_ENV] = tempdir
        if copy:
            copy_tree(old_dcos_dir, tempdir)

        yield tempdir

        if old_dcos_dir_env:
            os.environ[constants.DCOS_DIR_ENV] = old_dcos_dir_env
        else:
            os.environ.pop(constants.DCOS_DIR_ENV)
Example #8
0
def global_subcommand_dir():
    """ Returns global subcommand dir. defaults to ~/.dcos/subcommands """

    return os.path.join(config.get_config_dir_path(),
                        constants.DCOS_SUBCOMMAND_SUBDIR)
Example #9
0
def _subcommand_dir():
    """ Returns subcommand dir. defaults to ~/.dcos/subcommands """

    return os.path.join(config.get_config_dir_path(),
                        constants.DCOS_SUBCOMMAND_SUBDIR)