genproto.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. if [[ $(protoc --version | cut -f2 -d' ') != "3.7.1" ]]; then
  12. echo "could not find protoc 3.7.1, is it installed + in PATH?"
  13. exit 255
  14. fi
  15. # directories containing protos to be built
  16. DIRS="./wal/walpb ./etcdserver/etcdserverpb ./etcdserver/api/snap/snappb ./raft/raftpb ./mvcc/mvccpb ./lease/leasepb ./auth/authpb ./etcdserver/api/v3lock/v3lockpb ./etcdserver/api/v3election/v3electionpb"
  17. # disable go mod
  18. export GO111MODULE=off
  19. # exact version of packages to build
  20. GOGO_PROTO_SHA="1adfc126b41513cc696b209667c8656ea7aac67c"
  21. GRPC_GATEWAY_SHA="92583770e3f01b09a0d3e9bdf64321d8bebd48f2"
  22. SCHWAG_SHA="b7d0fc9aadaaae3d61aaadfc12e4a2f945514912"
  23. # set up self-contained GOPATH for building
  24. export GOPATH=${PWD}/gopath.proto
  25. export GOBIN=${PWD}/bin
  26. export PATH="${GOBIN}:${PATH}"
  27. ETCD_IO_ROOT="${GOPATH}/src/go.etcd.io"
  28. ETCD_ROOT="${ETCD_IO_ROOT}/etcd"
  29. GOGOPROTO_ROOT="${GOPATH}/src/github.com/gogo/protobuf"
  30. SCHWAG_ROOT="${GOPATH}/src/github.com/hexfusion/schwag"
  31. GOGOPROTO_PATH="${GOGOPROTO_ROOT}:${GOGOPROTO_ROOT}/protobuf"
  32. GRPC_GATEWAY_ROOT="${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway"
  33. function cleanup {
  34. # Remove the whole fake GOPATH which can really confuse go mod.
  35. rm -rf "${PWD}/gopath.proto"
  36. }
  37. cleanup
  38. trap cleanup EXIT
  39. mkdir -p "${ETCD_IO_ROOT}"
  40. ln -s "${PWD}" "${ETCD_ROOT}"
  41. # Ensure we have the right version of protoc-gen-gogo by building it every time.
  42. # TODO(jonboulle): vendor this instead of `go get`ting it.
  43. go get -u github.com/gogo/protobuf/{proto,protoc-gen-gogo,gogoproto}
  44. go get -u golang.org/x/tools/cmd/goimports
  45. pushd "${GOGOPROTO_ROOT}"
  46. git reset --hard "${GOGO_PROTO_SHA}"
  47. make install
  48. popd
  49. # generate gateway code
  50. go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
  51. go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
  52. pushd "${GRPC_GATEWAY_ROOT}"
  53. git reset --hard "${GRPC_GATEWAY_SHA}"
  54. go install ./protoc-gen-grpc-gateway
  55. popd
  56. for dir in ${DIRS}; do
  57. pushd "${dir}"
  58. protoc --gofast_out=plugins=grpc,import_prefix=go.etcd.io/:. -I=".:${GOGOPROTO_PATH}:${ETCD_IO_ROOT}:${GRPC_GATEWAY_ROOT}/third_party/googleapis" ./*.proto
  59. # shellcheck disable=SC1117
  60. sed -i.bak -E 's/go\.etcd\.io\/(gogoproto|github\.com|golang\.org|google\.golang\.org)/\1/g' ./*.pb.go
  61. # shellcheck disable=SC1117
  62. sed -i.bak -E 's/go\.etcd\.io\/(errors|fmt|io)/\1/g' ./*.pb.go
  63. # shellcheck disable=SC1117
  64. sed -i.bak -E 's/import _ \"gogoproto\"//g' ./*.pb.go
  65. # shellcheck disable=SC1117
  66. sed -i.bak -E 's/import fmt \"fmt\"//g' ./*.pb.go
  67. # shellcheck disable=SC1117
  68. sed -i.bak -E 's/import _ \"go\.etcd\.io\/google\/api\"//g' ./*.pb.go
  69. # shellcheck disable=SC1117
  70. sed -i.bak -E 's/import _ \"google\.golang\.org\/genproto\/googleapis\/api\/annotations\"//g' ./*.pb.go
  71. rm -f ./*.bak
  72. goimports -w ./*.pb.go
  73. popd
  74. done
  75. # remove old swagger files so it's obvious whether the files fail to generate
  76. rm -rf Documentation/dev-guide/apispec/swagger/*json
  77. for pb in etcdserverpb/rpc api/v3lock/v3lockpb/v3lock api/v3election/v3electionpb/v3election; do
  78. protobase="etcdserver/${pb}"
  79. protoc -I. \
  80. -I"${GRPC_GATEWAY_ROOT}"/third_party/googleapis \
  81. -I"${GOGOPROTO_PATH}" \
  82. -I"${ETCD_IO_ROOT}" \
  83. --grpc-gateway_out=logtostderr=true:. \
  84. --swagger_out=logtostderr=true:./Documentation/dev-guide/apispec/swagger/. \
  85. ${protobase}.proto
  86. # hack to move gw files around so client won't include them
  87. pkgpath=$(dirname "${protobase}")
  88. pkg=$(basename "${pkgpath}")
  89. gwfile="${protobase}.pb.gw.go"
  90. sed -i.bak -E "s/package $pkg/package gw/g" ${gwfile}
  91. # shellcheck disable=SC1117
  92. sed -i.bak -E "s/protoReq /&$pkg\./g" ${gwfile}
  93. sed -i.bak -E "s/, client /, client $pkg./g" ${gwfile}
  94. sed -i.bak -E "s/Client /, client $pkg./g" ${gwfile}
  95. sed -i.bak -E "s/[^(]*Client, runtime/${pkg}.&/" ${gwfile}
  96. sed -i.bak -E "s/New[A-Za-z]*Client/${pkg}.&/" ${gwfile}
  97. # darwin doesn't like newlines in sed...
  98. # shellcheck disable=SC1117
  99. sed -i.bak -E "s|import \(|& \"go.etcd.io/etcd/${pkgpath}\"|" ${gwfile}
  100. mkdir -p "${pkgpath}"/gw/
  101. go fmt ${gwfile}
  102. mv ${gwfile} "${pkgpath}/gw/"
  103. rm -f ./etcdserver/${pb}*.bak
  104. swaggerName=$(basename ${pb})
  105. mv Documentation/dev-guide/apispec/swagger/etcdserver/${pb}.swagger.json \
  106. Documentation/dev-guide/apispec/swagger/"${swaggerName}".swagger.json
  107. done
  108. rm -rf Documentation/dev-guide/apispec/swagger/etcdserver/
  109. # append security to swagger spec
  110. go get -u "github.com/hexfusion/schwag"
  111. pushd "${SCHWAG_ROOT}"
  112. git reset --hard "${SCHWAG_SHA}"
  113. go install .
  114. popd
  115. schwag -input=Documentation/dev-guide/apispec/swagger/rpc.swagger.json
  116. # install protodoc
  117. # go get -v -u go.etcd.io/protodoc
  118. #
  119. # run './scripts/genproto.sh --skip-protodoc'
  120. # to skip protodoc generation
  121. #
  122. if [ "$1" != "--skip-protodoc" ]; then
  123. echo "protodoc is auto-generating grpc API reference documentation..."
  124. go get -v -u go.etcd.io/protodoc
  125. SHA_PROTODOC="484ab544e116302a9a6021cc7c427d334132e94a"
  126. PROTODOC_PATH="${GOPATH}/src/go.etcd.io/protodoc"
  127. pushd "${PROTODOC_PATH}"
  128. git reset --hard "${SHA_PROTODOC}"
  129. go install
  130. echo "protodoc is updated"
  131. popd
  132. protodoc --directories="etcdserver/etcdserverpb=service_message,mvcc/mvccpb=service_message,lease/leasepb=service_message,auth/authpb=service_message" \
  133. --title="etcd API Reference" \
  134. --output="Documentation/dev-guide/api_reference_v3.md" \
  135. --message-only-from-this-file="etcdserver/etcdserverpb/rpc.proto" \
  136. --disclaimer="This is a generated documentation. Please read the proto files for more."
  137. protodoc --directories="etcdserver/api/v3lock/v3lockpb=service_message,etcdserver/api/v3election/v3electionpb=service_message,mvcc/mvccpb=service_message" \
  138. --title="etcd concurrency API Reference" \
  139. --output="Documentation/dev-guide/api_concurrency_reference_v3.md" \
  140. --disclaimer="This is a generated documentation. Please read the proto files for more."
  141. echo "protodoc is finished..."
  142. else
  143. echo "skipping grpc API reference document auto-generation..."
  144. fi