2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2020

10/15/2018: Provisioning SonarQube With CloudFormation

Provisioning SonarQube typically takes a few manual steps after the software itself is installed. For example, you need to change the default password and generate a token.

The process shown below uses the internal database so needs to be modified so large scale use. However, if you just have a few projects, this approach should be fine.

As always, please adjust the files to your situation. Pay special attention to the parameters in the CloudFormation template.

The project is located at https://github.com/medined/provision-sonarqube-using-cloudformation. You’ll find the CloudFormation yaml file there.

In this post, I want to show the script I use to wrap around the CloudFormation script. Some things are done first. And some afterward.

  • Before
    • Generate random password.
    • Save password to Parameter Store.
  • Execute the CloudFormation stack.
  • After
    • Get IP of SonarQube server.
    • Wait for server to respond to API request.
    • Wait until server is no longer STARTING.
    • Verify server is responding with UP.
    • Get password from Parameter Store.
    • Change the default password.
    • Get a sonar token.
    • Save token in Parameter Store.

Below is the shell script that implements the above steps.

#!/bin/bash

if [ -z $REGION_NAME ]; then
  echo "Set REGION_NAME"
  exit 1
fi

#############################
echo "Saving random password to Parameter Store"
#############################
echo "$(date)" > floop.tmp
RPASSWORD=\$(sha256sum floop.tmp)
rm floop.tmp

aws ssm put-parameter \
  --name sonar-password \
  --value ${RPASSWORD} \
  --type String \
  --overwrite
#
#############################

echo "Sonar started."
aws cloudformation deploy \
  --stack-name "sonar" \
  --region $REGION_NAME \
  --capabilities CAPABILITY_NAMED_IAM \
  --template-file sonar.yaml

#############################
echo "Getting IP address of SonarQube server."
#############################

HOST_SONAR=$(aws cloudformation list-exports \
  --query "Exports[?Name==\`sonar:PublicIp\`].Value" \
  --output text)

if [ -z ${HOST_SONAR} ]; then
  echo "ERROR: Missing CloudFormat export: sonar:PublicIp";
  exit
fi

#############################
echo "Waiting for SonarQube to start."
#############################

# I tried using the healthcheck url but it started to return the
# empty string even when I knew the server was running. I switched
# to the system status.

