techium

このブログは何かに追われないと頑張れない人たちが週一更新をノルマに技術情報を発信するブログです。もし何か調査して欲しい内容がありましたら、@kobashinG or @muchiki0226 までいただけますと気が向いたら調査するかもしれません。

vagrantでSonarQubeを立ち上げる(環境構築編

Motivation

ソースをCI回すに当たって、複雑度とか重複度とか規模感とか、トレースできるといいなと思って探してたらSonarQube(旧Sonar)というものを見つけたのでサーバー立ち上げまでを進める。 次回はAndroidのプロジェクトに対しての実施についてまとめていきたい。

環境

Macbook Pro(EI capitan) Vagrant(Ubuntu16.04)

vagrantの新しいBoxを作成する

今回はせっかくなので、最近リリースされた16.04を利用してすすめる。

$ vagrant init ubuntu/xenial64

SoanrQubeはデフォルト9000番ポートで動作するので、そのポートフォワーディングに設定と メモリが少ないと安定軌道しないので2Gのメモリ要求をしておく。

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/xenial64"

  config.vm.network "forwarded_port", guest: 9000, host: 19000

  config.vm.provider "virtualbox" do |vb|
      vb.cpus = 2
      vb.memory = "2048"
  end
 end

もろもろの設定が追えたら、vagrant up; vagrant sshする。

SonarQube向けの環境を整える

必要なインストール物は以下の通り

http://docs.sonarqube.org/display/SONAR/Requirements

このエントリでは以下をインストールした。

  • java (OpenJDK 8)
  • MySQL
$ sudo apt install python-software-properties
$ sudo add-apt-repository ppa:webupd8team/java 
$ sudo apt update
$ sudo apt install oracle-java8-installer mysql-server

データベース設定

SonarQubeが利用するデータベースの設定を行っておく。 ログイン後、以下のクエリを叩いておく。

$ mysql -u root
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> exit

SonarQubeの設定

まずはSonarQubeをダウンロードしてくる

SonarQube » Download

$ wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.zip
$ unzip sonarqube-5.6.zip
$ cd sonarqube-5.6

ダウンロード解凍の後、コンフィグ設定を行う 下記項目を参考にどうぞ。必要な環境ではProxyの設定もここで行う

#--------------------------------------------------------------------------------------------------
# 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

#----- MySQL 5.6 or greater
# 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


#--------------------------------------------------------------------------------------------------
# WEB SERVER

sonar.web.javaOpts=-Xmx768m -Xms256m -XX:+HeapDumpOnOutOfMemoryError -Djava.net.preferIPv4Stack=true

sonar.web.host=0.0.0.0

sonar.web.context=/sonar

sonar.web.port=9000

#--------------------------------------------------------------------------------------------------
# UPDATE CENTER

# Update Center requires an internet connection to request http://update.sonarsource.org
# It is enabled by default.
#sonar.updatecenter.activate=true

# HTTP proxy (default none)
#http.proxyHost=
#http.proxyPort=
# HTTPS proxy (defaults are values of http.proxyHost and http.proxyPort)
#https.proxyHost=
#https.proxyPort=

# NT domain name if NTLM proxy is used
#http.auth.ntlm.domain=

# SOCKS proxy (default none)
#socksProxyHost=
#socksProxyPort=

# Proxy authentication (used for HTTP, HTTPS and SOCKS proxies)
#http.proxyUser=
#http.proxyPassword=

起動

設定がうまくいっていれば、下記コマンドで起動できる。

$ sudo ./bin/linux-x86-64/sonar.sh start

起動後にホストPCからlocalhost:19000/sonarにアクセスするとページが確認できる。 デフォルトでは admin/admin でログインできる

f:id:kobashin__G:20160704053748p:plain

繋がらないときは?

手元の環境では、メモリが足りなかったり、DBの設定がおかしかったりで色々と立ち上がらない日々が続いた。 おかしいなと思ったら、起動をDaemonでなくコンソール上で直接起動する以下のコマンドで試すとよい。

$ sudo ./bin/linux-x86-64/sonar.sh console

またエラーログは、sonarqube以下のディレクトリ直下に保存されている。 ログファイルの冒頭にエラー詳細が表示されているので、確認してつぶしていくといい。

さて、次回はAndroidのプロジェクトつないで色々見れるようにする。