当前位置: 代码迷 >> 综合 >> 基于 Putty 的自动部署(Linux)
  详细解决方案

基于 Putty 的自动部署(Linux)

热度:88   发布时间:2023-12-02 20:18:18.0

 

假设当前项目的名称为 osmp

1、在 Linux 服务器上创建一个新的账户, 账户名为 dev。该账户主要用户部署程序

2、在 ~ 目录(用户的根目录)下,新建 osmp 文件夹,在 osmp 下新建 deploy 文件夹

# op、office、meeting 为模块名, app.sh 为服务器自动部署脚本。apm 为链路监控的固定文件夹。
~
|-- osmp |-- deploy   |-- apm|-- agent|-- skywalking-agent.jar|-- op|-- app.sh|-- office|-- app.sh|-- meeting|-- app.sh

一、服务器执行脚本( app.sh )

app.sh 的内容如下,修改 SERVICE_NAME 与当前文件夹的名称相同。

SERVICE_NAME=op
JAR=$SERVICE_NAME-webstart() {echo "=============================start=============================="PID_CMD="ps -ef | grep $JAR | grep -v grep | awk '{print \$2}'"PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenecho "Application is already running, PID is $PID"elsenohup java -javaagent:/dev/osmp/deploy/apm/agent/skywalking-agent.jar -Dskywalking.agent.service_name=$SERVICE_NAME -Dskywalking.collector.backend_service=10.14.2.27:11800  -jar $JAR.jar >/dev/null 2>&1 &echo "Application has run by skywalking-agent!!!"PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenecho "Start Application successfully, PID is $PID"elseecho "Failed to start Application!!!"fifi  echo "=============================start=============================="
}stop() {echo "=============================stop=============================="PID_CMD="ps -ef | grep $JAR | grep -v grep | awk '{print \$2}'"PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenkill -15 $PIDsleep 5PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenecho "Stop Application failed by kill -15 $PID, begin to kill -9 $PID"kill -9 $PIDsleep 2echo "Stop Application successfully by kill -9 $PID"else echo "Stop Application successfully by kill -15 $PID"fi elseecho "Application is not running!!!"fiecho "=============================stop=============================="
}restart() {echo "=============================restart=============================="stopstartecho "=============================restart=============================="
}status() {echo "=============================status==============================" PID_CMD="ps -ef | grep $JAR | grep -v grep | awk '{print \$2}'"PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenecho "Application is running, PID is $PID"elseecho "Application is not running!!!"fiecho "=============================status=============================="
}info() {echo "help: help info"
}help() {echo "start: start server"echo "stop: shutdown server"echo "restart: restart server"echo "status: display status of server"echo "info: display info of server"echo "help: help info"
}case $1 in
start)start;;
stop)stop;;
restart)restart;;
status)status;;
info)info;;
help)help;;
*)help;;
esac
exit $?

sh 中命令, 使用 ./app.sh start 来启动

  • ./app.sh start
  • ./app.sh stop
  • ./app.sh restart
  • ./app.sh status

## app.sh
JAR=op-gdbh-web
SERVICE_NAME=op_gdbh
JAVA_OPTS=" -Xms256M -Xmx512M"
start() {echo "=============================start=============================="PID_CMD="ps -ef | grep $JAR | grep -v grep | awk '{print \$2}'"PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenecho "Application is already running, PID is $PID"elsenohup java $JAVA_OPTS -javaagent:/home/admin/osmp_deploy/ytgz/apm/agent/skywalking-agent.jar -Dskywalking.agent.service_name=$SERVICE_NAME -Dskywalking.collector.backend_service=10.14.2.183:11800  -jar $JAR.jar >/dev/null 2>&1 &echo "Application has run by skywalking-agent!!!"PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenecho "Start Application successfully, PID is $PID"elseecho "Failed to start Application!!!"fifi  echo "=============================start=============================="
}stop() {echo "=============================stop=============================="PID_CMD="ps -ef | grep $JAR | grep -v grep | awk '{print \$2}'"PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenkill -15 $PIDsleep 5PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenecho "Stop Application failed by kill -15 $PID, begin to kill -9 $PID"kill -9 $PIDsleep 2echo "Stop Application successfully by kill -9 $PID"else echo "Stop Application successfully by kill -15 $PID"fi elseecho "Application is not running!!!"fiecho "=============================stop=============================="
}restart() {echo "=============================restart=============================="stopstartecho "=============================restart=============================="
}status() {echo "=============================status==============================" PID_CMD="ps -ef | grep $JAR | grep -v grep | awk '{print \$2}'"PID=$(eval $PID_CMD)if [[ -n $PID ]]; thenecho "Application is running, PID is $PID"elseecho "Application is not running!!!"fiecho "=============================status=============================="
}info() {echo "help: help info"
}help() {echo "start: start server"echo "stop: shutdown server"echo "restart: restart server"echo "status: display status of server"echo "info: display info of server"echo "help: help info"
}case $1 in
start)start;;
stop)stop;;
restart)restart;;
status)status;;
info)info;;
help)help;;
*)help;;
esac
exit $?

二、本地部署脚本 (deploy.bat

本地安装 putty,并 putty 安装路径加入到环境变量。

将 deploy.bat 放在 web工程的根目录。执行 bat 文件,将会自动打包部署。

# 工程的目录结构
|-- osmp|-- op|-- op-web|-- deploy.bat 

deploy.bat 的内容如下

@echo off:: 需要设置服务器的 ip、密码、服务名
set User=dev
set IP=???, 如 10.14.2.172
set PassWd=???
set Service=??? 如 opplink %User%@%IP% -pw %PassWd% "cd /dev/osmp/deploy/%Service%;ls"
call mvn clean compile package -DskipTests
pscp -l root -pw %PassWd%  op-web/target/%Service%-web.jar %IP%:/root/osmp_deploy/%Service%/%Service%-web.jar.deploying
plink %User%@%IP% -pw %PassWd%  "cd /dev/osmp/deploy/%Service%;./app.sh stop"
plink %User%@%IP% -pw %PassWd%  "cd /dev/osmp/deploy/%Service%;mv %Service%-web.jar %Service%-web.jar.old;mv %Service%-web.jar.deploying %Service%-web.jar"
plink %User%@%IP% -pw %PassWd%  "cd /dev/osmp/deploy/%Service%;./app.sh start"
plink %User%@%IP% -pw %PassWd%  "cd /dev/osmp/deploy/%Service%;./app.sh status"
pause