Skip to content

Commit

Permalink
Merge pull request #2024 from dtcenter/feature_2020_run_sonarqube
Browse files Browse the repository at this point in the history
Feature 2020 run sonarqube
  • Loading branch information
hsoh-u authored Jan 27, 2022
2 parents 7aaddba + 11daa46 commit cfb4759
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scripts/environment/development.seneca
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ export MET_TEST_RSCRIPT=/usr/local/R-4.1.2/bin/Rscript
export PATH="/usr/local/nco/bin:/usr/local/netcdf/bin:\
/usr/local/sbin:/usr/local/bin:/usr/sbin:\
/usr/bin:/sbin:/bin:/usr/bin/X11:/opt/bin:$PATH"

# SonarQube
export SONARQUBE_DIR=/d1/projects/SonarQube/
export SONARQUBE_WRAPPER_BIN=$SONARQUBE_DIR/build-wrapper-linux-x86
export SONARQUBE_SCANNER_BIN=$SONARQUBE_DIR/sonar-scanner-4.6.2.2472-linux/bin
75 changes: 75 additions & 0 deletions scripts/sonarqube/run_nightly.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash
#
# Run nightly SonarQube scan
#=======================================================================
#
# This run_nightly.sh script calls the run_sonarqube.sh script.
# It is intented to be run nightly through cron. Output should be
# directed to the LOGFILE, per cron convention. To run this script, use
# the following commands:
#
# git clone https://github.com/dtcenter/MET
# MET/scripts/sosnarqube/run_nightly.sh name
#
# Usage: run_nightly.sh name
# where "name" specifies a branch, tag, or hash
#
# For example, scan the develop branch:
# run_nightly.sh develop
#
#=======================================================================

# Constants
#EMAIL_LIST="johnhg@ucar.edu hsoh@ucar.edu jpresto@ucar.edu linden@ucar.edu mccabe@ucar.edu"
EMAIL_LIST="hsoh@ucar.edu"
KEEP_DAYS=5

function usage {
echo
echo "USAGE: run_nightly.sh name"
echo " where \"name\" specifies a branch, tag, or hash."
echo
}

