1. 程式人生 > >SonarQube+Scanners程式碼質量管理

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執行。

檢查啟動狀態

$ 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  # 開機啟動

安裝SonarQube外掛

管理外掛的方法有如下兩種:

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

Marketplace

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

20181023154027540815839.png

手動安裝

  • 下載所需版本的外掛到 *$SONARQUBE_HOME/extensions/plugins ,刪除舊版本的外掛;
  • 然後重啟sonar服務:systemctl restart sonar 。

ldap整合

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

安裝完成後“Restart”生效,可以再sonar伺服器檢視:

![image-20181023160837712](/Users/chunyu/Library/Application Support/typora-user-images/image-20181023160837712.png)

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

配置sonar.properties

示例:

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

安裝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介面看到掃描結果: