genproto.sh 5.9 KB

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