def save(toml_config): """ :param toml_config: TOML configuration object :type toml_config: MutableToml or Toml """ serial = toml.dumps(toml_config._dictionary) path = get_config_path() util.ensure_file_exists(path) util.enforce_file_permissions(path) with util.open_file(path, 'w') as config_file: config_file.write(serial)
def load_from_path(path, mutable=False): """Loads a TOML file from the path :param path: Path to the TOML file :type path: str :param mutable: True if the returned Toml object should be mutable :type mutable: boolean :returns: Map for the configuration file :rtype: Toml | MutableToml """ util.ensure_file_exists(path) with util.open_file(path, 'r') as config_file: try: toml_obj = toml.loads(config_file.read()) except Exception as e: raise DCOSException( 'Error parsing config file at [{}]: {}'.format(path, e)) return (MutableToml if mutable else Toml)(toml_obj)
def load_from_path(path, mutable=False): """Loads a TOML file from the path :param path: Path to the TOML file :type path: str :param mutable: True if the returned Toml object should be mutable :type mutable: boolean :returns: Map for the configuration file :rtype: Toml | MutableToml """ util.ensure_dir_exists(os.path.dirname(path)) util.ensure_file_exists(path) util.enforce_file_permissions(path) with util.open_file(path, 'r') as config_file: try: toml_obj = toml.loads(config_file.read()) except Exception as e: raise DCOSException('Error parsing config file at [{}]: {}'.format( path, e)) return (MutableToml if mutable else Toml)(toml_obj)
def get_attached_cluster_path(): """ The attached cluster is denoted by a file named "attached" in one of the cluster directories. Ex: $DCOS_DIR/clusters/CLUSTER_ID/attached :returns: path to the director of the attached cluster :rtype: str | None """ path = get_clusters_path() if not os.path.exists(path): return None attached = None clusters = os.listdir(get_clusters_path()) cluster_envvar = os.environ.get(constants.DCOS_CLUSTER) for c in clusters: cluster_path = os.path.join(path, c) name = get_config_val("cluster.name", load_from_path( os.path.join(cluster_path, "dcos.toml"))) if cluster_envvar is not None \ and (cluster_envvar == c or cluster_envvar == name): return cluster_path if os.path.exists(os.path.join( cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE)): attached = cluster_path if attached is not None: return attached # if only one cluster, set as attached if len(clusters) == 1: cluster_path = os.path.join(path, clusters[0]) util.ensure_file_exists(os.path.join( cluster_path, constants.DCOS_CLUSTER_ATTACHED_FILE)) return cluster_path return None
def test_get_clusters(): with env(), util.tempdir() as tempdir: os.environ[constants.DCOS_DIR_ENV] = tempdir # no config file of any type assert cluster.get_clusters() == [] # cluster dir exists, no cluster clusters_dir = os.path.join(tempdir, constants.DCOS_CLUSTERS_SUBDIR) util.ensure_dir_exists(clusters_dir) assert cluster.get_clusters() == [] # a valid cluster cluster_id = "a8b53513-63d4-4059-8b08-fde4fe1f1a83" add_cluster_dir(cluster_id, tempdir) # Make sure clusters dir can contain random files / folders # cf. https://jira.mesosphere.com/browse/DCOS_OSS-1782 util.ensure_file_exists(os.path.join(clusters_dir, '.DS_Store')) util.ensure_dir_exists(os.path.join(clusters_dir, 'not_a_cluster')) assert cluster.get_clusters() == [_cluster(cluster_id)]
def create_global_config(dcos_dir): global_toml = os.path.join(dcos_dir, "dcos.toml") util.ensure_file_exists(global_toml) return global_toml