I'm revisiting a project which hasn't been updated for a while.
In production/online
environment, it uses environment variables defined at:
openshift online console
> applications
> deployments
> my node app
> environment
In development/offline
environment, it uses environment variables defined at:
./src/js/my_modules/local_settings
(this file is ignored by .gitignore
)
The code looks something like:
// check which environment we are in
if (process.env.MONGODB_USER) {
var online_status = "online";
}
else {
var online_status = "offline";
}
// if online, use environment variables defined in red hat openshift
if (online_status === 'online') {
var site_title = process.env.SITE_TITLE;
var site_description = process.env.SITE_DESCRIPTION;
//etc
}
// if offline, get settings from a local file
else if (online_status === 'offline') {
var local_settings = require('./src/js/my_modules/local_settings');
var site_title = local_settings.SITE_TITLE;
var site_description = local_settings.SITE_DESCRIPTION;
// etc
}
I would like to install the dotenv package in my local project repo via:
npm install dotenv
So that I can:
- Have my local settings in a
.env
file in the root of my project (ignored in.gitignore
) - Be able to use
process.env.SOME_VARIABLE
rather thanlocal_settings.SOME_VARIABLE
- Get rid of some
if/else
blocks as both scenarios would point toprocess.env.SOME_VARIABLE
I'm a bit confused as to how this would effect the online environment.
Seeing as both production/online
and development/offline
environments would use:
var some_variable = process.env.SOME_VARIABLE_HERE
would the application automatically know to:
- Look at the local
.env
file when in development? - Look at the Red Hat environment variables when in production?
And would adding the required instantiation at the beginning of the server-side
file:
require('dotenv').config()
somehow make Red Hat OpenShift freak out (as it seems to already have its own 'things' in place to resolve references to process.env.SOME_VARIABLE_HERE
to the relevant values defined in the OpenShift console)?
To have a file by any environment (.dev .staging .prod) into the source code repository or manually in the server (it those are in .gitignore) worked for long time, but now it goes against to the devops.
The clean way is to use environment variables but managed remotely and obtained at the start of your application.
How it works?
Basically your apps don't read or need a file (.env .properties, etc) with variables anymore. It loads them from a remote http service.
Not intrusive
In this approach, you don't need specific languages variables (nodejs in your case). You just need to prepare your app to use environment variables. Your application don't care where the variables come from, just needs to be available at operative system level.
To achieve that, you just need to download the variables using a simple shell code or a very basic algorithm (http invocation) in your favorite language.
After that, after the start of your app, variables are ready to use at the most basic level.
This approach is not intrusive because your app don't need something complex like library or algorithm in some programing language. Just needs the environment variables.
Intrusive
Same as previous alternative but instead to read the variables direct from environment system, you should use or create a class/module in your language. This offer your the variables you need:
VariablesManager at the startup must have consumed the variables from a remote service (http) and the store them to offer them to whoever needs it through getProperty method.
Also this VariablesManager usually has a feature called hot-reload which at intervals, update the variables consuming the remote variables manager. With this, if your application is running in production with real users and some variable needs to be updated, you just need to change it in the variables manager. Automatically your app will load the new values, without restart or touching your app
This approach is intrusive because you need to load advanced libraries in some programing language or create it.
Devops
Your application just needs a few properties or settings related to the consume of remote variables. For example: variables of acme-web-staging:
You could hide the secure key and parametrize the application_id using environment variables (created in the platform console)
Or if you want one variable manager by each environment
staging
production
Variables manager
This concept was introduced many years ago. I used with java. It consist in a web application with features like:
Here a list of some ready to use alternatives:
In your specific case
If you can configure an execution of a shell script before the start of your openshift app, you could load and expose the variables at that stage and the previous snippet would not be necessary because the variables will be ready to be retrieved using process.env directly in your app