Sonar on AWS

Contents

About

Sonar (originally an acronym for SOund Navigation And Ranging) is a technique that uses sound propagation (usually underwater, as in submarine navigation) to navigate, communicate with or detect objects on or under the surface of the water, such as other vessels.
Sonar is an open source software quality platform. Sonar uses various static code analysis tools such as CheckstylePMDFindBugs to extract software metrics, which then can be used to improve software quality. It runs as a stand-alone web server that new code can be pushed to via a Maven command. It also stores a history of previous builds in an in-memory or MySQL database.

Features

  • All projects at a glance
  • Drill down to source code
  • Coding rules
  • Unit tests
  • Standard metrics 
  • Time machine (i.e. history)
  • Maven ready
  • Easy
  • Leverage existing components
  • Plugins
  • Security
  • Single source of truth for rules
  • Exporable to local IDEs

Details

AWS RegionSydney
Instance Namesonar
Instance IDi-c82837f2
Instance Typem1.micro
Public DNSec2-54-253-27-220.ap-southeast-2.compute.amazonaws.com
Security Groupsonar (all 22, 8080, 9000 and 9092)
Key Pair
sonar-key (sonar-private.ppk)
Key Pair Passphrase
Sonar Database
MySQL DB (ec2-54-253-27-220.ap-southeast-2.compute.amazonaws.com:3036) / In-memory H2
sonar / sonar / root / 
Sonar Dashboard
username: admin
password: admin
Stop/Start Sonar
/etc/init.d/sonar stop
/etc/init.d/sonar start
Sonar Logs/opt/sonar/logs/
Sonar Conf/opt/sonar/conf/sonar.properties
ec2-54-253-27-220.ap-southeast-2.compute.amazonaws.com

Installing

  • Spin up an EC2 instance on AWS 
  • Make sure to link a security group and give port/ip access on it. (9000 and 9002 would suffice or give ALL)
  • Connect to the Instance using putty and issue the following commands:
# download  
sudo wget -O /etc/yum.repos.d/sonar.repo http://downloads.sourceforge.net/project/sonar-pkg/rpm/sonar.repo
# install
yum install sonar
# set permissions
sudo chmod 755 /etc/init.d/sonar
# start
/etc/init.d/sonar start

Maven

Sonar maven plugin operates as a very light bootstrap mechanism whose only responsibility is to download all the business logic to the Sonar web server and execute it.
A Sonar web server embeds a Maven repository which exposes to the Maven plugin all the business logic. Below is a simple diagram to better understand the mechanism : (http://www.sonarsource.org/we-had-a-dream-mvn-sonarsonar/)

Follow these steps to push code to the remote Sonar server:
  1. Open a command prompt 
  2. Navigate to the project folder containing a pom.xml (e.g. c:\workspace\phoenix-source\finance-service)
  3. Execute the following Maven goal: (When running the h2 database)
mvn sonar:sonar -Dsonar.jdbc.url=jdbc:h2:tcp://ec2-54-253-27-220.ap-southeast-2.compute.amazonaws.com/:9092/sonar -Dsonar.host.url=http://ec2-54-253-27-220.ap-southeast-2.compute.amazonaws.com:9000
Maven Configuration for My SQL Database
<profiles>
    <profile>
        <id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <sonar.jdbc.url>jdbc:mysql://ec2-54-253-27-220.ap-southeast-2.compute.amazonaws.com:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
            </sonar.jdbc.url>
            <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>
            <sonar.jdbc.username>sonar</sonar.jdbc.username>
            <sonar.jdbc.password>sonar</sonar.jdbc.password>
            <!-- optional URL to server. Default value is http://localhost:9000 -->
            <sonar.host.url>
              http://ec2-54-253-27-220.ap-southeast-2.compute.amazonaws.com:9000
            </sonar.host.url>
        </properties>
    </profile>
 </profiles>
This settings should be done on the settings.xml make sure maven uses this file or include it in the path as follows,
When running on the MySql database execute the following maven goal : 
mvn sonar:sonar -s D:\_Work\Repository\MavenLocalRepo\settings.xml
Issues and Solutions
  • On Enabling all rules on sonar server and executing the maven goal sonar:sonar there is a build failure as follows,
ERROR
Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.0:sonar (default-cli) on project finance-service: Can not execute Sonar: Can not execute Checkstyle: cannot initialize module Header - property 'headerFile' is missing or invalid in module Header ->
Reason: You cannot turn on all the rules as some of them require some settings to be working.
The following rules need to be disabled in sonar server,
  • Avoid too deep inheritenace tree
  • Header
  • Regexp
  • Regexp Header
  • Regexp Multiline
  • Regexp SingleLine
  • Regexp SingleLine Java
  • XPath rule template

  • MySQL

Instructions for installing a local MySQL database for Sonar.
sudo yum install mysql
sudo yum install mysql-server
sudo yum install mysql-devel
sudo chgrp -R mysql /var/lib/mysql
chmod -R 770 /var/lib/mysql
sudo service mysqld start
/usr/bin/mysqladmin -u root password l3arnings3at
mysql -u root -h localhost -p
#
# Create Sonar database and user.
#
# Command: mysql -u root -p < create_database.sql
#
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;
#
# Drop Sonar database and user.
#
# Command: mysql -u root -p < drop_database.sql
#
DROP DATABASE IF EXISTS sonar;
DROP USER 'sonar'@'localhost';
DROP USER 'sonar'@'%';
Edit conf/sonar.properties to configure database - ( /opt/sonar/conf/sonar.properties )
Uncomment MySql settings and comment in memory db settings

Some useful commands on linux

ps aux | grep mysql
ps aux | grep sonar

Comments

Popular posts from this blog

API design best practices

DB Connection Issues

Reading Excel Sheet on client side and processing it