genproto.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.4.0" ]]; then
  13. echo "could not find protoc 3.4.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 ./etcdserver/api/v3lock/v3lockpb ./etcdserver/api/v3election/v3electionpb"
  18. # exact version of protoc-gen-gogo to build
  19. GOGO_PROTO_SHA="100ba4e885062801d56799d78530b73b178a78f3"
  20. GRPC_GATEWAY_SHA="8cc3a55af3bcf171a1c23a90c4df9cf591706104"
  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. # remove old swagger files so it's obvious whether the files fail to generate
  61. rm -rf Documentation/dev-guide/apispec/swagger/*json
  62. for pb in etcdserverpb/rpc api/v3lock/v3lockpb/v3lock api/v3election/v3electionpb/v3election; do
  63. protobase="etcdserver/${pb}"
  64. protoc -I. \
  65. -I${GRPC_GATEWAY_ROOT}/third_party/googleapis \
  66. -I${GOGOPROTO_PATH} \
  67. -I${COREOS_ROOT} \
  68. --grpc-gateway_out=logtostderr=true:. \
  69. --swagger_out=logtostderr=true:./Documentation/dev-guide/apispec/swagger/. \
  70. ${protobase}.proto
  71. # hack to move gw files around so client won't include them
  72. pkgpath=`dirname ${protobase}`
  73. pkg=`basename ${pkgpath}`
  74. gwfile="${protobase}.pb.gw.go"
  75. sed -i.bak -E "s/package $pkg/package gw/g" ${gwfile}
  76. sed -i.bak -E "s/protoReq /&$pkg\./g" ${gwfile}
  77. sed -i.bak -E "s/, client /, client $pkg./g" ${gwfile}
  78. sed -i.bak -E "s/Client /, client $pkg./g" ${gwfile}
  79. sed -i.bak -E "s/[^(]*Client, runtime/${pkg}.&/" ${gwfile}
  80. sed -i.bak -E "s/New[A-Za-z]*Client/${pkg}.&/" ${gwfile}
  81. # darwin doesn't like newlines in sed...
  82. sed -i.bak -E "s|import \(|& \"github.com/coreos/etcd/${pkgpath}\"|" ${gwfile}
  83. mkdir -p ${pkgpath}/gw/
  84. go fmt ${gwfile}
  85. mv ${gwfile} ${pkgpath}/gw/
  86. rm -f ./etcdserver/${pb}*.bak
  87. swaggerName=`basename ${pb}`
  88. mv Documentation/dev-guide/apispec/swagger/etcdserver/${pb}.swagger.json \
  89. Documentation/dev-guide/apispec/swagger/${swaggerName}.swagger.json
  90. done
  91. rm -rf Documentation/dev-guide/apispec/swagger/etcdserver/
  92. # install protodoc
  93. # go get -v -u github.com/coreos/protodoc
  94. #
  95. # by default, do not run this option.
  96. # only run when './scripts/genproto.sh -g'
  97. #
  98. if [ "$1" = "-g" ]; then
  99. echo "protodoc is auto-generating grpc API reference documentation..."
  100. go get -v -u github.com/coreos/protodoc
  101. SHA_PROTODOC="4372ee725035a208404e2d5465ba921469decc32"
  102. PROTODOC_PATH="${GOPATH}/src/github.com/coreos/protodoc"
  103. pushd "${PROTODOC_PATH}"
  104. git reset --hard "${SHA_PROTODOC}"
  105. go install
  106. echo "protodoc is updated"
  107. popd
  108. protodoc --directories="etcdserver/etcdserverpb=service_message,mvcc/mvccpb=service_message,lease/leasepb=service_message,auth/authpb=service_message" \
  109. --title="etcd API Reference" \
  110. --output="Documentation/dev-guide/api_reference_v3.md" \
  111. --message-only-from-this-file="etcdserver/etcdserverpb/rpc.proto" \
  112. --disclaimer="This is a generated documentation. Please read the proto files for more."
  113. protodoc --directories="etcdserver/api/v3lock/v3lockpb=service_message,etcdserver/api/v3election/v3electionpb=service_message,mvcc/mvccpb=service_message" \
  114. --title="etcd concurrency API Reference" \
  115. --output="Documentation/dev-guide/api_concurrency_reference_v3.md" \
  116. --disclaimer="This is a generated documentation. Please read the proto files for more."
  117. echo "protodoc is finished..."
  118. else
  119. echo "skipping grpc API reference document auto-generation..."
  120. fi