]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/config: extract get_cluster_name()
authorKefu Chai <kchai@redhat.com>
Wed, 29 Jul 2020 11:00:00 +0000 (19:00 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 1 Aug 2020 16:01:47 +0000 (00:01 +0800)
for two reasons

* to consolidate the rules for figuring out the cluster name
* to reuse it in another implementation of parse_config_files(),
  crimson will need to restructure the existing parse_config_files(),
  and implement an async version of it.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/common/config.cc
src/common/config.h

index f53d453825f73c7664b64e369d18ccd4abf9ba6d..fc52433be40a01d0675874e3c84db3e8e2170054 100644 (file)
  */
 
 #include <boost/type_traits.hpp>
-
+#if __has_include(<filesystem>)
+#include <filesystem>
+namespace fs = std::filesystem;
+#else
+#include <experimental/filesystem>
+namespace fs = std::experimental::filesystem;
+#endif
 #include "common/ceph_argparse.h"
 #include "common/common_init.h"
 #include "common/config.h"
@@ -351,11 +357,7 @@ int md_config_t::parse_config_files(ConfigValues& values,
     return -ENOSYS;
 
   if (values.cluster.empty() && !conf_files_str) {
-    /*
-     * set the cluster name to 'ceph' when neither cluster name nor
-     * configuration file are specified.
-     */
-    values.cluster = "ceph";
+    values.cluster = get_cluster_name(nullptr);
   }
   // open new conf
   string conffile;
@@ -377,23 +379,8 @@ int md_config_t::parse_config_files(ConfigValues& values,
   if (conffile.empty())
     return -ENOENT;
 
-  if (values.cluster.size() == 0) {
-    /*
-     * If cluster name is not set yet, use the prefix of the
-     * basename of configuration file as cluster name.
-     */
-    auto start = conffile.rfind('/') + 1;
-    auto end = conffile.find(".conf", start);
-    if (end == conffile.npos) {
-        /*
-         * If the configuration file does not follow $cluster.conf
-         * convention, we do the last try and assign the cluster to
-         * 'ceph'.
-         */
-        values.cluster = "ceph";
-    } else {
-      values.cluster = conffile.substr(start, end - start);
-    }
+  if (values.cluster.empty()) {
+    values.cluster = get_cluster_name(conffile.c_str());
   }
 
   std::vector<std::string> my_sections = get_my_sections(values);
@@ -473,6 +460,25 @@ md_config_t::get_conffile_paths(const ConfigValues& values,
   return paths;
 }
 
+std::string md_config_t::get_cluster_name(const char* conffile)
+{
+  if (conffile) {
+    // If cluster name is not set yet, use the prefix of the
+    // basename of configuration file as cluster name.
+    if (fs::path path{conffile}; path.extension() == ".conf") {
+      return path.stem();
+    } else {
+      // If the configuration file does not follow $cluster.conf
+      // convention, we do the last try and assign the cluster to
+      // 'ceph'.
+      return "ceph";
+    }
+  } else {
+    // set the cluster name to 'ceph' when configuration file is not specified.
+    return "ceph";
+  }
+}
+
 void md_config_t::parse_env(unsigned entity_type,
                            ConfigValues& values,
                            const ConfigTracker& tracker,
index 0fa89382b8a25258c43da56e2d9a729abb5dd892..a3f54c0b65c2455ecb5a274476af1e2c1b749076 100644 (file)
@@ -317,7 +317,7 @@ private:
                                            const char *conf_files,
                                            std::ostream *warnings,
                                            int flags) const;
-
+  static std::string get_cluster_name(const char* conffile_path);
   // The configuration file we read, or NULL if we haven't read one.
   ConfFile cf;
 public: