From d9266413516f52d620967fb28c5fe99dac9180ff Mon Sep 17 00:00:00 2001 From: zhuyasen Date: Sat, 11 Mar 2023 19:24:49 +0800 Subject: [PATCH] add binary deployment scripts --- .github/RELEASE.md | 3 +- Makefile | 19 +++++-- deployments/binary/README.md | 26 +++++++++ deployments/binary/deploy.sh | 30 +++++++++++ deployments/binary/run.sh | 46 ++++++++++++++++ deployments/docker-compose/README.md | 6 +-- scripts/binary-package.sh | 21 ++++++++ scripts/deploy-binary.sh | 80 ++++++++++------------------ 8 files changed, 171 insertions(+), 60 deletions(-) create mode 100644 deployments/binary/README.md create mode 100644 deployments/binary/deploy.sh create mode 100644 deployments/binary/run.sh create mode 100644 scripts/binary-package.sh diff --git a/.github/RELEASE.md b/.github/RELEASE.md index d29d8e0..2bf6261 100644 --- a/.github/RELEASE.md +++ b/.github/RELEASE.md @@ -1,3 +1,4 @@ ## New -- Add i18n to the command line interface UI +- Update template code +- Add binary automated deployment scripts diff --git a/Makefile b/Makefile index b84559f..4f4c8e9 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SHELL := /bin/bash PROJECT_NAME := "github.com/zhufuyi/sponge" PKG := "$(PROJECT_NAME)" PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/ | grep -v /api/) -GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go) +#GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go) .PHONY: install @@ -90,14 +90,14 @@ proto-doc: .PHONY: build # build serverNameExample_mixExample for linux amd64 binary build: - @echo "building 'serverNameExample_mixExample', binary file will output to 'cmd/serverNameExample_mixExample'" + @echo "building 'serverNameExample_mixExample', linux binary file will output to 'cmd/serverNameExample_mixExample'" @cd cmd/serverNameExample_mixExample && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,direct go build -gcflags "all=-N -l" # delete the templates code start .PHONY: build-sponge # build sponge for linux amd64 binary build-sponge: - @echo "building 'sponge', linux amd64 binary file will output to 'cmd/sponge'" + @echo "building 'sponge', linux binary file will output to 'cmd/sponge'" @cd cmd/sponge && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,direct go build -ldflags "all=-s -w" # delete the templates code end @@ -153,6 +153,18 @@ deploy-docker: @bash scripts/deploy-docker.sh +.PHONY: binary-package +# packaged binary files +binary-package: build + @bash scripts/binary-package.sh + + +.PHONY: deploy-binary +# deploy binary, e.g. make deploy-binary USER=root PWD=123456 IP=192.168.1.10 +deploy-binary: binary-package + @expect scripts/deploy-binary.sh $(USER) $(PWD) $(IP) + + .PHONY: clean # clean binary file, cover.out, template file clean: @@ -162,6 +174,7 @@ clean: @rm -vrf internal/ecode/*go.gen.* @rm -vrf internal/handller/*go.gen.* @rm -vrf internal/service/*go.gen.* + @rm -rf serverNameExample-binary.tar.gz @echo "clean finished" diff --git a/deployments/binary/README.md b/deployments/binary/README.md new file mode 100644 index 0000000..e8dad23 --- /dev/null +++ b/deployments/binary/README.md @@ -0,0 +1,26 @@ + +copy the configuration file to the configs directory and binary file before starting the service. + +``` +├── configs +│ └── serverNameExample.yml +├── serverNameExample +├── deploy.sh +└── run.sh +``` + +### Running and stopping service manually + +Running service: + +> ./run.sh + +Stopping the service: + +> ./run.sh stop + +
+ +### Automated deployment service + +> ./deploy.sh diff --git a/deployments/binary/deploy.sh b/deployments/binary/deploy.sh new file mode 100644 index 0000000..1244560 --- /dev/null +++ b/deployments/binary/deploy.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +serviceName="serverNameExample" + +function checkResult() { + result=$1 + if [ ${result} -ne 0 ]; then + exit ${result} + fi +} + +# determine if the startup service script run.sh exists +runFile="~/app/${serviceName}/run.sh" +if [ ! -f "$runFile" ]; then + # if it does not exist, copy the entire directory + mkdir -p ~/app + cp -rf /tmp/${serviceName}-binary ~/app/ + checkResult $? + rm -rf /tmp/${serviceName}-binary* +else + # replace only the binary file if it exists + cp -f ${serviceName}-binary/${serviceName} ~/app/${serviceName}-binary/${serviceName} + checkResult $? + rm -rf /tmp/${serviceName}-binary* +fi + +# running service +cd ~/app/${serviceName}-binary +chmod +x run.sh +./run.sh diff --git a/deployments/binary/run.sh b/deployments/binary/run.sh new file mode 100644 index 0000000..5034b6f --- /dev/null +++ b/deployments/binary/run.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +serviceName="serverNameExample" +cmdStr="./${serviceName} -c configs/${serviceName}.yml" + +chmod +x ./${serviceName} + +stopService(){ + NAME=$1 + + ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'` + if [ -n "$ID" ]; then + for id in $ID + do + kill -9 $id + echo "Stopped ${NAME} service successfully, process ID=${ID}" + done + fi +} + +startService() { + NAME=$1 + + nohup ${cmdStr} > ${serviceName}.log 2>&1 & + sleep 1 + + ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'` + if [ -n "$ID" ]; then + echo "Start the ${NAME} service ...... process ID=${ID}" + else + echo "Failed to start ${NAME} service" + return 1 + fi + return 0 +} + + +stopService ${serviceName} +if [ "$1"x != "stop"x ] ;then + sleep 1 + startService ${serviceName} + exit $? + echo "" +else + echo "Service ${serviceName} has stopped" +fi diff --git a/deployments/docker-compose/README.md b/deployments/docker-compose/README.md index b5318df..5d87a38 100644 --- a/deployments/docker-compose/README.md +++ b/deployments/docker-compose/README.md @@ -1,5 +1,5 @@ -copy the configuration file to the configs directory before starting the service +copy the configuration file to the configs directory before starting the service. ``` ├── configs @@ -7,6 +7,6 @@ copy the configuration file to the configs directory before starting the service └── docker-compose.yml ``` -running services: +running service: -> docker-compose up +> docker-compose up -d diff --git a/scripts/binary-package.sh b/scripts/binary-package.sh new file mode 100644 index 0000000..cab4ca0 --- /dev/null +++ b/scripts/binary-package.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +serviceName="serverNameExample" + +mkdir -p ${serviceName}-binary/configs + +cp -f deployments/binary/run.sh ${serviceName}-binary +chmod +x ${serviceName}-binary/run.sh + +cp -f deployments/binary/deploy.sh ${serviceName}-binary +chmod +x ${serviceName}-binary/deploy.sh + +cp -f cmd/${serviceName}_mixExample/${serviceName}_mixExample ${serviceName}-binary +cp -f configs/${serviceName}.yml ${serviceName}-binary/configs +cp -f configs/${serviceName}_cc.yml ${serviceName}-binary/configs + +tar zcvf ${serviceName}-binary.tar.gz ${serviceName}-binary +rm -rf ${serviceName}-binary + +echo "" +echo "package binary successfully, output file = ${serviceName}-binary.tar.gz" diff --git a/scripts/deploy-binary.sh b/scripts/deploy-binary.sh index 44b5385..5ccd89e 100644 --- a/scripts/deploy-binary.sh +++ b/scripts/deploy-binary.sh @@ -1,61 +1,35 @@ -#!/bin/bash +#!/usr/bin/expect -# chkconfig: - 85 15 -# description: serverNameExample +set serviceName "serverNameExample" -serverName="serverNameExample_mixExample" -cmdStr="cmd/${serverName}/${serverName}" +# parameters +set username [lindex $argv 0] +set password [lindex $argv 1] +set hostname [lindex $argv 2] +set timeout 30 -function checkResult() { - result=$1 - if [ ${result} -ne 0 ]; then - exit ${result} - fi -} +spawn scp -r ./${serviceName}-binary.tar.gz ${username}@${hostname}:/tmp/ +#expect "*yes/no*" +#send "yes\r" +expect "*password:*" +send "${password}\r" +expect eof -stopService(){ - NAME=$1 +spawn ssh ${username}@${hostname} +#expect "*yes/no*" +#send "yes\r" +expect "*password:*" +send "${password}\r" - ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'` - if [ -n "$ID" ]; then - for id in $ID - do - kill -9 $id - echo "Stopped ${NAME} service successfully, process ID=${ID}" - done - fi -} +# execute a command or script +expect "*${username}@*" +send "cd /tmp && tar zxvf ${serviceName}-binary.tar.gz\r" +expect "*${username}@*" +send "bash /tmp/${serviceName}-binary/deploy.sh\r" -startService() { - NAME=$1 +# logging out of a session +expect "*${username}@*" +send "exit\r" - if [ -f "${NAME}" ] ;then - rm "${NAME}" - fi - sleep 0.2 - go build -o ${cmdStr} cmd/${NAME}/main.go - checkResult $? - - nohup ${cmdStr} > ${NAME}.log 2>&1 & - sleep 1 - - ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'` - if [ -n "$ID" ]; then - echo "Start the ${NAME} service successfully, process ID=${ID}" - else - echo "Failed to start ${NAME} service" - return 1 - fi - return 0 -} - - -stopService ${serverName} -if [ "$1"x != "stop"x ] ;then - sleep 1 - startService ${serverName} - checkResult $? -else - echo "Service ${serverName} has stopped" -fi +expect eof