add: sonarqube installation script

This commit is contained in:
Enzo Legrand 2025-09-30 16:51:05 +02:00
parent 5925552ae1
commit 93809e5784

View File

@ -0,0 +1,93 @@
#!/usr/bin/env bash
# Copyright (c) 2021-2025 community-scripts ORG
# Author: prop4n
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
# Source: https://docs.sonarsource.com/sonarqube-server
source /dev/stdin <<<"$FUNCTIONS_FILE_PATH"
color
verb_ip6
catch_errors
setting_up_container
network_check
update_os
msg_info "Installing Dependencies (Patience)"
$STD apt-get install -y openjdk-17-jdk curl unzip
msg_ok "Installed Dependencies"
RELEASE=$(curl -s https://api.github.com/repos/SonarSource/sonarqube/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
PG_VERSION="17" setup_postgresql
msg_info "Installing Postgresql"
DB_NAME="sonarqube"
DB_USER="sonarqube"
DB_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13)
systemctl start postgresql
$STD sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';"
$STD sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
{
echo "Application Credentials"
echo "DB_NAME: $DB_NAME"
echo "DB_USER: $DB_USER"
echo "DB_PASS: $DB_PASS"
} >>~/sonarqube.creds
msg_ok "Installed PostgreSQL"
msg_info "Creating SonarQube User"
useradd -r -m -U -d /opt/sonarqube -s /bin/bash sonarqube 2>/dev/null || true
msg_info "SonarQube Installation"
curl -fsSL -o /tmp/sonarqube.zip "https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-${RELEASE}.zip"
unzip -q /tmp/sonarqube.zip -d /tmp
cp -r /tmp/sonarqube-*/* /opt/sonarqube/
rm -rf /tmp/sonarqube*
chown -R sonarqube:sonarqube /opt/sonarqube
chmod -R 755 /opt/sonarqube
msg_ok "Installed SonarQube"
msg_info "Configuring SonarQube"
mkdir -p /opt/sonarqube/conf
cat >/opt/sonarqube/conf/sonar.properties <<EOF
sonar.jdbc.username=${DB_USER}
sonar.jdbc.password=${DB_PASS}
sonar.jdbc.url=jdbc:postgresql://localhost/${DB_NAME}
sonar.web.host=0.0.0.0
sonar.web.port=9000
EOF
chmod +x /opt/sonarqube/bin/linux-x86-64/sonar.sh
msg_ok "Configured SonarQube"
msg_info "Creating Systemd Service"
cat >/etc/systemd/system/sonarqube.service <<EOF
[Unit]
Description=SonarQube service
After=postgresql.service
[Service]
Type=forking
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
User=sonarqube
Group=sonarqube
Restart=on-failure
LimitNOFILE=131072
LimitNPROC=8192
[Install]
WantedBy=multi-user.target
EOF
$STD systemctl daemon-reload
msg_ok "Created Systemd Service"
msg_info "Starting SonarQube service"
$STD systemctl enable --now sonarqube
msg_ok "SonarQube service started"
motd_ssh
customize
msg_info "Cleaning up"
$STD apt-get -y autoremove
$STD apt-get -y autoclean
msg_ok "Cleaned"