genproto.sh 5.8 KB

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