by Priit Liivak, Partner, Head of Engineering, April 20, 2016
You probably know and hopefully have used SonarQube. It's a great tool that is actively evolving and improving. This is great if you are a developer using SonarQube. However, if you happen to administer SonarQube instance, the constant upgrades are quite a hassle since upgrade guide consists of 11 steps.
This was way too much steps to execute manually. So I decided to automate it by writing an Ansible role for it.
Before I started writing Ansible playbook, I needed to find a way to test my code. So I started off with Vagrant spinning up a virtual machine and provisioning it with simple script. This was needed to have similar environment to our actual SonarQube production instance. I was able to incrementally build my Ansible playbook and test it on this VM. Rather soon I learned that working with Vagrant is slowing me down since I needed to reset the environment more than initially expected. So I decided to consider Docker.
I found the official SonarQube Docker image, but was unable to use that since our SonarQube instance is installed on CentOS and the official image used different base image. Also, I needed to customize the specific SonarQube version I wanted to start with. So I created our own Dockerfile with CentOS 6 base and Ansible installed.
This Docker image didn’t need to start SonarQube since I just wanted to verify that proper file changes are made and my Ansible playbook has no errors. One of the first steps of upgrade is shutting down the server anyway.
I mounted my playbook as volume to the container and ran Ansible within that container applying changes to that local environment.
#!/bin/bash # Path of current script TESTS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # Command to test playbook with TEST_COMMAND="cd /playbooks && ansible-playbook $@ -i 'localhost,' -c local upgrade-sonar.yml" # Run container mounting playbooks as volume docker run -v "$TESTS_DIR/../playbooks/:/playbooks" 'sonar-upgrade-test' /bin/bash -c "${TEST_COMMAND}"
Running my playbook inside Docker decreased the feedback loop significantly so I decided that Docker was the way to go. You can take a look all my scripts in GitHub
Moving forward step by step I reached a point where everything seemed to be working and I had a very useful Ansible role at my hands. It is not yet published to Ansible Galaxy, but you can find the source and documentation in GitHub under SonarQube upgrade Ansible role project.
Current Ansible role upgrades SonarQube by creating new installation next to current one. This provides means to rollback, if needed.
At the moment the role performs the following tasks:
Still the process requires a couple manual steps.
Before running the upgrade playbook you should manually ensure that the plugins list that will be installed during upgrade is up to date. You could get the latest supported version numbers from administration view of SonarQube.
As recommended by SonarQube, this Ansible role does not just copy configuration from previous installation, but uses template with variables to create configuration for new instance. This means that before executing upgrade, a developer could compare the template and base configuration from new SonarQube download. Although this is a manual step, it’s easier compared to how it was done before, because the comparison can be done using IDE not just diff tool from command line.
Both of these steps need follow-up manual upgrade as well and I consider these upgrade preparation activities. I managed to simplify and decrease the upgrade process from initial 11 steps to the following 3:
Current configuration expects that SonarQube is configured to use MySQL. Adding additional database engines seems to be an easy improvement.
Atlassian Crowd is configured to be an authentication provider. This could simply be omitted in template if appropriate variable values are missing.
There are more configuration files, just sonar.properties that should all be configured to be loaded from outside of the role.
So contributions are highly welcome and if SonarQube keeps up its release pace then we’ll probably add some ourselves.