Skip to content

Commit

Permalink
Stop depending on pip packages for proto generation
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 718380231
  • Loading branch information
torsm authored and copybara-github committed Jan 22, 2025
1 parent 789e3b3 commit 5301a18
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
- name: Set up Go
run: |
go get -u golang.org/x/lint/golint
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.34.1
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.4
fleetspeak/generate_protos_setup.sh
PATH="$HOME/.local/bin:$PATH"
- uses: actions/setup-python@v4
with:
python-version: '3.11'
Expand All @@ -46,10 +46,10 @@ jobs:
go vet ./... || true
- name: Check generated protos
run: |
fleetspeak/generate_go_py_protos.sh
fleetspeak/generate_protos.sh
if [[ "$(git status --porcelain | grep .pb.go)" != "" ]]; then
echo "At least one generated proto file is not in sync with the committed generated proto files."
echo "Please run \`PATH=~/go/bin:\$PATH fleetspeak/generate_go_py_protos.sh\`."
echo "Please run \`PATH=~/go/bin:\$PATH fleetspeak/generate_protos.sh\`."
echo "pip packages:"
pip freeze
echo "git diff:"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ echo 'Transpiling required protos to Go and Python.'
# Go to this script's directory.
cd "$(/usr/bin/dirname "$(/bin/readlink -e "${0}")")"

find .. -name '*.proto' -print0 | xargs -0 -t -I % python -m grpc_tools.protoc \
# Check dependencies.
./generate_protos_setup.sh check

find .. -name '*.proto' -print0 | xargs -0 -t -I % protoc \
--go_out=.. --go-grpc_out=.. \
--go_opt=paths=source_relative --go-grpc_opt=paths=source_relative \
--proto_path=.. %
49 changes: 49 additions & 0 deletions fleetspeak/generate_protos_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
set -e

PROTOC_VER="26.1" # https://github.com/protocolbuffers/protobuf/releases
GEN_GO_VER="1.34.1" # https://pkg.go.dev/google.golang.org/protobuf/cmd/protoc-gen-go?tab=versions
GEN_GO_GRPC_VER="1.4.0" # https://pkg.go.dev/google.golang.org/grpc/cmd/protoc-gen-go-grpc?tab=versions

function err() {
echo "$*" >&2
}

# Installs protoc from https://github.com/protocolbuffers/protobuf/releases to
# ~/.local/bin. Well known proto types are placed in ~/.local/include.
# Globals:
# PROTOC_VER
# Arguments:
# None
function install_protoc() {
local temp_zip
temp_zip="$(mktemp)"
trap "rm -f ${temp_zip}" EXIT
curl -L "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VER}/protoc-${PROTOC_VER}-linux-x86_64.zip" -o "${temp_zip}"
unzip -o "${temp_zip}" -x readme.txt -d "${HOME}/.local"
echo "Installed protoc v${PROTOC_VER}. Ensure ${HOME}/.local/bin is in your PATH."
}

if [[ "$(protoc --version)" != "libprotoc ${PROTOC_VER}" ]]; then
if [[ "$1" == "check" ]]; then
err "Wrong protobuf version, please run fleetspeak/generate_protos_setup.sh"
exit 1
fi
install_protoc
fi

if [[ "$(protoc-gen-go --version)" != "protoc-gen-go v${GEN_GO_VER}" ]]; then
if [[ "$1" == "check" ]]; then
err "Wrong protoc-gen-go version, please run fleetspeak/generate_protos_setup.sh"
exit 1
fi
go install "google.golang.org/protobuf/cmd/protoc-gen-go@v${GEN_GO_VER}"
fi

if [[ "$(protoc-gen-go-grpc --version)" != "protoc-gen-go-grpc ${GEN_GO_GRPC_VER}" ]]; then
if [[ "$1" == "check" ]]; then
err "Wrong protoc-gen-go-grpc version, please run fleetspeak/generate_protos_setup.sh"
exit 1
fi
go install "google.golang.org/grpc/cmd/protoc-gen-go-grpc@v${GEN_GO_GRPC_VER}"
fi
7 changes: 4 additions & 3 deletions sandboxes/shared/fleetspeak-client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ SHELL ["/bin/bash", "-c"]
RUN git clone https://github.com/google/fleetspeak.git && \
cd fleetspeak && \
go get -u golang.org/x/lint/golint && \
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.0 && \
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
./fleetspeak/generate_protos_setup.sh

ENV PATH="$HOME/.local/bin:$PATH"

RUN mkdir -p /app/bin

Expand All @@ -36,7 +37,7 @@ RUN cd /fleetspeak && \
pip install wheel pytest && \
pip install -e ./fleetspeak_python[test] && \
pip install -e ./frr_python && \
./fleetspeak/generate_go_py_protos.sh && \
./fleetspeak/generate_protos.sh && \
go build -o /app/bin/server ./cmd/fleetspeak_server && \
go build -o /app/bin/client ./cmd/fleetspeak_client && \
go build -o /app/bin/fleetspeak_config ./cmd/fleetspeak_config
Expand Down
7 changes: 4 additions & 3 deletions sandboxes/shared/fleetspeak-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ SHELL ["/bin/bash", "-c"]
RUN git clone https://github.com/google/fleetspeak.git && \
cd fleetspeak && \
go get -u golang.org/x/lint/golint && \
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.0 && \
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
./fleetspeak/generate_protos_setup.sh

ENV PATH="$HOME/.local/bin:$PATH"

ENV GOBIN=/fleetspeak-bin

Expand All @@ -38,7 +39,7 @@ RUN cd /fleetspeak && \
pip install wheel pytest && \
pip install -e ./fleetspeak_python[test] && \
pip install -e ./frr_python && \
./fleetspeak/generate_go_py_protos.sh && \
./fleetspeak/generate_protos.sh && \
go install ./cmd/fleetspeak_server ./cmd/fleetspeak_client \
./cmd/fleetspeak_config

Expand Down

0 comments on commit 5301a18

Please sign in to comment.