def set_attached(cluster_path): """ Set the cluster specified in `cluster_path` as the attached cluster :param cluster_path: path to cluster directory :type cluster_path: str :rtype: None """ # get currently attached cluster attached_cluster_path = config.get_attached_cluster_path() if attached_cluster_path and attached_cluster_path != cluster_path: # set cluster as attached attached_file = os.path.join(attached_cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE) try: util.sh_move(attached_file, cluster_path) except DCOSException as e: msg = "Failed to attach cluster: {}".format(e) raise DCOSException(msg) else: util.ensure_file_exists( os.path.join(cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE))
def _store_cluster_cert(cert, no_check): """Store cluster certificate bundle downloaded from cluster and store settings in core.ssl_verify :param cert: ca cert from cluster :type cert: str :param no_check: whether to verify downloaded cert :type no_check: bool :returns: whether or not we are storing the downloaded cert bundle :rtype: bool """ if not no_check: if not _user_cert_validation(cert): # we don't have a cert, but we still want to validate SSL config.set_val("core.ssl_verify", "true") return False with util.temptext() as temp_file: _, temp_path = temp_file with open(temp_path, 'w') as f: f.write(cert) cert_path = os.path.join( config.get_attached_cluster_path(), "dcos_ca.crt") util.sh_copy(temp_path, cert_path) config.set_val("core.ssl_verify", cert_path) return True
def _store_cluster_cert(cert, no_check): """Store cluster certificate bundle downloaded from cluster and store settings in core.ssl_verify :param cert: ca cert from cluster :type cert: str :param no_check: whether to verify downloaded cert :type no_check: bool :returns: whether or not we are storing the downloaded cert bundle :rtype: bool """ if not no_check and not _user_cert_validation(cert): raise DCOSException("Couldn't get confirmation for the fingerprint.") with util.temptext() as temp_file: _, temp_path = temp_file with open(temp_path, 'w') as f: f.write(cert) cert_path = os.path.join( config.get_attached_cluster_path(), "dcos_ca.crt") util.sh_copy(temp_path, cert_path) config.set_val("core.ssl_verify", cert_path) return True
def _cluster_subcommand_dir(): """ :returns: cluster specific subcommand dir or None :rtype: str | None """ attached_cluster = config.get_attached_cluster_path() if attached_cluster is not None: return os.path.join(attached_cluster, constants.DCOS_SUBCOMMAND_SUBDIR) else: return None
def test_get_attached_cluster_path(): with env(), util.tempdir() as tempdir: os.environ[constants.DCOS_DIR_ENV] = tempdir # no clusters dir assert config.get_attached_cluster_path() is None # clusters dir, no clusters _create_clusters_dir(tempdir) assert config.get_attached_cluster_path() is None # 1 cluster, not attached cluster_id = "fake-cluster" cluster_path = add_cluster_dir(cluster_id, tempdir) assert config.get_attached_cluster_path() == cluster_path attached_path = os.path.join(cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE) assert os.path.exists(attached_path) # attached cluster assert config.get_attached_cluster_path() == cluster_path
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