summaryrefslogtreecommitdiff
path: root/scripts/rabbitmq-env
blob: 48aa3e12eef56028037c1d286e8f2df349a68527 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/sh -e
##  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 https://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-2020 VMware, Inc. or its affiliates.  All rights reserved.
##

if [ "$RABBITMQ_ENV_LOADED" = 1 ]; then
    return 0;
fi

if [ -z "$RABBITMQ_SCRIPTS_DIR" ]; then
    # We set +e here since since our test for "readlink -f" below needs to
    # be able to fail.
    set +e
    # Determine where this script is really located (if this script is
    # invoked from another script, this is the location of the caller)
    SCRIPT_PATH="$0"
    while [ -h "$SCRIPT_PATH" ] ; do
        # Determine if readlink -f is supported at all. TODO clean this up.
        FULL_PATH=`readlink -f $SCRIPT_PATH 2>/dev/null`
        if [ "$?" != "0" ]; then
          REL_PATH=`readlink $SCRIPT_PATH`
          if expr "$REL_PATH" : '/.*' > /dev/null; then
            SCRIPT_PATH="$REL_PATH"
          else
            SCRIPT_PATH="`dirname "$SCRIPT_PATH"`/$REL_PATH"
          fi
        else
          SCRIPT_PATH=$FULL_PATH
        fi
    done
    set -e

    RABBITMQ_SCRIPTS_DIR=`dirname $SCRIPT_PATH`
fi

_rmq_env_now()
{
    date '+%Y-%m-%d %H:%M:%S'
}

_rmq_env_print()
{
    _rmq_env_tmp="$1"
    _rmq_env_tmp_len="${#_rmq_env_tmp}"
    shift
    printf '%s %s %s\n' "$(_rmq_env_now)" "$_rmq_env_tmp" "$1" 1>&2
    shift
    _rmq_env_print_line=''
    _rmq_env_indent="$((_rmq_env_tmp_len + 21))"
    for _rmq_env_print_line in "$@"
    do
        printf "%${_rmq_env_indent}s%s\n" ' ' "$_rmq_env_print_line" 1>&2
    done
    unset _rmq_env_print_line
    unset _rmq_env_indent
    unset _rmq_env_tmp_len
    unset _rmq_env_tmp
}

_rmq_env_perr()
{
    _rmq_env_print '[error]' "$@"
}

_rmq_env_pwarn()
{
    _rmq_env_print '[warning]' "$@"
}

rmq_realpath() {
    local path=$1

    if [ -d "$path" ]; then
        cd "$path" && pwd
    elif [ -f "$path" ]; then
        cd "$(dirname "$path")" && echo $(pwd)/$(basename "$path")
    else
        echo "$path"
    fi
}

RABBITMQ_HOME="$(rmq_realpath "${RABBITMQ_SCRIPTS_DIR}/..")"
ESCRIPT_DIR="${RABBITMQ_HOME}/escript"

## Set defaults
. ${RABBITMQ_SCRIPTS_DIR}/rabbitmq-defaults

# We save the current value of $RABBITMQ_PID_FILE in case it was set by
# an init script. If $CONF_ENV_FILE overrides it again, we must ignore
# it and warn the user.
saved_RABBITMQ_PID_FILE="$RABBITMQ_PID_FILE"

## Get configuration variables from the configure environment file
[ "x" = "x$RABBITMQ_CONF_ENV_FILE" ] && RABBITMQ_CONF_ENV_FILE=${CONF_ENV_FILE}
if [ -f "${RABBITMQ_CONF_ENV_FILE}" ]; then
	CONF_ENV_FILE_PHASE=rabbitmq-env
	. ${RABBITMQ_CONF_ENV_FILE} || true
fi

[ -n "$ERL_EPMD_PORT" ] && export ERL_EPMD_PORT
[ -n "$ERL_EPMD_ADDRESS" ] && export ERL_EPMD_ADDRESS

DEFAULT_SCHEDULER_BIND_TYPE="db"
[ -n "$SCHEDULER_BIND_TYPE" ] || SCHEDULER_BIND_TYPE="$DEFAULT_SCHEDULER_BIND_TYPE"
[ -n "$RABBITMQ_SCHEDULER_BIND_TYPE" ] || RABBITMQ_SCHEDULER_BIND_TYPE="$SCHEDULER_BIND_TYPE"

