This repository has been archived by the owner on Jul 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
nifi-registry.sh
357 lines (301 loc) · 12.2 KB
/
nifi-registry.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
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
#!/bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (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.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Script structure inspired from Apache Karaf and other Apache projects with similar startup approaches
# Discover the path of the file
# Since MacOS X, FreeBSD and some other systems lack gnu readlink, we use a more portable
# approach based on following StackOverflow comment http://stackoverflow.com/a/1116890/888876
TARGET_FILE=$0
cd $(dirname $TARGET_FILE)
TARGET_FILE=$(basename $TARGET_FILE)
# Iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ]
do
TARGET_FILE=$(readlink $TARGET_FILE)
cd $(dirname $TARGET_FILE)
TARGET_FILE=$(basename $TARGET_FILE)
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
PHYS_DIR=$(pwd -P)
SCRIPT_DIR=$PHYS_DIR
PROGNAME=$(basename "$0")
. "${SCRIPT_DIR}/nifi-registry-env.sh"
warn() {
echo "${PROGNAME}: $*"
}
die() {
warn "$*"
exit 1
}
detectOS() {
# OS specific support (must be 'true' or 'false').
cygwin=false;
aix=false;
os400=false;
darwin=false;
case "$(uname)" in
CYGWIN*)
cygwin=true
;;
AIX*)
aix=true
;;
OS400*)
os400=true
;;
Darwin)
darwin=true
;;
esac
# For AIX, set an environment variable
if ${aix}; then
export LDR_CNTRL=MAXDATA=0xB0000000@DSA
echo ${LDR_CNTRL}
fi
# In addition to those, go around the linux space and query the widely
# adopted /etc/os-release to detect linux variants
if [ -f /etc/os-release ]
then
source /etc/os-release
fi
}
unlimitFD() {
# Use the maximum available, or set MAX_FD != -1 to use that
if [ "x${MAX_FD}" = "x" ]; then
MAX_FD="maximum"
fi
# Increase the maximum file descriptors if we can
if [ "${os400}" = "false" ] && [ "${cygwin}" = "false" ]; then
MAX_FD_LIMIT=$(ulimit -H -n)
if [ "${MAX_FD_LIMIT}" != 'unlimited' ]; then
if [ $? -eq 0 ]; then
if [ "${MAX_FD}" = "maximum" -o "${MAX_FD}" = "max" ]; then
# use the system max
MAX_FD="${MAX_FD_LIMIT}"
fi
ulimit -n ${MAX_FD} > /dev/null
# echo "ulimit -n" `ulimit -n`
if [ $? -ne 0 ]; then
warn "Could not set maximum file descriptor limit: ${MAX_FD}"
fi
else
warn "Could not query system maximum file descriptor limit: ${MAX_FD_LIMIT}"
fi
fi
fi
}
locateJava() {
# Setup the Java Virtual Machine
if $cygwin ; then
[ -n "${JAVA}" ] && JAVA=$(cygpath --unix "${JAVA}")
[ -n "${JAVA_HOME}" ] && JAVA_HOME=$(cygpath --unix "${JAVA_HOME}")
fi
if [ "x${JAVA}" = "x" ] && [ -r /etc/gentoo-release ] ; then
JAVA_HOME=$(java-config --jre-home)
fi
if [ "x${JAVA}" = "x" ]; then
if [ "x${JAVA_HOME}" != "x" ]; then
if [ ! -d "${JAVA_HOME}" ]; then
die "JAVA_HOME is not valid: ${JAVA_HOME}"
fi
JAVA="${JAVA_HOME}/bin/java"
else
warn "JAVA_HOME not set; results may vary"
JAVA=$(type java)
JAVA=$(expr "${JAVA}" : '.* \(/.*\)$')
if [ "x${JAVA}" = "x" ]; then
die "java command not found"
fi
fi
fi
# if command is env, attempt to add more to the classpath
if [ "$1" = "env" ]; then
[ "x${TOOLS_JAR}" = "x" ] && [ -n "${JAVA_HOME}" ] && TOOLS_JAR=$(find -H "${JAVA_HOME}" -name "tools.jar")
[ "x${TOOLS_JAR}" = "x" ] && [ -n "${JAVA_HOME}" ] && TOOLS_JAR=$(find -H "${JAVA_HOME}" -name "classes.jar")
if [ "x${TOOLS_JAR}" = "x" ]; then
warn "Could not locate tools.jar or classes.jar. Please set manually to avail all command features."
fi
fi
}
init() {
# Determine if there is special OS handling we must perform
detectOS
# Unlimit the number of file descriptors if possible
unlimitFD
# Locate the Java VM to execute
locateJava "$1"
}
install() {
detectOS
if [ "${darwin}" = "true" ] || [ "${cygwin}" = "true" ]; then
echo 'Installing Apache NiFi Registry as a service is not supported on OS X or Cygwin.'
exit 1
fi
SVC_NAME=nifi-registry
if [ "x$2" != "x" ] ; then
SVC_NAME=$2
fi
# since systemd seems to honour /etc/init.d we don't still create native systemd services
# yet...
initd_dir='/etc/init.d'
SVC_FILE="${initd_dir}/${SVC_NAME}"
if [ ! -w "${initd_dir}" ]; then
echo "Current user does not have write permissions to ${initd_dir}. Cannot install NiFi Registry as a service."
exit 1
fi
# Create the init script, overwriting anything currently present
cat <<SERVICEDESCRIPTOR > ${SVC_FILE}
#!/bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (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.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# chkconfig: 2345 20 80
# description: Apache NiFi Registry is a complementary application that provides a central location for storage and management of shared resources across one or more instances of NiFi and/or MiNiFi.
# Make use of the configured NIFI_REGISTRY_HOME directory and pass service requests to the nifi-registry.sh executable
NIFI_REGISTRY_HOME=${NIFI_REGISTRY_HOME}
bin_dir=\${NIFI_REGISTRY_HOME}/bin
nifi_registry_executable=\${bin_dir}/nifi-registry.sh
\${nifi_registry_executable} "\$@"
SERVICEDESCRIPTOR
if [ ! -f "${SVC_FILE}" ]; then
echo "Could not create service file ${SVC_FILE}"
exit 1
fi
# Provide the user execute access on the file
chmod u+x ${SVC_FILE}
# If SLES or OpenSuse...
if [ "${ID}" = "opensuse" ] || [ "${ID}" = "sles" ]; then
rm -f "/etc/rc.d/rc2.d/S65${SVC_NAME}"
ln -s "/etc/init.d/${SVC_NAME}" "/etc/rc.d/rc2.d/S65${SVC_NAME}" || { echo "Could not create link /etc/rc.d/rc2.d/S65${SVC_NAME}"; exit 1; }
rm -f "/etc/rc.d/rc2.d/K65${SVC_NAME}"
ln -s "/etc/init.d/${SVC_NAME}" "/etc/rc.d/rc2.d/K65${SVC_NAME}" || { echo "Could not create link /etc/rc.d/rc2.d/K65${SVC_NAME}"; exit 1; }
echo "Service ${SVC_NAME} installed"
# Anything other fallback to the old approach
else
rm -f "/etc/rc2.d/S65${SVC_NAME}"
ln -s "/etc/init.d/${SVC_NAME}" "/etc/rc2.d/S65${SVC_NAME}" || { echo "Could not create link /etc/rc2.d/S65${SVC_NAME}"; exit 1; }
rm -f "/etc/rc2.d/K65${SVC_NAME}"
ln -s "/etc/init.d/${SVC_NAME}" "/etc/rc2.d/K65${SVC_NAME}" || { echo "Could not create link /etc/rc2.d/K65${SVC_NAME}"; exit 1; }
echo "Service ${SVC_NAME} installed"
fi
}
run() {
BOOTSTRAP_CONF_DIR="${NIFI_REGISTRY_HOME}/conf"
BOOTSTRAP_CONF="${BOOTSTRAP_CONF_DIR}/bootstrap.conf";
BOOTSTRAP_LIBS="${NIFI_REGISTRY_HOME}/lib/bootstrap/*"
SHARED_LIBS="${NIFI_REGISTRY_HOME}/lib/shared/*"
run_as_user=$(grep '^\s*run.as' "${BOOTSTRAP_CONF}" | cut -d'=' -f2)
# If the run as user is the same as that starting the process, ignore this configuration
if [ "${run_as_user}" = "$(whoami)" ]; then
unset run_as_user
fi
if $cygwin; then
if [ -n "${run_as_user}" ]; then
echo "The run.as option is not supported in a Cygwin environment. Exiting."
exit 1
fi;
NIFI_REGISTRY_HOME=$(cygpath --path --windows "${NIFI_REGISTRY_HOME}")
NIFI_REGISTRY_LOG_DIR=$(cygpath --path --windows "${NIFI_REGISTRY_LOG_DIR}")
NIFI_REGISTRY_PID_DIR=$(cygpath --path --windows "${NIFI_REGISTRY_PID_DIR}")
BOOTSTRAP_CONF=$(cygpath --path --windows "${BOOTSTRAP_CONF}")
BOOTSTRAP_CONF_DIR=$(cygpath --path --windows "${BOOTSTRAP_CONF_DIR}")
BOOTSTRAP_LIBS=$(cygpath --path --windows "${BOOTSTRAP_LIBS}")
SHARED_LIBS=$(cygpath --path --windows "${SHARED_LIBS}")
BOOTSTRAP_CLASSPATH="${BOOTSTRAP_CONF_DIR};${SHARED_LIBS};${BOOTSTRAP_LIBS}"
if [ -n "${TOOLS_JAR}" ]; then
TOOLS_JAR=$(cygpath --path --windows "${TOOLS_JAR}")
BOOTSTRAP_CLASSPATH="${TOOLS_JAR};${BOOTSTRAP_CLASSPATH}"
fi
else
if [ -n "${run_as_user}" ]; then
if ! id -u "${run_as_user}" >/dev/null 2>&1; then
echo "The specified run.as user ${run_as_user} does not exist. Exiting."
exit 1
fi
fi;
BOOTSTRAP_CLASSPATH="${BOOTSTRAP_CONF_DIR}:${SHARED_LIBS}:${BOOTSTRAP_LIBS}"
if [ -n "${TOOLS_JAR}" ]; then
BOOTSTRAP_CLASSPATH="${TOOLS_JAR}:${BOOTSTRAP_CLASSPATH}"
fi
fi
echo
echo "Java home: ${JAVA_HOME}"
echo "NiFi Registry home: ${NIFI_REGISTRY_HOME}"
echo
echo "Bootstrap Config File: ${BOOTSTRAP_CONF}"
echo
# run 'start' in the background because the process will continue to run, monitoring NiFi Registry.
# all other commands will terminate quickly so want to just wait for them
#setup directory parameters
BOOTSTRAP_LOG_PARAMS="-Dorg.apache.nifi.registry.bootstrap.config.log.dir='${NIFI_REGISTRY_LOG_DIR}'"
BOOTSTRAP_PID_PARAMS="-Dorg.apache.nifi.registry.bootstrap.config.pid.dir='${NIFI_REGISTRY_PID_DIR}'"
BOOTSTRAP_CONF_PARAMS="-Dorg.apache.nifi.registry.bootstrap.config.file='${BOOTSTRAP_CONF}'"
BOOTSTRAP_DIR_PARAMS="${BOOTSTRAP_LOG_PARAMS} ${BOOTSTRAP_PID_PARAMS} ${BOOTSTRAP_CONF_PARAMS}"
run_nifi_registry_cmd="'${JAVA}' -cp '${BOOTSTRAP_CLASSPATH}' -Xms12m -Xmx24m ${BOOTSTRAP_DIR_PARAMS} org.apache.nifi.registry.bootstrap.RunNiFiRegistry $@"
if [ -n "${run_as_user}" ]; then
# Provide SCRIPT_DIR and execute nifi-env for the run.as user command
run_nifi_registry_cmd="sudo -u ${run_as_user} sh -c \"SCRIPT_DIR='${SCRIPT_DIR}' && . '${SCRIPT_DIR}/nifi-registry-env.sh' && ${run_nifi_registry_cmd}\""
fi
if [ "$1" = "run" ]; then
# Use exec to handover PID to RunNiFi java process, instead of forking it as a child process
run_nifi_registry_cmd="exec ${run_nifi_registry_cmd}"
fi
if [ "$1" = "start" ]; then
( eval "cd ${NIFI_REGISTRY_HOME} && ${run_nifi_registry_cmd}" & )> /dev/null 1>&-
else
eval "cd ${NIFI_REGISTRY_HOME} && ${run_nifi_registry_cmd}"
fi
EXIT_STATUS=$?
# Wait just a bit (3 secs) to wait for the logging to finish and then echo a new-line.
# We do this to avoid having logs spewed on the console after running the command and then not giving
# control back to the user
sleep 3
echo
}
main() {
init "$1"
run "$@"
}
case "$1" in
install)
install "$@"
;;
start|stop|run|status|dump|env)
main "$@"
;;
restart)
init
run "stop"
run "start"
;;
*)
echo "Usage nifi-registry {start|stop|run|restart|status|dump|install}"
;;
esac