脚本内容
#!/usr/bin/env bash
if [ -f $(pwd $0)/bin/env.sh ]; then
echo "source $(pwd $0)/bin/env.sh"
source $(pwd $0)/bin/env.sh
fi
if [ -f $(pwd $0)/../env.sh ]; then
echo "source $(pwd $0)/../env.sh"
source $(pwd $0)/../env.sh
fi
ENV_NAME=" -Dspring.profiles.active=${ENV_NAME:='test'} "
SERVER_PORT=${SERVER_PORT:=9300}
EUREKA_MASTERS=${EUREKA_MASTERS%*,}
if [ -n "$EUREKA_MASTERS" ]; then
DEFAULT_ZONE="-Deureka.client.service-url.defaultZone=${EUREKA_MASTERS}"
fi
echo "eureka default zone value is: ${EUREKA_MASTERS}"
DEPLOY_DIR=$(pwd)
CONF_DIR=$DEPLOY_DIR/config
LIB_DIR=$DEPLOY_DIR/lib
BIN_DIR=$DEPLOY_DIR/bin
JAR_NAME=$(ls -lt $BIN_DIR | grep .jar$ | head -n 1 | awk '{print $9}')
echo "Found Jar file '$JAR_NAME', we are starting it"
PIDS=$(ps -f | grep java | grep "$CONF_DIR" | awk '{print $2}')
if [ "$1" = "status" ]; then
if [ -n "$PIDS" ]; then
echo "The $ARTIFACTID is running...!"
echo "PID: $PIDS"
exit 0
else
echo "The $ARTIFACTID is stopped"
exit 0
fi
fi
if [ -n "$PIDS" ]; then
echo "ERROR: The $ARTIFACTID already started!"
echo "PID: $PIDS"
exit 1
fi
if [ -n "$2" ]; then
SERVER_PORT_COUNT=$(netstat -tln | grep $2 | wc -l)
if [ $SERVER_PORT_COUNT -gt 0 ]; then
echo "ERROR: The $ARTIFACTID port $2 already used!"
exit 1
fi
fi
LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d $LOGS_DIR ]; then
mkdir $LOGS_DIR
fi
STDOUT_FILE=$LOGS_DIR/stdout.log
JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi
JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi
JAVA_MEM_OPTS=""
BITS=$(java -version 2>&1 | grep -i 64-bit)
if [ -n "$BITS" ]; then
JAVA_MEM_OPTS=" -server -Xms$XMS_VALUE -Xmx$XMX_VALUE -Xmn$XMN_VALUE -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
else
JAVA_MEM_OPTS=" -server -Xms$XMS_VALUE -Xmx$XMX_VALUE -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi
CONFIG_FILES=" -Dlogging.path=$LOGS_DIR -Dspring.config.location=$CONF_DIR/ "
LIB_JARS=$(ls $LIB_DIR | grep .jar | grep -v $ARTIFACTID | awk '{print "'$LIB_DIR'/"$0}' | tr "\n" ":")
CLASS_PATH_VALUE=" -classpath $BIN_DIR/$JAR_NAME:$LIB_JARS "
JAVA_SYSTEM_PROPERTY=" -Dspring.devtools.restart.enabled=false "
if [ "$ENVIRONMENT" = "K8S" ]; then
echo "exec java -Denv=$APOLLO_ENV -Dapollo.configService=$APOLLO_CONFIG_SERVICE $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES -cp $LIB_DIR -jar $BIN_DIR/$JAR_NAME"
exec java -Denv=$APOLLO_ENV -Dapollo.configService=$APOLLO_CONFIG_SERVICE $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES -cp $LIB_DIR -jar $BIN_DIR/$JAR_NAME
echo "K8S deploy success ~ "
else
echo "nohup java -Dspring.main.allow-bean-definition-overriding=true -Dserver.port=$SERVER_PORT $ENV_NAME $DEFAULT_ZONE $JAVA_SYSTEM_PROPERTY $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES $CLASS_PATH_VALUE $MAIN_CLASS >$STDOUT_FILE 2>&1 &"
nohup java -Dspring.main.allow-bean-definition-overriding=true -Dserver.port=$SERVER_PORT $ENV_NAME $DEFAULT_ZONE $JAVA_SYSTEM_PROPERTY $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES $CLASS_PATH_VALUE $MAIN_CLASS >$STDOUT_FILE 2>&1 &
PIDS=$(ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}')
echo "PID: $PIDS"
echo "STDOUT: $STDOUT_FILE"
echo "Columbus deploy success ~ "
fi