diff --git a/Makefile b/Makefile index 4f4c8e9..8b6091c 100644 --- a/Makefile +++ b/Makefile @@ -91,7 +91,8 @@ proto-doc: # build serverNameExample_mixExample for linux amd64 binary build: @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" + @cd cmd/serverNameExample_mixExample && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,direct go build + # delete the templates code start .PHONY: build-sponge @@ -107,34 +108,37 @@ build-sponge: run: @bash scripts/run.sh + .PHONY: run-nohup -# run service with nohup, if you want to stop the service, pass the parameter stop tag, e.g. make run-nohup TAG=stop +# run server with nohup to local, if you want to stop the server, pass the parameter stop, e.g. make run-nohup CMD=stop run-nohup: - @bash scripts/run-nohup.sh $(TAG) + @bash scripts/run-nohup.sh $(CMD) .PHONY: docker-image -# build docker image, use binary files to build +# build image for local docker, tag=latest, use binary files to build docker-image: build - @bash scripts/grpc_health_probe.sh - @mv -f cmd/serverNameExample_mixExample/serverNameExample_mixExample build/serverNameExample - @mkdir -p build/configs && cp -f configs/serverNameExample.yml build/configs/ - docker build -t project-name-example.server-name-example:latest build/ - @rm -rf build/serverNameExample build/configs build/grpc_health_probe + @bash scripts/image-build-local.sh .PHONY: image-build -# build docker image with parameters, use binary files to build, e.g. make image-build REPO_HOST=addr TAG=latest +# build image for remote repositories, use binary files to build, e.g. make image-build REPO_HOST=addr TAG=latest image-build: @bash scripts/image-build.sh $(REPO_HOST) $(TAG) .PHONY: image-build2 -# build docker image with parameters, phase II build, e.g. make image-build2 REPO_HOST=addr TAG=latest +# build image for remote repositories, phase II build, e.g. make image-build2 REPO_HOST=addr TAG=latest image-build2: @bash scripts/image-build2.sh $(REPO_HOST) $(TAG) +.PHONY: image-build-rpc-test +# build rpc test image for remote repositories, e.g. make image-build-rpc-test REPO_HOST=addr TAG=latest +image-build-rpc-test: + @bash scripts/image-rpc-test.sh $(REPO_HOST) $(TAG) + + .PHONY: image-push # push docker image to remote repositories, e.g. make image-push REPO_HOST=addr TAG=latest image-push: @@ -148,9 +152,9 @@ deploy-k8s: .PHONY: deploy-docker -# deploy service to docker +# deploy service to local docker, you must first run 'make docker-image' to generate a docker image, if you want to stop the server, pass the parameter stop, e.g. make deploy-docker CMD=stop deploy-docker: - @bash scripts/deploy-docker.sh + @bash scripts/deploy-docker.sh $(CMD) .PHONY: binary-package diff --git a/build/Dockerfile_build b/build/Dockerfile_build index 629ed7a..b3ef4c5 100644 --- a/build/Dockerfile_build +++ b/build/Dockerfile_build @@ -6,13 +6,19 @@ WORKDIR /go/src/serverNameExample RUN tar zxf serverNameExample.tar.gz RUN go env -w GOPROXY=https://goproxy.cn,direct RUN go mod download -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l" -o /serverNameExample cmd/serverNameExample/main.go +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /serverNameExample cmd/serverNameExample/main.go # build grpc_health_probe RUN go install github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 RUN cd $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 \ && go mod download \ - && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /grpc_health_probe + && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "all=-s -w" -o /grpc_health_probe + +# compressing binary files +#cd / +#upx -9 serverNameExample +#upx -9 grpc_health_probe + # building images with binary FROM alpine:latest diff --git a/build/Dockerfile_test b/build/Dockerfile_test new file mode 100644 index 0000000..8c2a49e --- /dev/null +++ b/build/Dockerfile_test @@ -0,0 +1,16 @@ +# Need to package the code first `tar zcf serverNameExample.tar.gz $(ls)` and move it to the same directory as Dokerfile +# rpc server source code, used to test rpc methods +FROM golang:1.18-alpine +MAINTAINER zhufuyi "g.zhufuyi@gmail.com" + +# go test dependency packages +RUN apk add bash alpine-sdk build-base gcc + +COPY . /go/src/serverNameExample +WORKDIR /go/src/serverNameExample +RUN tar zxf serverNameExample.tar.gz +RUN go env -w GOPROXY=https://goproxy.cn,direct +RUN go mod download +RUN rm -f serverNameExample.tar.gz + +CMD ["sleep","86400"] diff --git a/scripts/binary-package.sh b/scripts/binary-package.sh index cab4ca0..40976f3 100644 --- a/scripts/binary-package.sh +++ b/scripts/binary-package.sh @@ -14,6 +14,9 @@ cp -f cmd/${serviceName}_mixExample/${serviceName}_mixExample ${serviceName}-bin cp -f configs/${serviceName}.yml ${serviceName}-binary/configs cp -f configs/${serviceName}_cc.yml ${serviceName}-binary/configs +# compressing binary file +#upx -9 ${serviceName}_mixExample + tar zcvf ${serviceName}-binary.tar.gz ${serviceName}-binary rm -rf ${serviceName}-binary diff --git a/scripts/deploy-docker.sh b/scripts/deploy-docker.sh index 0775e99..78bce88 100644 --- a/scripts/deploy-docker.sh +++ b/scripts/deploy-docker.sh @@ -3,9 +3,18 @@ dockerComposeFilePath="deployments/docker-compose" mkdir -p ${dockerComposeFilePath}/configs -cp configs/serverNameExample.yml ${dockerComposeFilePath}/configs +if [ ! -f "${dockerComposeFilePath}/configs/serverNameExample.yml" ];then + cp configs/serverNameExample.yml ${dockerComposeFilePath}/configs +fi # shellcheck disable=SC2164 cd ${dockerComposeFilePath} +if [ "$1"x = "stop"x ] ;then + docker-compose down + exit 0 +fi + docker-compose up -d + +echo "path is 'deployments/docker-compose'" diff --git a/scripts/grpc_health_probe.sh b/scripts/grpc_health_probe.sh index 68bbe5a..7005a94 100644 --- a/scripts/grpc_health_probe.sh +++ b/scripts/grpc_health_probe.sh @@ -6,4 +6,4 @@ GOPROXY=https://goproxy.cn,direct go install github.com/grpc-ecosystem/grpc-heal cd $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 \ && go mod download \ - && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "${DOCKERFILE_PATH}" + && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "all=-s -w" -o "${DOCKERFILE_PATH}" diff --git a/scripts/image-build-local.sh b/scripts/image-build-local.sh new file mode 100644 index 0000000..75fda94 --- /dev/null +++ b/scripts/image-build-local.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# build the image for local docker, using the binaries, if you want to reduce the size of the image, +# use upx to compress the binaries before building the image. + +serverName="serverNameExample_mixExample" +# image name of the service, no capital letters +SERVER_NAME="project-name-example.server-name-example" +# Dockerfile file directory +DOCKERFILE_PATH="build" +DOCKERFILE="${DOCKERFILE_PATH}/Dockerfile" + +bash scripts/grpc_health_probe.sh +mv -f cmd/${serverName}/${serverName} ${DOCKERFILE_PATH}/${serverName} + +# compressing binary file +#cd ${DOCKERFILE_PATH} +#upx -9 ${serverName} +#upx -9 grpc_health_probe +#cd - + +mkdir -p ${DOCKERFILE_PATH}/configs && cp -f configs/${serverName}.yml ${DOCKERFILE_PATH}/configs/ +echo "docker build -f ${DOCKERFILE} -t ${SERVER_NAME}:latest ${DOCKERFILE_PATH}" +docker build -f ${DOCKERFILE} -t ${SERVER_NAME}:latest ${DOCKERFILE_PATH} + + +if [ X"${serverName}" = X ];then + exit 0 +fi +rm -rf ./${DOCKERFILE_PATH}/${serverName} ${DOCKERFILE_PATH}/configs ${DOCKERFILE_PATH}/grpc_health_probe diff --git a/scripts/image-build.sh b/scripts/image-build.sh index e8b6bcb..37226e7 100644 --- a/scripts/image-build.sh +++ b/scripts/image-build.sh @@ -1,6 +1,7 @@ #!/bin/bash -# Image built by directly copying the compiled binaries. Advantage: fast build, disadvantage: the image is twice as large as a two-stage build. +# build the docker image using the binaries, if you want to reduce the size of the image, +# use upx to compress the binaries before building the image. serverName="serverNameExample_mixExample" # image name of the service, no capital letters @@ -31,9 +32,20 @@ CONFIG_PATH="configs" # only grpc use start bash scripts/grpc_health_probe.sh # only grpc use end -CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,direct go build -gcflags "all=-N -l" -o ${BIN_FILE} cmd/${serverName}/*.go +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,direct go build -o ${BIN_FILE} cmd/${serverName}/*.go mv -f ${BIN_FILE} ${DOCKERFILE_PATH} mkdir -p ${DOCKERFILE_PATH}/${CONFIG_PATH} && cp -f ${CONFIG_PATH}/${serverName}.yml ${DOCKERFILE_PATH}/${CONFIG_PATH} + +# compressing binary file +#cd ${DOCKERFILE_PATH} +#upx -9 ${serverName} +#upx -9 grpc_health_probe +#cd - + echo "docker build -f ${DOCKERFILE} -t ${IMAGE_NAME_TAG} ${DOCKERFILE_PATH}" docker build -f ${DOCKERFILE} -t ${IMAGE_NAME_TAG} ${DOCKERFILE_PATH} + +if [ X"${serverName}" = X ];then + exit 0 +fi rm -rf ./${DOCKERFILE_PATH}/${serverName} ${DOCKERFILE_PATH}/configs ${DOCKERFILE_PATH}/grpc_health_probe diff --git a/scripts/image-build2.sh b/scripts/image-build2.sh index f7443a2..225ed42 100644 --- a/scripts/image-build2.sh +++ b/scripts/image-build2.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Two-stage build mirror, advantage: smallest image size, disadvantage: slower build speed, each build produces a larger intermediate image +# two-stage build docker image serverName="serverNameExample_mixExample" # image name of the service, no capital letters diff --git a/scripts/image-rpc-test.sh b/scripts/image-rpc-test.sh new file mode 100644 index 0000000..eee1cad --- /dev/null +++ b/scripts/image-rpc-test.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# build rpc service test image + +serverName="serverNameExample_mixExample" +# image name of the service, no capital letters +SERVER_NAME="project-name-example.server-name-example.rpc-test" +# Dockerfile file directory +DOCKERFILE_PATH="build" +DOCKERFILE="${DOCKERFILE_PATH}/Dockerfile_test" + +# image repo address, REPO_HOST="ip or domain", passed in via the first parameter +REPO_HOST=$1 +if [ "X${REPO_HOST}" = "X" ];then + echo "param 'repo host' cannot be empty, example: ./image-rpc-test.sh hub.docker.com v1.0.0" + exit 1 +fi +# the version tag, which defaults to latest if empty, is passed in via the second parameter +TAG=$2 +if [ "X${TAG}" = "X" ];then + TAG="latest" +fi +# image name and tag +IMAGE_NAME_TAG="${REPO_HOST}/${SERVER_NAME}:${TAG}" + +PROJECT_FILES=$(ls) +tar zcf ${serverName}.tar.gz ${PROJECT_FILES} +mv -f ${serverName}.tar.gz ${DOCKERFILE_PATH} + +echo "docker build -f ${DOCKERFILE} -t ${IMAGE_NAME_TAG} ${DOCKERFILE_PATH}" +docker build --force-rm -f ${DOCKERFILE} -t ${IMAGE_NAME_TAG} ${DOCKERFILE_PATH} + +rm -rf ${DOCKERFILE_PATH}/${serverName}.tar.gz diff --git a/scripts/run-nohup.sh b/scripts/run-nohup.sh new file mode 100644 index 0000000..44b5385 --- /dev/null +++ b/scripts/run-nohup.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +# chkconfig: - 85 15 +# description: serverNameExample + +serverName="serverNameExample_mixExample" +cmdStr="cmd/${serverName}/${serverName}" + + +function checkResult() { + result=$1 + if [ ${result} -ne 0 ]; then + exit ${result} + fi +} + +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 + + 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 diff --git a/scripts/run.sh b/scripts/run.sh index 7317875..5417684 100644 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -17,7 +17,6 @@ function checkResult() { sleep 0.2 -# CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${serverName} go build -o ${binaryFile} cmd/${serverName}/main.go checkResult $?