Note: this is an excerpt extracted from my talk at Red Hat Developer Day London. You can see more about the talk in my post here:
Sonar is a popular code profiler and dashboard that excels when used along a Continuous Integration engine:
- Seamless integration with Maven.
- Leverages best-of-breed tools as Checkstyle, PMD or FindBugs.
- Configurable quality profiles.
- Re-execution of tests and test code coverage (UT, IT).
- Design Structure Matrix analysis.
- Flexible and highly customisable dashboard.
- Actions plans / peer reviews.
- Historic views / run charts.
- Can be used with Java, .Net, C/C++, Groovy, PHP,…
Assuming you already have an OpenShift account (see my previous post here for details: http://deors.wordpress.com/2011/12/13/red-hat-openshift/), the steps to install Sonar in OpenShift as a DIY (do-it-yourself) application are the following:
- Create a DIY application
- Add MySQL cartridge
- Unzip Sonar distribution
- Update action hooks
- Upload the changes
- Configure some existing build job in Jenkins
1. Create a DIY application
To create a DIY application simply execute a command like this in your console (the same can be achieved using OpenShift web console):
>rhc app create -a sonar -t diy-0.1
After a few seconds, you should get some important information about the new application, so take note of them. In my case they were something like this (note that this corresponds to an OpenShift origin installation):
UUID: 4cd9eacb71b34401b81ea712352ffccb Git URL: ssh://firstname.lastname@example.org/~/git/sonar.git/ Public URL: http://sonar-demo.example.com/
2. Add MySQL cartridge
Next step is to add a MySQL cartridge to the application we’ve just created. Sonar will use this database to store the information collected during analysis, so take note of database URL, user and password. Let’s create the database using the following command:
>rhc app cartridge add -a sonar -c mysql-5.1
In my case, the database information was something like this:
Root User: admin Root Password: VyikSHzlLvjx Database Name: sonar Connection URL: mysql://127.0.251.129:3306/
3. Unzip Sonar distribution
For next step, download a blank Sonar installation from Sonar’s web site (http://www.sonarsource.org/). In my case I was working with Sonar 3.1.1. You can start from an existing Sonar installation if you like.
Next, unzip Sonar Zip file so all files and folders are placed inside a sonar-x.x.x folder, like this:
Once Sonar is unzipped, edit the configuration file conf/sonar.properties so it points to the MySQL database just created:
... sonar.web.host: 127.0.251.129 sonar.web.port: 8080 sonar.web.context: / sonar.jdbc.username: admin sonar.jdbc.password: VyikSHzlLvjx sonar.jdbc.driver: com.mysql.jdbc.Driver sonar.jdbc.url: jdbc:mysql://127.0.251.129:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true ...
4. Update action hooks
Next step is to update action hooks so OpenShift broker knows how to start and stop the Sonar process. We will leverage the scripts that came with Sonar, which means we will be using Jetty as the container where Sonar will be executed.
First, update start action hook that can be found in folder .openshift/action_hooks:
#!/bin/bash cd $OPENSHIFT_REPO_DIR/sonar-3.1.1/bin/linux-x86-64 chmod 755 wrapper sh ./sonar.sh start
Next, update stop action hook in the same folder:
#!/bin/bash cd $OPENSHIFT_REPO_DIR/sonar-3.1.1/bin/linux-x86-64 sh ./sonar.sh stop
5. Upload the changes
Now that we are ready to go, commit and push the changes back to OpenShift. After a few minutes (first time Sonar will initialize the database schema and master data) you should be able to access Sonar dashboard.
6. Configure some existing build job in Jenkins
To verify that everything is working fine and, more important, to do something useful with Sonar, you may want to add Sonar analysis to any of your existing jobs in Jenkins. For this to work, you will need to pass the right configuration properties referring to Sonar web dashboard and database. In my case it was something like this:
# leave all initial content of Jenkins build script up to when # Maven is executed to package the app for OpenShift mvn --global-settings $OPENSHIFT_MAVEN_MIRROR clean test export SONAR_JDBC="-Dsonar.jdbc.url=jdbc:mysql://127.0.251.129:3306/sonar?useUnicode=true&characterEncoding=utf8" export SONAR_DRIVER=-Dsonar.jdbc.driver=com.mysql.jdbc.Driver export SONAR_DBUSER=-Dsonar.jdbc.username=admin export SONAR_DBPASS=-Dsonar.jdbc.password=VyikSHzlLvjx export SONAR_URL=-Dsonar.host.url=http://sonar-demo.example.com export SONAR_OPTS="$SONAR_JDBC $SONAR_DRIVER $SONAR_DBUSER $SONAR_DBPASS $SONAR_URL" mvn --global-settings $OPENSHIFT_MAVEN_MIRROR sonar:sonar $SONAR_OPTS # rest of original Jenkins build script follows
Launch your job manually or commit some code and… magic! Sonar is now analysing your application and giving you valuable insights!
And this is just the beginning: you can install additional Sonar plug-ins and make more elaborate build cycles:
You may for example, use the Build Breaker plug-in. Build Breaker let’s you flag a build as a failure if some quality thresholds are not met. For example, if you analyse the application using a quality profile that sets a minimum code coverage of 60%, or a maximum average complexity of 4.0, the Sonar build will break (and the code will not be deployed into OpenShift) if some of these thresholds are not met.
You may as well launch integration tests (i.e. Selenium, Arquillian) and get code coverage for integration tests as well.
And why not add performance metrics? Using JMeter and the Sonar JMeter plug-in you can run some load tests in your app and add results to Sonar dashboard!
Agile with style!