代碼質量管理平臺之SonarQube安裝部署

  1、簡介javascript

  Sonar是一個用於代碼質量管理的開放平臺,經過插件機制,sonar能夠收集不一樣的測試工具,代碼分析工具,以及持續集成工具。與持續集成工具(好比jenkins)不一樣,sonar並非簡單地把不一樣的代碼檢查工具結果直接顯示在web頁面,而是經過不一樣的插件對這些結果進行加工處理,經過量化的方式度量代碼質量的變化,從而能夠方便地對不一樣規模和種類的工程進行代碼質量管理。在對其餘工具的支持方面,sonar不只提供了對IDE的支持,能夠在Eclipse和Intellij IDEA這些工具裏聯機查看結果;同時sonar還對大量的持續集成工具提供了接口支持,能夠很方便地在持續集成中使用sonar,此外,sonar的插件還能夠對java之外的其餘編程語言提供支持,對國際化及報告文檔也有很良好的支持;官方網站https://www.sonarqube.orgphp

  2、sonar平臺部署java

  sonarqube是一款用java語言編寫的程序,它主要做用是提供一個web界面,展現掃描分析結果以及系統管理,插件管理等;掃描代碼仍是sonar-scanner這個插件作的,它的工做原理是sonar-scanner經過識別項目中的sonar-project.properties配置文件中定義的內容,把對應的項目源碼進行掃描,把掃描後的結果保存到指定的數據庫;而後sonarqube經過鏈接配置的數據庫,把sonar-scanner存入數據庫中的數據加載到web界面,從而用戶就能夠經過web界面查看掃描的項目源碼的結果;node

  一、安裝數據庫python

  上傳mysql5.6安裝包和腳本mysql

[root@node03 ~]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%     256 bytes  256 bytes/s 00:00:01       0 Errors
  100%  321268 KB 35696 KB/s 00:00:09       0 Errors.gz...
  100%       1 KB    1 KB/s 00:00:01       0 Errors

[root@node03 ~]# ll
total 321280
-rw-r--r-- 1 root root       256 Aug 20  2019 my.cnf
-rw-r--r-- 1 root root 328979165 Aug 20  2019 mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz
-rw-r--r-- 1 root root      1470 Aug 20  2019 mysql-install.sh
[root@node03 ~]# 

  安裝腳本linux

#!/bin/bash
DIR=`pwd`
NAME="mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz"
FULL_NAME=${DIR}/${NAME}
DATA_DIR="/data/mysql"

yum install vim gcc gcc-c++ wget autoconf  net-tools lrzsz iotop lsof iotop bash-completion -y
yum install curl policycoreutils openssh-server openssh-clients postfix -y

if [ -f ${FULL_NAME} ];then
    echo "安裝文件存在"
else
    echo "安裝文件不存在"
    exit 3
fi
if [ -h /usr/local/mysql ];then
    echo "Mysql 已經安裝"
    exit 3 
