-
Notifications
You must be signed in to change notification settings - Fork 231
/
extended_bin
1057 lines (946 loc) · 35.2 KB
/
extended_bin
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
set -e
# http://erlang.org/doc/man/run_erl.html
# If defined, disables input and output flow control for the pty
# opend by run_erl. Useful if you want to remove any risk of accidentally
# blocking the flow control by using Ctrl-S (instead of Ctrl-D to detach),
# which can result in blocking of the entire Beam process, and in the case
# of running heart as supervisor even the heart process becomes blocked
# when writing log message to terminal, leaving the heart process unable
# to do its work.
RUN_ERL_DISABLE_FLOWCNTRL=${RUN_ERL_DISABLE_FLOWCNTRL:-true}
export RUN_ERL_DISABLE_FLOWCNTRL
if [ "$TERM" = "dumb" ] || [ -z "$TERM" ]; then
export TERM=screen
fi
# OSX does not support readlink '-f' flag, work
# around that
# shellcheck disable=SC2039,SC3000-SC4000
case $OSTYPE in
darwin*)
SCRIPT=$(readlink "$0" || true)
;;
*)
SCRIPT=$(readlink -f "$0" || true)
;;
esac
[ -z "$SCRIPT" ] && SCRIPT=$0
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT")" && pwd -P)"
RELEASE_ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd -P)"
# Make the value available to variable substitution calls below
export REL_NAME="{{ rel_name }}"
REL_VSN="{{ rel_vsn }}"
# export these to match mix release environment variables
export RELEASE_NAME="{{ rel_name }}"
export RELEASE_VSN="{{ rel_vsn }}"
export RELEASE_PROG="${SCRIPT}"
ERTS_VSN="{{ erts_vsn }}"
REL_DIR="$RELEASE_ROOT_DIR/releases/$REL_VSN"
RUNNER_LOG_DIR="${RUNNER_LOG_DIR:-$RELEASE_ROOT_DIR/log}"
export ESCRIPT_NAME="${ESCRIPT_NAME-$SCRIPT}"
# if RELX_RPC_TIMEOUT is set then use that
# otherwise check for NODETOOL_TIMEOUT and convert to seconds
if [ -z "$RELX_RPC_TIMEOUT" ]; then
# if NODETOOL_TIMEOUT exists then turn the old nodetool timeout into the rpc timeout
if [ -n "$NODETOOL_TIMEOUT" ]; then
# will exit the script if NODETOOL_TIMEOUT isn't a number
RELX_RPC_TIMEOUT=$((NODETOOL_TIMEOUT / 1000))
else
RELX_RPC_TIMEOUT=60
fi
fi
export RELX_RPC_TIMEOUT
# start/stop/install/upgrade pre/post hooks
PRE_START_HOOKS="{{{ pre_start_hooks }}}"
POST_START_HOOKS="{{{ post_start_hooks }}}"
PRE_STOP_HOOKS="{{{ pre_stop_hooks }}}"
POST_STOP_HOOKS="{{{ post_stop_hooks }}}"
PRE_INSTALL_UPGRADE_HOOKS="{{{ pre_install_upgrade_hooks }}}"
POST_INSTALL_UPGRADE_HOOKS="{{{ post_install_upgrade_hooks }}}"
STATUS_HOOK="{{{ status_hook }}}"
EXTENSIONS="{{{ extensions }}}"
relx_usage() {
command="$1"
case "$command" in
rpc)
echo "Usage: $REL_NAME rpc [Mod [Fun [Args]]]]"
echo "Applies the specified function and returns the result."
echo "Mod must be specified. However, start and [] are assumed"
echo "for unspecified Fun and Args, respectively. Args is to "
echo "be in the same format as for erlang:apply/3 in ERTS."
;;
"eval")
echo "Usage: $REL_NAME eval [Exprs]"
echo "Executes a sequence of Erlang expressions, separated by"
echo "comma (,) and ended with a full stop (.)"
;;
foreground)
echo "Usage: $REL_NAME foreground"
echo "Starts the release in the foreground, meaning all output"
echo "going to stdout but without an interactive shell."
;;
console)
echo "Usage: $REL_NAME console"
echo "Starts the release in the with an interactive shell."
;;
remote_console|remote|remsh)
echo "Usage: $REL_NAME remote"
echo "Attach a remote shell to an already running Erlang node for this release."
;;
unpack)
echo "Usage: $REL_NAME unpack [VERSION]"
echo "Unpacks a release package VERSION, it assumes that this"
echo "release package tarball has already been deployed at one"
echo "of the following locations:"
echo " releases/<relname>-<version>.tar.gz"
echo " releases/<version>/<relname>-<version>.tar.gz"
echo " releases/<version>/<relname>.tar.gz"
;;
install)
echo "Usage: $REL_NAME install [VERSION]"
echo "Installs a release package VERSION, it assumes that this"
echo "release package tarball has already been deployed at one"
echo "of the following locations:"
echo " releases/<relname>-<version>.tar.gz"
echo " releases/<version>/<relname>-<version>.tar.gz"
echo " releases/<version>/<relname>.tar.gz"
echo ""
echo " --no-permanent Install release package VERSION but"
echo " don't make it permanent"
;;
uninstall)
echo "Usage: $REL_NAME uninstall [VERSION]"
echo "Uninstalls a release VERSION, it will only accept"
echo "versions that are not currently in use"
;;
upgrade)
echo "Usage: $REL_NAME upgrade [VERSION]"
echo "Upgrades the currently running release to VERSION, it assumes"
echo "that a release package tarball has already been deployed at one"
echo "of the following locations:"
echo " releases/<relname>-<version>.tar.gz"
echo " releases/<version>/<relname>-<version>.tar.gz"
echo " releases/<version>/<relname>.tar.gz"
echo ""
echo " --no-permanent Install release package VERSION but"
echo " don't make it permanent"
;;
downgrade)
echo "Usage: $REL_NAME downgrade [VERSION]"
echo "Downgrades the currently running release to VERSION, it assumes"
echo "that a release package tarball has already been deployed at one"
echo "of the following locations:"
echo " releases/<relname>-<version>.tar.gz"
echo " releases/<version>/<relname>-<version>.tar.gz"
echo " releases/<version>/<relname>.tar.gz"
echo ""
echo " --no-permanent Install release package VERSION but"
echo " don't make it permanent"
;;
status)
echo "Usage: $REL_NAME status"
echo "Obtains node status information through optionally defined hooks."
;;
*)
# check for extension
IS_EXTENSION=$(relx_is_extension "$command")
if [ "$IS_EXTENSION" = "1" ]; then
EXTENSION_SCRIPT=$(relx_get_extension_script "$command")
relx_run_extension "$EXTENSION_SCRIPT" help
else
EXTENSIONS=$(echo $EXTENSIONS | sed -e 's/|undefined//g')
echo "Usage: ${REL_NAME} [COMMAND] [ARGS]
Commands:
foreground Start release with output to stdout
remote_console Connect remote shell to running node
rpc [Mod [Fun [Args]]]] Run apply(Mod, Fun, Args) on running node
eval [Exprs] Run expressions on running node
stop Stop the running node
restart Restart the applications but not the VM
reboot Reboot the entire VM
pid Print the PID of the OS process
ping Print pong if the node is alive
console Start the release with an interactive shell
console_clean Start an interactive shell without the release's applications
console_boot [File] Start an interactive shell for boot script [File]
daemon Start release in the background with run_erl (named pipes)
daemon_boot [File] Start boot script [File] in the background with run_erl (named pipes)
daemon_attach Connect to node started as daemon with to_erl (named pipes)
upgrade [Version] Upgrade the running release to a new version
downgrade [Version] Downgrade the running release to a new version
install [Version] Install a release
uninstall [Version] Uninstall a release
unpack [Version] Unpack a release tarball
versions Print versions of the release available
escript Run an escript in the same environment as the release
status Verify node is running and then run status hook scripts
$EXTENSIONS"
fi
;;
esac
}
find_erts_dir() {
__erts_dir="$RELEASE_ROOT_DIR/erts-$ERTS_VSN"
if [ -d "$__erts_dir" ]; then
ERTS_DIR="$__erts_dir";
else
__erl="$(command -v erl)"
code="io:format(\"~s\", [code:root_dir()]), halt()."
__erl_root="$("$__erl" -boot no_dot_erlang -sasl errlog_type error -noshell -eval "$code")"
ERTS_DIR="$__erl_root/erts-$ERTS_VSN"
if [ ! -d "$ERTS_DIR" ]; then
erts_version_code="io:format(\"~s\", [erlang:system_info(version)]), halt()."
__erts_version="$("$__erl" -boot no_dot_erlang -sasl errlog_type error -noshell -eval "$erts_version_code")"
ERTS_DIR="${__erl_root}/erts-${__erts_version}"
if [ -d "$ERTS_DIR" ]; then
echo "Exact ERTS version (${ERTS_VSN}) match not found, instead using ${__erts_version}. The release may fail to run." 1>&2
ERTS_VSN=${__erts_version}
else
echo "Can not run the release. There is no ERTS bundled with the release or found on the system."
exit 1
fi
fi
fi
}
find_erl_call() {
# users who depend on stdout when running rpc calls must still use nodetool
# so we have an overload option to force use of nodetool instead of erl_call
if [ "$USE_NODETOOL" ]; then
ERL_RPC=relx_nodetool
else
# only OTP-23 and above have erl_call in the erts bin directory
# and only those versions have the features and bug fixes needed
# to work properly with this script
__erl_call="$ERTS_DIR/bin/erl_call"
if [ -f "$__erl_call" ]; then
ERL_RPC="$__erl_call";
else
ERL_RPC=relx_nodetool
fi
fi
}
# Get node pid
relx_get_pid() {
if output="$(erl_rpc os getpid 2>/dev/null)"
then
echo "$output" | sed -e 's/"//g'
return 0
else
echo "$output"
return 1
fi
}
ping_or_exit() {
if ! erl_rpc erlang is_alive > /dev/null 2>&1; then
echo "Node is not running!"
exit 1
fi
}
relx_get_nodename() {
id="longname$(relx_gen_id)-${NAME}"
if [ -z "$COOKIE" ]; then
# shellcheck disable=SC2086
"$BINDIR/erl" -boot "$REL_DIR"/start_clean \
-mode interactive \
-boot_var SYSTEM_LIB_DIR "$SYSTEM_LIB_DIR" \
-eval '[_,H]=re:split(atom_to_list(node()),"@",[unicode,{return,list}]), io:format("~s~n",[H]), halt()' \
-dist_listen false \
${START_EPMD} \
-noshell "${NAME_TYPE}" "$id"
else
# running with setcookie prevents a ~/.erlang.cookie from being created
# shellcheck disable=SC2086
"$BINDIR/erl" -boot "$REL_DIR"/start_clean \
-mode interactive \
-boot_var SYSTEM_LIB_DIR "$SYSTEM_LIB_DIR" \
-eval '[_,H]=re:split(atom_to_list(node()),"@",[unicode,{return,list}]), io:format("~s~n",[H]), halt()' \
-setcookie "${COOKIE}" \
-dist_listen false \
${START_EPMD} \
-noshell "${NAME_TYPE}" "$id"
fi
}
# Connect to a remote node
relx_rem_sh() {
# Remove remote_nodename when OTP-23 is the oldest version supported by rebar3/relx.
# sort the used erts version against 11.0 to see if it is less than 11.0 (OTP-23)
# if it is then we must generate a node name to use for the remote node.
# But this feature is only for short names in 23.0 (erts 11.0). It can be used
# for long names with 23.1 (erts 11.1) and above.
if [ "${NAME_TYPE}" = "-sname" ] && [ "11.0" = "$(printf "%s\n11.0" "${ERTS_VSN}" | sort -V | head -n1)" ] ; then
remote_nodename="${NAME_TYPE} undefined@${RELX_HOSTNAME}"
# if the name type is longnames then make sure this is erts 11.1+
elif [ "${NAME_TYPE}" = "-name" ] && [ "11.1" = "$(printf "%s\n11.1" "${ERTS_VSN}" | sort -V | head -n1)" ] ; then
remote_nodename="${NAME_TYPE} undefined@${RELX_HOSTNAME}"
else
# Generate a unique id used to allow multiple remsh to the same node transparently
remote_nodename="${NAME_TYPE} remsh$(relx_gen_id)-${NAME}"
fi
# Get the node's ticktime so that we use the same one
TICKTIME="$(erl_rpc net_kernel get_net_ticktime)"
# Setup remote shell command to control node
# -dist_listen is new in OTP-23. It keeps the remote node from binding to a listen port
# and implies the option -hidden
# shellcheck disable=SC2086
exec "$BINDIR/erl" ${remote_nodename} -remsh "$NAME" -boot "$REL_DIR"/start_clean -mode interactive \
-boot_var SYSTEM_LIB_DIR "$SYSTEM_LIB_DIR" \
-setcookie "$COOKIE" -hidden -kernel net_ticktime "$TICKTIME" \
-dist_listen false \
$DIST_ARGS \
$EXTRA_DIST_ARGS
}
erl_rpc() {
case "$ERL_RPC" in
"relx_nodetool")
relx_nodetool rpc "$@"
;;
*)
command=$*
# erl_call -R is recommended for generating dynamic node name but is only available in 23.0+
if [ "11.0" = "$(printf "%s\n11.0" "${ERTS_VSN}" | sort -V | head -n1)" ] ; then
DYNAMIC_NAME="-R"
else
DYNAMIC_NAME="-r"
fi
if [ "$ADDRESS" ]; then
result=$("$ERL_RPC" "${DYNAMIC_NAME}" -c "${COOKIE}" -address "${ADDRESS}" -timeout "${RELX_RPC_TIMEOUT}" -a "${command}")
else
result=$("$ERL_RPC" "$NAME_TYPE" "$NAME" "${DYNAMIC_NAME}" -c "${COOKIE}" -timeout "${RELX_RPC_TIMEOUT}" -a "${command}")
fi
code=$?
if [ $code -eq 0 ]; then
echo "$result"
else
return $code
fi
;;
esac
}
erl_eval() {
case "$ERL_RPC" in
"relx_nodetool")
relx_nodetool eval "$@"
;;
*)
command=$*
if [ "$ERL_DIST_PORT" ]; then
result=$(echo "${command}" | "$ERL_RPC" "${DYNAMIC_NAME}" -c "${COOKIE}" -address "${ADDRESS}" -timeout "${RELX_RPC_TIMEOUT}" -e)
else
result=$(echo "${command}" | "$ERL_RPC" "$NAME_TYPE" "$NAME" "${DYNAMIC_NAME}" -c "${COOKIE}" -timeout "${RELX_RPC_TIMEOUT}" -e)
fi
code=$?
if [ $code -eq 0 ]; then
echo "$result" | sed 's/^{ok, \(.*\)}$/\1/'
else
return $code
fi
;;
esac
}
# Generate a random id
relx_gen_id() {
# To prevent exhaustion of atoms on target node, optionally avoid
# generation of random node prefixes, if it is guaranteed calls
# are entirely sequential.
if [ -z "${NODETOOL_NODE_PREFIX}" ]; then
dd count=1 bs=4 if=/dev/urandom 2> /dev/null | od -x | head -n1 | awk '{print $2$3}'
else
echo "${NODETOOL_NODE_PREFIX}"
fi
}
# Control a node with nodetool if erl_call isn't from OTP-23+
relx_nodetool() {
command="$1"; shift
# Generate a unique id used to allow multiple nodetool calls to the
# same node transparently
nodetool_id="maint$(relx_gen_id)-${NAME}"
if [ -z "${START_EPMD}" ]; then
ERL_FLAGS="${ERL_FLAGS} ${DIST_ARGS} ${EXTRA_DIST_ARGS} ${NAME_TYPE} $nodetool_id -setcookie ${COOKIE}" \
"$ERTS_DIR/bin/escript" \
"$ROOTDIR/bin/nodetool" \
"$NAME_TYPE" "$NAME" \
"$command" "$@"
else
# shellcheck disable=SC2086
ERL_FLAGS="${ERL_FLAGS} ${DIST_ARGS} ${EXTRA_DIST_ARGS} ${NAME_TYPE} $nodetool_id -setcookie ${COOKIE}" \
"$ERTS_DIR/bin/escript" \
"$ROOTDIR/bin/nodetool" \
$START_EPMD "$NAME_TYPE" "$NAME" "$command" "$@"
fi
}
# Run an escript in the node's environment
relx_escript() {
scriptpath="$1"; shift
export RELEASE_ROOT_DIR
"$ERTS_DIR/bin/escript" "$ROOTDIR/$scriptpath" "$@"
}
# Convert {127,0,0,1} to 127.0.0.1 (inet:ntoa/1)
addr_tuple_to_str() {
addr="$1"
saved_IFS="$IFS"
IFS="{,}'\" "
# shellcheck disable=SC2086
eval set -- $addr
IFS="$saved_IFS"
case $# in
4) printf '%u.%u.%u.%u' "$@";;
8) printf '%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x' "$@";;
*) echo "Cannot parse IP address tuple: '$addr'" 1>&2;;
esac
}
make_out_file_path() {
# Use output directory provided in the RELX_OUT_FILE_PATH environment variable
# (default to the current location of vm.args and sys.config)
DIR=$(dirname "$1")
[ -d "${RELX_OUT_FILE_PATH}" ] && DIR="${RELX_OUT_FILE_PATH}"
FILE=$(basename "$1")
IN="${DIR}/${FILE}"
PFX=$(echo "$IN" | awk '{sub(/\.[^.]+$/, "", $0)}1')
SFX=$(echo "$FILE" | awk -F . '{if (NF>1) print $NF}')
if [ "$RELX_MULTI_NODE" ]; then
echo "${PFX}.${NAME}.${SFX}"
else
echo "${PFX}.${SFX}"
fi
}
# Replace environment variables
replace_os_vars() {
awk '{
while(match($0,"[$]{[^}]*}")) {
var=substr($0,RSTART+2,RLENGTH -3)
slen=split(var,arr,":-")
v=arr[1]
e=ENVIRON[v]
gsub("&","\\\\\\&",e)
if(slen > 1 && e=="") {
i=index(var, ":-"arr[2])
def=substr(var,i+2)
gsub("[$]{"var"}",def)
} else {
gsub("[$]{"var"}",e)
}
}
}1' < "$1" > "$2"
}
add_path() {
# Use $CWD/$1 if exists, otherwise releases/VSN/$1
IN_FILE_PATH=$2
if [ -z "$IN_FILE_PATH" ]; then
if [ -f "$RELEASE_ROOT_DIR/$1" ]; then
IN_FILE_PATH="$RELEASE_ROOT_DIR/$1"
else
IN_FILE_PATH="$REL_DIR/$1"
fi
fi
echo "$IN_FILE_PATH"
}
check_replace_os_vars() {
IN_FILE_PATH=$(add_path "$1" "$2")
OUT_FILE_PATH="$IN_FILE_PATH"
SRC_FILE_PATH="$IN_FILE_PATH.src"
ORIG_FILE_PATH="$IN_FILE_PATH.orig"
if [ -f "$SRC_FILE_PATH" ]; then
OUT_FILE_PATH=$(make_out_file_path "$IN_FILE_PATH")
replace_os_vars "$SRC_FILE_PATH" "$OUT_FILE_PATH"
elif [ "$RELX_REPLACE_OS_VARS" ]; then
OUT_FILE_PATH=$(make_out_file_path "$IN_FILE_PATH")
# If vm.args.orig or sys.config.orig is present then use that
if [ -f "$ORIG_FILE_PATH" ]; then
IN_FILE_PATH="$ORIG_FILE_PATH"
fi
# apply the environment variable substitution to $IN_FILE_PATH
# the result is saved to $OUT_FILE_PATH
# if they are both the same, then ensure that we don't clobber
# the file by saving a backup with the .orig extension
if [ "$IN_FILE_PATH" = "$OUT_FILE_PATH" ]; then
cp "$IN_FILE_PATH" "$ORIG_FILE_PATH"
replace_os_vars "$ORIG_FILE_PATH" "$OUT_FILE_PATH"
else
replace_os_vars "$IN_FILE_PATH" "$OUT_FILE_PATH"
fi
else
# If vm.arg.orig or sys.config.orig is present then use that
if [ -f "$ORIG_FILE_PATH" ]; then
OUT_FILE_PATH=$(make_out_file_path "$IN_FILE_PATH")
cp "$ORIG_FILE_PATH" "$OUT_FILE_PATH"
fi
fi
echo "$OUT_FILE_PATH"
}
relx_run_hooks() {
HOOKS=$1
for hook in $HOOKS
do
# the scripts arguments at this point are separated
# from each other by | , we now replace these
# by empty spaces and give them to the `set`
# command in order to be able to extract them
# separately
# shellcheck disable=SC2046
set $(echo "$hook" | sed -e 's/|/ /g')
HOOK_SCRIPT=$1; shift
# all hook locations are expected to be
# relative to the start script location
# shellcheck disable=SC1090,SC2240
[ -f "$SCRIPT_DIR/$HOOK_SCRIPT" ] && . "$SCRIPT_DIR/$HOOK_SCRIPT" "$@"
done
}
relx_disable_hooks() {
PRE_START_HOOKS=""
POST_START_HOOKS=""
PRE_STOP_HOOKS=""
POST_STOP_HOOKS=""
PRE_INSTALL_UPGRADE_HOOKS=""
POST_INSTALL_UPGRADE_HOOKS=""
STATUS_HOOK=""
}
relx_is_extension() {
EXTENSION=$1
case "$EXTENSION" in
{{{ extensions }}})
echo "1"
;;
*)
echo "0"
;;
esac
}
relx_get_extension_script() {
EXTENSION=$1
# below are the extensions declarations
# of the form:
# foo_extension="path/to/foo_script";bar_extension="path/to/bar_script"
{{{extension_declarations}}}
# get the command extension (eg. foo) and
# obtain the actual script filename that it
# refers to (eg. "path/to/foo_script"
eval echo "$""${EXTENSION}_extension"
}
relx_run_extension() {
# drop the first argument which is the name of the
# extension script
EXTENSION_SCRIPT=$1
shift
# all extension script locations are expected to be
# relative to the start script location
# shellcheck disable=SC1090,SC2240
[ -f "$SCRIPT_DIR/$EXTENSION_SCRIPT" ] && . "$SCRIPT_DIR/$EXTENSION_SCRIPT" "$@"
}
# given a list of arguments, identify the internal ones
# --relx-disable-hooks
# and process them accordingly
process_internal_args() {
for arg in "$@"
do
shift
case "$arg" in
--relx-disable-hooks)
relx_disable_hooks
;;
*)
;;
esac
done
}
# process internal arguments
process_internal_args "$@"
find_erts_dir
find_erl_call
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"
SYSTEM_LIB_DIR="$(dirname "$ERTS_DIR")/lib"
VMARGS_PATH=$(add_path vm.args "$VMARGS_PATH")
VMARGS_PATH=$(check_replace_os_vars vm.args "$VMARGS_PATH")
RELX_CONFIG_PATH=$(check_replace_os_vars sys.config "$RELX_CONFIG_PATH")
# Check vm.args and other files referenced via -args_file parameters for:
# - nonexisting -args_files
# - circular dependencies of -args_files
# - relative paths in -args_file parameters
# - multiple/mixed occurrences of -name and -sname parameters
# - missing -name or -sname parameters
# If all checks pass, extract the target node name
set +e
TMP_NAME_ARG=$(awk 'function check_name(file)
{
# if file exists, then it should be readable
if (system("test -f "file) == 0 && system("test -r "file) != 0) {
print file" not readable"
exit 3
}
while ((getline line<file)>0) {
if (line~/^-args_file +/) {
gsub(/^-args_file +| *$/, "", line)
if (line in files) {
print "circular reference to "line" encountered in "file
exit 5
}
files[line]=line
check_name(line)
}
else if (line~/^-s?name +/) {
if (name!="") {
print "\""line"\" parameter found in "file" but already specified as \""name"\""
exit 2
}
name=line
}
}
}
BEGIN {
split("", files)
name=""
}
{
files[FILENAME]=FILENAME
check_name(FILENAME)
if (name=="") {
print "need to have exactly one of either -name or -sname parameters but none found"
exit 1
}
print name
exit 0
}' "$VMARGS_PATH")
TMP_NAME_ARG_RC=$?
case $TMP_NAME_ARG_RC in
0) NAME_ARG="$TMP_NAME_ARG";;
*) echo "$TMP_NAME_ARG"
exit $TMP_NAME_ARG_RC;;
esac
unset TMP_NAME_ARG
unset TMP_NAME_ARG_RC
set -e
# Perform replacement of variables in ${NAME_ARG}
NAME_ARG=$(eval echo "${NAME_ARG}")
# 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 dist arguments
DIST_ARGS=""
PROTO_DIST="$(grep '^-proto_dist' "$VMARGS_PATH" || true)"
if [ "$PROTO_DIST" ]; then
DIST_ARGS="${PROTO_DIST}"
fi
START_EPMD="$(grep '^-start_epmd' "$VMARGS_PATH" || true)"
if [ "$START_EPMD" ]; then
DIST_ARGS="${DIST_ARGS} ${START_EPMD}"
fi
EPMD_MODULE="$(grep '^-epmd_module' "$VMARGS_PATH" || true)"
if [ "$EPMD_MODULE" ]; then
DIST_ARGS="${DIST_ARGS} ${EPMD_MODULE}"
fi
INET_DIST_USE_INTERFACE="$(grep '^-kernel *inet_dist_use_interface' "$VMARGS_PATH" || true)"
if [ "$INET_DIST_USE_INTERFACE" ]; then
DIST_ARGS="${DIST_ARGS} ${INET_DIST_USE_INTERFACE}"
fi
if [ "$ERL_DIST_PORT" ]; then
if [ "$INET_DIST_USE_INTERFACE" ]; then
ADDRESS="$(addr_tuple_to_str "${INET_DIST_USE_INTERFACE#*inet_dist_use_interface }"):$ERL_DIST_PORT"
else
ADDRESS="$ERL_DIST_PORT"
fi
if [ "11.1" = "$(printf "%s\n11.1" "${ERTS_VSN}" | sort -V | head -n1)" ] ; then
# unless set by the user, set start_epmd to false when ERL_DIST_PORT is used
if [ ! "$START_EPMD" ]; then
EXTRA_DIST_ARGS="-erl_epmd_port ${ERL_DIST_PORT} -start_epmd false"
else
EXTRA_DIST_ARGS="-erl_epmd_port ${ERL_DIST_PORT}"
fi
else
ERL_DIST_PORT_WARNING="ERL_DIST_PORT is set and used to set the port, but doing so on ERTS version ${ERTS_VSN} means remsh/rpc will not work for this release"
if ! command -v logger > /dev/null 2>&1
then
echo "WARNING: ${ERL_DIST_PORT_WARNING}"
else
logger -p warning -t "${REL_NAME}[$$]" "${ERL_DIST_PORT_WARNING}"
fi
EXTRA_DIST_ARGS="-kernel inet_dist_listen_min ${ERL_DIST_PORT} -kernel inet_dist_listen_max ${ERL_DIST_PORT}"
fi
fi
# Extract the target cookie
# Do this before relx_get_nodename so we can use it and not create a ~/.erlang.cookie
if [ -n "$RELX_COOKIE" ]; then
COOKIE="$RELX_COOKIE"
else
COOKIE_ARG="$(grep '^-setcookie' "$VMARGS_PATH" || true)"
DEFAULT_COOKIE_FILE="$HOME/.erlang.cookie"
if [ -z "$COOKIE_ARG" ]; then
if [ -f "$DEFAULT_COOKIE_FILE" ]; then
COOKIE="$(cat "$DEFAULT_COOKIE_FILE")"
else
echo "No cookie is set or found. This limits the scripts functionality, installing, upgrading, rpc and getting a list of versions will not work."
fi
else
# Extract cookie name from COOKIE_ARG
COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')"
fi
fi
# User can specify an sname without @hostname
# This will fail when creating remote shell
# So here we check for @ and add @hostname if missing
case "${NAME}" in
*@*) ;; # Nothing to do
*) NAME=${NAME}@$(relx_get_nodename);; # Add @hostname
esac
# Export the variable so that it's available in the 'eval' calls
export NAME
# create a variable of just the hostname part of the nodename
RELX_HOSTNAME=$(echo "${NAME}" | cut -d'@' -f2)
test -z "$PIPE_DIR" && PIPE_BASE_DIR='/tmp/erl_pipes/'
PIPE_DIR="${PIPE_DIR:-/tmp/erl_pipes/$NAME/}"
cd "$ROOTDIR"
# Check the first argument for instructions
case "$1" in
start|daemon|daemon_boot)
case "$1" in
daemon)
shift
START_OPTION="console"
HEART_OPTION="daemon"
;;
daemon_boot)
shift
START_OPTION="console_boot"
HEART_OPTION="daemon_boot"
;;
start)
# TODO, add here the right annoying message asking users to consider
# instead using systemd or some such other init system
echo "'start' has been deprecated, replaced by 'daemon' and will be removed in the short-term, please consult rebar3.org on why you should be"\
"using 'foreground' and an init tool such as 'systemd'"
shift
START_OPTION="console"
HEART_OPTION="daemon"
;;
esac
ARGS="$(printf "'%s' " "$@")"
export HEART_COMMAND="\"${RELEASE_ROOT_DIR}/bin/${REL_NAME}\" \"${HEART_OPTION}\" ${ARGS}"
# shellcheck disable=SC2174
test -z "$PIPE_BASE_DIR" || mkdir -m 1777 -p "$PIPE_BASE_DIR"
mkdir -p "$PIPE_DIR"
if [ ! -w "$PIPE_DIR" ]
then
echo "failed to start, user '$USER' does not have write privileges on '$PIPE_DIR', either delete it or run node as a different user"
exit 1
fi
# Make sure log directory exists
mkdir -p "$RUNNER_LOG_DIR"
relx_run_hooks "$PRE_START_HOOKS"
"$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
"exec \"$RELEASE_ROOT_DIR/bin/$REL_NAME\" \"$START_OPTION\" $ARGS --relx-disable-hooks"
# wait for node to be up before running hooks
while ! erl_rpc erlang is_alive > /dev/null 2>&1
do
sleep 1
done
relx_run_hooks "$POST_START_HOOKS"
;;
stop)
relx_run_hooks "$PRE_STOP_HOOKS"
# Wait for the node to completely stop...
PID="$(relx_get_pid)"
if ! erl_rpc init stop > /dev/null 2>&1; then
exit 1
fi
while kill -s 0 "$PID" 2>/dev/null;
do
sleep 1
done
# wait for node to be down before running hooks
while erl_rpc erlang is_alive > /dev/null 2>&1
do
sleep 1
done
relx_run_hooks "$POST_STOP_HOOKS"
;;
restart)
## Restart the VM without exiting the process
if ! erl_rpc init restart > /dev/null; then
exit 1
fi
;;
reboot)
## Restart the VM completely (uses heart to restart it)
if ! erl_rpc init reboot > /dev/null; then
exit 1
fi
;;
pid)
## Get the VM's pid
if ! relx_get_pid; then
exit 1
fi
;;
ping)
## See if the VM is alive
ping_or_exit
echo "pong"
;;
escript)
## Run an escript under the node's environment
shift
if ! relx_escript "$@"; then
exit 1
fi
;;
daemon_attach|attach)
case "$1" in
attach)
# TODO, add here the right annoying message asking users to consider
# instead using systemd or some such other init system
echo "'attach' has been deprecated, replaced by 'daemon_attach' and will be removed in the short-term, please consult rebar3.org on why you should be"\
"using 'foreground' and an init tool such as 'systemd'"
;;
esac
# Make sure a node IS running
ping_or_exit
if [ ! -w "$PIPE_DIR" ]
then
echo "failed to attach, user '$USER' does not have sufficient privileges on '$PIPE_DIR', please run node as a different user"
exit 1
fi
shift
exec "$BINDIR/to_erl" "$PIPE_DIR"
;;
remote_console|remote|remsh)
# Make sure a node IS running
ping_or_exit
shift
relx_rem_sh
;;
upgrade|downgrade|install|unpack|uninstall)
if [ -z "$2" ]; then
echo "Missing version argument"
echo "Usage: $REL_NAME $1 {version}"
exit 1
fi
COMMAND="$1"; shift
# Make sure a node IS running
ping_or_exit
relx_run_hooks "$PRE_INSTALL_UPGRADE_HOOKS"
exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
"$COMMAND" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
relx_run_hooks "$POST_INSTALL_UPGRADE_HOOKS"
;;
versions)
# Make sure a node IS running
ping_or_exit
COMMAND="$1"; shift
exec "$BINDIR/escript" "$ROOTDIR/bin/install_upgrade.escript" \
"versions" "{'$REL_NAME', \"$NAME_TYPE\", '$NAME', '$COOKIE'}" "$@"
;;
console|console_clean|console_boot|foreground)
FOREGROUNDOPTIONS=""
# .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 [ -f "$REL_DIR/$REL_NAME.boot" ]; then
BOOTFILE="$REL_DIR/$REL_NAME"
else
BOOTFILE="$REL_DIR/start"
fi
;;
foreground)
# start up the release in the foreground for use by runit
# or other supervision services
if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
BOOTFILE="$REL_DIR/$REL_NAME"
else
BOOTFILE="$REL_DIR/start"
fi
FOREGROUNDOPTIONS="-noinput +Bd"
;;
console_clean)
# if not set by user use interactive mode for console_clean
CODE_LOADING_MODE="${CODE_LOADING_MODE:-interactive}"
BOOTFILE="$REL_DIR/start_clean"
;;
console_boot)
shift
BOOTFILE="$1"
shift
;;
esac
# if not set by user or console_clean use embedded
CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}"
# Setup beam-required vars
EMU="beam"
PROGNAME="${0#*/}"
export EMU
export PROGNAME
# Dump environment info for logging purposes
# shellcheck disable=SC2086
echo "Exec: $BINDIR/erlexec" $FOREGROUNDOPTIONS \
-boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \
-boot_var SYSTEM_LIB_DIR "$SYSTEM_LIB_DIR" \
-config "$RELX_CONFIG_PATH" \
-args_file "$VMARGS_PATH" \
$EXTRA_DIST_ARGS -- "$@"
echo "Root: $ROOTDIR"
# Log the startup
echo "$RELEASE_ROOT_DIR"
if ! command -v logger > /dev/null 2>&1
then
echo "${REL_NAME}[$$] Starting up"
else
logger -t "${REL_NAME}[$$]" "Starting up"
fi
relx_run_hooks "$PRE_START_HOOKS"
# Start the VM
# The variabre FOREGROUNDOPTIONS must NOT be quoted.
# shellcheck disable=SC2086
exec "$BINDIR/erlexec" $FOREGROUNDOPTIONS \