make+lnrpc: use docker to compile protos

This commit aims to make it easier for developers to compile our
protobuf definitions. They now only need to have docker installed
instead of a whole set of binaries and libraries all pinned to very
specific versions.
This commit is contained in:
Oliver Gugger 2021-01-15 13:44:50 +01:00
parent 91364056f7
commit e9a0f36786
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
4 changed files with 86 additions and 25 deletions

View File

@ -262,7 +262,7 @@ list:
rpc:
@$(call print, "Compiling protos.")
cd ./lnrpc; ./gen_protos.sh
cd ./lnrpc; ./gen_protos_docker.sh
rpc-format:
@$(call print, "Formatting protos.")

22
lnrpc/Dockerfile Normal file
View File

@ -0,0 +1,22 @@
FROM golang:1.15.6-buster
RUN apt-get update && apt-get install -y \
git \
protobuf-compiler='3.6.1*' \
clang-format='1:7.0*'
# We don't want any default values for these variables to make sure they're
# explicitly provided by parsing the go.mod file. Otherwise we might forget to
# update them here if we bump the versions.
ARG PROTOC_GEN_VERSION
ARG GRPC_GATEWAY_VERSION
RUN cd /tmp \
&& export GO111MODULE=on \
&& go get github.com/golang/protobuf/protoc-gen-go@${PROTOC_GEN_VERSION} \
&& go get github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@${GRPC_GATEWAY_VERSION} \
&& go get github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@${GRPC_GATEWAY_VERSION}
WORKDIR /build
CMD ["/bin/bash", "/build/lnrpc/gen_protos.sh"]

View File

@ -1,28 +1,44 @@
#!/bin/sh
#!/bin/bash
echo "Generating root gRPC server protos"
set -e
PROTOS="rpc.proto walletunlocker.proto **/*.proto"
# generate compiles the *.pb.go stubs from the *.proto files.
function generate() {
echo "Generating root gRPC server protos"
PROTOS="rpc.proto walletunlocker.proto **/*.proto"
# For each of the sub-servers, we then generate their protos, but a restricted
# set as they don't yet require REST proxies, or swagger docs.
for file in $PROTOS; do
DIRECTORY=$(dirname "${file}")
echo "Generating protos from ${file}, into ${DIRECTORY}"
# Generate the protos.
protoc -I/usr/local/include -I. \
--go_out=plugins=grpc,paths=source_relative:. \
"${file}"
# Generate the REST reverse proxy.
protoc -I/usr/local/include -I. \
--grpc-gateway_out=logtostderr=true,paths=source_relative,grpc_api_configuration=rest-annotations.yaml:. \
"${file}"
# Finally, generate the swagger file which describes the REST API in detail.
protoc -I/usr/local/include -I. \
--swagger_out=logtostderr=true,grpc_api_configuration=rest-annotations.yaml:. \
"${file}"
done
}
# For each of the sub-servers, we then generate their protos, but a restricted
# set as they don't yet require REST proxies, or swagger docs.
for file in $PROTOS; do
DIRECTORY=$(dirname "${file}")
echo "Generating protos from ${file}, into ${DIRECTORY}"
# format formats the *.proto files with the clang-format utility.
function format() {
find . -name "*.proto" -print0 | xargs -0 clang-format --style=file -i
}
# Generate the protos.
protoc -I/usr/local/include -I. \
--go_out=plugins=grpc,paths=source_relative:. \
"${file}"
# Generate the REST reverse proxy.
protoc -I/usr/local/include -I. \
--grpc-gateway_out=logtostderr=true,paths=source_relative,grpc_api_configuration=rest-annotations.yaml:. \
"${file}"
# Finally, generate the swagger file which describes the REST API in detail.
protoc -I/usr/local/include -I. \
--swagger_out=logtostderr=true,grpc_api_configuration=rest-annotations.yaml:. \
"${file}"
done
# Compile and format the lnrpc package.
pushd lnrpc
format
generate
popd

23
lnrpc/gen_protos_docker.sh Executable file
View File

@ -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 github.com/golang/protobuf)
GRPC_GATEWAY_VERSION=$(go list -f '{{.Version}}' -m github.com/grpc-ecosystem/grpc-gateway)
echo "Building protobuf compiler docker image..."
docker build -q -t lnd-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" \
lnd-protobuf-builder