Many of our recent projects have been based on building online web-based applications. These applications are using some of the latest technologies, such as, Ruby on Rails, MongoDB, and OpenShift. For those of you who are not familiar with these technologies, Ruby on Rails (RoR) is a robust web application framework, MongoDB is a NoSQL document-oriented database management system, and OpenShift is a platform-as-a-service (PaaS) offering from Red Hat. Netsource is a partner company with both Red Hat OpenShift and MongoDB.
From these efforts, we are extracting an open source project that will enable quicker development of subscription based online web applications. We call this project "Ignition". It is currently under development and hosted on gitHub. The project is based on RoR, MongoDB, and OpenShift. If you sign up for an Openshift account and follow the instructions in the README file, you should be able to get an instance of the application scaffold up and running in about 5-10 minutes.
One of the common issues with open source projects is how to manage configuration data that should be kept private. These data elements include items such as API keys, server names, and other elements that are specific to your installation and should not be stored in a public source code repository. A common approach in the RoR community is to store this information in a YAML file such as the config/application.yml file that is available to all RoR based applications. This file is not checked into your repository, but an example application.yml file is often stored there, so developers know what configuration variables are required for your application to operate correctly. The one inconvenience with this approach is the need to create this application.yml.
Openshift just released a new feature that enables us to create a single application.yml that can be used on any Openshft Gear (i.e. server) installation. This feature called "Custom Environment Variables" was just introduced in the September 2013 OpenShift Blog. The feature allows us to dynamically create environment variables on an OpenShift cloud server, which will be made available to the application running on the platform.
We use this new feature by creating an application.yml that uses environment variable placeholders. In our example, we need to specify configuration variables for an email server and for login credentials associated with the initial administrative user. These configuration variables and the corresponding environment variable placeholders are shown in the example file below. We setup separate sets of variables for development versus production. You can do the same thing for the test environment, but we left it out of the example for brevity.
development:
SMTP_HOST: <%= ENV['SMTP_HOST'] %>
SMTP_PORT: <%= ENV['SMTP_PORT'] %>
SMTP_DOMAIN: <%= ENV['SMTP_DOMAIN'] %>
SMTP_USER: <%= ENV['SMTP_USER'] %>
SMTP_PASSWORD: <%= ENV['SMTP_PASSWORD'] %>ADMIN_FIRST_NAME: <%= ENV['ADMIN_FIRST_NAME'] %>
ADMIN_LAST_NAME: <%= ENV['ADMIN_LAST_NAME'] %>
ADMIN_PHONE: <%= ENV['ADMIN_PHONE'] %>
ADMIN_EMAIL: <%= ENV['ADMIN_EMAIL'] %>
ADMIN_PASSWORD: <%= ENV['ADMIN_PASSWORD'] %>production:
SMTP_HOST: <%= ENV['SMTP_HOST'] %>
SMTP_PORT: <%= ENV['SMTP_PORT'] %>
SMTP_DOMAIN: <%= ENV['SMTP_DOMAIN'] %>
SMTP_USER: <%= ENV['SMTP_USER'] %>
SMTP_PASSWORD: <%= ENV['SMTP_PASSWORD'] %>
ADMIN_FIRST_NAME: <%= ENV['ADMIN_FIRST_NAME'] %>
ADMIN_LAST_NAME: <%= ENV['ADMIN_LAST_NAME'] %>
ADMIN_PHONE: <%= ENV['ADMIN_PHONE'] %>
ADMIN_EMAIL: <%= ENV['ADMIN_EMAIL'] %>
ADMIN_PASSWORD: <%= ENV['ADMIN_PASSWORD'] %>
In the above example, the environment variable placeholder codes are prefixed by the ENV string. An example would be the environment variable for the administrator's first name, which is shown as ENV['ADMIN_FIRST_NAME']. Now that the placeholders are specified in the application.yml file, the file can be checked into the source code repository.
To ensure that the environment variables are set properly in the OpenShift Gear (i.e. server), we use the OpenShift command line utility rhc. The rhc command is described in the Openshift User Manual or you can use rhc --help. The rhc command allows us to set environment variables on the OpenShift Gear individually on the command line or read them from a file. The general usage for setting environment variables on the command line is:
Usage: rhc set-env VARIABLE=VALUE [... VARIABLE=VALUE] [--namespace NAME] [--app NAME]
An example for setting environment variables on the command line is:
rhc set-env ADMIN_FIRST_NAME=Some ADMIN_LAST_NAME=Admin --app myapp
An alternative to setting each variable on the command line would be to create a temporary file to load all the environment variables at once. An example environment variable file is shown below:
SMTP_HOST=smtpout.someserver.net
SMTP_PORT=3535
SMTP_DOMAIN=somedomain.com
SMTP_USER=someuser@somedomain.com
SMTP_PASSWORD=somepassword
ADMIN_FIRST_NAME=Some
ADMIN_LAST_NAME=Admin
ADMIN_PHONE=800.555.1212
ADMIN_EMAIL=sadmin@example.com
ADMIN_PASSWORD=somepassword
You can then use the rhc command to load all the environment variables from the temporary file. In our example, we store the variables in a file called variables.txt. The command to load the variables is shown below:
rhc set-env /path/to/variables.txt --app myapp
Once you have successfully loaded the environment variables, you can delete your temporary file and push your code to the OpenShift Gear. The code will then import the environment variables into your application.yml and you should be ready to run.
Categories