From: Kefu Chai Date: Tue, 15 Dec 2020 15:24:46 +0000 (+0800) Subject: ceph-conf: add --pid option X-Git-Tag: v17.0.0~253^2~1 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=c17c6c66ebadf14cf7c632ab55dffafc620b9ff2;p=ceph.git ceph-conf: add --pid option 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 --- diff --git a/doc/man/8/ceph-conf.rst b/doc/man/8/ceph-conf.rst index 3743813e11931..b8b99c2aab163 100644 --- a/doc/man/8/ceph-conf.rst +++ b/doc/man/8/ceph-conf.rst @@ -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 diff --git a/src/test/cli/ceph-conf/show-config-value.t b/src/test/cli/ceph-conf/show-config-value.t index a0ab4cbdfce26..2e0528e939000 100644 --- a/src/test/cli/ceph-conf/show-config-value.t +++ b/src/test/cli/ceph-conf/show-config-value.t @@ -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 diff --git a/src/tools/ceph_conf.cc b/src/tools/ceph_conf.cc index 755bb90233aa4..d26cbb0399496 100644 --- a/src/tools/ceph_conf.cc +++ b/src/tools/ceph_conf.cc @@ -55,6 +55,7 @@ FLAGS [--format plain|json|json-pretty] dump variables in plain text, json or pretty json + [--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 -maybe_override_name_pid(vector args) +static void maybe_override_pid(vector& 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 defaults = {{"log_to_file", "false"}}; return global_init(&defaults, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_DAEMON,