iwind/rockmongo

Reading configuration from environment variables

Closed this issue · 4 comments

Hi,

First off all Rockmongo Rocks!. Thanks for creating this great app.

I am having some issues connecting to Rockmongo running inside Docker container. One of the limitation with Docker is that it's hard to modify existing configuration once the image has been built. One has to recreate the image with the new configuration. Currently I am not able to override the default configuration to allow it to connect to my Mongo instance running on a separate container.

I would like to propose that Rockmongo configuration can be overridden from environment variables? is that something that Rockmongo already capable off? Or can it be implemented?

I think having the capability to read from environment variables is a good feature especially for users who uses Rockmongo in virtual container like Docker.

GilaCode

Do you have an example of an app that allows conf overriding with env variables? It sounds fairly easy, but I'd like to know the best practices before attempting to contribute.

I am not sure about any specific open source application which uses environment variables to store configurations information but the original idea of using this method was proposed by the author of this site:

http://12factor.net/config

You should find a lot of discussion about the "12 factor app" approach. My favorite is from this site:

http://www.slashnode.com/the-12-factor-php-app-part-1/ <-- under section III. Configuration

I am not really good with PHP but I think maybe in "config.php" we can check for values from environment variables if it's there before we use the preconfigured one.

$mongo_name = getenv("mongo_name");
if(!$mongo_name) $mongo_name = "Localhost";
$MONGO["servers"][$i]["mongo_name"] = $mongo_name;//mongo server name

GilaCode

Well, I can think of two inconvenients:

  1. PHP env variables don't allow for arrays, so $MONGO["servers"][$i]["mongo_name"]would need to be read from a concatenated key such as "servers_{$i}_mongo_name". The index interpolation is by itself a bit confusing.
  2. AFAIK, PHP env variables only accept strings. You can set their value as a number but it's parsed as a string anyway. This means that RockMongo would need to add typecasting to the conf values to convert strings to integers or booleans.

However, both of them are just a matter of convention.

Well sounds like the issue is what we want to expose as environment variables. The whole configuration index or just the individual configs. I am more keen towards the latter. But anyway as you've said, it's a matter of convention.

Anyhow, the intention of exposing the configurations is just to make it convenient to automate Rockmongo deployment without the need to modify the configuration code itself. if it's potentially breaks Rockmongo maybe we can leave it as it's

GilaCode