Skip to content

Commit

Permalink
Add cli option to use settings from env variables
Browse files Browse the repository at this point in the history
Currently [in docker it is possible to do configuration through
environment variables](https://col.la/dockercodeconfigviaenv), which
works using the start-collabora-online.sh start-collabora-online.pl
scripts. This commit lets COOLWSD listen to the same environment
variables directly

Change-Id: I75762ad620132037523fa82167a3ff17075c7027
Signed-off-by: Skyler Grey <skyler.grey@collabora.com>
  • Loading branch information
Minion3665 authored and timar committed Sep 22, 2023
1 parent fb5aab4 commit 7459122
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 2 additions & 0 deletions man/coolwsd.1
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ coolwsd OPTIONS
.PP
\fB\-\-nocaps\fR Use a non\-privileged forkit, with increase in security problems.
.PP
\fB\-\-use\-env\-vars\fR Override coolwsd.xml config with options from the environment variables described in https://col.la/dockercodeconfigviaenv
.PP
.SH "SEE ALSO"
coolforkit(1), coolconvert(1), coolconfig(1), coolwsd-systemplate-setup(1), coolmount(1)
55 changes: 54 additions & 1 deletion wsd/COOLWSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ bool COOLWSD::NoSeccomp = false;
bool COOLWSD::AdminEnabled = true;
bool COOLWSD::UnattendedRun = false;
bool COOLWSD::SignalParent = false;
bool COOLWSD::UseEnvVarOptions = false;
std::string COOLWSD::RouteToken;
#if ENABLE_DEBUG
bool COOLWSD::SingleKit = false;
Expand Down Expand Up @@ -2122,7 +2123,9 @@ void COOLWSD::innerInitialize(Application& self)
}
}

// Override any settings passed on the command-line.
// Override any settings passed on the command-line or via environment variables
if (UseEnvVarOptions)
initializeEnvOptions();
AutoPtr<AppConfigMap> overrideConfig(new AppConfigMap(_overrideSettings));
conf.addWriteable(overrideConfig, PRIO_APPLICATION); // Highest priority

Expand Down Expand Up @@ -2888,6 +2891,16 @@ void COOLWSD::defineOptions(OptionSet& optionSet)
.required(false)
.repeatable(false));

optionSet.addOption(Option("use-env-vars", "",
"Use the environment variables defined on "
"https://sdk.collaboraonline.com/docs/installation/"
"CODE_Docker_image.html#setting-the-application-configuration-"
"dynamically-via-environment-variables to set options. "
"'DONT_GEN_SSL_CERT' is forcibly enabled and 'extra_params' is "
"ignored even when using this option.")
.required(false)
.repeatable(false));

#if ENABLE_DEBUG
optionSet.addOption(Option("unitlib", "", "Unit testing library path.")
.required(false)
Expand Down Expand Up @@ -2956,6 +2969,8 @@ void COOLWSD::handleOption(const std::string& optionName,
LoTemplate = value;
else if (optionName == "signal")
SignalParent = true;
else if (optionName == "use-env-vars")
UseEnvVarOptions = true;

#if ENABLE_DEBUG
else if (optionName == "unitlib")
Expand Down Expand Up @@ -2986,6 +3001,44 @@ void COOLWSD::handleOption(const std::string& optionName,
#endif
}

void COOLWSD::initializeEnvOptions()
{
int n = 0;
char* aliasGroup;
while ((aliasGroup = std::getenv(("aliasgroup" + std::to_string(n + 1)).c_str())) != nullptr)
{
bool first = true;
std::istringstream aliasGroupStream;
aliasGroupStream.str(aliasGroup);
for (std::string alias; std::getline(aliasGroupStream, alias, ',');)
{
if (first)
{
_overrideSettings["storage.wopi.alias_groups.group[" + std::to_string(n) +
"].host"] = alias;
first = false;
}
else
{
_overrideSettings["storage.wopi.alias_groups.group[" + std::to_string(n) +
"].alias"] = alias;
}
}

n++;
}
if (n >= 1)
{
_overrideSettings["alias_groups[@mode]"] = "groups";
}

char* optionValue;
if ((optionValue = std::getenv("username")) != nullptr) _overrideSettings["admin_console.username"] = optionValue;
if ((optionValue = std::getenv("password")) != nullptr) _overrideSettings["admin_console.password"] = optionValue;
if ((optionValue = std::getenv("server_name")) != nullptr) _overrideSettings["server_name"] = optionValue;
if ((optionValue = std::getenv("dictionaries")) != nullptr) _overrideSettings["allowed_languages"] = optionValue;
}

#if !MOBILEAPP

void COOLWSD::displayHelp()
Expand Down
2 changes: 2 additions & 0 deletions wsd/COOLWSD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class COOLWSD final : public Poco::Util::ServerApplication
static bool AdminEnabled;
static bool UnattendedRun; //< True when run from an unattended test, not interactive.
static bool SignalParent;
static bool UseEnvVarOptions;
static std::string RouteToken;
#if ENABLE_DEBUG
static bool SingleKit;
Expand Down Expand Up @@ -518,6 +519,7 @@ class COOLWSD final : public Poco::Util::ServerApplication

void defineOptions(Poco::Util::OptionSet& options) override;
void handleOption(const std::string& name, const std::string& value) override;
void initializeEnvOptions();
int main(const std::vector<std::string>& args) override;

/// Handle various global static destructors.
Expand Down

0 comments on commit 7459122

Please sign in to comment.