Everything around a CM's Life & Work

Menu

Environment templates – part 1 – Create your template

Ok, time for a big topic. Environment templates is a really useful practice which can improve many Continuous Integration areas, so I will probably split its description over several posts.
This first post involves, obviously, template creation.
The second one will show you how to reuse such template for integration tests automation.Third one will add some detail over deploy abstraction and management.

But, first of all, WHY would you need environment templates?
Supposing you are already performing CI over your product libraries and applications, you will probably face the problem on how such application have to be deployed over your different middlewares (apache, tomcat, jboss, etc) and on different kind on environments (dev, qa, preprod, prod, etc).

The first step you will probably have to adopt is to create configuration agnostic libraries/applications so that you can use the very same artifact on dev as well on prod environment (and your CM can sleep fine for this!).
Any hardcoded configurations about working directories, IP addresses, users et similia have to be removed from the application itself and delegated elsewhere.

BUT if the application artifact is unaware of its configuration it’s up to the middleware or even to the underlying environment to know all information and to communicate them to the running code.
You may therefore say that you are not solving a problem but just moving it on another point of the chain: this is partially true, since we are centralising environment configuration.
You may have plenty of libraries and web applications running on your environment, you may have also several different middleware talking to each other and managing shared configuration among all of them can be a mess, but you will always have one environment to be deployed at once.
On all these environments we want to run the very same artifact, built just once by our CI chain and propagated with no differences on all relevant installations.

To achieve all of these, you have to extend your CI chain from library and application, to full environments automated builds.

Since we are all Java fans (really?) we will use as usual Maven to create our template and to properly inject all our configurations stuffs.
To make all more clear we will do a very simple example, we will build a Jboss Wildfly template where we will create a parametrized configuration to add a JNDI datasource for our application and be able to change memory allocation.
For sure we want to be able to use different database reference accordingly to the kind of environment we are going to deploy (prod, preprod, qa, test) and also to allocare different RAM size in the same way.
You will see that all we need is already present in any maven distribution, with just few plugin addition for some extra features.

OK, let’s start, first of all you will have to download the standard Wildfly 8.1 archive. You can get if from the official site, a good idea is to store it (with no changes!) also into your internal artifact repository to have it immediately available within your intranet for any successive build and save time and bandwidth.

You should always start creating your template from an official distribution package for a couple of very simple (but extremely useful) reasons.

If you start from scratch, your template maven build will contains all the know-how regarding the customization you applied to the official version, so you will never miss a step. If you upload to your artifactory an already-patched version you may forgot why and what you patched in a very close future.

Starting from an official version makes upgrade to future minor version easier, since you will have to change only the sections of your build affected by the upgrade, none at all if you are lucky. Moreover, versioning over the build script will preserve backward compatibility if you will have to reproduce an old template for any reason.

While you read my 5 cents on this topic your package download (and re-upload to repo) should be complete, so we can move forward.
Now we must create a new maven project with the following features:

project must be of pom type

a src/main/resources folder must be available

a src/main/filter folder must be available

our official package is declared as a runtime dependency.

more or less something like this (there are a couple of additional properties I already added to save time, they will come in handy soon):

Now we have to use maven dependency:unpack-depedency plugin to unpack our middleware archive under target/template folder, which will become our temporary working directory. We skip the docs directory since we do not expect to need it in a running environment.

Now what we want to do is to add a mysql JNDI resource to the jboss, so we will have to change the standalone/configuration/standalone.xml configuration file.
We want also to change memory setting so the other file to change is bin/standalone.conf file.
So not forget that we also need a mysql connector module, so we will add it under the ${mysql.module.path} we declared in the initial properties.

Since we are starting from the official distribution we will copy both files from the package to our src/main/resources folder preserving the relative path, so they will be:

src/main/resources/standalone/configuration/standalone.xml

src/main/resources/bin/standalone.conf

our mysql jboss module will need its path with a module xml file as well:

As you can see in all these file we just created the configuration structure, but never hardcoded any deploy-dependend parameter (IPs, Users, Password, etc)
In replace of all of them, we used maven-like placeholders as ${myappds.ds.server.pool.name}We will manage them in few minutes, now let’s understand what to do with our configuration files.

We have unpacked our original wildfly zip in target/template folder with our first maven plugin, we have replicated the same structure with our customization in our src/main/resources folder, now we have to copy these files in the same folder as the distribution, overwriting the original one.
Maven plugin to perform this is maven-resource-plugin:copy-resources, we must check that it’s execution runs after the first one or our custom file will not replace the original ones, package phase is fine.

Note: proper plugin execution may become a critical issue when modifying templates, mainly over high level of pom inheritance, always check everything is running in the order you are expecting.

We also have to add the mysql jar with proper version in the same folder as the module.xml configuration file.
Since we can reuse the already declared maven-dependeny-plugin, this time with the copy goal, we can just ad a different execution to the previous section:

Ok, now all our files are in place, we just have to resolve all placeholder with final values providing a way to distinguish different environment.
It’s time to create in src/main/filters folder a new properties file for each profile we want to implement, e.g.

src/main/filters/local.properties will contains all placeholders resolution for developers local environment

src/main/filters/devel.properties will contains same resolutions for a development integration environment

src/main/filters/prod.properties will do the same for production environment

…

A sample development file ( so devel.properties ) with all placeholders we used in out code above could be:

Last step is to replace all configuration placeholder with the values of our favorite properties file.
That’s why we prepared everything in a temporary working directory called templates: with our last plugin we will copy everything in the final environment location applying resource filtering to all relevant files type:

This plugin is quite verbose since we want to apply resource filtering only to relevant files types, that is the ones containing placeholders.
Applying resource filtering to binaries or other non-text based files will result in corrupted file which will cause havoc on our applications, so be careful.
Obviously the filter file used for resolution will be the one activated by the maven profile declaration!

So a quick recap, if now we are going to submit our CI build with a very simple

mvn clean package -Pdevel

our build will:

Download original Wildfly distribution from our internal repository.

Unpack it into template folder

Overlay and overwrite any template content with the one provided into our src/main/resources folder

Copy everything back into the environment folder, replacing all placeholders with values coming from the devel.properties filter from src/main/filters directory

Well, it seem to me we have now a CI-ready build for our jboss middleware, with a totally pre-environment customizable configuration set.
Notice that the result of this build is not something you are expected to store on a maven repository (that’s why we triggered a package and not a deploy lifecycle) but an environment ready to be deployed on your live server, would it be a Qa, Prod or Development one.

Ok, this is enough for now, since this is just a quick sample of the environment template approach you can put in place on your CI system.