m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

centos6.xにsonarqubeをインストール

品質管理してみる

ソースコードは書いても基本、コードに対する評価はあまりしてもらったことがない。
まあ、そんな感じの仕事ばかりなのでいろいろ気になってきた

ツール

http://www.sonarqube.org/
これを入れてみる

環境

OS

VagrantのCentOS6.x

Java
wget --no-check-certificate --no-cookies - --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.rpm
sudo yum -y install jdk-8u5-linux-x64.rpm
MySQL

5.6

sudo yum -y install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
sudo yum -y install mysql-community-*
sudo service mysqld start
sudo chkconfig mysqld on
PHP

5.6

sudo rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof

Qonarqubeサーバインストール

ダウンロード、解凍
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.2.zip
unzip sonarqube-5.2.zip
配置
sudo mv sonarqube-5.2 /usr/local/sonarqube
MySQLデータベース
mysql -uroot -e "create database sonar;"
mysql -uroot -e "CREATE USER 'sonar' IDENTIFIED BY 'sonar';"
mysql -uroot -e "GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';"
mysql -uroot -e "GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';"
環境設定ファイル修正

/usr/local/sonarqube/conf/sonar.properties

#--------------------------------------------------------------------------------------------------
# DATABASE
#
# IMPORTANT: the embedded H2 database is used by default. It is recommended for tests but not for
# production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092


#----- MySQL 5.x
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
BINLOG_FORMATを変更
[vagrant@localhost ~]$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW GLOBAL VARIABLES LIKE 'binlog_format';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)

mysql> SET GLOBAL binlog_format = 'MIXED';
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> SHOW GLOBAL VARIABLES LIKE 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
1 row in set (0.00 sec)

mysql> quit
Bye
[vagrant@localhost ~]$

※標準の状態では起動後にエラーになるおそれがある

起動
sudo /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start

f:id:m_shige1979:20160107000609p:plain

QonarqubeScannerをインストール

ダウンロード、解凍
wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
unzip sonar-runner-dist-2.4.zip
配置
sudo mv sonar-runner-2.4 /usr/local/sonar-runner
sudo echo 'export PATH="/usr/local/sonar-runner/bin:$PATH"' >> ~/.bash_profile
source  ~/.bash_profile
環境設定ファイル修正

/usr/local/sonar-runner/conf/sonar-runner.properties

#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://192.168.33.10:9000

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor

#----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

#----- Security (when 'sonar.forceAuthentication' is set to 'true')
sonar.login=admin
sonar.password=admin
自動起動
sudo ln -s /usr/local/sonarqube/bin/linux-x86-64/sonar.sh /etc/init.d/sonar
sudo chkconfig sonar on
プラグインのダウンロード

f:id:m_shige1979:20160107002701p:plain
phpや他のプラグインもインストールする

サンプルの解析

サンプルをダウンロード
[vagrant@localhost ~]$ git clone https://github.com/SonarSource/sonar-examples.git
Initialized empty Git repository in /home/vagrant/sonar-examples/.git/
remote: Counting objects: 7469, done.
remote: Total 7469 (delta 0), reused 0 (delta 0), pack-reused 7469
Receiving objects: 100% (7469/7469), 7.52 MiB | 553 KiB/s, done.
Resolving deltas: 100% (3202/3202), done.
[vagrant@localhost ~]$
phpのやつへ移動
cd sonar-examples/projects/languages/php/php-sonar-runner
[vagrant@localhost php-sonar-runner]$ ll
合計 16
-rw-rw-r-- 1 vagrant vagrant  451  1月  6 15:19 2016 README.md
-rw-rw-r-- 1 vagrant vagrant  331  1月  6 15:19 2016 sonar-project.properties
drwxrwxr-x 2 vagrant vagrant 4096  1月  6 15:19 2016 src
-rw-rw-r-- 1 vagrant vagrant  272  1月  6 15:19 2016 validation.txt
[vagrant@localhost php-sonar-runner]$
プロジェクトファイル

/home/vagrant/sonar-examples/projects/languages/php/php-sonar-runner/sonar-project.properties

# Required metadata
sonar.projectKey=org.sonarqube:php-simple-sq-scanner
sonar.projectName=PHP :: Simple Project :: SonarQube Scanner
sonar.projectVersion=1.0

# Comma-separated paths to directories with sources (required)
sonar.sources=src

# Language
sonar.language=php

# Encoding of the source files
sonar.sourceEncoding=UTF-8
起動
sonar-runner
15:34:08.067 INFO  - Analysis reports sent to server in 527ms
15:34:08.068 INFO  - ANALYSIS SUCCESSFUL, you can browse http://192.168.33.10:9000/dashboard/index/org.sonarqube:php-simple-sq-scanner
15:34:08.068 INFO  - Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report.
15:34:08.068 INFO  - More about the report processing at http://192.168.33.10:9000/api/ce/task?id=AVIXkuEll9B3bJ3AZgEx
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
Total time: 9.238s
Final Memory: 8M/102M
INFO: ------------------------------------------------------------------------

f:id:m_shige1979:20160107003549p:plain

できた
とりあえず、ここまで

所感

マシンのパワーが多少いるけど、結果を視覚的に判定できるので結構よさげ…
英語で書かれているので問題の部分のコードを理解するのに時間がかかるかもしれないけど〜