DEFAULT_DISTRIBUTION_BUFFER_SIZE=128000
[ -n "$DISTRIBUTION_BUFFER_SIZE" ] || DISTRIBUTION_BUFFER_SIZE="$DEFAULT_DISTRIBUTION_BUFFER_SIZE"
[ -n "$RABBITMQ_DISTRIBUTION_BUFFER_SIZE" ] || RABBITMQ_DISTRIBUTION_BUFFER_SIZE="$DISTRIBUTION_BUFFER_SIZE"

DEFAULT_MAX_NUMBER_OF_PROCESSES=1048576
[ -n "$MAX_NUMBER_OF_PROCESSES" ] || MAX_NUMBER_OF_PROCESSES="$DEFAULT_MAX_NUMBER_OF_PROCESSES"
[ -n "$RABBITMQ_MAX_NUMBER_OF_PROCESSES" ] || RABBITMQ_MAX_NUMBER_OF_PROCESSES="$MAX_NUMBER_OF_PROCESSES"

DEFAULT_MAX_NUMBER_OF_ATOMS=5000000
[ -n "$MAX_NUMBER_OF_ATOMS" ] || MAX_NUMBER_OF_ATOMS="$DEFAULT_MAX_NUMBER_OF_ATOMS"
[ -n "$RABBITMQ_MAX_NUMBER_OF_ATOMS" ] || RABBITMQ_MAX_NUMBER_OF_ATOMS="$MAX_NUMBER_OF_ATOMS"

## Common server defaults
SERVER_ERL_ARGS=" +P $RABBITMQ_MAX_NUMBER_OF_PROCESSES +t $RABBITMQ_MAX_NUMBER_OF_ATOMS +stbt $RABBITMQ_SCHEDULER_BIND_TYPE +zdbbl $RABBITMQ_DISTRIBUTION_BUFFER_SIZE "

##--- Set environment vars RABBITMQ_<var_name> to defaults if not set

# FIXME
[ "x" = "x$RABBITMQ_IO_THREAD_POOL_SIZE" ] && RABBITMQ_IO_THREAD_POOL_SIZE=${IO_THREAD_POOL_SIZE}
[ "x" = "x$RABBITMQ_SERVER_ERL_ARGS" ] && RABBITMQ_SERVER_ERL_ARGS=${SERVER_ERL_ARGS}
[ "x" = "x$RABBITMQ_SERVER_START_ARGS" ] && RABBITMQ_SERVER_START_ARGS=${SERVER_START_ARGS}
[ "x" = "x$RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS" ] && RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS=${SERVER_ADDITIONAL_ERL_ARGS}
[ "x" = "x$RABBITMQ_SERVER_CODE_PATH" ] && RABBITMQ_SERVER_CODE_PATH=${SERVER_CODE_PATH}
[ "x" = "x$RABBITMQ_IGNORE_SIGINT" ] && RABBITMQ_IGNORE_SIGINT="true"
[ "xtrue" = "x$RABBITMQ_IGNORE_SIGINT" ] && RABBITMQ_IGNORE_SIGINT_FLAG="+B i"

if [ -n "$saved_RABBITMQ_PID_FILE" ] && \
    [ "$saved_RABBITMQ_PID_FILE" != "$RABBITMQ_PID_FILE" ]
then
    _rmq_env_pwarn 'RABBITMQ_PID_FILE was already set by the init script to:' \
        "$saved_RABBITMQ_PID_FILE" \
        'The value set in rabbitmq-env.conf is ignored because it would break the init script.'

    RABBITMQ_PID_FILE="$saved_RABBITMQ_PID_FILE"
fi

[ "x" = "x$RABBITMQ_BOOT_MODULE" ] && RABBITMQ_BOOT_MODULE=${BOOT_MODULE}

##--- End of overridden <var_name> variables

_rmq_env_set_erl_libs()
{
    if [ -n "$ERL_LIBS" ]
    then
        export ERL_LIBS="$RABBITMQ_HOME/plugins:$ERL_LIBS"
    else
        export ERL_LIBS="$RABBITMQ_HOME/plugins"
    fi
}

run_escript()
{
    escript_main="${1:?escript_main must be defined}"
    shift
    escript="${1:?escript must be defined}"
    shift

    _rmq_env_set_erl_libs

    # Important: do not quote RABBITMQ_CTL_ERL_ARGS as they must be
    # word-split
    # shellcheck disable=SC2086
    exec erl +B \
        -boot "$CLEAN_BOOT_FILE" \
        -noinput -noshell -hidden -smp enable \
        $RABBITMQ_CTL_ERL_ARGS \
        -run escript start \
        -escript main "$escript_main" \
        -extra "$escript" "$@"
}

RABBITMQ_ENV_LOADED=1

# Since we source this elsewhere, don't accidentally stop execution
true