Skip to content

Commit

Permalink
Merge pull request alibaba#185 from wongoo/fix-issue184
Browse files Browse the repository at this point in the history
fix alibaba#184: support attach jvm starting by diff user
  • Loading branch information
oldmanpushcart authored Aug 30, 2019
2 parents b692579 + 5b68f48 commit 3e73cb3
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions bin/sandbox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ typeset SANDBOX_SERVER_NETWORK
typeset SANDBOX_LIB_DIR=${SANDBOX_HOME_DIR}/lib

# define sandbox attach token file
typeset SANDBOX_TOKEN_FILE=${HOME}/.sandbox.token
typeset SANDBOX_TOKEN_FILENAME=.sandbox.token
typeset SANDBOX_TOKEN_FILE=${HOME}/${SANDBOX_TOKEN_FILENAME}

# define JVM OPS
typeset SANDBOX_JVM_OPS="-Xms128M -Xmx128M -Xnoclassgc -ea";

# define target JVM Process ID
typeset TARGET_JVM_PID

# define target JVM starting user
typeset TARGET_JVM_USER=${USER}

# define target SERVER network interface
typeset TARGET_SERVER_IP
typeset DEFAULT_TARGET_SERVER_IP="0.0.0.0"
Expand Down Expand Up @@ -193,6 +197,13 @@ check_permission()
# reset some options for env
reset_for_env()
{
TARGET_JVM_USER=$(ps aux | grep ${TARGET_JVM_PID} | grep java | awk '{print $1}')
if [[ "${USER}" != "${TARGET_JVM_USER}" ]]; then
SANDBOX_TOKEN_FILE=$(eval echo "~${TARGET_JVM_USER}")/${SANDBOX_TOKEN_FILENAME}
# touch attach token file
su - ${TARGET_JVM_USER} -c "touch ${SANDBOX_TOKEN_FILE}" \
|| exit_on_err 1 "permission denied, ${SANDBOX_TOKEN_FILE} is not readable."
fi

# use the env JAVA_HOME for default
[[ ! -z ${JAVA_HOME} ]] \
Expand Down Expand Up @@ -236,13 +247,20 @@ function attach_jvm() {
local token=`date |head|cksum|sed 's/ //g'`

# attach target jvm
"${SANDBOX_JAVA_HOME}/bin/java" \
ATTACH_CMD="${SANDBOX_JAVA_HOME}/bin/java \
${SANDBOX_JVM_OPS} \
-jar ${SANDBOX_LIB_DIR}/sandbox-core.jar \
${TARGET_JVM_PID} \
"${SANDBOX_LIB_DIR}/sandbox-agent.jar" \
"home=${SANDBOX_HOME_DIR};token=${token};server.ip=${TARGET_SERVER_IP};server.port=${TARGET_SERVER_PORT};namespace=${TARGET_NAMESPACE}" \
|| exit_on_err 1 "attach JVM ${TARGET_JVM_PID} fail."
\"${SANDBOX_LIB_DIR}/sandbox-agent.jar\" \
\"home=${SANDBOX_HOME_DIR};token=${token};server.ip=${TARGET_SERVER_IP};server.port=${TARGET_SERVER_PORT};namespace=${TARGET_NAMESPACE}\""

if [[ "${USER}" == "${TARGET_JVM_USER}" ]]; then
eval ${ATTACH_CMD} \
|| exit_on_err 1 "attach JVM ${TARGET_JVM_PID} fail. Attach command: $ATTACH_CMD"
else
su - ${TARGET_JVM_USER} -c "${ATTACH_CMD}" \
|| exit_on_err 1 "attach JVM ${TARGET_JVM_PID} fail. Attach command: $ATTACH_CMD"
fi

# get network from attach result
SANDBOX_SERVER_NETWORK=$(grep ${token} ${SANDBOX_TOKEN_FILE}|grep ${TARGET_NAMESPACE}|tail -1|awk -F ";" '{print $3";"$4}');
Expand Down

0 comments on commit 3e73cb3

Please sign in to comment.