Skip to content

Instantly share code, notes, and snippets.

@shakedlokits
Created August 22, 2017 16:18
Show Gist options
  • Save shakedlokits/829c458ca7529fa0cf0bd420483bd9df to your computer and use it in GitHub Desktop.
Save shakedlokits/829c458ca7529fa0cf0bd420483bd9df to your computer and use it in GitHub Desktop.

Launch SonarQube as a Service

Manual

  1. setup environment variables:
export SONAR_VERSION=6.4
export SONARQUBE_HOME=/opt/sonarqube
export SONARQUBE_JDBC_USERNAME=sonar
export SONARQUBE_JDBC_PASSWORD=sonar
export SONARQUBE_JDBC_URL=
  1. install gpg & unzip:
sudo dnf install -y gpg unzip
  1. run setup.sh in sudo:
#!/bin/sh -

# set server encryption
set -x
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys F1182E81C792928921DBCAB4CFCA4A29D26468DE

# fetch, unzip and deploy the server
cd /opt
curl -o sonarqube.zip -fSL https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-$SONAR_VERSION.zip
curl -o sonarqube.zip.asc -fSL https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-$SONAR_VERSION.zip.asc
gpg --batch --verify sonarqube.zip.asc sonarqube.zip
unzip sonarqube.zip
mv sonarqube-$SONAR_VERSION sonarqube
  1. set up a systemd unit for Sonar service
[Unit]
Description=SonarQube Server
Documentation=https://docs.sonarqube.org/display/SONAR/Documentation
After=network.target network-online.target
Wants=network-online.target

[Service]
Type=forking
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
ExecReload=/opt/sonarqube/bin/linux-x86-64/sonar.sh restart
PIDFile=/opt/sonarqube/bin/linux-x86-64/SonarQube.pid
  1. run systemctl to launch the service
sudo systemctl daemon-reload
sudo systemctl start sonar

Ansible

- hosts: sonarqube
  environment:
    SONAR_VERSION: 6.4
    SONARQUBE_HOME: /opt/sonarqube
    SONARQUBE_JDBC_USERNAME: sonar
    SONARQUBE_JDBC_PASSWORD: sonar
  remote_user: root
  become: yes

  tasks:
  - name: install dependencies
    dnf:
      name: "{{ item }}"
      state: present
    with_items:
    - gpg
    - unzip
    - java-1.8.0-openjdk.x86_64
    - java-1.8.0-openjdk-devel.x86_64
    - libselinux-python

  - name: import gpg keys
    command: "{{ item }}"
    with_items:
    - gpg --keyserver ha.pool.sks-keyservers.net --recv-keys F1182E81C792928921DBCAB4CFCA4A29D26468DE
    - touch /root/.gpgset
    args:
      creates: /root/.gpgset


  - name: fetch and deploy sonar server
    shell: "{{ item }}"
    args:
      chdir: /opt
      creates: /opt/sonarqube
    with_items:
    - curl -o sonarqube.zip -fSL https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-$SONAR_VERSION.zip
    - curl -o sonarqube.zip.asc -fSL https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-$SONAR_VERSION.zip.asc
    - gpg --batch --verify sonarqube.zip.asc sonarqube.zip
    - unzip sonarqube.zip
    - mv sonarqube-$SONAR_VERSION sonarqube

  - name: set sonar service unit
    copy:
      src: ./sonar.service
      dest: /etc/systemd/system

  - name: start sonar server
    systemd:
      name: sonar
      state: started
      daemon_reload: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment