Skip to content

Commit 622df68

Browse files
bootstrap: Install protobuf gRPC Python plugin via PyPi and do not compile it from a Git checkout.
Tested: - I've rebuilt bootstrap docker images locally and all tests passed on them. - I ran `bootstrap.sh` and `make proto` manually on my Linux workstation and my MacOS laptop. History of changes for references: - vitessio#3220 - I got rid of compiling gRPC from source. Because of that, the gRPC Python plugin was no longer installed and "make proto" stopped working. - vitessio#3461 - This PR reverted my changes and changed back to compile gRPC from source again. However, that would not have been necessary in the first place. The "grpcio-tools" package has the protobuf compiler and the protobuf gRPC Python plugin. See: https://grpc.io/docs/quickstart/python.html#install-grpc-tools - vitessio#3516 - This PR removed the gRPC compilation, but still compiled the protobuf gRPC Python plugin. Note: As part of the now removed Python plugin compilation, we also had a compiled "protoc" binary. Since we will no longer have this binary, we're using the recommended procedure from the "grpcio-tools" package instead. See: https://grpc.io/docs/quickstart/python.html#generate-grpc-code They provide a Python wrapper script around a shared library of the "protoc" compiler, but not a binary anymore. Therefore, we have to call "python -m grpc_tools.protoc" to invoke the protobuf compiler. Other changes: - This protobuf compiler knows the Python plugin by default and does not require the --plugin flag for generating the Python services. - In contrast, --plugin is now required to pass the gRPC Go plugin. - The Python plugin changed its flag from --grpc_out to --grpc_python_out. - The Python plugin emits different files since gRPC 1.7.0. I'll regenerate the files in a separate commit. See: https://github.com/grpc/grpc/releases/tag/v1.7.0 - Removed unlinking and linking back of Homebrew "protobuf" package. It looks to me like an existing package should not interfere with our virtualenv environment. - Removed cleanup call where we deleted the obsolete $VTROOT/dist/protobuf directory. If users still have that directory on disk, they can delete it manually themselves. Existing directories should not interfere with the new approach. Signed-off-by: Michael Berlin <[email protected]>
1 parent 54a5237 commit 622df68

File tree

5 files changed

+45
-111
lines changed

5 files changed

+45
-111
lines changed

.codeclimate.yml

-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,3 @@ exclude_paths:
4242
- "go/vt/proto/"
4343
- "go/vt/sqlparser/sql.go"
4444
- "py/util/grpc_with_metadata.py"
45-
- "travis/install_grpc.sh"

Makefile

+13-11
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,15 @@ install_protoc-gen-go:
120120
cp -a vendor/github.com/golang/protobuf $${GOPATH}/src/github.com/golang/
121121
go install github.com/golang/protobuf/protoc-gen-go
122122

123-
PROTOC_BINARY := $(shell type -p $(VTROOT)/dist/grpc/usr/local/bin/protoc)
124-
ifeq (,$(PROTOC_BINARY))
125-
PROTOC_BINARY := $(shell which protoc)
126-
endif
127-
128-
ifneq (,$(PROTOC_BINARY))
129-
PROTOC_DIR := $(dir $(PROTOC_BINARY))
123+
# Find protoc compiler.
124+
# NOTE: We are *not* using the "protoc" binary (as suggested by the grpc Go
125+
# quickstart for example). Instead, we run "protoc" via the Python
126+
# wrapper script which is provided by the "grpcio-tools" PyPi package.
127+
# (The package includes the compiler as library, but not as binary.
128+
# Therefore, we have to use the wrapper script they provide.)
129+
ifneq ($(wildcard $(VTROOT)/dist/grpc/usr/local/lib/python2.7/site-packages/grpc_tools/protoc.py),)
130+
# IMPORTANT: The next line must not be indented.
131+
PROTOC_COMMAND := python -m grpc_tools.protoc
130132
endif
131133

