SonarQube+Scanners代碼質量管理 原 薦

簡介

sonaqube是一個開源代碼質量管理平臺,致力於持續分析和測量技術質量。

系統構成

SonarQube平臺由4部分組成:

  1. SonarQube服務器
    1. Web服務器的開發者,管理者,瀏覽質量快照和配置SonarQube實例
    2. 基於Elasticsearch搜索服務器從UI向後搜索
    3. 負責處理代碼分析報告計算引擎服務器,並將其保存在數據庫SonarQube
  2. SonarQube數據庫來存儲
  3. 多個插件
  4. 一個或多個SonarQube Scanners:可以與CI服務進行集成

系統構成和相互關係:

2018102315402886362228.png

安裝SonarQube

sonarqube安裝很簡單,下載(直接win下載,然後上傳到linux服務器)安裝包後直接解壓即可:

# 創建sonar用戶及工作目錄
$ useradd sonar

# 解壓
$ unzip sonarqube-6.7.5.zip 

$ cd /home/sonar/sonarqube-6.7.5

# 在當前終端啓動(方便排錯)
$ ./bin/linux-x86-64/sonar.sh console

# 直接在後臺運行
$ ./bin/linux-x86-64/sonar.sh start

說明: sonarqube依賴於Elasticsearch插件,es插件不能用root運行,所以使用普通用戶運行sonar,否則將會出現如下報錯:

Caused by: java.lang.RuntimeException: can not run elasticsearch as root
# Elasticsearch不能用root運行。

參考:https://blog.csdn.net/zdyueguanyun/article/details/79447260

檢查啓動狀態

$ netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      771/sshd            
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      5258/java           
tcp6       0      0 :::22                   :::*                    LISTEN      771/sshd            
tcp6       0      0 127.0.0.1:9092          :::*                    LISTEN      5343/java           
tcp6       0      0 127.0.0.1:35048         :::*                    LISTEN      5443/java           
tcp6       0      0 :::9000                 :::*                    LISTEN      5343/java           
tcp6       0      0 127.0.0.1:9001          :::*                    LISTEN      5273/java 

web管理

默認由9000端口提供UI管理頁面,在瀏覽器訪問:http://192.168.228.129:9000 ,界面如下(管理員:admin,密碼:admin): 20181022154021134084311.png

數據庫配置

創建數據庫並授權

  • 創建一個單獨的庫sonar並授權給sonar用戶:

    mysql> create database sonar;
    mysql> CREATE USER 'sonarqube'@'%' IDENTIFIED BY '123456';
    
  • mysql調優:

    $ vim /etc/my.cnf
    binlog-format=MIXED             # 指定binlog格式爲mixed。默認爲STATEMENT,數據無法正常migration
    innodb_buffer_pool_size = 200M  # 最佳數值爲70%~80%服務器內存
    query_cache_size = 15M
    # 參考:https://www.percona.com/blog/2007/11/01/innodb-performance-optimization-basics/
    
    $ systemctl restart mysqld
    
  • 排錯1:

    • 無法初始化數據庫:

      # 報錯:logs/web.log
      Cannot execute statement: impossible to write to binary log since BINLOG_FORMAT = STATEMENT and at least one table uses a storage engine limited to row-based logging. InnoDB is limited to row-logging when transaction isolation level is READ COMMITTED or READ UNCOMMITTED.
      
    • 解決辦法:

      $ vim /etc/my.cnf
      binlog-format=MIXED             # 指定binlog格式爲mixed。默認爲STATEMENT,數據無法正常
      # 參考:https://www.devside.net/wamp-server/mysql-error-impossible-to-write-to-binary-log-since-binlog_format-statement
      
  • 排錯2:

    • 歷史髒數據衝突:

      # 報錯:
      Web server startup failed: Current version is too old. Please upgrade to Long Term Support version firstly.
      
    • 解決辦法: 清理歷史數據(因首次安裝,直接drop掉sonar表,然後重建),然後重啓sonar即可!

配置sonarqube

系統要求

內核
  • vm.max_map_count 大於等於 262144
  • fs.file-max 大於等於 65536
  • 文件描述符 65536
  • 線程數 2048
sysctl -w vm.max_map_count=262144
sysctl -w fs.file-max=65536
ulimit -n 65536
ulimit -u 2048
系統文件

默認情況下,Elasticsearch數據存儲在 <install_directory> / data中,但不建議用於生產實例。相反,您應該將此數據存儲在其他位置,最好是在具有快速I / O的專用卷中。除了保持可接受的性能之外,這樣做還可以簡化SonarQube的升級。

$ mkdir -p /var/sonarqube/data
$ mkdir -p /var/sonarqube/temp
$ chown -R sonar:sonar /var/sonarqube

配置sonar