# Check for arguments
if [ $# -lt 1 ]; then usage; exit 1; fi

# Store the full path to the scripts directory
SCRIPT_DIR=`dirname $0`
if [[ ${0:0:1} != "/" ]]; then SCRIPT_DIR=$(pwd)/${SCRIPT_DIR}; fi

# Define the development environment
ENV_FILE=${SCRIPT_DIR}/../environment/development.`hostname`
if [[ ! -e ${ENV_FILE} ]]; then
echo "$0: ERROR -> Development environment file missing: ${ENV_FILE}"
exit 1
fi
source ${ENV_FILE}

# Delete old directories
find ${MET_PROJ_DIR}/MET_regression/sonarqube \
-mtime +${KEEP_DAYS} -name "NB*" | \
xargs rm -rf

# Create and switch to a run directory
TODAY=`date +%Y%m%d`
YESTERDAY=`date -d "1 day ago" +%Y%m%d`
RUN_DIR=${MET_PROJ_DIR}/MET_regression/sonarqube/NB${TODAY}
if [[ -e ${RUN_DIR} ]]; then rm -rf ${RUN_DIR}; fi
mkdir -p ${RUN_DIR}
cd ${RUN_DIR}

# Create a logfile
LOGFILE=${RUN_DIR}/run_sonarqube_${TODAY}.log

# Run scan and check for bad return status
${SCRIPT_DIR}/run_sonarqube.sh ${1} > ${LOGFILE}
if [[ $? -ne 0 ]]; then
echo "$0: The nightly SonarQube scan FAILED in `basename ${RUN_DIR}`." >> ${LOGFILE}
cat ${LOGFILE} | mail -s "MET SonarQube scan Failed for ${1} in `basename ${RUN_DIR}` (autogen msg)" ${EMAIL_LIST}
exit 1
fi

# Convert SonarQube report from pdf to html

exit 0
144 changes: 144 additions & 0 deletions scripts/sonarqube/run_sonarqube.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#!/bin/bash
#
# Run SonarQube Source Code Analyzer on a specified revision of MET
#=======================================================================
#
# This run_sonarqube.sh script will check out the specified version
# of MET and run the SonarQube Source Code Analyzer on it. First,
# go to the directory where you would like the SCA output written and
# then run:
#
# git clone https://github.com/dtcenter/MET
# MET/scripts/sonarqube/run_sonarqube_sca.sh name
#
# Usage: run_sonarqube_sca.sh name
# Test the specified branched version of MET:
# run_sonarqube_sca.sh {branch name}
# Test the specified tagged version of MET:
# run_sonarqube_sca.sh {tag name}
#
#=======================================================================

# Constants
GIT_REPO="https://github.com/dtcenter/MET"

function usage {
echo
echo "USAGE: $(basename $0) name"
echo " where \"name\" specifies a branch, tag, or hash."
echo
}

# Check for arguments
if [[ $# -lt 1 ]]; then usage; exit; fi

# Check that SONARQUBE_WRAPPER_BIN is defined
if [ -z ${SONARQUBE_WRAPPER_BIN} ]; then
which build-wrapper-linux-x86-64 2> /dev/null
if [ $? -eq 0 ]; then
SONARQUBE_WRAPPER_BIN=$(which build-wrapper-linux-x86-64 2> /dev/null)
else
which build-wrapper 2> /dev/null
if [ $? -eq 0 ]; then
SONARQUBE_WRAPPER_BIN=$(which build-wrapper 2> /dev/null)
else
echo "ERROR: SONARQUBE_WRAPPER_BIN must be set"
exit 1
fi
fi
fi
if [ ! -e ${SONARQUBE_WRAPPER_BIN} ]; then
echo "ERROR: SONARQUBE_WRAPPER_BIN (${SONARQUBE_WRAPPER_BIN}) does not exist"
exit 1
fi

# Check that SONARQUBE_SCANNER_BIN is defined
if [ -z ${SONARQUBE_SCANNER_BIN} ]; then
which sonar-scanner 2> /dev/null
if [ $? -eq 0 ]; then
SONARQUBE_SCANNER_BIN=$(which sonar-scanner 2> /dev/null)
else
echo "ERROR: SONARQUBE_SCANNER_BIN must be set"
exit 1
fi
fi
if [ ! -e ${SONARQUBE_SCANNER_BIN} ]; then
echo "ERROR: SONARQUBE_SCANNER_BIN (${SONARQUBE_SCANNER_BIN}) does not exist"
exit 1
fi

if [ -z ${SONARQUBE_OUT_DIR} ]; then
export SONARQUBE_OUT_DIR=bw-outputs
fi

# Sub-routine for running a command and checking return status
function run_command() {

# Print the command being called
echo "CALLING: $1"

# Run the command and store the return status
$1
STATUS=$?

# Check return status
if [[ ${STATUS} -ne 0 ]]; then
echo "ERROR: Command returned with non-zero status ($STATUS): $1"
exit ${STATUS}
fi

return ${STATUS}
}


# Store the full path to the scripts directory
SCRIPT_DIR=`dirname $0`
if [[ ${0:0:1} != "/" ]]; then SCRIPT_DIR=$(pwd)/${SCRIPT_DIR}; fi

# Clone repo into a sub-directory and checkout the requested version
REPO_DIR="MET-${1}"

if [ -e ${REPO_DIR} ]; then
run_command "rm -rf ${REPO_DIR}"
fi
run_command "git clone ${GIT_REPO} ${REPO_DIR}"
run_command "cd ${REPO_DIR}"
run_command "git checkout ${1}"

# Build the MET instance
run_command "cd met"

# Run bootstrap
run_command "./bootstrap"

# Do no manually set the CXX and F77 compilers.
# Let the configure script pick them.
# Otherwise, the SonarQube logic does not work.
export MET_DEVELOPMENT=true

# Run the configure script
run_command "./configure --prefix=`pwd` \
--enable-grib2 \
--enable-modis \
--enable-mode_graphics \
--enable-lidar2nc \
--enable-python"

# Set the build id
#BUILD_ID="MET-${1}"

# Copy sonar-project.properties
[ ! -e "sonar-project.properties" ] && cp -p $SCRIPT_DIR/sonar-project.properties .

# Run SonarQube clean
run_command "make clean"


# Run SonarQube make
run_command "${SONARQUBE_WRAPPER_BIN}/build-wrapper-linux-x86-64 --out-dir $SONARQUBE_OUT_DIR make"

# Run SonarQube scan
run_command "${SONARQUBE_SCANNER_BIN}/sonar-scanner"

# Run SonarQube report generator to make a PDF file
#TODAY=`date +%Y%m%d`
18 changes: 18 additions & 0 deletions scripts/sonarqube/sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
sonar.projectKey=org.sonarqube:MET_develop_NB
sonar.projectName=MET Nightly build
sonar.projectVersion=1.0

sonar.sources=src

# The build-wrapper output dir
sonar.cfamily.build-wrapper-output=bw-outputs

# Encoding of the source files
sonar.sourceEncoding=UTF-8

#----- Default SonarQube server
#sonar.host.url=http://localhost:9000
sonar.host.url=http://mandan:9000

sonar.login=met
sonar.password=met@sonar.ucar

0 comments on commit cfb4759

Please sign in to comment.