summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSara Golemon <sara.golemon@mongodb.com>2018-07-31 14:57:56 +0000
committerSara Golemon <sara.golemon@mongodb.com>2018-08-13 21:43:42 +0000
commit55ca9666a9f40ab480f399f4ed9d915bf55925ef (patch)
tree9c61b6302cc626c51bce2121810aada299b3129f
parentd8c1ad1bd4154ebbd19ceae2ca0c46164d57fcc0 (diff)
downloadmongo-55ca9666a9f40ab480f399f4ed9d915bf55925ef.tar.gz
SERVER-36380 Add --outputConfig command line option
-rw-r--r--src/mongo/db/server_options_server_helpers.cpp7
-rw-r--r--src/mongo/util/options_parser/environment.cpp56
-rw-r--r--src/mongo/util/options_parser/environment.h5
-rw-r--r--src/mongo/util/options_parser/options_parser_init.cpp11
-rw-r--r--src/mongo/util/options_parser/options_parser_test.cpp61
5 files changed, 140 insertions, 0 deletions
diff --git a/src/mongo/db/server_options_server_helpers.cpp b/src/mongo/db/server_options_server_helpers.cpp
index e5b59443cbc..9342ae91302 100644
--- a/src/mongo/db/server_options_server_helpers.cpp
+++ b/src/mongo/db/server_options_server_helpers.cpp
@@ -88,6 +88,13 @@ Status addGeneralServerOptions(moe::OptionSection* options) {
"config", "config,f", moe::String, "configuration file specifying additional options")
.setSources(moe::SourceAllLegacy);
+ options
+ ->addOptionChaining("outputConfig",
+ "outputConfig",
+ moe::Switch,
+ "Display the resolved configuration and exit")
+ .setSources(moe::SourceCommandLine)
+ .hidden();
options
->addOptionChaining(
diff --git a/src/mongo/util/options_parser/environment.cpp b/src/mongo/util/options_parser/environment.cpp
index 021fedec677..08f68009da1 100644
--- a/src/mongo/util/options_parser/environment.cpp
+++ b/src/mongo/util/options_parser/environment.cpp
@@ -28,6 +28,7 @@
#include "mongo/util/options_parser/environment.h"
#include <iostream>
+#include <yaml-cpp/yaml.h>
#include "mongo/bson/util/builder.h"
#include "mongo/db/jsobj.h"
@@ -357,6 +358,54 @@ Status valueMapToBSON(const std::map<Key, Value>& params,
}
return Status::OK();
}
+
+void buildYAMLNode(YAML::Emitter& out, const BSONObj& in, bool isMap = true) {
+ if (isMap) {
+ out << YAML::BeginMap;
+ } else {
+ out << YAML::BeginSeq;
+ }
+
+ for (const auto& elem : in) {
+ if (isMap) {
+ out << YAML::Key << elem.fieldName();
+ out << YAML::Value;
+ }
+ switch (elem.type()) {
+ case BSONType::Bool:
+ out << elem.Bool();
+ break;
+ case BSONType::NumberInt:
+ out << elem.Int();
+ break;
+ case BSONType::NumberLong:
+ out << elem.Long();
+ break;
+ case BSONType::NumberDouble:
+ out << elem.Double();
+ break;
+ case BSONType::String:
+ out << elem.String();
+ break;
+ case BSONType::Array:
+ buildYAMLNode(out, elem.Obj(), false);
+ break;
+ case BSONType::Object:
+ buildYAMLNode(out, elem.Obj(), true);
+ break;
+ default:
+ // Other types should not be produced by MOE.
+ uasserted(ErrorCodes::BadValue,
+ str::stream() << "Invalid type encountered in config: " << elem.type());
+ }
+ }
+
+ if (isMap) {
+ out << YAML::EndMap;
+ } else {
+ out << YAML::EndSeq;
+ }
+}
} // namespace
BSONObj Environment::toBSON() const {
@@ -368,5 +417,12 @@ BSONObj Environment::toBSON() const {
return builder.obj();
}
+std::string Environment::toYAML() const {
+ auto bson = toBSON();
+ YAML::Emitter root;
+ buildYAMLNode(root, bson);
+ return root.c_str();
+}
+
} // namespace optionenvironment
} // namespace mongo
diff --git a/src/mongo/util/options_parser/environment.h b/src/mongo/util/options_parser/environment.h
index 624e711b30d..d7791eabf44 100644
--- a/src/mongo/util/options_parser/environment.h
+++ b/src/mongo/util/options_parser/environment.h
@@ -211,6 +211,11 @@ public:
*/
BSONObj toBSON() const;
+ /**
+ * Produce serialized YAML.
+ */
+ std::string toYAML() const;
+
/* Debugging */
void dump() const;
diff --git a/src/mongo/util/options_parser/options_parser_init.cpp b/src/mongo/util/options_parser/options_parser_init.cpp
index f75c98c603a..1bf0e8851c4 100644
--- a/src/mongo/util/options_parser/options_parser_init.cpp
+++ b/src/mongo/util/options_parser/options_parser_init.cpp
@@ -49,6 +49,17 @@ MONGO_STARTUP_OPTIONS_PARSE(StartupOptions)(InitializerContext* context) {
std::cerr << "try '" << context->args()[0] << " --help' for more information" << std::endl;
quickExit(EXIT_BADOPTIONS);
}
+ if (startupOptionsParsed.count("outputConfig")) {
+ bool output = false;
+ auto status = startupOptionsParsed.get(Key("outputConfig"), &output);
+ if (!status.isOK()) {
+ return status;
+ }
+ if (output) {
+ std::cout << startupOptionsParsed.toYAML() << std::endl;
+ quickExit(EXIT_CLEAN);
+ }
+ }
return Status::OK();
}
diff --git a/src/mongo/util/options_parser/options_parser_test.cpp b/src/mongo/util/options_parser/options_parser_test.cpp
index cec439a5626..beeea6c6448 100644
--- a/src/mongo/util/options_parser/options_parser_test.cpp
+++ b/src/mongo/util/options_parser/options_parser_test.cpp
@@ -4624,6 +4624,67 @@ TEST(NumericalBaseParsing, YAMLConfigFile) {
ASSERT_EQUALS(unsignedVal, 0x10U);
}
+TEST(YAMLConfigFile, OutputConfig) {
+ moe::OptionSection options;
+ options.addOptionChaining("cacheSize", "cacheSize", moe::Long, "");
+ options.addOptionChaining("command", "command", moe::StringVector, "");
+ options.addOptionChaining("config", "config", moe::String, "");
+ options.addOptionChaining("math.pi", "pi", moe::Double, "");
+ options.addOptionChaining("net.port", "port", moe::Int, "");
+ options.addOptionChaining("net.bindIp", "bind_ip", moe::String, "");
+ options.addOptionChaining("net.bindIpAll", "bind_ip_all", moe::Switch, "");
+ options.addOptionChaining("security.javascriptEnabled", "javascriptEnabled", moe::Bool, "");
+ options.addOptionChaining("setParameter", "setParameter", moe::StringMap, "");
+ options.addOptionChaining("systemLog.path", "logPath", moe::String, "");
+
+ OptionsParserTester parser;
+ parser.setConfig("config.yaml",
+ "systemLog: { path: /tmp/mongod.log }\n"
+ "command: [ mongo, mongod, mongos ]");
+
+ const std::vector<std::string> argv = {
+ "binaryname",
+ "--port",
+ "31337",
+ "--bind_ip",
+ "127.0.0.1,::1",
+ "--bind_ip_all",
+ "--setParameter",
+ "scramSHAIterationCount=12345",
+ "--javascriptEnabled",
+ "false",
+ "--cacheSize",
+ "12345",
+ "--pi",
+ "3.14159265",
+ "--config",
+ "config.yaml",
+ };
+
+ std::map<std::string, std::string> env_map;
+ moe::Environment env;
+ ASSERT_OK(parser.run(options, argv, env_map, &env));
+ ASSERT_EQ(env.toYAML(),
+ "cacheSize: 12345\n"
+ "command:\n"
+ " - mongo\n"
+ " - mongod\n"
+ " - mongos\n"
+ "config: config.yaml\n"
+ "math:\n"
+ " pi: 3.14159265\n"
+ "net:\n"
+ " bindIp: 127.0.0.1,::1\n"
+ " bindIpAll: true\n"
+ " port: 31337\n"
+ "security:\n"
+ " javascriptEnabled: false\n"
+ "setParameter:\n"
+ " scramSHAIterationCount: 12345\n"
+ "systemLog:\n"
+ " path: /tmp/mongod.log");
+}
+
void TestFile(std::vector<unsigned char> contents, bool valid) {
mongo::unittest::TempDir tempdir("options_testpath");
boost::filesystem::path p(tempdir.path());