aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/logs.sh
blob: 9b84391726b7e1a8599983018ee1a4741138f3ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
###############################
# usage: logs.sh [ -e ARG ] [ -l ARG ] [ -n ARG ]
#  -e ARG: edition to run
#          valid values are 'oss' (Open Source), 'dev' (Developer), 'ent' (Enterprise) and 'ha' (HA) (case insensitive)
#          default value is 'oss'.
#  -l ARG: name of log file to display.
#          valid values are 'all', 'sonar', 'web', 'ce' and 'es' (case insensitive).
#          default value is 'all'.
#  -n ARG: number of log line to display. Value is passed to n option of tail.
#          default value is 25
###############################

set -euo pipefail

ROOT="$(pwd)"
source "$ROOT/scripts/editions.sh"
if [ -r "$ROOT/private/scripts/editions.sh" ]; then
  source "$ROOT/private/scripts/editions.sh"
fi

DEFAULT_LOG="all"
DEFAULT_LINES="25"
LOGS="sonar web ce es"

function toLower() {
  echo "$1" | tr '[:upper:]' '[:lower:]'
}

function checkLogArgument() {
  local logArg="$1"
  local lowerLogArg=$(toLower $logArg)

  if [ "$lowerLogArg" == "$DEFAULT_LOG" ]; then
    return
  fi

  for t in $LOGS; do
    if [ "$lowerLogArg" == "$t" ]; then
      return
    fi
  done

  echo "Unsupported -l argument $logArg"
  exit 1
}

function buildTailArgs() {
  local logArg="$(toLower $1)"
  local logLines="$2"
  local res=""

  for t in $LOGS; do
    if [ "$logArg" == "$DEFAULT_LOG" ] || [ "$logArg" == "$t" ]; then
      res="$res -Fn $logLines $SQ_HOME/logs/$t.log"
    fi
  done

  echo "$res"
}

function doTail() {
  local logArg="$1"
  local logLines="${2:-"$DEFAULT_LINES"}"
  TAIL_ARG=$(buildTailArgs "$logArg" "$logLines")
  tail $TAIL_ARG
}

# check the script was called to avoid execute when script is only sourced
script_name=$(basename "$0")
if [ "$script_name" == "logs.sh" ]; then
  LOG="$DEFAULT_LOG"
  LINES="$DEFAULT_LINES"
  EDITION="$DEFAULT_EDITION"
  while getopts ":e:l:n:" opt; do
    case "$opt" in
      e) EDITION=${OPTARG:=$DEFAULT_EDITION}
         ;;
      l) LOG=${OPTARG:=$DEFAULT_LOG}
         ;;
      n) LINES=${OPTARG:=$DEFAULT_LINES}
         ;;
      \?)
        echo "Unsupported option $OPTARG" >&2
        exit 1
        ;;
    esac
  done

  checkEditionArgument "$EDITION"
  checkLogArgument "$LOG"

  SQ_HOME_WILDCARD="$(distributionDirOf "$EDITION")/$(targetDirOf "$EDITION")/sonarqube-*"
  if ! ls ${SQ_HOME_WILDCARD} &> /dev/null; then
    echo "$(targetDirOf "$EDITION") is not unpacked"
    exit 1
  fi
  cd ${SQ_HOME_WILDCARD}
  SQ_HOME=$(pwd)
  cd "$ROOT"

  doTail "$LOG" "$LINES"
fi