else
    tar xvf ${FULL_NAME}   -C /usr/local/src
    ln -sv /usr/local/src/mysql-5.6.42-linux-glibc2.12-x86_64  /usr/local/mysql
    if id  mysql;then
        echo "mysql 用戶已經存在,跳過建立用戶過程"
    fi
        useradd  mysql  -s /sbin/nologin
    if  id  mysql;then
        chown  -R mysql.mysql  /usr/local/mysql/* -R
        if [ ! -d  /data/mysql ];then
            mkdir -pv /data/mysql /var/lib/mysql && chown  -R mysql.mysql  /data   -R
            /usr/local/mysql/scripts/mysql_install_db  --user=mysql --datadir=/data/mysql  --basedir=/usr/local/mysql/
        cp  /usr/local/src/mysql-5.6.42-linux-glibc2.12-x86_64/support-files/mysql.server /etc/init.d/mysqld
        chmod a+x /etc/init.d/mysqld
         cp ${DIR}/my.cnf   /etc/my.cnf
        ln -sv /usr/local/mysql/bin/mysql  /usr/bin/mysql
            ln -sv /data/mysql/mysql.sock  /var/lib/mysql/mysql.sock
        /etc/init.d/mysqld start
    else
            echo "MySQL數據目錄已經存在,"
            exit 3
    fi
    fi
fi
View Code

  安裝mysqlc++

[root@node03 ~]# bash mysql-install.sh 

  提示:自動安裝腳本執行完成後,它會自動啓動mysql,若是啓動成功,說明mysql已經安裝完成;git

  驗證:查看msyql是否啓動,是否能夠鏈接到mysql數據庫?web

  建立數據庫和用戶受權

mysql>  CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.05 sec)

mysql> GRANT ALL ON sonar.* TO sonar@"192.168.0.%" IDENTIFIED BY "admin"; 
Query OK, 0 rows affected (0.02 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> 

  驗證:使用建立的用戶鏈接數據庫,看看是否能夠鏈接?

[root@node03 ~]# mysql -usonar -padmin -h192.168.0.43
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'sonar'@'node03.test.org' (using password: YES)
[root@node03 ~]# 

  提示:這裏主要是mysql把ip地址反解成主機名了;

  配置mysql,忽略ip地址反解成主機名

  重啓mysql,再次測試新建的用戶是否可以鏈接到mysql?

  到此mysql安裝和測試就完成了

  二、安裝jdk

[root@node03 ~]# yum install -y java-1.8.0-openjdk-devel

  驗證java版本

[root@node03 ~]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
[root@node03 ~]# 

  提示:sonar 依賴於 java 環境,並且 java 版本必須是 1.8 版本或更高,不然 sonar 啓動失敗;

  三、上傳sonarqube安裝包,安裝sonarqube

  提示:在官方下載太慢了,我這裏下載好了,直接傳上來的;如今最新版本7.9不支持mysql;

  解壓壓縮包

[root@node03 src]# unzip sonarqube-6.5.zip 

  新建軟鏈接

[root@node03 src]# ll
total 139932
drwxr-xr-x 13 root root       205 Oct 15 23:27 mysql-5.6.42-linux-glibc2.12-x86_64
drwxr-xr-x 10 root root       120 Aug  1  2017 sonarqube-6.5
-rw-r--r--  1 root root 143286376 Aug 20  2019 sonarqube-6.5.zip
[root@node03 src]# ln -sv /usr/local/src/sonarqube-6.5 /usr/local/sonaqube
‘/usr/local/sonaqube’ -> ‘/usr/local/src/sonarqube-6.5’
[root@node03 src]# ll /usr/local/
total 0
drwxr-xr-x. 2 root root  6 Nov  5  2016 bin
drwxr-xr-x. 2 root root  6 Nov  5  2016 etc
drwxr-xr-x. 2 root root  6 Nov  5  2016 games
drwxr-xr-x. 2 root root  6 Nov  5  2016 include
drwxr-xr-x. 2 root root  6 Nov  5  2016 lib
drwxr-xr-x. 2 root root  6 Nov  5  2016 lib64
drwxr-xr-x. 2 root root  6 Nov  5  2016 libexec
lrwxrwxrwx  1 root root 50 Oct 15 23:27 mysql -> /usr/local/src/mysql-5.6.42-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root  6 Nov  5  2016 sbin
drwxr-xr-x. 5 root root 49 Sep 15 20:33 share
lrwxrwxrwx  1 root root 28 Oct 15 23:39 sonaqube -> /usr/local/src/sonarqube-6.5
drwxr-xr-x. 4 root root 95 Oct 15 23:39 src
[root@node03 src]# 

  配置sonarqube鏈接192.168.0.43上的數據庫,並讓其web端口監聽在本機全部地址的9000端口

[root@node03 sonaqube]# grep ^[a-z] conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=admin
sonar.jdbc.url=jdbc:mysql://192.168.0.43:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.web.host=0.0.0.0
sonar.web.port=9000
[root@node03 sonaqube]# 

  提示:sonar.jdbc.username是指鏈接數據庫的用戶名;sonar.jdbc.password指鏈接數據庫的用戶名密碼;sonar.jdbc.url指鏈接數據庫的驅動名以及數據庫地址,端口和數據庫名稱,後面是指定的參數保持默認便可;sonar.web.host用於指定監聽的ip地址,0.0.0.0表示監聽本機全部可用地址;sonar.web.port指定監聽的端口;

  啓動sonarqube

[root@node03 sonaqube]# bin/linux-x86-64/sonar.sh --help
Usage: bin/linux-x86-64/sonar.sh { console | start | stop | restart | status | dump }
[root@node03 sonaqube]# bin/linux-x86-64/sonar.sh start
Starting SonarQube...
Started SonarQube.
[root@node03 sonaqube]#

  驗證:查看9000端口是否處於監聽?

[root@node03 ~]# ss -tnl
State      Recv-Q Send-Q         Local Address:Port                        Peer Address:Port              
LISTEN     0      128                        *:22                                     *:*                  
LISTEN     0      100                127.0.0.1:25                                     *:*                  
LISTEN     0      128                       :::22                                    :::*                  
LISTEN     0      100                      ::1:25                                    :::*                  
LISTEN     0      128                       :::3306                                  :::*                  
[root@node03 ~]# 

  提示:9000端口並無監聽;

  查看日誌

  提示:日誌裏提示說內存不足;

  查看內存使用狀況

[root@node03 sonaqube]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1823        1662          73           0          87          30
Swap:          1023         687         336
[root@node03 sonaqube]# 

  提示:2G內存還剩73M,交換分區仍是用了687M,內存的確有點小;解決辦法只有從新分配內存;我這裏是虛擬機,直接調整內存便可;

  調整好內存後,在啓動sonarqube,看看是否啓動起來?

  提示:調整內存爲4G,勉強啓動起來;因此若是數據庫和sonarqube在一臺主機上,建議將內存調到8G,甚至更高;

  訪問9000端口

  登陸試試

  提示:點擊登陸,彈出一個輸入token的界面,咱們能夠忽略它,直接進入便可;到此sonarqube服務就正常跑起來了;

  四、安裝掃描器 sonar-scanner

  上傳安裝包,並解壓

[root@node03 ~]# cd /usr/local/src/
[root@node03 src]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%     489 KB  489 KB/s 00:00:01       0 Errorsp...

[root@node03 src]# ll
total 140424
drwxr-xr-x 13 root root       205 Oct 15 23:27 mysql-5.6.42-linux-glibc2.12-x86_64
drwxr-xr-x 10 root root       146 Oct 15 23:43 sonarqube-6.5
-rw-r--r--  1 root root 143286376 Aug 20  2019 sonarqube-6.5.zip
-rw-r--r--  1 root root    501750 Aug 20  2019 sonar-scanner-2.6.1.zip
[root@node03 src]# unzip sonar-scanner-2.6.1.zip 
Archive:  sonar-scanner-2.6.1.zip
   creating: sonar-scanner-2.6.1/bin/
  inflating: sonar-scanner-2.6.1/bin/sonar-scanner  
  inflating: sonar-scanner-2.6.1/bin/sonar-runner  
   creating: sonar-scanner-2.6.1/conf/
  inflating: sonar-scanner-2.6.1/conf/sonar-scanner.properties  
   creating: sonar-scanner-2.6.1/lib/
  inflating: sonar-scanner-2.6.1/lib/sonar-scanner-cli-2.6.1.jar  
  inflating: sonar-scanner-2.6.1/bin/sonar-runner.bat  
  inflating: sonar-scanner-2.6.1/bin/sonar-scanner.bat  
[root@node03 src]# 

  建立軟鏈接

  配置 sonar-scanner

  提示:掃描器主要配置它須要鏈接的數據相關配置,以及soanrqube服務的地址;掃描器不須要啓動,它的工做方式是在對應項目裏sonar-porject.properties配置文件所在目錄運行sonar-scanner,它默認會去找項目中的sonar-porject.properties配置文件,進行掃描項目源代碼;

  測試:上傳測試代碼進行掃描

  解壓,並進入到項目目錄,進入sonar-project.properties文件所在目錄

  提示:sonar.projectKey、sonar.projectName、sonar.projectVersion這三個能夠根據本身的項目實際狀況來定,這個只是標記項目的,不影響掃描結果;最重要的是要告訴掃描器去哪裏找源碼;sonar.sources用來指定源碼位置,一般這裏都是一個相對當前目錄的目錄;sonar.language這個是指定項目的語言,掃描器經過這裏的配置,肯定用哪一種插件去掃描;sonar.sourceEncoding這個是指定源碼的編碼;

  在sonar-project.properties配置文件所在目錄執行sonar-scanner命令進行掃描

[root@node03 python-sonar-runner]# ll
total 12
-rw-r--r-- 1 root root 461 Jul 25  2016 README.md
-rw-r--r-- 1 root root 338 Jul 25  2016 sonar-project.properties
drwxr-xr-x 5 root root  93 Jul 25  2016 src
-rw-r--r-- 1 root root 290 Jul 25  2016 validation.txt
[root@node03 python-sonar-runner]# /usr/local/sonar-scanner/bin/sonar-scanner 
INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /root/sonar-examples-master/projects/languages/python/python-sonar-runner/sonar-project.properties
INFO: SonarQube Scanner 2.6.1
INFO: Java 1.8.0_262 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-693.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Load global settings
INFO: Load global settings (done) | time=148ms
WARN: Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
INFO: User cache: /root/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=80ms
INFO: Download sonar-csharp-plugin-5.10.1.1411.jar
INFO: Download sonar-python-plugin-1.8.0.1496.jar
INFO: Download sonar-java-plugin-4.12.0.11033.jar
INFO: Download sonar-scm-git-plugin-1.2.jar
INFO: Download sonar-flex-plugin-2.3.jar
INFO: Download sonar-xml-plugin-1.4.3.1027.jar
INFO: Download sonar-php-plugin-2.10.0.2087.jar
INFO: Download sonar-scm-svn-plugin-1.5.0.715.jar
INFO: Download sonar-javascript-plugin-3.1.1.5128.jar
INFO: SonarQube server 6.5.0
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=41ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=42ms
INFO: Load active rules
INFO: Load active rules (done) | time=782ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=86ms
WARN: SCM provider autodetection failed. No SCM provider claims to support this project. Please use sonar.scm.provider to define SCM of your project.
INFO: Publish mode
INFO: Project key: org.sonarqube:python-simple-sonar-scanner
INFO: -------------  Scan Python :: Simple Project : SonarQube Scanner
INFO: Load server rules
INFO: Load server rules (done) | time=49ms
INFO: Language is forced to py
INFO: Base dir: /root/sonar-examples-master/projects/languages/python/python-sonar-runner
INFO: Working dir: /root/sonar-examples-master/projects/languages/python/python-sonar-runner/.sonar
INFO: Source paths: src
INFO: Source encoding: UTF-8, default locale: en_US
INFO: Index files
INFO: 9 files indexed
INFO: Quality profile for py: Sonar way
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=7ms
INFO: Sensor Python Squid Sensor [python]
INFO: Python unit test coverage
INFO: Python integration test coverage
INFO: Python overall test coverage
INFO: Sensor Python Squid Sensor [python] (done) | time=218ms
INFO: Sensor SonarJavaXmlFileSensor [java]
INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=1ms
INFO: Sensor Analyzer for "php.ini" files [php]
INFO: Sensor Analyzer for "php.ini" files [php] (done) | time=2ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=11ms
INFO: Sensor CPD Block Indexer
INFO: Sensor CPD Block Indexer (done) | time=14ms
INFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.
INFO: 5 files had no CPD blocks
INFO: Calculating CPD for 4 files
INFO: CPD calculation finished
INFO: Analysis report generated in 50ms, dir size=54 KB
INFO: Analysis reports compressed in 11ms, zip size=27 KB
INFO: Analysis report uploaded in 520ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://192.168.0.43:9000/dashboard/index/org.sonarqube:python-simple-sonar-scanner
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://192.168.0.43:9000/api/ce/task?id=AXUtFHMGxcHkiMKcN6ov
INFO: Task total time: 3.414 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 6.631s
INFO: Final Memory: 47M/181M
INFO: ------------------------------------------------------------------------
[root@node03 python-sonar-runner]# 

  掃描結果如上所示

  查看掃描結果

  安裝中文支持

  上傳插件到sonarqube的插件目錄

  重啓sonarqube,讓插件生效

[root@node03 plugins]# /usr/local/sonaqube/bin/linux-x86-64/sonar.sh restart
Stopping SonarQube...
Stopped SonarQube.
Starting SonarQube...
Started SonarQube.
[root@node03 plugins]# 

  驗證:從新刷新web頁面,看看是否有中文支持了?

  在線安裝插件

  提示:它這個安裝插件的方式和jenkins安裝插件的方式同樣,你把須要的安裝的插件,在availabe中進行搜索;而後點擊後面的install便可;

  到此,代碼管理平臺sonarqube+sonar-scanner的部署和測試就完成了;

相關文章
相關標籤/搜索