server.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/bin/bash
  2. #echo color
  3. WHITE_COLOR="\E[1;37m";
  4. RED_COLOR="\E[1;31m";
  5. BLUE_COLOR='\E[1;34m';
  6. GREEN_COLOR="\E[1;32m";
  7. YELLOW_COLOR="\E[1;33m";
  8. RES="\E[0m";
  9. echo_r () {
  10. # Color red: Error, Failed
  11. [ $# -ne 1 ] && return 1
  12. printf "[${BLUE_COLOR}opencron${RES}] ${RED_COLOR}$1${RES}\n"
  13. }
  14. echo_g () {
  15. # Color green: Success
  16. [ $# -ne 1 ] && return 1
  17. printf "[${BLUE_COLOR}opencron${RES}] ${GREEN_COLOR}$1${RES}\n"
  18. }
  19. echo_y () {
  20. # Color yellow: Warning
  21. [ $# -ne 1 ] && return 1
  22. printf "[${BLUE_COLOR}opencron${RES}] ${YELLOW_COLOR}$1${RES}\n"
  23. }
  24. echo_w () {
  25. # Color yellow: White
  26. [ $# -ne 1 ] && return 1
  27. printf "[${BLUE_COLOR}opencron${RES}] ${WHITE_COLOR}$1${RES}\n"
  28. }
  29. # Make sure prerequisite environment variables are set
  30. if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then
  31. if $darwin; then
  32. # Bugzilla 54390
  33. if [ -x '/usr/libexec/java_home' ] ; then
  34. export JAVA_HOME=`/usr/libexec/java_home`
  35. # Bugzilla 37284 (reviewed).
  36. elif [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" ]; then
  37. export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home"
  38. fi
  39. else
  40. JAVA_PATH=`which java 2>/dev/null`
  41. if [ "x$JAVA_PATH" != "x" ]; then
  42. JAVA_PATH=`dirname $JAVA_PATH 2>/dev/null`
  43. JRE_HOME=`dirname $JAVA_PATH 2>/dev/null`
  44. fi
  45. if [ "x$JRE_HOME" = "x" ]; then
  46. # XXX: Should we try other locations?
  47. if [ -x /usr/bin/java ]; then
  48. JRE_HOME=/usr
  49. fi
  50. fi
  51. fi
  52. if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then
  53. echo_r "Neither the JAVA_HOME nor the JRE_HOME environment variable is defined"
  54. echo_r "At least one of these environment variable is needed to run this program"
  55. exit 1
  56. fi
  57. fi
  58. if [ -z "$JAVA_HOME" -a "$1" = "debug" ]; then
  59. echo_r "JAVA_HOME should point to a JDK in order to run in debug mode."
  60. exit 1
  61. fi
  62. if [ -z "$JRE_HOME" ]; then
  63. JRE_HOME="$JAVA_HOME"
  64. fi
  65. # If we're running under jdb, we need a full jdk.
  66. if [ "$1" = "debug" ] ; then
  67. if [ "$os400" = "true" ]; then
  68. if [ ! -x "$JAVA_HOME"/bin/java -o ! -x "$JAVA_HOME"/bin/javac ]; then
  69. echo_r "The JAVA_HOME environment variable is not defined correctly"
  70. echo_r "This environment variable is needed to run this program"
  71. echo_r "NB: JAVA_HOME should point to a JDK not a JRE"
  72. exit 1
  73. fi
  74. else
  75. if [ ! -x "$JAVA_HOME"/bin/java -o ! -x "$JAVA_HOME"/bin/jdb -o ! -x "$JAVA_HOME"/bin/javac ]; then
  76. echo_r "The JAVA_HOME environment variable is not defined correctly"
  77. echo_r "This environment variable is needed to run this program"
  78. echo_r "NB: JAVA_HOME should point to a JDK not a JRE"
  79. exit 1
  80. fi
  81. fi
  82. fi
  83. # Don't override the endorsed dir if the user has set it previously
  84. if [ -z "$JAVA_ENDORSED_DIRS" ]; then
  85. # Set the default -Djava.endorsed.dirs argument
  86. JAVA_ENDORSED_DIRS="$OPENCRON_HOME"/endorsed
  87. fi
  88. # Set standard commands for invoking Java, if not already set.
  89. if [ -z "$RUNJAVA" ]; then
  90. RUNJAVA="$JRE_HOME"/bin/java
  91. fi
  92. if [ "$os400" != "true" ]; then
  93. if [ -z "$_RUNJDB" ]; then
  94. _RUNJDB="$JAVA_HOME"/bin/jdb
  95. fi
  96. fi
  97. #check java exists.
  98. $RUNJAVA >/dev/null 2>&1
  99. if [ $? -ne 1 ];then
  100. echo_r "ERROR: java is not install,please install java first!"
  101. exit 1;
  102. fi
  103. #check openjdk
  104. if [ "`${RUNJAVA} -version 2>&1 | head -1|grep "openjdk"|wc -l`"x == "1"x ]; then
  105. echo_r "ERROR: please uninstall OpenJDK and install jdk first"
  106. exit 1;
  107. fi
  108. # OS specific support. $var _must_ be set to either true or false.
  109. cygwin=false
  110. darwin=false
  111. os400=false
  112. case "`uname`" in
  113. CYGWIN*) cygwin=true;;
  114. Darwin*) darwin=true;;
  115. OS400*) os400=true;;
  116. esac
  117. # resolve links - $0 may be a softlink
  118. PRG="$0"
  119. while [ -h "$PRG" ]; do
  120. ls=`ls -ld "$PRG"`
  121. link=`expr "$ls" : '.*-> \(.*\)$'`
  122. if expr "$link" : '/.*' > /dev/null; then
  123. PRG="$link"
  124. else
  125. PRG=`dirname "$PRG"`/"$link"
  126. fi
  127. done
  128. # Get standard environment variables
  129. PRGDIR=`dirname "$PRG"`
  130. WORKDIR=`cd "$PRGDIR" >/dev/null; pwd`;
  131. APP_ARTIFACT=opencron-server
  132. APP_VERSION="1.1.0-RELEASE";
  133. APP_WAR_NAME=${APP_ARTIFACT}.war
  134. MAVEN_TARGET_WAR="${WORKDIR}"/${APP_ARTIFACT}/target/${APP_WAR_NAME}
  135. DIST_PATH=${WORKDIR}/dist/
  136. [ ! -d "${DIST_PATH}" ] && mkdir -p "${DIST_PATH}"
  137. DEPLOY_PATH=${WORKDIR}/dist/opencron-server
  138. STARTUP_SHELL=${WORKDIR}/${APP_ARTIFACT}/startup.sh
  139. #先检查dist下是否有war包
  140. if [ ! -f "${DIST_PATH}/${APP_WAR_NAME}" ] ; then
  141. #dist下没有war包则检查server的target下是否有war包.
  142. if [ ! -f "${MAVEN_TARGET_WAR}" ] ; then
  143. echo_w "[opencron] please build project first!"
  144. exit 0;
  145. else
  146. cp ${MAVEN_TARGET_WAR} ${DIST_PATH};
  147. fi
  148. fi
  149. [ -d "${DEPLOY_PATH}" ] && rm -rf ${DEPLOY_PATH}/* || mkdir -p ${DEPLOY_PATH}
  150. # unpackage war to dist
  151. cp ${DIST_PATH}/${APP_WAR_NAME} ${DEPLOY_PATH} && cd ${DEPLOY_PATH} && jar xvf ${APP_WAR_NAME} >/dev/null 2>&1 && rm -rf ${DEPLOY_PATH}/${APP_WAR_NAME}
  152. #copy jars...
  153. cp -r ${WORKDIR}/${APP_ARTIFACT}/work ${DEPLOY_PATH}
  154. #copy startup.sh
  155. cp ${STARTUP_SHELL} ${DEPLOY_PATH}
  156. #startup
  157. /bin/bash +x "${DEPLOY_PATH}/startup.sh" "$@"