From 98b613b5ffe712e5548852ea5b1fddc01f297292 Mon Sep 17 00:00:00 2001 From: YusukeShimizu Date: Sun, 22 Oct 2023 11:10:49 +0900 Subject: [PATCH] [peerswaprpc]Implemented to be able to compile protobufs in docker This commit aims to make it easier to compile protobuf. Only need to have docker installed instead of a whole set of binaries and libraries all pinned to very specific versions. --- peerswaprpc/Dockerfile | 28 ++++++++++++++++++++++++++++ peerswaprpc/Makefile | 4 ++++ peerswaprpc/gen_protos.sh | 16 ++++++++++++++++ peerswaprpc/gen_protos_docker.sh | 23 +++++++++++++++++++++++ peerswaprpc/peerswaprpc.pb.go | 2 +- peerswaprpc/peerswaprpc_grpc.pb.go | 4 ---- 6 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 peerswaprpc/Dockerfile create mode 100755 peerswaprpc/gen_protos.sh create mode 100755 peerswaprpc/gen_protos_docker.sh diff --git a/peerswaprpc/Dockerfile b/peerswaprpc/Dockerfile new file mode 100644 index 00000000..1f7d2aaa --- /dev/null +++ b/peerswaprpc/Dockerfile @@ -0,0 +1,28 @@ +FROM golang:1.21.0-bookworm + +RUN apt-get update && apt-get install -y \ + git \ + protobuf-compiler='3.21.12*' \ + clang-format='1:14.0*' + +ARG PROTOC_GEN_VERSION +ARG GRPC_GATEWAY_VERSION + +ENV PROTOC_GEN_GO_GRPC_VERSION="v1.1.0" +ENV GOCACHE=/tmp/build/.cache +ENV GOMODCACHE=/tmp/build/.modcache + +RUN echo ${PROTOC_GEN_VERSION} + +RUN cd /tmp \ + && mkdir -p /tmp/build/.cache \ + && mkdir -p /tmp/build/.modcache \ + && go install google.golang.org/protobuf/cmd/protoc-gen-go@${PROTOC_GEN_VERSION} \ + && go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@${PROTOC_GEN_GO_GRPC_VERSION} \ + && go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@${GRPC_GATEWAY_VERSION} \ + && go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@${GRPC_GATEWAY_VERSION} \ + && chmod -R 777 /tmp/build/ + +WORKDIR /build + +CMD ["/bin/sh", "/build/peerswaprpc/gen_protos.sh"] diff --git a/peerswaprpc/Makefile b/peerswaprpc/Makefile index ebca81c2..e79ffcc4 100644 --- a/peerswaprpc/Makefile +++ b/peerswaprpc/Makefile @@ -30,3 +30,7 @@ ${DIR}/peerswaprpc.swagger.json: ${DIR}/peerswaprpc_grpc.pb.go ${DIR}/peerswaprpc.pb.go ${DIR}/peerswaprpc.pb.gw.go: ${DIR}/peerswaprpc.proto protoc ${PROTOC_OPTS} ${DIR}/peerswaprpc.proto PHONY: + +.PHONY: all-rpc-docker +all-rpc-docker: + ./gen_protos_docker.sh \ No newline at end of file diff --git a/peerswaprpc/gen_protos.sh b/peerswaprpc/gen_protos.sh new file mode 100755 index 00000000..57321645 --- /dev/null +++ b/peerswaprpc/gen_protos.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +protoc -I. \ + --go_out=paths=source_relative:. \ + --go-grpc_out=paths=source_relative:. \ + --grpc-gateway_out=. \ + --grpc-gateway_opt logtostderr=true \ + --grpc-gateway_opt paths=source_relative \ + --grpc-gateway_opt grpc_api_configuration=peerswaprpc/peerswap.yaml \ + peerswaprpc/peerswaprpc.proto + +protoc \ + --openapiv2_out=. \ + --openapiv2_opt logtostderr=true \ + --openapiv2_opt grpc_api_configuration=peerswaprpc/peerswap.yaml \ + peerswaprpc/peerswaprpc.protos diff --git a/peerswaprpc/gen_protos_docker.sh b/peerswaprpc/gen_protos_docker.sh new file mode 100755 index 00000000..68c3732f --- /dev/null +++ b/peerswaprpc/gen_protos_docker.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +set -e + +# Directory of the script file, independent of where it's called from. +DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" + +PROTOC_GEN_VERSION=$(go list -f '{{.Version}}' -m google.golang.org/protobuf) +GRPC_GATEWAY_VERSION=$(go list -f '{{.Version}}' -m github.com/grpc-ecosystem/grpc-gateway/v2) + +echo "Building protobuf compiler docker image..." +docker build -t protobuf-builder \ + --build-arg PROTOC_GEN_VERSION="$PROTOC_GEN_VERSION" \ + --build-arg GRPC_GATEWAY_VERSION="$GRPC_GATEWAY_VERSION" \ + . + +echo "Compiling and formatting *.proto files..." +docker run \ + --rm \ + --user "$UID:$(id -g)" \ + -e UID=$UID \ + -v "$DIR/../:/build" \ + protobuf-builder diff --git a/peerswaprpc/peerswaprpc.pb.go b/peerswaprpc/peerswaprpc.pb.go index ec2e9ce3..b4846001 100644 --- a/peerswaprpc/peerswaprpc.pb.go +++ b/peerswaprpc/peerswaprpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.28.1 -// protoc v3.12.4 +// protoc v3.21.12 // source: peerswaprpc/peerswaprpc.proto package peerswaprpc diff --git a/peerswaprpc/peerswaprpc_grpc.pb.go b/peerswaprpc/peerswaprpc_grpc.pb.go index cec626ce..e55393a5 100644 --- a/peerswaprpc/peerswaprpc_grpc.pb.go +++ b/peerswaprpc/peerswaprpc_grpc.pb.go @@ -1,8 +1,4 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. -// versions: -// - protoc-gen-go-grpc v1.2.0 -// - protoc v3.12.4 -// source: peerswaprpc/peerswaprpc.proto package peerswaprpc