summaryrefslogtreecommitdiff
path: root/scripts/rabbitmq-script-wrapper
blob: 2b02a3ab1c00e83cf3a218ead77009b12fe57f21 (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
#!/bin/sh
##  The contents of this file are subject to the Mozilla Public License
##  Version 1.1 (the "License"); you may not use this file except in
##  compliance with the License. You may obtain a copy of the License
##  at http://www.mozilla.org/MPL/
##
##  Software distributed under the License is distributed on an "AS IS"
##  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
##  the License for the specific language governing rights and
##  limitations under the License.
##
##  The Original Code is RabbitMQ.
##
##  The Initial Developer of the Original Code is GoPivotal, Inc.
##  Copyright (c) 2007-2015 Pivotal Software, Inc.  All rights reserved.
##

for arg in "$@" ; do
    # Wrap each arg in single quotes and wrap single quotes in double quotes, so that they're passed through cleanly.
    arg=`printf %s "$arg" | sed -e "s#'#'\"'\"'#g"`
    CMDLINE="${CMDLINE} '${arg}'"
done

SCRIPT="$(basename "$0")"
RABBITMQ_ENV=/usr/lib/rabbitmq/bin/rabbitmq-env
RABBITMQ_SCRIPTS_DIR="$(dirname "$RABBITMQ_ENV")"

main() {
  if current_user_is_rabbitmq
  then
    if calling_rabbitmq_server
    then
      exec_rabbitmq_server "$@"
    else
      exec_script_as_rabbitmq "$@"
    fi
  elif current_user_is_root
  then
    exec_script_as_root
  else
    run_script_help_and_fail
  fi
}

current_user_is_rabbitmq() {
  [ "$(id -un)" = "rabbitmq" ]
}

current_user_is_root() {
  [ "$(id -u)" = 0 ]
}

calling_rabbitmq_server() {
  [ "$SCRIPT" = "rabbitmq-server" ]
}

exec_rabbitmq_server() {
  RABBITMQ_ENV=/usr/lib/rabbitmq/bin/rabbitmq-env
  # RABBITMQ_SCRIPTS_DIR is used in rabbitmq-env
  # shellcheck disable=SC2034
  RABBITMQ_SCRIPTS_DIR="$(dirname "$RABBITMQ_ENV")"
  # shellcheck source=/dev/null
  . "$RABBITMQ_ENV"

  exec /usr/lib/rabbitmq/bin/rabbitmq-server "$@" @STDOUT_STDERR_REDIRECTION@
}

exec_script_as_rabbitmq() {
  HOME="$(cd ~rabbitmq && pwd)" exec "/usr/lib/rabbitmq/bin/$SCRIPT" "$@"
}

exec_script_as_root() {
  @SU_RABBITMQ_SH_C@ "/usr/lib/rabbitmq/bin/$SCRIPT $CMDLINE"
}

run_script_help_and_fail() {
  "/usr/lib/rabbitmq/bin/$SCRIPT" help

  echo "
Only root or rabbitmq can run $SCRIPT
"
  exit 1
}

main "$@"