aboutsummaryrefslogtreecommitdiffstats
path: root/priv/templates/extended_bin.dtl
blob: 26c18b606a4ea231d2d265a1a84a467a2e083029 (plain) (blame)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/sh

set -e

SCRIPT_DIR="$(dirname "$0")"
RELEASE_ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
REL_NAME="{{ rel_name }}"
REL_VSN="{{ rel_vsn }}"
ERTS_VSN="{{ erts_vsn }}"
REL_DIR="$RELEASE_ROOT_DIR/releases/$REL_VSN"
ERL_OPTS="{{ erl_opts }}"
PIPE_DIR="/tmp/erl_pipes/{{ rel_name }}/"
RUNNER_LOG_DIR="${RUNNER_LOG_DIR:-$RELEASE_ROOT_DIR/log}"

find_erts_dir() {
    local erts_dir="$RELEASE_ROOT_DIR/erts-$ERTS_VSN"
    if [ -d "$erts_dir" ]; then
        ERTS_DIR="$erts_dir";
        ROOTDIR="$RELEASE_ROOT_DIR"
    else
        local erl="$(which erl)"
        local erl_root="$("$erl" -noshell -eval "io:format(\\"~s\\", [code:root_dir()])." -s init stop)"
        ERTS_DIR="$erl_root/erts-$ERTS_VSN"
        ROOTDIR="$erl_root"
    fi
}

# Connect to a remote node
relx_rem_sh() {
    # Generate a unique id used to allow multiple remsh to the same node
    # transparently
    id="remsh$(relx_gen_id)-${NAME}"

    # Setup remote shell command to control node
    exec "$BINDIR/erl" "$NAME_TYPE" "$id" -remsh "$NAME" -boot start_clean \
         -setcookie "$COOKIE"
}

# Generate a random id
relx_gen_id() {
    od -X /dev/urandom | head -n1 | cut -d ' ' -f2
}

# Control a node
relx_nodetool() {
    command="$1"; shift
    "erts-$ERTS_VSN/bin/escript" "$ROOTDIR/bin/nodetool" "$NAME_TYPE" "$NAME" \
                                 -setcookie "$COOKIE" "$command"
}

# Output a start command for the last argument of run_erl
relx_start_command() {
    printf "exec \"%s\" \"%s\"" "$RELEASE_ROOT_DIR/bin/$REL_NAME" \
           "$START_OPTION"
}

# Use $CWD/vm.args if exists, otherwise releases/APP_VSN/vm.args, or else etc/vm.args
if [ -z "$VMARGS_PATH" ]; then
    if [ -f "$RELEASE_ROOT_DIR/vm.args" ]; then
        VMARGS_PATH="$RELEASE_ROOT_DIR/vm.args"
        USE_DIR="$RELEASE_ROOT_DIR"
    else
        USE_DIR="$REL_DIR"
        if [ -f "$REL_DIR/vm.args" ]; then
            VMARGS_PATH="$REL_DIR/vm.args"
        else
            VMARGS_PATH="$REL_DIR/vm.args"
        fi
    fi
fi

# Make sure log directory exists
mkdir -p "$RUNNER_LOG_DIR"

# Use releases/VSN/sys.config if it exists otherwise use etc/app.config
if [ -z "$CONFIG_PATH" ]; then
    if [ -f "$USE_DIR/sys.config" ]; then
        CONFIG_PATH="$USE_DIR/sys.config"
    else
        if [ -f "$REL_DIR/sys.config" ]; then
            CONFIG_PATH="$REL_DIR/sys.config"
        else
            CONFIG_PATH="$REL_DIR/app.config"
        fi
    fi
fi

# Extract the target node name from node.args
NAME_ARG=$(egrep '^-s?name' "$VMARGS_PATH")
if [ -z "$NAME_ARG" ]; then
    echo "vm.args needs to have either -name or -sname parameter."
    exit 1
fi

# Extract the name type and name from the NAME_ARG for REMSH
NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
NAME="$(echo "$NAME_ARG" | awk '{print $2}')"

# Extract the target cookie
COOKIE_ARG="$(grep '^-setcookie' "$VMARGS_PATH")"
if [ -z "$COOKIE_ARG" ]; then
    echo "vm.args needs to have a -setcookie parameter."
    exit 1
fi

# Extract cookie name from COOKIE_ARG
COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')"

find_erts_dir
export ROOTDIR="$RELEASE_ROOT_DIR"
export BINDIR="$ERTS_DIR/bin"
export EMU="beam"
export PROGNAME="erl"
export LD_LIBRARY_PATH="$ERTS_DIR/lib:$LD_LIBRARY_PATH"

cd "$ROOTDIR"

