/
stop-us-web-native-build.sh
executable file
·322 lines (200 loc) · 8.43 KB
/
stop-us-web-native-build.sh
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
#!/bin/sh
# Stops a US-Web instance, supposedly already running as a native build on the
# current host.
# Script possibly:
# - placed in /usr/local/bin of a gateway
# - run from systemctl, as root, as:
# 'systemctl stop us-web-as-native-build.service'
#
# (hence to be triggered by /etc/systemd/system/us-web-as-native-build.service)
#
# Note: if run through systemd, all outputs of this script (standard and error
# ones) are automatically redirected by systemd to its journal.
#
# To consult them, use:
# $ journalctl --pager-end --unit=us-web-as-native-build.service
# See also:
# - kill-us-web-native-build.sh
# - start-us-web-native-build.sh
# - deploy-us-web-native-build.sh
# Implementation notes:
# To stop US-Web, we only have to know the target host (which is here the local
# one), the node name ('us_web' here) and the right cookie and EPMD port, which
# requires thus to find and read the relevant US (not US-Web) configuration
# file. For that we rely our helper shell scripts, and thus we have to locate
# the right US-Web installation as a first move.
# We use a dedicated stop client rather than pipes, as currently pipes are found
# unresponsive:
#
# $ to_erl /tmp/launch-erl-1258957
#Attaching to /tmp/launch-erl-1258957 (^D to exit)
#
#^L
# And no entered command is managed (only CTRL-C and CTL-D work), altough the
# TERM environment variable was set before run_erl and to_erl.
usage="Usage: $(basename $0) [US_CONF_DIR]: stops a US-Web server, running as a native build, based on a US configuration directory specified on the command-line (which must end by a 'universal-server' directory), otherwise found through the default US search paths.
The US-Web installation itself will be looked up relatively to this script, otherwise in the standard path applied by our deploy-us-web-native-build.sh script.
Example: '$0 /opt/test/universal-server' is to read /opt/test/universal-server/us.config."
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "${usage}"
exit 0
fi
# Not necessarily run as root.
# Either this script is called during development, directly from within a US-Web
# installation, in which case this installation shall be used, or (typically if
# called through systemd) the standard US-Web base directory shall be targeted:
#
this_script_dir="$(dirname $0)"
# Possibly:
local_us_web_install_root="${this_script_dir}/../.."
# Checks based on the 'priv' subdirectory for an increased reliability:
if [ -d "${local_us_web_install_root}/priv" ]; then
us_web_install_root="$(realpath ${local_us_web_install_root})"
echo "Selecting US-Web development native build in clone-local '${us_web_install_root}'."
else
# The location enforced by deploy-us-web-native-build.sh:
us_web_install_root="/opt/universal-server/us_web-native/us_web"
echo "Selecting US-Web native build in standard server location '${us_web_install_root}'."
if [ ! -d "${us_web_install_root}/priv" ]; then
echo " Error, no valid US-Web native build found, neither locally (as '$(realpath ${local_us_web_install_root})') nor at the '${us_web_install_root}' standard server location." 1>&2
exit 15
fi
fi
if [ $# -gt 1 ]; then
echo " Error, extra argument expected.
${usage}" 1>&2
exit 10
fi
# XDG_CONFIG_DIRS defined, so that the US server as well can look it up (not
# only these scripts):
#
# (avoiding empty path in list)
#
if [ -n "${XDG_CONFIG_DIRS}" ]; then
xdg_cfg_dirs="${XDG_CONFIG_DIRS}:/etc/xdg"
else
xdg_cfg_dirs="/etc/xdg"
fi
maybe_us_config_dir="$1"
if [ -n "${maybe_us_config_dir}" ]; then
# Otherwise would remain in the extra arguments transmitted in CMD_LINE_OPT:
shift
case "${maybe_us_config_dir}" in
/*)
# Already absolute, OK:
echo "Using specified absolute directory '${maybe_us_config_dir}'."
;;
*)
# Relative, to be made absolute:
maybe_us_config_dir="$(pwd)/${maybe_us_config_dir}"
echo "Transformed specified relative directory in '${maybe_us_config_dir}' absolute one."
;;
esac
if [ ! -d "${maybe_us_config_dir}" ]; then
echo " Error, the specified US configuration directory, '${maybe_us_config_dir}', does not exist." 1>&2
exit 20
fi
# Better for messages output:
maybe_us_config_dir="$(realpath ${maybe_us_config_dir})"
# As a 'universal-server/us.config' suffix will be added to each candidate
# configuration directory, we remove the last directory:
#
candidate_dir="$(dirname ${maybe_us_config_dir})"
xdg_cfg_dirs="${candidate_dir}:${xdg_cfg_dirs}"
fi
epmd="$(which epmd 2>/dev/null)"
if [ ! -x "${epmd}" ]; then
echo " Error, no EPMD executable found." 1>&2
exit 8
fi
#echo "Stopping US-Web native build, as following user: $(id)"
# We need first to locate the us-web-common.sh script:
# Location expected also by us-common.sh afterwards:
cd "${us_web_install_root}"
# Will source in turn us-common.sh:
us_web_common_script_name="us-web-common.sh"
us_web_common_script="priv/bin/${us_web_common_script_name}"
if [ ! -f "${us_web_common_script}" ]; then
echo " Error, unable to find ${us_web_common_script_name} script (not found as '${us_web_common_script}', while being in '$(pwd)')." 1>&2
exit 35
fi
# Will source in turn us-common.sh:
us_web_common_script_name="us-web-common.sh"
us_web_common_script="priv/bin/${us_web_common_script_name}"
if [ ! -f "${us_web_common_script}" ]; then
echo " Error, unable to find ${us_web_common_script_name} script (not found as '${us_web_common_script}', while being in '$(pwd)')." 1>&2
exit 35
fi
# Hints for the helper scripts:
export us_launch_type="native"
#echo "Sourcing '${us_web_common_script}'."
. "${us_web_common_script}" #1>/dev/null
# We expect a pre-installed US configuration file to exist:
#echo "Reading US configuration file:"
read_us_config_file "${maybe_us_config_dir}" #1>/dev/null
# Now we also need to read the US-Web configuration file here, to fetch any EPMD
# port there:
#
#echo "Reading US-Web configuration file:"
read_us_web_config_file #1>/dev/null
# Not needed:
#secure_authbind
echo " -- Stopping US-Web natively-built application (EPMD port: ${us_web_erl_epmd_port})..."
# We must stop the VM with the right (Erlang) cookie, i.e. the actual runtime
# one, not the dummy, original one:
#
# Commented-out, as we can actually specify it directly on the command-line:
#update_us_web_config_cookie
if [ -n "${vm_cookie}" ]; then
#echo "Using cookie '${vm_cookie}'."
cookie_env="COOKIE=${vm_cookie}"
else
cookie_env=""
fi
cd src/apps || exit 17
# No sudo or authbind necessary here, no US_* environment variables either:
echo XDG_CONFIG_DIRS="${xdg_cfg_dirs}" make -s us_web_stop_exec EPMD_PORT=${us_web_erl_epmd_port} CMD_LINE_OPT="$* --target-cookie ${vm_cookie}"
# A correct way of passing environment variables:
XDG_CONFIG_DIRS="${xdg_cfg_dirs}" make -s us_web_stop_exec EPMD_PORT=${us_web_erl_epmd_port} CMD_LINE_OPT="$* --target-cookie ${vm_cookie}"
res=$?
if [ $res -eq 0 ]; then
echo " (stop success reported)"
echo
# If wanting to check or have more details:
#inspect_us_web_log
exit 0
else
# Despite following message: 'Node 'us_web_app@127.0.0.1' not responding to
# pings."
echo " Error: stop failure reported (code '$res')" 1>&2
echo
#inspect_us_web_log
# Finally wanting pseudo-failure to propagate:
res=0
fi
# Restore original cookie file:
#/bin/mv -f "${backup_vm_args_file}" "${vm_args_file}"
#/bin/cp -f "${backup_vm_args_file}" "${vm_args_file}"
# Unfortunately, at least in some cases, EPMD will believe that this just
# shutdown US-Web instance is still running; as the next epmd command may not
# work (if -relaxed_command_check was not used at EPMD startup; we nevertheless
# ensure that it is the case if using our start scripts), the only remaining
# solution would be to kill this EPMD; yet this may impact other running Erlang
# applications (see kill-us-web.sh to minimise the harm done), so it is not done
# here.
if [ -n "${us_web_erl_epmd_port}" ]; then
echo "Using user-defined US-Web EPMD port ${us_web_erl_epmd_port}."
export ERL_EPMD_PORT="${us_web_erl_epmd_port}"
else
# Using the default US-Web EPMD port (see the EPMD_PORT make variable),
# supposing that the instance was properly launched (see the 'launch-epmd'
# make target):
echo "Using default US-Web EPMD port ${default_us_web_epmd_port}."
export ERL_EPMD_PORT="${default_us_web_epmd_port}"
fi
if ! ${epmd} -stop us_web; then
echo " Error while unregistering the US-Web server from the EPMD daemon at port ${ERL_EPMD_PORT}." 1>&2
exit 25
fi
# kill-us-web.sh may also be your last-resort friend.
exit ${res}