代码质量协作平台之SonarQube安装部署

  一、简介

  Sonar是一个用于代码质量管理的开放平台,通过插件机制,sonar可以收集不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(比如jenkins)不同,sonar并不是简单地把不同的代码检查工具结果直接显示在web页面,而是通过不同的插件对这些结果进行加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。在对其他工具的支持方面,sonar不仅提供了对IDE的支持,可以在Eclipse和Intellij IDEA这些工具里联机查看结果;同时sonar还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用sonar,此外,sonar的插件还可以对java以外的其他编程语言提供支持,对国际化及报告文档也有很良好的支持;官方网站https://www.sonarqube.org;

  二、sonar平台部署

  sonarqube是一款用java语言编写的程序,它主要作用是提供一个web界面,展示扫描分析结果以及系统管理,插件管理等;扫描代码还是sonar-scanner这个插件做的,它的工作原理是sonar-scanner通过识别项目中的sonar-project.properties配置文件中定义的内容,把对应的项目源码进行扫描,把扫描后的结果保存到指定的数据库;然后sonarqube通过连接配置的数据库,把sonar-scanner存入数据库中的数据加载到web界面,从而用户就可以通过web界面查看扫描的项目源码的结果;

  1、安装数据库

  上传mysql5.6安装包和脚本

[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 ~]#

  安装脚本

#!/bin/bash

DIR=`pwd`

NAME="mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz"

FULL_NAME=${DIR}/${NAME}

DATA_DIR="/data/mysql"

yuminstall vim gccgcc-c++ wget autoconf net-tools lrzsz iotop lsof iotop bash-completion -y

yuminstall 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

ifid mysql;then

echo"mysql 用户已经存在,跳过创建用户过程"

fi

useradd mysql -s /sbin/nologin

ifid 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

  安装mysql

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

代码质量协作平台之SonarQube安装部署

  提示:自动安装脚本执行完成后,它会自动启动mysql,如果启动成功,说明mysql已经安装完成;

  验证:查看msyql是否启动,是否可以连接到mysql数据库?

代码质量协作平台之SonarQube安装部署

  创建数据库和用户授权

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地址反解成主机名

代码质量协作平台之SonarQube安装部署

  重启mysql,再次测试新建的用户是否能够连接到mysql?

代码质量协作平台之SonarQube安装部署

  到此mysql安装和测试就完成了

  2、安装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 启动失败;

  3、上传sonarqube安装包,安装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端口并没有监听;

  查看日志

代码质量协作平台之SonarQube安装部署

  提示:日志里提示说内存不足;

  查看内存使用情况

[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,看看是否启动起来?

代码质量协作平台之SonarQube安装部署

  提示:调整内存为4G,勉强启动起来;所以如果数据库和sonarqube在一台主机上,建议将内存调到8G,甚至更高;

  访问9000端口

代码质量协作平台之SonarQube安装部署

  登录试试

代码质量协作平台之SonarQube安装部署

  提示:点击登录,弹出一个输入token的界面,我们可以忽略它,直接进入即可;到此sonarqube服务就正常跑起来了;

  4、安装扫描器 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]#

  创建软连接

代码质量协作平台之SonarQube安装部署

  配置 sonar-scanner

代码质量协作平台之SonarQube安装部署

  提示:扫描器主要配置它需要连接的数据相关配置,以及soanrqube服务的地址;扫描器不需要启动,它的工作方式是在对应项目里sonar-porject.properties配置文件所在目录运行sonar-scanner,它默认会去找项目中的sonar-porject.properties配置文件,进行扫描项目源代码;

  测试:上传测试代码进行扫描

代码质量协作平台之SonarQube安装部署

  解压,并进入到项目目录,进入sonar-project.properties文件所在目录

代码质量协作平台之SonarQube安装部署

  提示: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安装部署

代码质量协作平台之SonarQube安装部署

  安装中文支持

  上传插件到sonarqube的插件目录

代码质量协作平台之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页面,看看是否有中文支持了?

代码质量协作平台之SonarQube安装部署

  在线安装插件

代码质量协作平台之SonarQube安装部署

  提示:它这个安装插件的方式和jenkins安装插件的方式一样,你把需要的安装的插件,在availabe中进行搜索;然后点击后面的install即可;

  到此,代码管理平台sonarqube+sonar-scanner的部署和测试就完成了;

以上是 代码质量协作平台之SonarQube安装部署 的全部内容, 来源链接: utcz.com/a/60051.html

回到顶部