This is a demo for starting up the sonarqube
from docker environment.
The source code is on [GitHub]
Please be aware that this demo is not production ready, please double-check for any security issues before going to production with / based on this code base.
Clone the project with the below script.
git clone https://github.com/xh-dev/sonarqube-how-to-demo.git
The IaC is achieved through the terraform
and ansible
. I use terraform
for the cloud VM provisioning and then use ansible
for the detailed VM configuration. The cloud provider for this project is Linode
, the user with another cloud provider, please modify the terraform configuration to meet their cloud platform needs.
Before we could start with the demo, the demo requires some basic setups
- terraform installed
- ansible installed
- python 3+ installed
-
Please create a
init.auto.tfvars
for storing neccessary secrete of variables.linode_token="{linode token}" root_pass="{VM root password}" ssh_key="{ssh access key}"
The project requires three terraform variables to be provided for making the terraform work well on provisioning.
-
Once the
init.auto.tfvars
ready, execute command to initialize the terraform projectterraform init
-
After the terraform project is initialized, execute the command to apply the change to the cloud platform.
# auto-approve to prevent prompting the confirmation terraform apply --auto-approve
-
Once the cloud VM is provisioned, we could start generating the required shell script for configuration of the project.
python main.py
After executing the above command you would observe a few files are generated(e.g.
inventory.yaml
,init.sh
,setup_hots.sh
, etc.).All the scripts are generated based on the IP address of new VM generated by the
terraform
,init.sh
is the command initializing the whole configuration process.source init.sh
After executing the command you would observe a list of operations happens, including,
- register
known_hosts
- apply system update, installation of docker, and system configuration through
ansible
- copy the docker compose to the remote VM through
SCP
P.S. make sure you see some output like
{ip} SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1
.Setup known_hosts for 172.104.40.40 Host 172.104.40.40 not found in /home/xeth/.ssh/known_hosts # 172.104.40.40:22 SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1 # 172.104.40.40:22 SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1 # 172.104.40.40:22 SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1 # 172.104.40.40:22 SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1 # 172.104.40.40:22 SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1
The script sometime fail due to the SSH on remote host not yet ready for connection the error should be liek
host unreachable
. Just re-execute the command again, the script is safe to be re-execute. - register
-
SSH into the remote server and start up the docker with
docker-compose.yaml
# If executed `source init.sh`, the IP address of the VM is stored as environment variable in `ip` ssh root@$ip
execute the below command to start up the
sonarqube
server.cd ~/sonarqube docker compose up
root@localhost:~/sonarqube# cd ~/sonarqube root@localhost:~/sonarqube# docker compose up # Output # #[+] Running 21/21 # ✔ db 13 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 19.6s # ✔ 26c5c85e47da Pull complete 4.2s # ✔ 1c30a4c3f519 Pull complete 4.4s #. #. #. # ✔ sonarqube 6 layers [⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 24.1s # ✔ 74ac377868f8 Pull complete 9.7s # ✔ a182a611d05b Pull complete 11.4s # ✔ cdb51f9f6377 Pull complete 15.0s # ✔ 1365afd8cae0 Pull complete 15.2s # ✔ a400e494cf72 Pull complete 20.3s # ✔ d0dc5f994ca8 Pull complete 20.4s #[+] Running 8/8 # ✔ Network sonarqube_default Created 0.1s #. #. #. # ✔ Container postgresql Created 0.0s # ✔ Container sonarqube Created 0.0s #Attaching to postgresql, sonarqube #postgresql | The files belonging to this database system will be owned by user "postgres". #postgresql | This user must also own the server process. #. #. #. #postgresql | 2023-04-20 08:00:58.116 UTC [62] LOG: database system was shut down at 2023-04-20 08:00:58 UTC #postgresql | 2023-04-20 08:00:58.121 UTC [1] LOG: database system is ready to accept connections #sonarqube | 2023.04.20 08:00:58 INFO app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /opt/sonarqube/temp #sonarqube | 2023.04.20 08:00:58 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on [HTTP: 127.0.0.1:9001, TCP
-
After the command runs completes and shows the web server started. The user can access the sonarqube dashboard through the public IP address.
curl "http://$ip:9000"
(venv) xeth@localhost:~/sonarqube/sonarqube-how-to-demo$ curl http://$ip:9000 # Output # #<!DOCTYPE html> #<html lang="en"> # #<head> #. #. #. #</head> # #<body> # <div id="content" data-base-url="" data-server-status="UP" data-instance="SonarQube" data-official="true"> # <div class="global-loading"> # <i class="spinner global-loading-spinner"></i> # <span aria-live="polite" class="global-loading-text">Loading...</span> # </div> # </div> # # <script type="module" src="/js/out4CRO2ANZ.js"></script> #</body> # #</html>