Skip to content

Instantly share code, notes, and snippets.

@nddipiazza
Last active January 6, 2020 22:35
Show Gist options
  • Save nddipiazza/58b4c18064f8b3d9d6f0e58b673f9495 to your computer and use it in GitHub Desktop.
Save nddipiazza/58b4c18064f8b3d9d6f0e58b673f9495 to your computer and use it in GitHub Desktop.
common.sh with kinit for connectors-classic jvm
#!/usr/bin/env bash
AGENT="apps/lucidworks-agent.jar"
# FUSION_VERSION_BIN is e.g. /opt/lucidworks/fusion/3.0.0/bin
# this may have been set by a script that is sourcing this file
# like init/systemd/install.sh. If it is not set, assume we are being
# sourced by a script in bin and set it accordingly.
if [[ -z $FUSION_VERSION_BIN ]]; then
FUSION_VERSION_BIN=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
fi
export FUSION_HOME=$(cd -P -- "${FUSION_VERSION_BIN}/.." && pwd -P)
#echo "FUSION_HOME=$FUSION_HOME"
export AGENT_OPTS="-Dlog4j.configurationFile=file:./conf/agent-shell-log4j2.xml"
FUSION_TOP_LEVEL_DIR=$(cd -P -- "${FUSION_HOME}/.." && pwd -P)
#echo "FUSION_TOP_LEVEL_DIR=$FUSION_TOP_LEVEL_DIR"
# VERSION_DIR is e.g. 3.0.0
VERSION_DIR=$(basename -- "$FUSION_HOME")
#echo "VERSION_DIR=$VERSION_DIR"
function create_symlinks {
cd "$FUSION_TOP_LEVEL_DIR"
if [ ! -L "latest" ]; then
ln -s $VERSION_DIR latest
fi
for link in "bin" "conf" "var"; do
if [ ! -L $link ]; then
ln -s latest/$link $link
fi
done
}
KEYTAB_FILE_PATH="/path/to/your/yourusername.keytab"
KERBEROS_PRINCIPAL_NAME="HTTP/yourusername@YOUR.REALM.COM"
function do_kinit() {
if [ "${SCRIPT}" == "connectors-classic" ]; then
echo "Running a kinit so that connectors-classic connectors can perform kerberos authentication."
kinit -kt "${KEYTAB_FILE_PATH}" "${KERBEROS_PRINCIPAL_NAME}"
echo "Kerberos kinit login was successful!"
klist
fi
}
function do_start() {
check_java
do_kinit
exec "$JAVA" "$AGENT_OPTS" -jar "$AGENT" start "$SCRIPT" $@
}
function do_status() {
check_java
"$JAVA" "$AGENT_OPTS" -jar "$AGENT" status "$SCRIPT" $@
}
function do_stop() {
check_java
"$JAVA" "$AGENT_OPTS" -jar "$AGENT" stop "$SCRIPT" $@
}
function do_run() {
check_java
do_kinit
exec "$JAVA" "$AGENT_OPTS" -Xmx256m -jar "$AGENT" run "$SCRIPT" $@
}
function do_run_in_shell() {
check_java
echo "Starting $SCRIPT within this process (PID $$)"
START_COMMAND=$("$JAVA" "$AGENT_OPTS" -jar "$AGENT" start --generate-command "$SCRIPT" $@ )
echo Executing $START_COMMAND
exec env $START_COMMAND
}
function do_usage() {
echo "Usage: $0 [start, stop, status, restart, run]"
exit 1
}
function do_agent() {
cd "$FUSION_HOME"
check_java
exec "$JAVA" "$AGENT_OPTS" -jar "$AGENT" $@
}
DATEFORMAT="+%Y-%m-%d %H:%M:%SZ"
function output() {
echo $(date -u "$DATEFORMAT") "$1"
}
function check_java() {
if [ -z "$JAVA_HOME" ]; then
if ! JAVA=$(command -v java) ; then
output "cannot find the java command. Install Oracle Java, adjust your PATH, or set JAVA_HOME."
exit 1
fi
else
JAVA="$JAVA_HOME/bin/java"
if ! test -x "$JAVA" ; then
echo "JAVA_HOME is set to $JAVA_HOME, but there is no $JAVA"
exit 1
fi
fi
JAVA_VERSION=$($JAVA -version 2>&1 | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q')
if [ -z "$JAVA_VERSION" ]; then
output "Cannot determine java version"
exit 1
fi
if [[ $JAVA_VERSION = "1."* ]]
then
JAVA_MINOR=$(echo $JAVA_VERSION | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q')
else
JAVA_MINOR=$(echo $JAVA_VERSION | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q')
fi
if [[ $JAVA_MINOR -ne 8 ]]; then
output "This product requires Java 1.8"
exit 1
fi
}
function main() {
arg="$1"
create_symlinks
cd "$FUSION_HOME"
if [ "$arg" = "start" ]; then
do_start ${@:2}
elif [ "$arg" = "stop" ]; then
do_stop ${@:2}
elif [ "$arg" = "restart" ]; then
do_stop ${@:2}
sleep 1
do_start ${@:2}
elif [ "$arg" = "status" ]; then
do_status ${@:2}
elif [ "$arg" = "run" ]; then
do_run ${@:2}
elif [ "$arg" = "run-in-shell" ]; then
do_run_in_shell ${@:2}
elif [ "$arg" = "help" ]; then
do_usage
elif [ -z "$arg" ]; then
do_run ${@:2}
else
echo "Unknown action: $arg"
do_usage
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment