add binary deployment scripts

This commit is contained in:
zhuyasen 2023-03-11 19:24:49 +08:00
parent 3591c0a90a
commit d926641351
8 changed files with 171 additions and 60 deletions

3
.github/RELEASE.md vendored
View File

@ -1,3 +1,4 @@
## New
- Add i18n to the command line interface UI
- Update template code
- Add binary automated deployment scripts

View File

@ -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"

View File

@ -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
<br>
### Automated deployment service
> ./deploy.sh

View File

@ -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

46
deployments/binary/run.sh Normal file
View File

@ -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

View File

@ -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

21
scripts/binary-package.sh Normal file
View File

@ -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"

View File

@ -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