From c31e0667170091488cfdaf1f4b7c576546832e11 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 3 Aug 2015 15:05:20 +0200 Subject: [PATCH] [SCRIPTS] add support for patches to start.sh use seperate start and stop to avoid unpredictable errors when applying patches on a live SQ instance --- .gitignore | 3 ++ scripts/patches/README.txt | 52 +++++++++++++++++++++++++++++++++ scripts/patches_utils.sh | 23 +++++++++++++++ scripts/property_utils.sh | 43 +++++++++++++++++++++++++++ scripts/start.sh | 59 ++++++++++++++++++++++++++++++++++++++ start.sh | 37 +----------------------- 6 files changed, 181 insertions(+), 36 deletions(-) create mode 100644 scripts/patches/README.txt create mode 100755 scripts/patches_utils.sh create mode 100755 scripts/property_utils.sh create mode 100755 scripts/start.sh mode change 100755 => 120000 start.sh diff --git a/.gitignore b/.gitignore index 92179a9359f..01585d7b123 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,6 @@ Desktop.ini # ---- Sonar .sonar/ + +# scripts patches, they are local to each developer +scripts/patches/*.sh diff --git a/scripts/patches/README.txt b/scripts/patches/README.txt new file mode 100644 index 00000000000..c120dda746c --- /dev/null +++ b/scripts/patches/README.txt @@ -0,0 +1,52 @@ +This directory contains patches to be used by the any script which use function call_patches from patches_utils.sh. + +Patches are files with extension ".sh". Name of the patch is the name of the file without extension (so name of patch "debug.sh" is "debug"). + +Patches run in the same directory as the calling script provided patches_utils.sh as been sourced in the calling script. + +Patches are passed on to start.sh script using command line argument "-p" by their name. +More than one can be specified using a colon. +Each patch is invoked with one argument: + * SQ_HOME: the path to the home of the started SQ instance + + +************************************************************************************************************************ + sample and common scripts are provided below +************************************************************************************************************************ + + + +****************************************** start of debug.sh ****************************************** +#!/bin/bash +############################### +# sets property sonar.web.javaAdditionalOpts in sonar.properties to activate debug +############################### + +set -euo pipefail + +source scripts/property_utils.sh + +SQ_HOME=$1 + +echo "enabling debug in conf/sonar.properties, listening on port 5005" +set_property sonar.web.javaAdditionalOpts -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 $SQ_HOME/conf/sonar.properties +****************************************** end of debug.sh ****************************************** + + +****************************************** start of views.sh ****************************************** +#!/bin/bash +############################### +# copies the sonar-views plugin jar to the extension directory +############################### + +set -euo pipefail + +source scripts/property_utils.sh + +SQ_HOME=$1 + +VIEWS_FILE=~/DEV/views/target/sonar-views-plugin-2.9-SNAPSHOT.jar +EXT_DIR=$SQ_HOME/extensions/plugins/ +echo "copy $VIEWS_FILE to $EXT_DIR" +cp $VIEWS_FILE $EXT_DIR +****************************************** end of views.sh ****************************************** diff --git a/scripts/patches_utils.sh b/scripts/patches_utils.sh new file mode 100755 index 00000000000..7a3dd39e57f --- /dev/null +++ b/scripts/patches_utils.sh @@ -0,0 +1,23 @@ +#!/bin/bash +############################### +# exposes library functions to modify properties in a property +############################### + +set -euo pipefail + +PATCHES_HOME=scripts/patches + +# $1: name(s) of patches to call, separated by a colon +# all other arguments are passed as is to the patches +function call_patches() { + PATCHES=$1 + ARGS=${@:2} + + IFS=',' + for PATCH in $PATCHES; do + #echo "calling $PATCHES_HOME/$PATCH.sh $ARGS" + $PATCHES_HOME/$PATCH.sh $ARGS + done +} + + diff --git a/scripts/property_utils.sh b/scripts/property_utils.sh new file mode 100755 index 00000000000..43c7dcb2085 --- /dev/null +++ b/scripts/property_utils.sh @@ -0,0 +1,43 @@ +#!/bin/bash +############################### +# exposes library functions to modify properties in a property +# +# TODO function append_property +# +############################### + +set -euo pipefail + +if [[ "$OSTYPE" == "darwin"* ]]; then + SED_DISABLE_BACKUP=" ''" +else + SED_DISABLE_BACKUP="" +fi + +function cnt_lines() { + FILE=$1 + cat $1 | wc -l +} + +function set_property() { + PROPERTY=$1 + VALUE=$2 + FILE=$3 + + # uncomment below to help debug calls to set_property + # echo "setting property $PROPERTY to value $VALUE in $FILE" + + # delete line of specified property + LINE_COUNT=$(cnt_lines $FILE) + REGEXP="${1//\./\\\.}\s*=" + sed -i $SED_DISABLE_BACKUP "/${REGEXP}/d" $FILE + + # add property if at least one line deleted + NEW_LINE_COUNT=$(cnt_lines $FILE) + if [ $LINE_COUNT -gt $NEW_LINE_COUNT ]; then + echo "" >> $FILE + echo "${PROPERTY}=${VALUE}" >> $FILE + fi +} + + diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100755 index 00000000000..e71adaf7d3d --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,59 @@ +#!/bin/bash +############################### +# usage: start.sh [ -p ARG ] +# -p ARG: name(s) of patch separated by colon (name of patch is filename without extension) +############################### + +ROOT=$(pwd) + +PATCHES="" +while getopts ":p:" opt; do + case "$opt" in + p) PATCHES=$OPTARG + ;; + \?) + >&2 echo "Unsupported option $OPTARG" + exit 1 + ;; + esac +done + +if [[ "$OSTYPE" == "darwin"* ]]; then + OS='macosx-universal-64' +else + OS='linux-x86-64' +fi + +ls sonar-application/target/sonarqube-*.zip 1> /dev/null 2>&1 +if [ "$?" != "0" ]; then + echo 'Sources are not built' + ./build.sh +fi + +cd sonar-application/target/ +ls sonarqube-*/bin/$OS/sonar.sh 1> /dev/null 2>&1 +if [ "$?" != "0" ]; then + unzip sonarqube-*.zip +fi +cd sonarqube-* + +# from that point on, strict bash +set -euo pipefail +SQ_HOME=$(pwd) +cd $ROOT + +source $ROOT/scripts/patches_utils.sh + +SQ_EXEC=$SQ_HOME/bin/$OS/sonar.sh + +$SQ_EXEC stop + +# invoke patches if at least one was specified +# each patch is passed the path to the SQ instance home directory as first and only argument +if [ "$PATCHES" != "" ]; then + call_patches $PATCHES $SQ_HOME +fi + +$SQ_EXEC start +sleep 1 +tail -100f $SQ_HOME/logs/sonar.log diff --git a/start.sh b/start.sh deleted file mode 100755 index 2c29ef02cbd..00000000000 --- a/start.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -############################### -# usage: use -d option to enable remote debugging of the web server on port 5005 -############################### - -if [[ "$OSTYPE" == "darwin"* ]]; then - OS='macosx-universal-64' - SED_DISABLE_BACKUP=" ''" -else - OS='linux-x86-64' - SED_DISABLE_BACKUP="" -fi - -ls sonar-application/target/sonarqube-*.zip 1> /dev/null 2>&1 -if [ "$?" != "0" ]; then - echo 'Sources are not built' - ./build.sh -fi - -cd sonar-application/target/ -ls sonarqube-*/bin/$OS/sonar.sh 1> /dev/null 2>&1 -if [ "$?" != "0" ]; then - unzip sonarqube-*.zip -fi -cd sonarqube-* - -if [ "$1" = "-d" ]; then - echo "enabling debug in conf/sonar.properties, listening on port 5005" - sed -i $SED_DISABLE_BACKUP '/javaAdditionalOpts/d' conf/sonar.properties - echo "" >> conf/sonar.properties - echo "sonar.web.javaAdditionalOpts=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" >> conf/sonar.properties -fi - -bin/$OS/sonar.sh restart -sleep 1 -tail -100f logs/sonar.log diff --git a/start.sh b/start.sh new file mode 120000 index 00000000000..35cf7e5364d --- /dev/null +++ b/start.sh @@ -0,0 +1 @@ +scripts/start.sh \ No newline at end of file -- 2.39.5