From e9a0f36786f1fb288393831b910c2b41dc400c99 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 15 Jan 2021 13:44:50 +0100 Subject: [PATCH] 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. --- Makefile | 2 +- lnrpc/Dockerfile | 22 +++++++++++++ lnrpc/gen_protos.sh | 64 ++++++++++++++++++++++++-------------- lnrpc/gen_protos_docker.sh | 23 ++++++++++++++ 4 files changed, 86 insertions(+), 25 deletions(-) create mode 100644 lnrpc/Dockerfile create mode 100755 lnrpc/gen_protos_docker.sh diff --git a/Makefile b/Makefile index efdcef17..05617ab3 100644 --- a/Makefile +++ b/Makefile @@ -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.") diff --git a/lnrpc/Dockerfile b/lnrpc/Dockerfile new file mode 100644 index 00000000..ee8a62cf --- /dev/null +++ b/lnrpc/Dockerfile @@ -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"] diff --git a/lnrpc/gen_protos.sh b/lnrpc/gen_protos.sh index 78c3270d..6c262573 100755 --- a/lnrpc/gen_protos.sh +++ b/lnrpc/gen_protos.sh @@ -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 diff --git a/lnrpc/gen_protos_docker.sh b/lnrpc/gen_protos_docker.sh new file mode 100755 index 00000000..f29c4629 --- /dev/null +++ b/lnrpc/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 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