NIFI-3294 Adjusting nifi.sh to invoke nifi-env.sh when running as another user such that properties are preserved across environments.
Updating nifi.sh to include improvements by NIFI-2689 This closes #1525. Signed-off-by: ijokarumawak <ijokaruma...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/9a5487cd Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/9a5487cd Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/9a5487cd Branch: refs/heads/support/nifi-0.7.x Commit: 9a5487cde360116ec309dce9eb526b2364632f33 Parents: 58744fc Author: Aldrin Piri <ald...@apache.org> Authored: Tue Feb 21 16:15:20 2017 -0500 Committer: ijokarumawak <ijokaruma...@apache.org> Committed: Thu Feb 23 08:23:42 2017 +0900 ---------------------------------------------------------------------- .../src/main/resources/bin/nifi.sh | 72 ++++++++++++++------ 1 file changed, 53 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/9a5487cd/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-resources/src/main/resources/bin/nifi.sh ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-resources/src/main/resources/bin/nifi.sh b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-resources/src/main/resources/bin/nifi.sh index b58a622..d827735 100755 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-resources/src/main/resources/bin/nifi.sh +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-resources/src/main/resources/bin/nifi.sh @@ -17,11 +17,35 @@ # Script structure inspired from Apache Karaf and other Apache projects with similar startup approaches -SCRIPT_DIR=$(dirname "$0") -SCRIPT_NAME=$(basename "$0") +# 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-env.sh +. "${SCRIPT_DIR}/nifi-env.sh" + + warn() { echo "${PROGNAME}: $*" @@ -210,20 +234,21 @@ run() { BOOTSTRAP_CONF="${BOOTSTRAP_CONF_DIR}/bootstrap.conf"; BOOTSTRAP_LIBS="${NIFI_HOME}/lib/bootstrap/*" - run_as=$(grep '^\s*run.as' "${BOOTSTRAP_CONF}" | cut -d'=' -f2) + 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" = "$(whoami)" ]; then - unset run_as + if [ "${run_as_user}" = "$(whoami)" ]; then + unset run_as_user fi - sudo_cmd_prefix="" if $cygwin; then - if [ -n "${run_as}" ]; then + if [ -n "${run_as_user}" ]; then echo "The run.as option is not supported in a Cygwin environment. Exiting." exit 1 fi; NIFI_HOME=$(cygpath --path --windows "${NIFI_HOME}") + NIFI_LOG_DIR=$(cygpath --path --windows "${NIFI_LOG_DIR}") + NIFI_PID_DIR=$(cygpath --path --windows "${NIFI_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}") @@ -233,11 +258,9 @@ run() { BOOTSTRAP_CLASSPATH="${TOOLS_JAR};${BOOTSTRAP_CLASSPATH}" fi else - if [ -n "${run_as}" ]; then - if id -u "${run_as}" >/dev/null 2>&1; then - sudo_cmd_prefix="sudo -u ${run_as}" - else - echo "The specified run.as user ${run_as} does not exist. Exiting." + 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; @@ -258,19 +281,30 @@ run() { # all other commands will terminate quickly so want to just wait for them #setup directory parameters - BOOTSTRAP_LOG_PARAMS="-Dorg.apache.nifi.bootstrap.config.log.dir="\""${NIFI_LOG_DIR}"\""" - BOOTSTRAP_PID_PARAMS="-Dorg.apache.nifi.bootstrap.config.pid.dir="\""${NIFI_PID_DIR}"\""" - BOOTSTRAP_CONF_PARAMS="-Dorg.apache.nifi.bootstrap.config.file="\""${BOOTSTRAP_CONF}"\""" + BOOTSTRAP_LOG_PARAMS="-Dorg.apache.nifi.bootstrap.config.log.dir='${NIFI_LOG_DIR}'" + BOOTSTRAP_PID_PARAMS="-Dorg.apache.nifi.bootstrap.config.pid.dir='${NIFI_PID_DIR}'" + BOOTSTRAP_CONF_PARAMS="-Dorg.apache.nifi.bootstrap.config.file='${BOOTSTRAP_CONF}'" BOOTSTRAP_DIR_PARAMS="${BOOTSTRAP_LOG_PARAMS} ${BOOTSTRAP_PID_PARAMS} ${BOOTSTRAP_CONF_PARAMS}" - RUN_NIFI_CMD="cd "\""${NIFI_HOME}"\"" && ${sudo_cmd_prefix} "\""${JAVA}"\"" -cp "\""${BOOTSTRAP_CLASSPATH}"\"" -Xms12m -Xmx24m ${BOOTSTRAP_DIR_PARAMS} org.apache.nifi.bootstrap.RunNiFi" + run_nifi_cmd="'${JAVA}' -cp '${BOOTSTRAP_CLASSPATH}' -Xms12m -Xmx24m ${BOOTSTRAP_DIR_PARAMS} org.apache.nifi.bootstrap.RunNiFi $@" + + if [ -n "${run_as_user}" ]; then + # Provide SCRIPT_DIR and execute nifi-env for the run.as user command + run_nifi_cmd="sudo -u ${run_as_user} sh -c \"SCRIPT_DIR='${SCRIPT_DIR}' && . '${SCRIPT_DIR}/nifi-env.sh' && ${run_nifi_cmd}\"" + fi + + if [ "$1" = "run" ]; then + # Use exec to handover PID to RunNiFi java process, instead of foking it as a child process + run_nifi_cmd="exec ${run_nifi_cmd}" + fi if [ "$1" = "start" ]; then - (eval $RUN_NIFI_CMD $@ &) + ( eval "cd ${NIFI_HOME} && ${run_nifi_cmd}" & ) else - (eval $RUN_NIFI_CMD $@) + eval "cd ${NIFI_HOME} && ${run_nifi_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