STATUS=$(curl --silent -u admin:admin --connect-timeout 2 --max-time 2 http://${HOST_SONAR}:9000/api/system/status | jq -r '.status')
while [ "${STATUS}x" == "x" ]; do
  echo -n "."
  sleep 10
  STATUS=$(curl --silent -u admin:admin --connect-timeout 2 --max-time 2 http://${HOST_SONAR}:9000/api/system/status | jq -r '.status')
done
echo ""

#############################
echo "Waiting for SonarQube to finish starting, after 10 second delay."
#############################

STATUS=$(curl --silent -u admin:admin --connect-timeout 2 --max-time 2 http://${HOST_SONAR}:9000/api/system/status | jq -r '.status')
while [ "${STATUS}x" == "STARTINGx" ]; do
  echo -n "."
  sleep 10
  STATUS=$(curl --silent -u admin:admin --connect-timeout 2 --max-time 2 http://${HOST_SONAR}:9000/api/system/status | jq -r '.status')
done
echo ""

#############################
echo "Verifying that SonarQube is UP, after 2 second delay."
#############################

sleep 2
STATUS=$(curl --silent -u admin:admin --connect-timeout 2 --max-time 2 http://${HOST_SONAR}:9000/api/system/status | jq -r '.status')
echo "s3: $STATUS"
if [ "$STATUS" != "UP" ]; then
  echo "ERROR: SonarQube is not UP. System status was ${STATUS}";
  exit
fi

#############################
echo "Getting new password from Parameter Store."
#############################

RPASSWORD=$(aws ssm get-parameter \
  --name sonar-password \
  --query 'Parameter.Value' \
  --output text 2>/dev/null)

if [ -z ${RPASSWORD} ]; then
  echo "ERROR: Missing value Parameter Store: sonar-password";
  exit
fi

#############################
echo "Changing Sonar default password."
#############################

curl -X POST \
  -u admin:admin \
  -d "login=admin&password=${RPASSWORD}&previousPassword=admin" \
  http://${HOST_SONAR}:9000/api/users/change_password

#############################
echo "Getting Sonar token."
#############################

TOKEN=$(curl \
  --silent \
  -u admin:${RPASSWORD} \
  -d "name=sonar" \
  http://${HOST_SONAR}:9000/api/user_tokens/generate \
  | jq -r '.token')

#############################
echo "Saving Sonar token to Parameter Store."
#############################

aws ssm put-parameter \
  --name sonar-token \
  --value $TOKEN \
  --type String \
  --overwrite > /dev/null

05/21/2018: OpenShift (with Docker) on Centos 7 inside Virtualbox

These steps assume that you are using Windows with Virtualbox already installed. It creates an everything version of Centos which uses a GNOME desktop.

Install Centos 7 inside Virtualbox

  • Download the ISO file for Everything Centos 7 from http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Everything-1804.iso. This ISO is 8.8GB so the download will take a few minutes.
  • Open VirtualBox
  • Click New
  • Use ‘centos everything’ for the Name. Use ‘Red Hat (64-bit)’ for the Version. Click Next
  • Use 2048MB or more for the memory size. Click Next
  • Click Create to create the virtual hard disk
  • Make sure that VDI is selected. Click Next
  • Make sure that Dynamically allocated is selected. Click Next
  • Use at least 50GB for the hard disk size. Click Create
  • Click Start to power on the virtual machine
  • Select the downloaded ISO file as the start-up disk. Click Start
  • Select Install CentOS 7. Press Enter
  • Leave English as the selected language. Click Continue
  • Click Software Selection. Select “GNOME Desktop” in left-hand column. Select “GNOME Applications”, “Development Tools”, and “System Administration Tools” in the right-hand column. Click Done
  • Click Installation Destination. Click Done
  • Click Network & Host Name. Click the on/off button. Click Done
  • Click Security Policy. Click the on/off button. Click Done
  • Click Begin Installation
  • Click Root Password. Enter password as the password. Click Done
  • Click User Creation. Enter ‘frog’ as the full name. Select ‘Make this user administrator’. Unselect ‘Require a password to use this account’. Click Done
  • Click Reboot
  • Click License Registration. Select ‘I accept the license agreement’. Click Done
  • Click Finish Configuration
  • Login as ‘frog. You won’t be asked for a password.

Install Guest Additions

  • With Centos running, select Devices > Insert Guest Additions CD image from the Virtualbox menu
  • Click Run
  • Press Enter to close the window.
  • Notice that the mouse pointer is no longer ‘captured’ by the Virtualbox window.
  • Right-click the Vbox_GAs_5.2.8 icon on the desktop. Select Eject.
  • Select Devices > Shared Clipboard from the Virtualbox menu. Then select bidirectional.
  • Install Terminator
  • In a terminal window, type the following commands.
sudo yum update –y
sudo yum install –y epel-release
sudo yum install –y terminator
exit
  • Press the Windows key. Then type ‘terminator’. Press Enter
  • You now have a much, much better terminal.

Install Docker

  • Open a terminal window.
  • Type the following:
sudo yum install docker –y
sudo systemctl start docker
sudo systemctl enable docker
sudo groupadd docker
sudo chown root:docker /var/run/docker.sock
sudo usermod –aG docker frog
  • Log out and back in to pick up the new group information.
  • Press Windows Key. Type terminal. Press Enter.
  • Enter ‘docker info’
  • Enter the following command to run the hello-world application inside a docker container.
docker run --rm hello-world
  • Docker is now installed.

Installing OpenShift

  • Open a terminal window.
  • OpenShift uses an insecure Docker registry which Docker needs to know about.
echo "{ \"insecure-registries\" : [ \"172.30.0.0/16\" ] }" | sudo tee /etc/docker/daemon.json > /dev/null
sudo systemctl daemon-reload
sudo systemctl restart docker
  • Download and install OpenShift
wget https://github.com/openshift/origin/releases/download/v3.9.0/openshift-origin-client-tools-v3.9.0-191fece-linux-64bit.tar.gz
tar xvfz openshift-origin-client-tools-v3.9.0-191fece-linux-64bit.tar.gz
sudo mv openshift-origin-client-tools-v3.9.0-191fece-linux-64bit/oc /usr/local/bin
rm openshift-origin-client-tools-v3.9.0-191fece-linux-64bit.tar.gz
rm –rf openshift-origin-client-tools-v3.9.0-191fece-linux-64bit
oc version
  • Launch OpenShift
oc cluster up
  • Take note of the URL and other information.
  • OpenShift is now running.