diff --git a/.gitignore b/.gitignore index b12b999..a684e5e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ node_modules # temp folders -build dist _book _jsdoc diff --git a/codes/build/java-app-boot.sh b/codes/build/java-app-boot.sh new file mode 100644 index 0000000..44c90dd --- /dev/null +++ b/codes/build/java-app-boot.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash + +################################################################################# +# JAVA 应用通用启动脚本 +# Author: Zhang Peng +################################################################################# + +# 检查脚本参数,如必要参数未传入,退出脚本。 +function checkInput() { + if [ "${app}" == "" ] || [ "${oper}" == "" ] || [ "${javaArgs}" == "" ] || [ "${classpathArgs}" == "" ] || [ "${bootstrapClass}" == "" ]; then + echo "请输入脚本参数:app oper javaArgs classpathArgs bootstrapClass" + echo " app: 应用名。" + echo " oper: 运行环境(必填)。可选值:start|stop|restart" + echo " javaArgs: JVM 参数(必填)。" + echo " classpathArgs: classpath参数(必填)。" + echo " bootstrapClass: 启动类(必填)。" + exit 0 + fi +} + +# 检查文件夹是否存在,不存在则创建 +function createFolderIfNotExist() { + if [ ! -d "$1" ];then + mkdir -p "$1" + fi +} + +# 检查服务是否已经启动 +pids="" +function checkStarted() { + pids=`ps -ef | grep java | grep ${app} | awk '{print $2}'` + if [ -n "${pids}" ]; then + return 0 + else + return 1 + fi +} + +function main() { + case "${oper}" in + start ) + echo -n "starting server: " + # 检查服务是否已经启动 + if checkStarted ;then + echo "ERROR: server already started!" + echo "PID: ${pids}" + exit 1 + fi + + args="${javaArgs} -classpath ${classpathArgs} ${bootstrapClass}" + echo -e "statup params:\n ${args}" + + #启动服务 + touch ${LOG_DIR}/${app}-startup.log + nohup java ${args} > ${LOG_DIR}/${app}-startup.log 2>&1 & + # echo -e "执行参数:\n${args}" + echo -e "\nthe server is started..." + ;; + stop ) + echo -n "stopping server: " + #dubbo提供优雅停机, 不能使用kill -9 + if checkStarted ;then + kill ${pids} + echo -e "\nthe server is stopped..." + else + echo -e "\nno server to be stopped..." + fi + ;; + restart ) + $0 ${app} stop "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" + sleep 5 + $0 ${app} start "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" + ;; + * ) + echo "Invalid oper: ${oper}." + exit 1 + esac + + exit 0 +} + +######################################## MAIN ######################################## +# 设置环境变量 +export LANG="zh_CN.UTF-8" + +# 获取输入参数 +app=`echo $1` +oper=`echo $2` +javaArgs=`echo $3` +classpathArgs=`echo $4` +bootstrapClass=`echo $5` +vars=$* +checkInput + +# 设置全局常量 +LOG_DIR=/home/zp/log +createFolderIfNotExist ${LOG_DIR} + +# 执行 main 方法 +main diff --git a/codes/build/java-app-release.sh b/codes/build/java-app-release.sh new file mode 100644 index 0000000..a5e9788 --- /dev/null +++ b/codes/build/java-app-release.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash + +################################################################################# +# JAVA 应用发布脚本 +# Author: Zhang Peng +################################################################################# + +# 检查脚本参数,如必要参数未传入,退出脚本。 +checkInput() { + if [ "${branch}" == "" ] || [ "${profile}" == "" ]; then + echo "请输入脚本参数:branch profile" + echo " branch: git分支(必填)。如 feature/1.1.16, master" + echo " profile: 运行环境(必填)。可选值:development | test" + echo "例:./java-app-release.sh feature/1.1.16 test" + exit 0 + fi +} + +# 检查文件是否存在,不存在则退出脚本 +checkFileExist() { + if [ ! -f "$1" ] + then + echo "关键文件 $1 找不到,脚本执行结束" + exit 0 + fi +} + +# 检查文件夹是否存在,不存在则创建 +createFolderIfNotExist() { + if [ ! -d "$1" ];then + mkdir -p "$1" + fi +} + +# 记录发布的版本信息 +saveVersionInfo() { + rm -rf ${VERSION_LOG_FILE} + touch ${VERSION_LOG_FILE} + chmod 777 ${VERSION_LOG_FILE} + + echo -e "\n=================== Version Info ===================" >> ${VERSION_LOG_FILE} + echo "Branch is: ${branch}" >> ${VERSION_LOG_FILE} + echo "Profile is: ${profile}" >> ${VERSION_LOG_FILE} + echo "CommitID is : $(git log --pretty=oneline -1)" >> ${VERSION_LOG_FILE} +} + +######################################## MAIN ######################################## +# 设置环境变量 +export LANG="zh_CN.UTF-8" + +# 设置全局常量 +LOG_DIR=/home/zp/log/ +SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) +SOURCE_DIR=/home/zp/source/ +APP_NAME=XXX +RESOURCES_DIR=/home/zp/source/${APP_NAME}/src/main/resources +UPDATE_CODE_SCRIPT_FILE=${SCRIPT_DIR}/update-code.sh +MAVEN_LOG_FILE=${LOG_DIR}/${APP_NAME}-maven.log +VERSION_LOG_FILE=${LOG_DIR/${APP_NAME}-version.log + +# 0. 获取传入参数并检查 +branch=`echo $1` +profile=`echo $2` +checkInput +checkFileExist ${UPDATE_CODE_SCRIPT_FILE} +createFolderIfNotExist ${LOG_DIR} +createFolderIfNotExist ${SOURCE_DIR} + +echo ">>>>>>>>>>>>>> 1. 停止应用" +${SCRIPT_DIR}/java-app-run.sh ${profile} stop + +echo ">>>>>>>>>>>>>> 2. 更新代码" +${UPDATE_CODE_SCRIPT_FILE} ${APP_NAME} ${branch} ${SOURCE_DIR} +execode=$? +if [ "${execode}" == "0" ]; then + echo "更新代码成功" +else + echo "更新代码失败" + exit 1 +fi + +echo ">>>>>>>>>>>>>> 3. 替换配置" +# 有则加,无则过 + +echo ">>>>>>>>>>>>>> 4. 构建编译" +cd ${SOURCE_DIR}/ck-lion +mvn clean package -e -Dmaven.test.skip=true | tee ${MAVEN_LOG_FILE} +eexecode=$? +if [ "${execode}" == "0" ]; then + echo "构建编译成功" + echo "编译详情见:${MAVEN_LOG_FILE}" +else + echo "构建编译失败" + echo "编译详情见:${MAVEN_LOG_FILE}" + exit 1 +fi + +echo ">>>>>>>>>>>>>> 5. 启动应用" +# 手动释放内存 +echo 3 > /proc/sys/vm/drop_caches +${SCRIPT_DIR}/java-app-run.sh ${profile} start +execode=$? +if [ "${execode}" == "0" ]; then + echo "启动应用成功" +else + echo "启动应用失败" + exit 1 +fi + +echo ">>>>>>>>>>>>>> 6. 记录发布的版本信息" +saveVersionInfo + +echo ">>>>>>>>>>>>>> 发布应用结束" diff --git a/codes/build/java-app-run.sh b/codes/build/java-app-run.sh new file mode 100644 index 0000000..783169e --- /dev/null +++ b/codes/build/java-app-run.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +################################################################################# +# JAVA 应用运行脚本 +# Author: Zhang Peng +################################################################################# + +# 检查脚本参数,如必要参数未传入,退出脚本。 +function checkInput() { + if [ "${profile}" == "" ] || [ "${oper}" == "" ]; then + echo "请输入脚本参数:profile oper [debug]" + echo " profile: 运行环境(必填)。可选值:development|test" + echo " oper: 运行环境(必填)。可选值:start|stop|restart" + echo " debug: debug启动开关。默认不填为不启动。" + exit 0 + fi +} + +#检查文件是否存在,不存在则退出脚本 +function checkFileExist() { + if [ ! -f "$1" ] + then + echo "关键文件 $1 找不到,脚本执行结束" + exit 0 + fi +} + +# 封装启动参数,调用启动脚本 +function main(){ + APP_NAME=ck-lion + + # JVM 参数 + JAVA_OPTS=" -Djava.awt.headless=true -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true -Ddubbo.shutdown.hook=true -Dspring.profiles.active=${profile} -Djava.security.egd=file:/dev/./urandom -Xms1024m -Xmx1024m -Xss2m " + JAVA_DEBUG_OPTS="" + if [ "$2" == "debug" ]; then + JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=2236,server=y,suspend=n " + shift + fi + javaArgs=" ${JAVA_OPTS} ${JAVA_DEBUG_OPTS} " + + # classpath 参数 + classpathArgs="${SERVER_ROOT}/WEB-INF/classes:${SERVER_ROOT}/WEB-INF/lib/*" + + # 启动类 + bootstrapClass="com.alibaba.dubbo.container.Main" + + ${SCRIPT_DIR}/java-app-boot.sh ${APP_NAME} ${oper} "${javaArgs}" "${classpathArgs}" "${bootstrapClass}" + execode=$? + if [ "${execode}" == "0" ]; then + echo "执行操作成功" + else + echo "执行操作失败" + exit 1 + fi +} + +######################################## MAIN ######################################## +# 设置环境变量 +export LANG="zh_CN.UTF-8" + +# 设置全局常量 +SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) +SOURCE_DIR=/home/zp/source/ +APP_NAME=XXX +SERVER_ROOT=/home/zp/source/${APP_NAME}/target/ + +# 0. 获取传入参数并检查 +profile=$1 +oper=$2 +debug=$3 +checkInput + +# 1. 执行操作 +main diff --git a/codes/build/js-app-release.sh b/codes/build/js-app-release.sh new file mode 100644 index 0000000..10f7f7f --- /dev/null +++ b/codes/build/js-app-release.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +################################################################################# +# 前端应用发布脚本 +# Author: Zhang Peng +################################################################################# + +# 检查脚本参数,如必要参数未传入,退出脚本。 +function checkInput() { + if [ "${branch}" == "" ]; then + echo "请输入脚本参数:branch" + echo " branch: git分支(必填)。如 feature/1.1.16, master" + echo "例:./js-app-release.sh feature/1.1.16" + exit 0 + fi +} + +# 检查文件是否存在,不存在则退出脚本 +function checkFileExist() { + if [ ! -f "$1" ] + then + echo "关键文件 $1 找不到,脚本执行结束" + exit 0 + fi +} + +# 检查文件夹是否存在,不存在则创建 +function createFolderIfNotExist() { + if [ ! -d "$1" ];then + mkdir -p "$1" + fi +} +##############################__MAIN__######################################## +# 设置全局常量 +SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) +SOURCE_DIR=/home/zp/source/ +APP_NAME=XXX +UPDATE_CODE_SCRIPT_FILE=${SCRIPT_DIR}/update-code.sh + +# 0. 获取传入参数并检查 +branch=`echo $1` +profile=`echo $2` +checkInput +checkFileExist ${UPDATE_CODE_SCRIPT_FILE} +createFolderIfNotExist ${SOURCE_DIR} + +echo ">>>>>>>>>>>>>> 1. 停止应用" +# 有则加,无则过 + +echo ">>>>>>>>>>>>>> 2. 更新代码" +${UPDATE_CODE_SCRIPT_FILE} ${APP_NAME} ${branch} ${SOURCE_DIR} +execode=$? +if [ "${execode}" == "0" ]; then + echo "更新代码成功" +else + echo "更新代码失败" + exit 1 +fi + +echo ">>>>>>>>>>>>>> 3. 替换配置" +# 有则加,无则过 + +echo ">>>>>>>>>>>>>> 4. 构建启动" +cd ${SOURCE_DIR}/${APP_NAME} +source "${HOME}/.nvm/nvm.sh" +nvm use 8.9 +npm install +npm run build diff --git a/codes/build/project-release.sh b/codes/build/project-release.sh new file mode 100644 index 0000000..7c03520 --- /dev/null +++ b/codes/build/project-release.sh @@ -0,0 +1,188 @@ +#!/usr/bin/env bash + +################################################################################### +# 项目发布脚本模板 +# Author: Zhang Peng +################################################################################### + +# 检查文件是否存在,不存在则退出脚本 +function checkFileExist() { + if [ ! -f "$1" ] + then + echo "关键文件 $1 找不到,脚本执行结束" + exit 0 + fi +} + +# 检查文件夹是否存在,不存在则创建 +function createFolderIfNotExist() { + if [ ! -d "$1" ];then + mkdir -p "$1" + fi +} + +function chooseAppName() { +cat << EOF +请选择应用名(数字或关键字均可)。 +可选值如下: + [0] all (所有应用) + [1] APP1 + [2] APP2 +EOF + +while read app +do + case ${app} in + 0 ) + app=all + break ;; + 1 ) + app=APP1 + break ;; + 2 ) + app=APP2 + break ;; + all | APP1 | APP2 ) + break ;; + * ) echo "无法识别 ${app}" ;; + esac +done +} + +function chooseBranch() { +cat << EOF +请输入 git 分支。 +如:develop、master、feature/xxx +EOF + +read branch +if [[ "${branch}" =~ ^(feature/)([^ \f\n\r\t\v]+) ]] || [ "${branch}" == "develop" ] || [ "${branch}" == "master" ]; then + echo "输入了 ${branch}" +else + echo "无法识别 ${branch}" + chooseBranch +fi +} + +function chooseProfile() { +cat << EOF +请选择运行环境(数字或关键字均可)。 +可选值: + [1] develop (开发环境) + [2] test (测试环境) + [3] preview (预发布环境) + [4] product (生产环境) +EOF + +while read profile +do + case ${profile} in + 1 ) + profile=develop + break ;; + 2 ) + profile=test + break ;; + 3 ) + profile=preview + break ;; + 4 ) + profile=product + break ;; + develop | test | preview | product ) + break ;; + * ) echo "无法识别 ${profile}" ;; + esac +done +} + +function inputParams() { + chooseAppName + chooseBranch + chooseProfile + +cat << EOF +=================================================== +请确认您的选择:Y/N + app: ${app} + branch: ${branch} + profile: ${profile} +=================================================== +EOF + + while read confirm + do + case ${confirm} in + y | Y ) + echo -e "\n\n>>>>>>>>>>>>>> 开始发布应用" + break ;; + n | N ) + echo -e "重新输入发布参数\n" + inputParams ;; + * ) + echo "无法识别 ${confirm}" ;; + esac + done +} + +function printHeadInfo() { +cat << EOF +*********************************************************************************** +* 欢迎使用项目引导式发布脚本。 +* 输入任意键进入脚本操作。 +*********************************************************************************** +EOF +} + +function printFootInfo() { +cat << EOF + + +*********************************************************************************** +* 安装过程结束。 +* 输入任意键进入脚本操作。 +* 如果不想安装其他应用,输入 exit 回车或输入 退出。 +*********************************************************************************** +EOF +} + +function main() { + # 输入执行参数 + app="" + branch="" + profile="" + inputParams + + if [ "${app}" == "all" ]; then + checkFileExist ${SCRIPT_DIR}/java-app-release.sh + checkFileExist ${SCRIPT_DIR}/js-app-release.sh + ${SCRIPT_DIR}/io-alch-release.sh ${branch} ${profile} + ${SCRIPT_DIR}/ck-alch-release.sh ${branch} ${profile} + else + checkFileExist ${SCRIPT_DIR}/${app}-release.sh + ${SCRIPT_DIR}/${app}-release.sh ${branch} ${profile} + fi +} + +######################################## MAIN ######################################## +# 设置环境变量 +export LANG="zh_CN.UTF-8" + +# 设置全局常量 +SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) +SOURCE_DIR=/home/zp/source/ +# 如果源码存放目录不存在则创建 +createFolderIfNotExist ${SOURCE_DIR} + +printHeadInfo +while read sign +do + case ${sign} in + exit ) + exit 0 ;; + * ) + main ;; + esac + + printFootInfo +done diff --git a/codes/demos/git/git-fetch-or-clone.sh b/codes/build/update-code.sh similarity index 63% rename from codes/demos/git/git-fetch-or-clone.sh rename to codes/build/update-code.sh index 46c7841..fcbba02 100644 --- a/codes/demos/git/git-fetch-or-clone.sh +++ b/codes/build/update-code.sh @@ -1,8 +1,12 @@ -#!/bin/bash +#!/usr/bin/env bash + +################################################################################### +# 更新代码脚本 +# 只有退出码(exit code)为 0 ,表示脚本执行成功。 +# Author: Zhang Peng +################################################################################### -# # 检查脚本参数,如必要参数未传入,退出脚本。 -# checkInput() { if [ "${repository}" == "" ] || [ "${branch}" == "" ]; then echo "请输入脚本参数:repository branch [source] [target]" @@ -10,18 +14,23 @@ checkInput() { echo " branch: git 分支(必填)。如 master/develop" echo " source: 代码存放目录。默认为/home/zp/source。" echo " target: 代码存放目录。默认为脚本所在目录。" - exit 0 + exit 1 + fi +} + +# 检查文件夹是否存在,不存在则创建 +function createFolderIfNotExist() { + if [ ! -d "$1" ];then + mkdir -p "$1" fi } -# # 判断 git 版本库是否存在。根据实际结果修改 ${gitok} 值。 -# gitok=false -isGitExist() { - cd ${SOURCE_PATH} - if [ -d "${SOURCE_PATH}/${repository}/${target}" ]; then - cd ${SOURCE_PATH}/${repository}/${target} +function isGitExist() { + cd ${SOURCE_DIR} + if [ -d "${SOURCE_DIR}/${repository}/${target}" ]; then + cd ${SOURCE_DIR}/${repository}/${target} #(1)删除git状态零时文件 if [ -f "gitstatus.tmp" ]; then rm -rf gitstatus.tmp @@ -35,19 +44,14 @@ isGitExist() { fi #返回到主目录 - cd ${SOURCE_PATH} + cd ${SOURCE_DIR} fi } -# # 如果 git 版本库存在(根据 ${gitok} 值),执行 fetch 操作;反之,执行 clone 操作。 -# -doFetchOrClone() { - if [ ! -d "${SOURCE_PATH}" ]; then - mkdir -p ${SOURCE_PATH} - fi +function doFetchOrClone() { if ${gitok}; then - cd ${SOURCE_PATH}/${repository}/${target} + cd ${SOURCE_DIR}/${repository}/${target} git reset --hard git clean -ffdx git fetch @@ -55,49 +59,47 @@ doFetchOrClone() { else #删除所有内容,便于重新进行git clone rm -rf ${repository} - git clone --no-checkout git@github.com:${ACCOUNT}/${repository}.git ${SOURCE_PATH}/${repository}/${target} + git clone --no-checkout git@github.com:${GITHUB_ACCOUNT}/${repository}.git ${SOURCE_DIR}/${repository}/${target} echo "git clone ${repository} remote repository 到本地成功" - cd ${SOURCE_PATH}/${repository}/${target} + cd ${SOURCE_DIR}/${repository}/${target} fi } -# # 切换到 ${branch} 分支 -# -doCheckout() { +function doCheckout() { echo "检出 ${repository} ${branch} 分支代码" isRemoteBranch=false gitRemoteBranch=`git branch -r` echo -e "$gitRemoteBranch" | grep -iwq ${branch} && isRemoteBranch=true || isRemoteBranch=false if ${isRemoteBranch}; then - echo "找到 ${branch} 对应的远端分支" + echo "找到 ${branch} 分支。" git checkout -f 'origin/'${branch} else - git checkout -f ${branch} + echo "未找到 ${branch} 分支!" + exit 2 fi echo "更新子模块代码" git submodule update --init --recursive --force } -##############################__MAIN__######################################## +######################################## MAIN ######################################## export LANG="zh_CN.UTF-8" -ACCOUNT=dunwu -SOURCE_PATH=/home/zp/source - -# 必填输入参数 -repository=`echo $1` -branch=`echo $2` - -# 可选输入参数 -source=`echo $3` -target=`echo $4` -if [ "${source}" != "" ]; then - SOURCE_PATH=${source} -fi # 0. 检查传入的参数 +repository=`echo $1` +branch=`echo $2` +source=`echo $3` +target=`echo $4` checkInput +# 设置全局常量 +GITHUB_ACCOUNT=dunwu +SOURCE_DIR=/home/xyz/source +if [ "${source}" != "" ]; then + SOURCE_DIR=${source} +fi +createFolderIfNotExist ${SOURCE_DIR} + # 1. 判断本地是否已存在 Git 仓库 isGitExist @@ -107,4 +109,7 @@ doFetchOrClone # 3. 切换到指定分支 doCheckout -echo "代码检出完成!" +# 4. 分配权限 +chmod -R 777 ${SOURCE_DIR} + +exit 0 diff --git a/codes/demos/git/startup.sh b/codes/demos/git/startup.sh deleted file mode 100644 index 6644c99..0000000 --- a/codes/demos/git/startup.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -export LANG="zh_CN.UTF-8" -export JAVA_HOME=/opt/software/jdk1.8.0_121 - -##############################__MAIN__######################################## -# 0. 检查传入的参数 -repository=`echo $1` -branch=`echo $2` -target=`echo $3` -if [ "${repository}" == "" ] || [ "${branch}" == "" ]; -then - echo "用法:repository branch target" - echo " repository: git仓储。" - echo " branch: git分支。" - echo " target: 代码存放目录。可选参数,默认为脚本所在目录。" - exit 0 -fi - -# 1. 更新代码 -sh update-code.sh ${repository} ${branch} ${target} - -# 2. 编译 -cd ${target}/codes -echo "编译开始" -mvn clean package -Dmaven.test.skip=true -echo "编译结束"