# Check the first argument for instructions
case "$1" in
    start|start_boot)

        # Make sure there is not already a node running
        #RES=`$NODETOOL ping`
        #if [ "$RES" = "pong" ]; then
        #    echo "Node is already running!"
        #    exit 1
        #fi
        case "$1" in
            start)
                shift
                START_OPTION="console"
                HEART_OPTION="start"
                ;;
            start_boot)
                shift
                START_OPTION="console_boot"
                HEART_OPTION="start_boot"
                ;;
        esac
        RUN_PARAM="$@"

        # Set arguments for the heart command
        set -- "$SCRIPT_DIR/$REL_NAME" "$HEART_OPTION"
        [ "$RUN_PARAM" ] && set -- "$@" "$RUN_PARAM"
        
        # Export the HEART_COMMAND
        HEART_COMMAND="$@"
        export HEART_COMMAND

        mkdir -p "$PIPE_DIR"

        "$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
                          "$(relx_start_command)"
        ;;

    stop)
        # Wait for the node to completely stop...
        case $(uname -s) in
            Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD)
                # PID COMMAND
                PID=$(ps ax -o pid= -o command=|
                      grep "$SCRIPT_DIR/.*/[b]eam"|awk '{print $1}')
                ;;
            SunOS)
                # PID COMMAND
                PID=$(ps -ef -o pid= -o args=|
                      grep "$SCRIPT_DIR/.*/[b]eam"|awk '{print $1}')
                ;;
            CYGWIN*)
                # UID PID PPID TTY STIME COMMAND
                PID=$(ps -efw|grep "$SCRIPT_DIR/.*/[b]eam"|awk '{print $2}')
                ;;
        esac
        relx_nodetool "stop"
        ES="$?"
        if [ "$ES" -ne 0 ]; then
            exit "$ES"
        fi
        while $(kill -0 "$PID" 2>/dev/null);
        do
            sleep 1
        done
        ;;

    restart)
        ## Restart the VM without exiting the process
        relx_nodetool "restart"
        ES="$?"
        if [ "$ES" -ne 0 ]; then
            exit $ES
        fi
        ;;

    reboot)
        ## Restart the VM completely (uses heart to restart it)
        relx_nodetool "reboot"
        ES="$?"
        if [ "$ES" -ne 0 ]; then
            exit $ES
        fi
        ;;

    ping)
        ## See if the VM is alive
        relx_nodetool "ping"
        ES="$?"
        if [ "$ES" -ne 0 ]; then
            exit $ES
        fi
        ;;

    attach)
        # Make sure a node IS running
        RES="$(relx_nodetool "ping")"
        ES="$?"
        if [ "$ES" -ne 0 ]; then
            echo "Node is not running!"
            exit $ES
        fi

        shift
        exec "$BINDIR/to_erl" "$PIPE_DIR"
        ;;

    remote_console)
        # Make sure a node IS running
        RES="$(relx_nodetool "ping")"
        ES="$?"
        if [ "$ES" -ne 0 ]; then
            echo "Node is not running!"
            exit $ES
        fi

        shift
        relx_rem_sh
        ;;

    upgrade|downgrade|install)
        if [ -z "$2" ]; then
            echo "Missing package argument"
            echo "Usage: $REL_NAME $1 {package base name}"
            echo "NOTE {package base name} MUST NOT include the .tar.gz suffix"
            exit 1
        fi

        # Make sure a node IS running
        RES="$(relx_nodetool "ping")"
        ES="$?"
        if [ "$ES" -ne 0 ]; then
            echo "Node is not running!"
            exit $ES
        fi

        exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
             "$REL_NAME" "$NAME" "$COOKIE" "$2"
        ;;

    console|console_clean|console_boot)
        # .boot file typically just $REL_NAME (ie, the app name)
        # however, for debugging, sometimes start_clean.boot is useful.
        # For e.g. 'setup', one may even want to name another boot script.
        case "$1" in
            console)
                if [ "$REL_DIR/$REL_NAME.boot" ]; then
                  BOOTFILE="$REL_DIR/$REL_NAME"
                else
                  BOOTFILE="$REL_DIR/start"
                fi
                ;;
            console_clean)
                BOOTFILE="$ROOTDIR/bin/start_clean"
                ;;
            console_boot)
                shift
                BOOTFILE="$1"
                shift
                ;;
        esac
        # Setup beam-required vars
        EMU="beam"
        PROGNAME="${0#*/}"

        export EMU
        export PROGNAME

        # Store passed arguments since they will be erased by `set`
        ARGS="$@"   

        # Build an array of arguments to pass to exec later on
        # Build it here because this command will be used for logging.
        set -- "$BINDIR/erlexec" -boot "$BOOTFILE" \
            -env ERL_LIBS "$REL_DIR/lib" -config "$CONFIG_PATH" \
            -args_file "$VMARGS_PATH"

        # Dump environment info for logging purposes
        echo "Exec: $@ -- ${1+$ARGS}"
        echo "Root: $ROOTDIR"

        # Log the startup
        echo "$RELEASE_ROOT_DIR"
        logger -t "$REL_NAME[$$]" "Starting up"

        # Start the VM
        exec "$@" -- "${1+$ARGS}"
        ;;

    foreground)
        # start up the release in the foreground for use by runit
        # or other supervision services

        [ -f "$REL_DIR/$REL_NAME.boot" ] && BOOTFILE="$REL_NAME" || BOOTFILE=start
        FOREGROUNDOPTIONS="-noinput +Bd"

        # Setup beam-required vars
        EMU=beam
        PROGNAME="${0#*/}"

        export EMU
        export PROGNAME

        # Store passed arguments since they will be erased by `set`
        ARGS="$@"   

        # Build an array of arguments to pass to exec later on
        # Build it here because this command will be used for logging.
        set -- "$BINDIR/erlexec" "$FOREGROUNDOPTIONS" \
            -boot "$REL_DIR/$BOOTFILE" -mode embedded -config "$CONFIG_PATH" \
            -args_file "$VMARGS_PATH"

        # Dump environment info for logging purposes
        echo "Exec: $@" -- "${1+$ARGS}"
        echo "Root: $ROOTDIR"

        # Start the VM
        exec "$@" -- "${1+$ARGS}"
        ;;
    *)
        echo "Usage: $REL_NAME {start|start_boot <file>|foreground|stop|restart|reboot|ping|console|console_clean|console_boot <file>|attach|remote_console|upgrade}"
        exit 1
        ;;
esac

exit 0