diff --git a/Makefile b/Makefile index dbfee0f..649bb55 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,8 @@ 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) - +# delete the templates code start .PHONY: install # installation of dependent tools install: @@ -18,10 +17,10 @@ install: go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@latest go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest - go install github.com/swaggo/swag/cmd/swag@latest + go install github.com/swaggo/swag/cmd/swag@v1.8.12 go install github.com/ofabry/go-callvis@latest go install golang.org/x/pkgsite/cmd/pkgsite@latest - +# delete the templates code end .PHONY: mod # add missing and remove unused modules @@ -60,12 +59,6 @@ cover: go tool cover -html=cover.out -.PHONY: docs -# generate swagger docs, the host address can be changed via parameters, e.g. make docs HOST=192.168.3.37 -docs: mod fmt - @bash scripts/swag-docs.sh $(HOST) - - .PHONY: graph # generate interactive visual function dependency graphs graph: @@ -75,8 +68,14 @@ graph: @rm -f main.go serverNameExample_mixExample.gv +.PHONY: docs +# generate swagger docs, only for ⓵ Web services created based on sql, the host address can be changed via parameters, e.g. make docs HOST=192.168.3.37 +docs: mod fmt + @bash scripts/swag-docs.sh $(HOST) + + .PHONY: proto -# generate *.go code from *.proto files +# generate *.go code from *.proto files, only for ⓶ Microservices created based on sql, ⓷ Web services created based on protobuf, ⓸ Microservices created based on protobuf, ⓹ RPC gateway service created based on protobuf proto: mod fmt @bash scripts/protoc.sh @@ -91,17 +90,17 @@ 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 - + @cd cmd/serverNameExample_mixExample && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build # delete the templates code start + .PHONY: build-sponge # build sponge for linux amd64 binary build-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 + @cd cmd/sponge && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "all=-s -w" +# delete the templates code end .PHONY: run # run service @@ -115,12 +114,30 @@ run-nohup: @bash scripts/run-nohup.sh $(CMD) -.PHONY: docker-image +.PHONY: binary-package +# packaged binary files +binary-package: build + @bash scripts/binary-package.sh + + +.PHONY: deploy-binary +# deploy binary to remote linux server, 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: image-build-local # build image for local docker, tag=latest, use binary files to build -docker-image: build +image-build-local: build @bash scripts/image-build-local.sh +.PHONY: deploy-docker +# deploy service to local docker, if you want to update the service, run the make deploy-docker command again. +deploy-docker: image-build-local + @bash scripts/deploy-docker.sh + + .PHONY: image-build # build image for remote repositories, use binary files to build, e.g. make image-build REPO_HOST=addr TAG=latest image-build: @@ -133,12 +150,6 @@ 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: @@ -151,22 +162,10 @@ deploy-k8s: @bash scripts/deploy-k8s.sh -.PHONY: deploy-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 $(CMD) - - -.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: 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: patch @@ -187,10 +186,10 @@ clean: @rm -vrf cmd/serverNameExample_mixExample/serverNameExample_mixExample @rm -vrf cover.out @rm -vrf main.go serverNameExample_mixExample.gv - @rm -vrf internal/ecode/*go.gen.* - @rm -vrf internal/routers/*go.gen.* - @rm -vrf internal/handler/*go.gen.* - @rm -vrf internal/service/*go.gen.* + @rm -vrf internal/ecode/*.go.gen* + @rm -vrf internal/routers/*.go.gen* + @rm -vrf internal/handler/*.go.gen* + @rm -vrf internal/service/*.go.gen* @rm -rf serverNameExample-binary.tar.gz @echo "clean finished" diff --git a/deployments/docker-compose/docker-compose.yml b/deployments/docker-compose/docker-compose.yml index b00936f..cde35b5 100644 --- a/deployments/docker-compose/docker-compose.yml +++ b/deployments/docker-compose/docker-compose.yml @@ -2,7 +2,7 @@ version: "3.7" services: server-name-example: - image: project-name-example.server-name-example:latest + image: project-name-example/server-name-example:latest container_name: server-name-example restart: always command: ["./serverNameExample", "-c", "/app/configs/serverNameExample.yml"] diff --git a/deployments/kubernetes/serverNameExample-deployment.yml b/deployments/kubernetes/serverNameExample-deployment.yml index 7740e02..db8de79 100644 --- a/deployments/kubernetes/serverNameExample-deployment.yml +++ b/deployments/kubernetes/serverNameExample-deployment.yml @@ -16,7 +16,7 @@ spec: spec: containers: - name: server-name-example - image: repo-addr-example/project-name-example.server-name-example:latest + image: repo-addr-example/project-name-example/server-name-example:latest # If using a local image, use Never, default is Always #imagePullPolicy: Never command: ["./serverNameExample", "-c", "/app/configs/serverNameExample.yml"] diff --git a/scripts/deploy-docker.sh b/scripts/deploy-docker.sh index 78bce88..a83f25a 100644 --- a/scripts/deploy-docker.sh +++ b/scripts/deploy-docker.sh @@ -2,6 +2,13 @@ dockerComposeFilePath="deployments/docker-compose" +function checkResult() { + result=$1 + if [ ${result} -ne 0 ]; then + exit ${result} + fi +} + mkdir -p ${dockerComposeFilePath}/configs if [ ! -f "${dockerComposeFilePath}/configs/serverNameExample.yml" ];then cp configs/serverNameExample.yml ${dockerComposeFilePath}/configs @@ -10,11 +17,16 @@ fi # shellcheck disable=SC2164 cd ${dockerComposeFilePath} -if [ "$1"x = "stop"x ] ;then - docker-compose down - exit 0 -fi +docker-compose down +checkResult $? docker-compose up -d +checkResult $? -echo "path is 'deployments/docker-compose'" +colorCyan='\e[1;36m' +highBright='\e[1m' +markEnd='\e[0m' + +echo "" +echo -e "run service successfully, if you want to stop the service, go into the ${highBright}${dockerComposeFilePath}${markEnd} directory and execute the command ${colorCyan}docker-compose down${markEnd}." +echo "" diff --git a/scripts/image-build-local.sh b/scripts/image-build-local.sh index 75fda94..4992262 100644 --- a/scripts/image-build-local.sh +++ b/scripts/image-build-local.sh @@ -4,15 +4,25 @@ # 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" +# image name of the service, prohibit uppercase letters in names. +IMAGE_NAME="project-name-example/server-name-example" # Dockerfile file directory -DOCKERFILE_PATH="build" +DOCKERFILE_PATH="scripts/build" DOCKERFILE="${DOCKERFILE_PATH}/Dockerfile" -bash scripts/grpc_health_probe.sh mv -f cmd/${serverName}/${serverName} ${DOCKERFILE_PATH}/${serverName} +# todo generate image-build-local code for http or grpc here +# delete the templates code start + +# install grpc-health-probe, for health check of grpc service +rootDockerFilePath=$(pwd)/${DOCKERFILE_PATH} +go install github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 +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 -ldflags "all=-s -w" -o "${rootDockerFilePath}/grpc_health_probe" +cd - + # compressing binary file #cd ${DOCKERFILE_PATH} #upx -9 ${serverName} @@ -20,11 +30,26 @@ mv -f cmd/${serverName}/${serverName} ${DOCKERFILE_PATH}/${serverName} #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} +echo "docker build -f ${DOCKERFILE} -t ${IMAGE_NAME}:latest ${DOCKERFILE_PATH}" +docker build -f ${DOCKERFILE} -t ${IMAGE_NAME}:latest ${DOCKERFILE_PATH} - -if [ X"${serverName}" = X ];then - exit 0 +if [ -f "${DOCKERFILE_PATH}/grpc_health_probe" ]; then + rm -f ${DOCKERFILE_PATH}/grpc_health_probe fi -rm -rf ./${DOCKERFILE_PATH}/${serverName} ${DOCKERFILE_PATH}/configs ${DOCKERFILE_PATH}/grpc_health_probe + +# delete the templates code end + +if [ -f "${DOCKERFILE_PATH}/${serverName}" ]; then + rm -f ${DOCKERFILE_PATH}/${serverName} +fi + +if [ -d "${DOCKERFILE_PATH}/configs" ]; then + rm -rf ${DOCKERFILE_PATH}/configs +fi + +# delete none image +noneImages=$(docker images | grep "" | awk '{print $3}') +if [ "X${noneImages}" != "X" ]; then + docker rmi ${noneImages} > /dev/null +fi +exit 0 diff --git a/scripts/image-build.sh b/scripts/image-build.sh index 37226e7..5937e2a 100644 --- a/scripts/image-build.sh +++ b/scripts/image-build.sh @@ -4,10 +4,10 @@ # 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" +# image name of the service, prohibit uppercase letters in names. +IMAGE_NAME="project-name-example/server-name-example" # Dockerfile file directory -DOCKERFILE_PATH="build" +DOCKERFILE_PATH="scripts/build" DOCKERFILE="${DOCKERFILE_PATH}/Dockerfile" # image repo address, REPO_HOST="ip or domain", passed in via the first parameter @@ -22,20 +22,28 @@ if [ "X${TAG}" = "X" ];then TAG="latest" fi # image name and tag -IMAGE_NAME_TAG="${REPO_HOST}/${SERVER_NAME}:${TAG}" +IMAGE_NAME_TAG="${REPO_HOST}/${IMAGE_NAME}:${TAG}" # binary executable files BIN_FILE="cmd/${serverName}/${serverName}" # configuration file directory 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 -o ${BIN_FILE} cmd/${serverName}/*.go +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 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} +# todo generate image-build code for http or grpc here +# delete the templates code start + +# install grpc-health-probe, for health check of grpc service +rootDockerFilePath=$(pwd)/${DOCKERFILE_PATH} +go install github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 +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 -ldflags "all=-s -w" -o "${rootDockerFilePath}/grpc_health_probe" +cd - + # compressing binary file #cd ${DOCKERFILE_PATH} #upx -9 ${serverName} @@ -45,7 +53,23 @@ mkdir -p ${DOCKERFILE_PATH}/${CONFIG_PATH} && cp -f ${CONFIG_PATH}/${serverName} 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 +if [ -f "${DOCKERFILE_PATH}/grpc_health_probe" ]; then + rm -f ${DOCKERFILE_PATH}/grpc_health_probe fi -rm -rf ./${DOCKERFILE_PATH}/${serverName} ${DOCKERFILE_PATH}/configs ${DOCKERFILE_PATH}/grpc_health_probe + +# delete the templates code end + +if [ -f "${DOCKERFILE_PATH}/${serverName}" ]; then + rm -f ${DOCKERFILE_PATH}/${serverName} +fi + +if [ -d "${DOCKERFILE_PATH}/configs" ]; then + rm -rf ${DOCKERFILE_PATH}/configs +fi + +# delete none image +noneImages=$(docker images | grep "" | awk '{print $3}') +if [ "X${noneImages}" != "X" ]; then + docker rmi ${noneImages} > /dev/null +fi +exit 0 diff --git a/scripts/image-build2.sh b/scripts/image-build2.sh index 225ed42..fce1969 100644 --- a/scripts/image-build2.sh +++ b/scripts/image-build2.sh @@ -3,10 +3,10 @@ # two-stage build docker image serverName="serverNameExample_mixExample" -# image name of the service, no capital letters -SERVER_NAME="project-name-example.server-name-example" +# image name of the service, prohibit uppercase letters in names. +IMAGE_NAME="project-name-example/server-name-example" # Dockerfile file directory -DOCKERFILE_PATH="build" +DOCKERFILE_PATH="scripts/build" DOCKERFILE="${DOCKERFILE_PATH}/Dockerfile_build" # image repo address, REPO_HOST="ip or domain", passed in via the first parameter @@ -21,7 +21,7 @@ if [ "X${TAG}" = "X" ];then TAG="latest" fi # image name and tag -IMAGE_NAME_TAG="${REPO_HOST}/${SERVER_NAME}:${TAG}" +IMAGE_NAME_TAG="${REPO_HOST}/${IMAGE_NAME}:${TAG}" PROJECT_FILES=$(ls) tar zcf ${serverName}.tar.gz ${PROJECT_FILES} @@ -29,3 +29,10 @@ mv -f ${serverName}.tar.gz ${DOCKERFILE_PATH} echo "docker build --force-rm -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 +# delete none image +noneImages=$(docker images | grep "" | awk '{print $3}') +if [ "X${noneImages}" != "X" ]; then + docker rmi ${noneImages} > /dev/null +fi +exit 0 + diff --git a/scripts/image-push.sh b/scripts/image-push.sh index 0911c3d..86f32ce 100644 --- a/scripts/image-push.sh +++ b/scripts/image-push.sh @@ -1,7 +1,7 @@ #!/bin/bash -# image name, no capital letters -SERVER_NAME="project-name-example.server-name-example" +# image name, prohibit uppercase letters in names. +IMAGE_NAME="project-name-example/server-name-example" # image repo address, passed in via the first parameter REPO_HOST=$1 @@ -16,7 +16,7 @@ if [ "X${TAG}" = "X" ];then TAG="latest" fi # image name and tag -IMAGE_NAME_TAG="${REPO_HOST}/${SERVER_NAME}:${TAG}" +IMAGE_NAME_TAG="${REPO_HOST}/${IMAGE_NAME}:${TAG}" function checkResult() { result=$1 diff --git a/scripts/image-rpc-test.sh b/scripts/image-rpc-test.sh index eee1cad..c5c28c4 100644 --- a/scripts/image-rpc-test.sh +++ b/scripts/image-rpc-test.sh @@ -3,10 +3,10 @@ # 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" +# image name of the service, prohibit uppercase letters in names. +IMAGE_NAME="project-name-example/server-name-example.rpc-test" # Dockerfile file directory -DOCKERFILE_PATH="build" +DOCKERFILE_PATH="scripts/build" DOCKERFILE="${DOCKERFILE_PATH}/Dockerfile_test" # image repo address, REPO_HOST="ip or domain", passed in via the first parameter @@ -21,7 +21,7 @@ if [ "X${TAG}" = "X" ];then TAG="latest" fi # image name and tag -IMAGE_NAME_TAG="${REPO_HOST}/${SERVER_NAME}:${TAG}" +IMAGE_NAME_TAG="${REPO_HOST}/${IMAGE_NAME}:${TAG}" PROJECT_FILES=$(ls) tar zcf ${serverName}.tar.gz ${PROJECT_FILES} diff --git a/scripts/protoc.sh b/scripts/protoc.sh index d3ec13a..858d46e 100644 --- a/scripts/protoc.sh +++ b/scripts/protoc.sh @@ -14,10 +14,19 @@ function checkResult() { # add the import of useless packages from the generated *.pb.go code here function deleteUnusedPkg() { file=$1 - sed -i "s#_ \"github.com/envoyproxy/protoc-gen-validate/validate\"##g" ${file} - sed -i "s#_ \"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options\"##g" ${file} - sed -i "s#_ \"github.com/srikrsna/protoc-gen-gotag/tagger\"##g" ${file} - sed -i "s#_ \"google.golang.org/genproto/googleapis/api/annotations\"##g" ${file} + osType = $(uname -s) + if [ "${osType}"x = "Darwin"x ];then + sed -i '' 's#_ \"github.com/envoyproxy/protoc-gen-validate/validate\"##g' ${file} + sed -i '' 's#_ \"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options\"##g' ${file} + sed -i '' 's#_ \"github.com/srikrsna/protoc-gen-gotag/tagger\"##g' ${file} + sed -i '' 's#_ \"google.golang.org/genproto/googleapis/api/annotations\"##g' ${file} + else + sed -i "s#_ \"github.com/envoyproxy/protoc-gen-validate/validate\"##g" ${file} + sed -i "s#_ \"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options\"##g" ${file} + sed -i "s#_ \"github.com/srikrsna/protoc-gen-gotag/tagger\"##g" ${file} + sed -i "s#_ \"google.golang.org/genproto/googleapis/api/annotations\"##g" ${file} + fi + checkResult $? } function listProtoFiles(){ @@ -99,8 +108,8 @@ function generateBySpecifiedProto(){ listProtoFiles ${protoBasePath}/serverNameExample cd .. specifiedProtoFiles=$allProtoFiles - # todo generate router code for gin here - # delete the templates code start 2 + # todo generate api template code command here + # delete the templates code start # generate the swagger document and merge all files into docs/apis.swagger.json protoc --proto_path=. --proto_path=./third_party \ @@ -132,7 +141,7 @@ function generateBySpecifiedProto(){ echo "" echo -e "${highBright}Tip:${markEnd} execute the command ${colorCyan}make run${markEnd} and then visit ${colorCyan}http://localhost:8080/apis/swagger/index.html${markEnd} in your browser." echo "" - # delete the templates code end 2 + # delete the templates code end } # generate pb.go by all proto files @@ -152,3 +161,4 @@ go mod tidy checkResult $? echo "generated code successfully." +echo "" diff --git a/scripts/swag-docs.sh b/scripts/swag-docs.sh index 0d65230..fbb46e3 100644 --- a/scripts/swag-docs.sh +++ b/scripts/swag-docs.sh @@ -20,3 +20,4 @@ echo "" echo -e "${highBright}Tip:${markEnd} execute the command ${colorCyan}make run${markEnd} and then visit ${colorCyan}http://${HOST_ADDR}:8080/swagger/index.html${markEnd} in your browser." echo "" echo "generated api docs successfully." +echo "" diff --git a/test/server/jenkins/README.md b/test/server/jenkins/README.md index 9ab5d91..78ae72c 100644 --- a/test/server/jenkins/README.md +++ b/test/server/jenkins/README.md @@ -172,7 +172,7 @@ Deployment, pod's resource configuration `imagePullSecrets` to specify the key spec: containers: - name: server-name-example - image: project-name-example.server-name-example:latest + image: project-name-example/server-name-example:latest # ...... imagePullSecrets: - name: docker-auth-secret