개발/Infra

[Infra] SonarQube centos 에 연동하기

hojak99 2018. 12. 9. 20:11

코드 정적 분석 툴인 SonarQube 를 centos 에 연동을 하려고 한다. (개인 프로젝트 때 사용해보려고.)

우선 필자는 centos 7 을 사용하고 있다. SonarQube 를 사용하기 위해선 서버에 jdk 가 깔려있어야 한다. jdk 8 을 설치를 해놓도록 하자


우선 SonarQube 를 다운 받도록 하자. 공식 사이트에서 ZIP 으로 받을 수 있다. 필자는 LTS 버전을 받았다.

그리고 필자는 /opt 경로에서 작업을 했다.

$ wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-6.7.6.zip

그러면 파일을 다운 받았을텐데 파일 확장자가 .ZIP 이다. 그래서 unzip 을 설치하도록 하자.

$ sudo yum install unzip
$ unzip sonarqube-6.7.6.zip

그리고 이제 SonarQube 셋팅을 해줘야 하는데, db 는 원래 h2 db가 디폴트라고 한다. (test 할 때만 추천하고 production 일 때에는 비추천 한다고 한다.) 하지만 필자는 mysql db 를 사용할 것이기 때문에 mysql 셋팅을 조금 해줘야 한다. 참고로 SonarQube 에서는 mysql 을 추천하지 않는다. (자세한 이야기는 docs 를 보자)

필자는SonarQube 를 위해 mysql user (sonarqube) 를 생성을 했다. 여기서 mysql user 를 생성할 때 localhost 용으로 sonarqube 라는 유저를 생성을 했는데 이제 sonarqube 서버를 킬 때 에러가 발생했다. 그래서 localhost 말고 % 로 생성을 하도록 하자. ~~ 'sonarqube'@'%' ~~

#--------------------------------------------------------------------------------------------------
# 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.
# - Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonarqube
sonar.jdbc.password=패스워드

jdbc username 과 password 를 설정한다.

#----- 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/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

jdbc url 도 설정한다. db pool 셋팅도 해주려고 했는데 필자는 그 부분에 대해서는 잘 몰라서 default 그대로 냅뒀다.

# Binding IP address. For servers with more than one IP address, this property specifies which
# address will be used for listening on the specified ports.
# By default, ports will be used on all IP addresses associated with the server.
sonar.web.host=0.0.0.0

# Web context. When set, it must start with forward slash (for example /sonarqube).
# The default value is root context (empty value).
sonar.web.context=/sonar
# TCP port for incoming HTTP connections. Default value is 9000.
sonar.web.port=9000

그리고 웹서버 셋팅도 해주다. sonar.web.host=0.0.0.0 은 모든 ip 허용이란 뜻이다.


그리고 이제 서버를 키면 된다. 하지만 여기서 중요한 점은 root 사용자로 sonarqube 서버를 켤 때 엘라스틱서치에서 오류가 발생한다.

can not run elasticsearch as root

검색해보니 이번 버전부터인가 root 로 서버를 켜지 못한다고 한다. 그래서 필자는 또 centos 에 sonar 이라는 유저를 생성한 후 (패스워드 설정도 해주자) chown -r sonar:sonar 'sonarqube 폴더'로 권한 변경을 해주자.

그리고 또 해줘야 할 것이 있다.

$ vi 'sonarqube 폴더경로'/bin/linux-x86-64/sonar.sh

...
RUN_AS_USER=sonar
...

해당 스크립트처럼 RUN_AS_USER 를 생성한 유저 sonar로 변경을 해줘야 한다. (아마 주석처리 돼 있을 것이다.)

그러면 서버가 켜지는데 성공할 것이다.

그리고 sonarqube 는 따로 설정하지 않는 이상 포트가 9000으로 잡히니 방화벽에서 포트 열어줘야 한다.

반응형