$ vim conf/sonar.properties
sonar.jdbc.username=sonar  # 數據庫用戶名
sonar.jdbc.password=123456 # 數據庫密碼
sonar.jdbc.url=jdbc:mysql://192.168.228.129:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false  # 數據庫服務器

sonar.path.data=/var/sonarqube/data
sonar.path.temp=/var/sonarqube/temp

$ ./bin/linux-x86-64/sonar.sh restart
加入systemd管理
$ vim /usr/lib/systemd/system/sonar.service
[Unit]
Description=SonarQube service
After=network.target syslog.target

[Service]
Type=forking

User=sonar
Group=sonar

ExecStart=/bin/bash /home/sonar/sonarqube-6.7.5/bin/linux-x86-64/sonar.sh start
ExecStop=/bin/bash /home/sonar/sonarqube-6.7.5/bin/linux-x86-64/sonar.sh stop

Restart=always

LimitNOFILE=65536


[Install]
WantedBy=multi-user.target

$ systemctl enable sonar  # 開機啓動

部署sonaqube集羣: https://docs.sonarqube.org/display/SONAR/Installing+a+SonarQube+cluster

安裝SonarQube插件

管理插件的方法有如下兩種:

  • 在soanrqube UI界面,通過Marketplace安裝插件;
  • 手動安裝——當sonar主機無法訪問外網時需要進行手動安裝。

Marketplace

  • 前提:
    • sonar服務器能上網;
    • 擁有管理員權限。
  • 安裝方法:
    • 在marketplace找到需要的插件及捆綁的插件;
    • 點擊安裝即可!
    • 安裝完成後點擊“Restart”使新插件生效。

20181023154027540815839.png

手動安裝

ldap集成

直接使用admin用戶在UI界面安裝(可能有點慢): 20181023154027987156744.png

安裝完成後“Restart”生效,可以再sonar服務器查看:

20181025154045332688353.png

如果因網絡問題安裝失敗,可以手動安裝:

配置sonar.properties

參考:https://docs.sonarqube.org/display/SONAR/LDAP+Integration#LDAPIntegration-ConfigurationExamples

示例:

# 配置ldap服務器
sonar.security.realm=LDAP
ldap.url=ldap://ldap.chunyu.club
ldap.bindDn=cn=reader,dc=chunyu,dc=club
ldap.bindPassword=xxxxxxxxxxx
 
# User Configuration
ldap.user.baseDn=OU=People,DC=chunyu,DC=club
ldap.user.request=(uid={login})
ldap.user.realNameAttribute=uid
ldap.user.emailAttribute=mail
 
# Group Configuration
ldap.group.baseDn=ou=groups,DC=chunyu,DC=club
ldap.group.request=(&(objectClass=posixGroup)(memberUid={uid}))

升級SonarQube

官方文檔:https://docs.sonarqube.org/display/SONAR/Upgrading

安裝Scanner

下載地址 ,本地下載然後上傳到sonar服務器。

# 解壓到任何目錄都可以,後續通過系統環境變量配置scanner相關命令
$ unzip sonar-scanner-cli-3.2.0.1227-linux.zip
## 爲了方便管理,將解壓文件放到sonar安裝目錄下:/home/sonar/sonarqube-6.7.5/
$ mv sonar-scanner-3.2.0.1227-linux /home/sonar/sonarqube-6.7.5/sonar-scanner-3.2.0
$ chown -R sonar:sonar /home/sonar/sonarqube-6.7.5/

配置系統環境變量

$ vim /etc/profile
export SONAR_SCANNER_HOME=/home/sonar/sonarqube-6.7.5/sonar-scanner-3.2.0
export PATH=${SONAR_SCANNER_HOME}/bin:${PATH}

$ source /etc/profile

# 檢查配置結果
$ sonar-scanner -v
INFO: Scanner configuration file: /home/sonar/sonarqube-6.7.5/sonar-scanner-3.2.0/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-693.el7.x86_64 amd64
# Success!

SonarQube使用指南

創建sonar-project.properties文件

進入要進行代碼分析的項目根目錄,新建sonar-project.properties文件,內容如下:

# must be unique in a given SonarQube instance
sonar.projectKey=test:python
# this is the name displayed in the SonarQube UI
sonar.projectName=chunyu_community  # 要展示在UI界面的項目名稱
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set. 
# If not set, SonarQube starts looking for source code from the directory containing 
# the sonar-project.properties file.
sonar.sources=./                  # 項目文件目錄
 
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

執行命令:

$ sonar-scanner

查看結果

待上述命令執行結束後便可以在UI界面看到掃描結果:

20181023154029770619481.png

  • 進入chunyu_community項目,概覽: 20181024154038725542549.png
  • 點擊 issues 查看具體bug: 20181024154038754011216.png
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章