Environment Variables in Open Source Projects

By October 31, 2012 HowTo No Comments
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Many operating systems use environment variables to pass configuration information to applications. Like properties on the Java platform, environment variables are key/value pairs, where both the key and the value are strings. The conventions for setting and using environment variables vary between operating systems, and also between command line interpreters.

Jelastic allows you to set different environment variables for your application’s needs. You can simply add your custom variables to the application server’s configuration:

Let’s imagine that you want to use these variables to deploy open source project directly from Github repository with database credentials stored not in the publicly available source code.

Below is a short example which helps to understand how to use environment variables in Jelastic PaaS.

Create the environment

1. Go to Jelastic.com and sign up if you haven’t done it yet or log in with your Jelastic credentials by clicking the Sign In link on the page.

2. While in Jelastic dashboard, click the Create environment button:

3. In the Environment Topology dialog, pick your application server (for example, Tomcat 7), the database you want to use (e.g. MySQL) and Maven as your build tool. Then type your environment name, for example, variablestest and click Create.

In a minute your environment will be successfully created.

Configure the database

1. Click on Open in browser button for your database.

2. When you created the environment Jelastic sent you an email with credentials to the database. Create an account and the database with the application using these credentials.

Build and deploy the project

Let’s imagine that you have uploaded your project to Github and there are specified the next environment variables:

String url = System.getProperty("database.url").toString();
String username = System.getProperty("database.username").toString();
String password = System.getProperty("database.password").toString();
String driver = System.getProperty("database.driver").toString();

1. Add your project to Maven.

In the dialog, navigate to the Git tab (or SVN) if you want to use Git as your revision control system. Specify your project name, Path to your project, Branch, Login and Password of your repository, name of your Environment, Context in which you will deploy your project, and click Add.

2. Click Buid and deploy button for your project.

Your project must appear in the context that you have specified.

Configure application

1. Click Config button for your application server (Tomcat 6, 7 or Jetty).

2. Navigate to server folder. Choose variables.conf file. There you’??ll see a short instruction on adding your environment variables. Write your custom environment variables. Each variable should be either separated by space or start from a new line.

-Ddatabase.url=jdbc:mysql://mysql-{your_env_name}.{hoster_domain}/{db_name}
-Ddatabase.username=root
-Ddatabase.password=password
-Ddatabase.driver=your_database_driver

Don’t forget to save the changes!

4. Restart the server.

Now you can open your application in a web browser.

If you use GlassFish as your application server:

1. Click the last button for GlassFish in your environment, choose Admin panel >Login in the appeared list (or you can follow the link to the Admin console which you’ve got in the email after creating environment).

2. Fill in Username and Password which you’ve got in the email after creating environment.

3. Choose gfcluster-config > JVM Settings in the list. Then click on the JVM Options tab.

4. Click on Add JVM option and specify your variables.

Restart your server and voila! – your application is up and running!

Related articles:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Leave a Reply

Subscribe to get the latest updates