]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-conf: add --pid option
authorKefu Chai <kchai@redhat.com>
Tue, 15 Dec 2020 15:24:46 +0000 (23:24 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 15 Dec 2020 18:01:12 +0000 (02:01 +0800)
this change partially reverts 1f09b196657d21cec8fca9bc868bae307200aae8,
and add "--pid" option, so user can specify the pid to override the $pid
meta variable when expanding options.

Fixes: https://tracker.ceph.com/issues/47977
Signed-off-by: Kefu Chai <kchai@redhat.com>
doc/man/8/ceph-conf.rst
src/test/cli/ceph-conf/show-config-value.t
src/tools/ceph_conf.cc

index 3743813e11931c47aa898cfdcfd8b6703e93c16e..b8b99c2aab163ba32e618410a38ae3c3f8127ed4 100644 (file)
@@ -84,6 +84,13 @@ Options
    For example, if we specify ``--name osd.0``, the following sections will be
    searched: [osd.0], [osd], [global]
 
+.. option:: --pid *pid*
+
+   override the ``$pid`` when expanding options. For example, if an option is
+   configured like ``/var/log/$name.$pid.log``, the ``$pid`` portion in its
+   value will be substituded using the PID of **ceph-conf** instead of the
+   PID of the process specfied using the ``--name`` option.
+
 .. option:: -r, --resolve-search
 
    search for the first file that exists and can be opened in the resulted
index a0ab4cbdfce26d44ae316e9467de6defb5302163..2e0528e93900050368a93fec07e9e4221344d77c 100644 (file)
@@ -30,7 +30,7 @@ Name option test to strip the PID
   > [global]
   >     admin socket = \$name.asok
   > EOF
-  $ ceph-conf --name client.admin.133423 --show-config-value admin_socket -c $TESTDIR/ceph.conf
+  $ ceph-conf --name client.admin --pid 133423 --show-config-value admin_socket -c $TESTDIR/ceph.conf
   client.admin.133423.asok
   $ ceph-conf --name mds.a --show-config-value admin_socket -c $TESTDIR/ceph.conf
   mds.a.asok
index 755bb90233aa49adf2be429d3e3c844f05588413..d26cbb03994969a93e5e211a442fc6479e6dc2b6 100644 (file)
@@ -55,6 +55,7 @@ FLAGS
   [--format plain|json|json-pretty]
                                   dump variables in plain text, json or pretty
                                   json
+  [--pid <pid>]                   Override the $pid when expanding options
 
 If there is no action given, the action will default to --lookup.
 
@@ -163,45 +164,15 @@ static int dump_all(const string& format)
   }
 }
 
-bool is_name_pid(std::string name, std::string& id)
-{
-  if (id.empty()) {
-    return false;
-  }
-  static const char* daemon_types[] = {"mon", "osd", "mds", "mgr"};
-  if (std::find(std::begin(daemon_types), std::end(daemon_types), name) !=
-      std::end(daemon_types)) {
-    // only override name and pid for non-daemon names
-    return false;
-  }
-  try {
-    std::stoi(id);
-  } catch (const std::logic_error&) {
-    // only override pid for $id which looks like pid
-    return false;
-  }
-  return true;
-}
-
-std::pair<std::string, std::string>
-maybe_override_name_pid(vector<const char*> args)
+static void maybe_override_pid(vector<const char*>& args)
 {
   for (auto i = args.begin(); i != args.end(); ++i) {
     string val;
-    if (ceph_argparse_witharg(args, i, &val, "--name", "-n", (char*)NULL)) {
-      size_t dot_pos = val.rfind('.');
-      if (dot_pos != val.npos) {
-       string name = val.substr(0, dot_pos);
-       string id = val.substr(dot_pos + 1);
-        if (is_name_pid(name, id)) {
-          // override name
-          return {name, id};
-        }
-      }
-      return {val, ""};
+    if (ceph_argparse_witharg(args, i, &val, "--pid", (char*)NULL)) {
+      setenv("PID", val.c_str(), 1);
+      break;
     }
   }
-  return {};
 }
 
 int main(int argc, const char **argv)
@@ -220,16 +191,8 @@ int main(int argc, const char **argv)
 
   auto orig_args = args;
   auto cct = [&args] {
-    // override the name and PID for non-daemon names
-    auto [name, pid] = maybe_override_name_pid(args);
-    if (!name.empty()) {
-      // push the name option back
-      args.push_back("--name");
-      args.push_back(name.c_str());
-    }
-    if (!pid.empty()) {
-      setenv("PID", pid.c_str(), 1);
-    }
+    // override the PID before options are expanded
+    maybe_override_pid(args);
     std::map<std::string,std::string> defaults = {{"log_to_file", "false"}};
     return global_init(&defaults, args, CEPH_ENTITY_TYPE_CLIENT,
                       CODE_ENVIRONMENT_DAEMON,