132134
PROTO_SRCS = $(wildcard proto/*.proto)
@@ -139,16 +141,16 @@ PROTO_GO_TEMPS = $(foreach name, $(PROTO_SRC_NAMES), go/vt/.proto.tmp/$(name).pb
139141
proto: proto_banner $(PROTO_GO_OUTS) $(PROTO_PY_OUTS)
140142

141143
proto_banner:
142-
ifeq (,$(PROTOC_DIR))
143-
$(error "Cannot find protoc binary. Did bootstrap.sh succeed, and did you execute 'source dev.env'?")
144+
ifeq (,$(PROTOC_COMMAND))
145+
$(error "Cannot find protoc compiler. Did bootstrap.sh succeed, and did you execute 'source dev.env'?")
144146
endif
145147

146148
ifndef NOBANNER
147149
echo $$(date): Compiling proto definitions
148150
endif
149151

150152
$(PROTO_PY_OUTS): py/vtproto/%_pb2.py: proto/%.proto
151-
$(PROTOC_DIR)/protoc -Iproto $< --python_out=py/vtproto --grpc_out=py/vtproto --plugin=protoc-gen-grpc=$(PROTOC_DIR)/grpc_python_plugin
153+
$(PROTOC_COMMAND) -Iproto $< --python_out=py/vtproto --grpc_python_out=py/vtproto
152154

153155
$(PROTO_GO_OUTS): $(PROTO_GO_TEMPS)
154156
for name in $(PROTO_SRC_NAMES); do \
@@ -160,7 +162,7 @@ $(PROTO_GO_TEMPS): install_protoc-gen-go
160162

161163
$(PROTO_GO_TEMPS): go/vt/.proto.tmp/%.pb.go: proto/%.proto
162164
mkdir -p go/vt/.proto.tmp
163-
$(PROTOC_DIR)/protoc -Iproto $< --go_out=plugins=grpc:go/vt/.proto.tmp
165+
$(PROTOC_COMMAND) -Iproto $< --plugin=grpc=protoc-gen-go --go_out=plugins=grpc:go/vt/.proto.tmp
164166
sed -i -e 's,import \([a-z0-9_]*\) ".",import \1 "vitess.io/vitess/go/vt/proto/\1",g' $@
165167

166168
# Helper targets for building Docker images.

bootstrap.sh

+32-23
Original file line numberDiff line numberDiff line change
@@ -101,38 +101,47 @@ else
101101
fi
102102
ln -snf $consul_dist/consul $VTROOT/bin/consul
103103

104-
# Install gRPC proto compilers. There is no download for grpc_python_plugin.
105-
# So, we need to build it.
106-
export grpc_dist=$VTROOT/dist/grpc
107-
export grpc_ver="v1.10.0"
108-
if [[ -f $grpc_dist/.build_finished && "$(cat $grpc_dist/.build_finished)" == "$grpc_ver" ]]; then
109-
echo "skipping gRPC build. remove $grpc_dist to force rebuild."
104+
# Install the gRPC Python library (grpcio) and the protobuf gRPC Python plugin (grpcio-tools) from PyPI.
105+
# Dependencies like the Python protobuf package will be installed automatically.
106+
grpc_dist=$VTROOT/dist/grpc
107+
grpc_ver="1.10.0"
108+
grpc_version_file="$grpc_dist/.build_finished"
109+
if [[ -f "$grpc_version_file" && "$(cat "$grpc_version_file")" == "$grpc_ver" ]]; then
110+
echo "skipping gRPC build. remove $grpc_dist to force reinstall."
110111
else
111-
echo "installing grpc $grpc_ver"
112-
# unlink homebrew's protobuf, to be able to compile the downloaded protobuf package
113-
if [[ `uname -s` == "Darwin" && "$(brew list -1 | grep google-protobuf)" ]]; then
114-
brew unlink grpc/grpc/google-protobuf
115-
fi
112+
echo "installing gRPC $grpc_ver"
113+
# Cleanup any existing data and re-create the directory.
114+
rm -rf "$grpc_dist"
116115

117-
# protobuf used to be a separate package, now we use the gRPC one.
118-
rm -rf $VTROOT/dist/protobuf
116+
trap "fail 'gRPC build failed'; exit 1" ERR
117+
mkdir -p "$grpc_dist"
118+
pushd "$grpc_dist" >/dev/null
119119

120-
# Cleanup any existing data and re-create the directory.
121-
rm -rf $grpc_dist
122-
mkdir -p $grpc_dist
120+
# Python requires a very recent version of virtualenv.
121+
# We also require a recent version of pip, as we use it to
122+
# upgrade the other tools.
123+
# For instance, setuptools doesn't work with pip 6.0:
124+
# https://github.com/pypa/setuptools/issues/945
125+
# (and setuptools is used by grpc install).
126+
grpc_virtualenv="$grpc_dist/usr/local"
127+
$VIRTUALENV -v "$grpc_virtualenv"
128+
PIP=$grpc_virtualenv/bin/pip
129+
$PIP install --upgrade pip
130+
$PIP install --upgrade --ignore-installed virtualenv
131+
132+
grpcio_ver=$grpc_ver
133+
$PIP install --upgrade grpcio==$grpcio_ver grpcio-tools==$grpcio_ver
123134

124-
./travis/install_grpc.sh $grpc_dist || fail "gRPC build failed"
125-
echo "$grpc_ver" > $grpc_dist/.build_finished
135+
popd >/dev/null
136+
trap - ERR
126137

127-
# link homebrew's protobuf back
128-
if [[ `uname -s` == "Darwin" && "$(brew list -1 | grep google-protobuf)" ]]; then
129-
brew link grpc/grpc/google-protobuf
130-
fi
138+
echo "$grpc_ver" > "$grpc_version_file"
131139

132140
# Add newly installed Python code to PYTHONPATH such that other Python module
133141
# installations can reuse it. (Once bootstrap.sh has finished, run
134142
# source dev.env instead to set the correct PYTHONPATH.)
135-
export PYTHONPATH=$(prepend_path $PYTHONPATH $grpc_dist/usr/local/lib/python2.7/dist-packages)
143+
PYTHONPATH=$(prepend_path "$PYTHONPATH" "$grpc_virtualenv/lib/python2.7/dist-packages")
144+
export PYTHONPATH
136145
fi
137146

138147
# Install third-party Go tools used as part of the development workflow.

docker/bootstrap/Dockerfile.common

-7
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-ins
2929
libz-dev \
3030
&& rm -rf /var/lib/apt/lists/*
3131

32-
# Not sure if this is needed, but it was part of php init, which we don't do any more.
33-
RUN mkdir -p /vt/bin
34-
3532
# Install Maven 3.1+
3633
RUN mkdir -p /vt/dist && \
3734
cd /vt/dist && \
@@ -61,10 +58,6 @@ COPY tools /vt/src/vitess.io/vitess/tools
6158
COPY travis /vt/src/vitess.io/vitess/travis
6259
COPY vendor/vendor.json /vt/src/vitess.io/vitess/vendor/
6360

64-
# grpcio runtime is needed for python tests.
65-
RUN grpcio_ver="1.7.0" && \
66-
pip install --upgrade grpcio==$grpcio_ver
67-
6861
# Download vendored Go dependencies
6962
RUN cd /vt/src/vitess.io/vitess && \
7063
go get -u github.com/kardianos/govendor && \

travis/install_grpc.sh

-69
This file was deleted.

0 commit comments

Comments
 (0)