genproto.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env bash
  2. #
  3. # Generate all etcd protobuf bindings.
  4. # Run from repository root.
  5. #
  6. set -e
  7. if ! [[ "$0" =~ "scripts/genproto.sh" ]]; then
  8. echo "must be run from repository root"
  9. exit 255
  10. fi
  11. # for now, be conservative about what version of protoc we expect
  12. if ! [[ $(protoc --version) =~ "3.1.0" ]]; then
  13. echo "could not find protoc 3.1.0, is it installed + in PATH?"
  14. exit 255
  15. fi
  16. # directories containing protos to be built
  17. DIRS="./wal/walpb ./etcdserver/etcdserverpb ./snap/snappb ./raft/raftpb ./mvcc/mvccpb ./lease/leasepb ./auth/authpb"
  18. # exact version of protoc-gen-gogo to build
  19. GOGO_PROTO_SHA="8d70fb3182befc465c4a1eac8ad4d38ff49778e2"
  20. GRPC_GATEWAY_SHA="84398b94e188ee336f307779b57b3aa91af7063c"
  21. # set up self-contained GOPATH for building
  22. export GOPATH=${PWD}/gopath.proto
  23. export GOBIN=${PWD}/bin
  24. export PATH="${GOBIN}:${PATH}"
  25. COREOS_ROOT="${GOPATH}/src/github.com/coreos"
  26. ETCD_ROOT="${COREOS_ROOT}/etcd"
  27. GOGOPROTO_ROOT="${GOPATH}/src/github.com/gogo/protobuf"
  28. GOGOPROTO_PATH="${GOGOPROTO_ROOT}:${GOGOPROTO_ROOT}/protobuf"
  29. GRPC_GATEWAY_ROOT="${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway"
  30. rm -f "${ETCD_ROOT}"
  31. mkdir -p "${COREOS_ROOT}"
  32. ln -s "${PWD}" "${ETCD_ROOT}"
  33. # Ensure we have the right version of protoc-gen-gogo by building it every time.
  34. # TODO(jonboulle): vendor this instead of `go get`ting it.
  35. go get -u github.com/gogo/protobuf/{proto,protoc-gen-gogo,gogoproto}
  36. go get -u golang.org/x/tools/cmd/goimports
  37. pushd "${GOGOPROTO_ROOT}"
  38. git reset --hard "${GOGO_PROTO_SHA}"
  39. make install
  40. popd
  41. # generate gateway code
  42. go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
  43. go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
  44. pushd "${GRPC_GATEWAY_ROOT}"
  45. git reset --hard "${GRPC_GATEWAY_SHA}"
  46. go install ./protoc-gen-grpc-gateway
  47. popd
  48. for dir in ${DIRS}; do
  49. pushd ${dir}
  50. protoc --gofast_out=plugins=grpc,import_prefix=github.com/coreos/:. -I=.:"${GOGOPROTO_PATH}":"${COREOS_ROOT}":"${GRPC_GATEWAY_ROOT}/third_party/googleapis" *.proto
  51. sed -i.bak -E "s/github\.com\/coreos\/(gogoproto|github\.com|golang\.org|google\.golang\.org)/\1/g" *.pb.go
  52. sed -i.bak -E 's/github\.com\/coreos\/(errors|fmt|io)/\1/g' *.pb.go
  53. sed -i.bak -E 's/import _ \"gogoproto\"//g' *.pb.go
  54. sed -i.bak -E 's/import fmt \"fmt\"//g' *.pb.go
  55. sed -i.bak -E 's/import _ \"github\.com\/coreos\/google\/api\"//g' *.pb.go
  56. rm -f *.bak
  57. goimports -w *.pb.go
  58. popd
  59. done
  60. protoc -I. \
  61. -I${GRPC_GATEWAY_ROOT}/third_party/googleapis \
  62. -I${GOGOPROTO_PATH} \
  63. -I${COREOS_ROOT} \
  64. --grpc-gateway_out=logtostderr=true:. \
  65. --swagger_out=logtostderr=true:./Documentation/dev-guide/apispec/swagger/. \
  66. ./etcdserver/etcdserverpb/rpc.proto
  67. # TODO: change this whenever we add more swagger API
  68. mv \
  69. Documentation/dev-guide/apispec/swagger/etcdserver/etcdserverpb/rpc.swagger.json \
  70. Documentation/dev-guide/apispec/swagger/rpc.swagger.json
  71. rm -rf Documentation/dev-guide/apispec/swagger/etcdserver/etcdserverpb
  72. # install protodoc
  73. # go get -v -u github.com/coreos/protodoc
  74. #
  75. # by default, do not run this option.
  76. # only run when './scripts/genproto.sh -g'
  77. #
  78. if [ "$1" = "-g" ]; then
  79. echo "protodoc is auto-generating grpc API reference documentation..."
  80. go get -v -u github.com/coreos/protodoc
  81. SHA_PROTODOC="f4164b1cce80b5eba4c835d08483f552dc568b7c"
  82. PROTODOC_PATH="${GOPATH}/src/github.com/coreos/protodoc"
  83. pushd "${PROTODOC_PATH}"
  84. git reset --hard "${SHA_PROTODOC}"
  85. go install
  86. echo "protodoc is updated"
  87. popd
  88. protodoc --directories="etcdserver/etcdserverpb=service_message,mvcc/mvccpb=service_message,lease/leasepb=service_message,auth/authpb=service_message" \
  89. --title="etcd API Reference" \
  90. --output="Documentation/dev-guide/api_reference_v3.md" \
  91. --message-only-from-this-file="etcdserver/etcdserverpb/rpc.proto" \
  92. --disclaimer="This is a generated documentation. Please read the proto files for more."
  93. echo "protodoc is finished..."
  94. else
  95. echo "skipping grpc API reference document auto-generation..."
  96. fi