Files
vault/tools/tools.sh
Ryan Cragun 9a10689ca3 [QT-645] Restructure dev tools (#24559)
We're on a quest to reduce our pipeline execution time to both enhance
our developer productivity but also to reduce the overall cost of the CI
pipeline. The strategy we use here reduces workflow execution time and
network I/O cost by reducing our module cache size and using binary
external tools when possible. We no longer download modules and build
many of the external tools thousands of times a day.

Our previous process of installing internal and external developer tools
was scattered and inconsistent. Some tools were installed via `go
generate -tags tools ./tools/...`,
others via various `make` targets, and some only in Github Actions
workflows. This process led to some undesirable side effects:
  * The modules of some dev and test tools were included with those
    of the Vault project. This leads to us having to manage our own
    Go modules with those of external tools. Prior to Go 1.16 this
    was the recommended way to handle external tools, but now
    `go install tool@version` is the recommended way to handle
    external tools that need to be build from source as it supports
    specific versions but does not modify the go.mod.
  * Due to Github cache constraints we combine our build and test Go
    module caches together, but having our developer tools as deps in
    our module results in a larger cache which is downloaded on every
    build and test workflow runner. Removing the external tools that were
    included in our go.mod reduced the expanded module cache by size
    by ~300MB, thus saving time and network I/O costs when downloading
    the module cache.
  * Not all of our developer tools were included in our modules. Some were
    being installed with `go install` or `go run`, so they didn't take
    advantage of a single module cache. This resulted in us downloading
    Go modules on every CI and Build runner in order to build our
    external tools.
  * Building our developer tools from source in CI is slow. Where possible
    we can prefer to use pre-built binaries in CI workflows. No more
    module download or tool compiles if we can avoid them.

I've refactored how we define internal and external build tools
in our Makefile and added several new targets to handle both building
the developer tools locally for development and verifying that they are
available. This allows for an easy developer bootstrap while also
supporting installation of many of the external developer tools from
pre-build binaries in CI. This reduces our network IO and run time
across nearly all of our actions runners.

While working on this I caught and resolved a few unrelated issue:
* Both our Go and Proto format checks we're being run incorrectly. In
  CI they we're writing changes but not failing if changes were
  detected. The Go was less of a problem as we have git hooks that
  are intended to enforce formatting, however we drifted over time.
* Our Git hooks couldn't handle removing a Go file without failing. I
  moved the diff check into the new Go helper and updated it to handle
  removing files.
* I combined a few separate scripts and into helpers and added a few
  new capabilities.
* I refactored how we install Go modules to make it easier to download
  and tidy all of the projects go.mod's.
* Refactor our internal and external tool installation and verification
  into a tools.sh helper.
* Combined more complex Go verification into `scripts/go-helper.sh` and
  utilize it in the `Makefile` and git commit hooks.
* Add `Makefile` targets for executing our various tools.sh helpers.
* Update our existing `make` targets to use new tool targets.
* Normalize our various scripts and targets output to have a consistent
  output format.
* In CI, install many of our external dependencies as binaries wherever
  possible. When not possible we'll build them from scratch but not mess
  with the shared module cache.
* [QT-641] Remove our external build tools from our project Go modules.
* [QT-641] Remove extraneous `go list`'s from our `set-up-to` composite
  action.
* Fix formatting and regen our protos

Signed-off-by: Ryan Cragun <me@ryan.ec>
2024-01-09 17:50:46 +00:00

154 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1
set -euo pipefail
# Determine the root directory of the repository
repo_root() {
git rev-parse --show-toplevel
}
# Install an external Go tool.
go_install() {
if go install "$1"; then
echo "--> $1"
else
echo "--> $1"
return 1
fi
}
# Check for a tool binary in the path.
check_tool() {
if builtin type -P "$2" &> /dev/null; then
echo "--> $2"
else
echo "--> $2"
echo "Could not find required $1 tool $2. Run 'make tools-$1' to install it." 1>&2
return 1
fi
}
# Install external tools.
install_external() {
local tools
# If you update this please update check_external below as well as our external tools
# install action ./github/actions/install-external-tools.yml
tools=(
github.com/bufbuild/buf/cmd/buf@v1.25.0
github.com/favadi/protoc-go-inject-tag@latest
github.com/golangci/misspell/cmd/misspell@latest
github.com/golangci/revgrep/cmd/revgrep@latest
golang.org/x/tools/cmd/goimports@latest
google.golang.org/protobuf/cmd/protoc-gen-go@latest
google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
honnef.co/go/tools/cmd/staticcheck@latest
mvdan.cc/gofumpt@latest
)
echo "==> Installing external tools..."
for tool in "${tools[@]}"; do
go_install "$tool"
done
}
# Check that all tools are installed
check_external() {
# Ensure that all external tools are available. In CI we'll prefer installing pre-built external
# tools for speed instead of go install so that we don't require downloading Go modules and
# compiling tools from scratch in every CI job.
# See .github/actions/install-external-tools.yml for that workflow.
local tools
tools=(
buf
gofumpt
goimports
gotestsum
misspell
protoc-gen-go
protoc-gen-go-grpc
protoc-go-inject-tag
revgrep
staticcheck
)
echo "==> Checking for external tools..."
for tool in "${tools[@]}"; do
check_tool external "$tool"
done
}
# Install internal tools.
install_internal() {
local tools
# If you update this please update check tools below.
tools=(
codechecker
stubmaker
)
echo "==> Installing internal tools..."
pushd "$(repo_root)" &> /dev/null
for tool in "${tools[@]}"; do
go_install ./tools/"$tool"
done
popd &> /dev/null
}
# Check internal that all tools are installed
check_internal() {
# Ensure that all required internal tools are available.
local tools
tools=(
codechecker
stubmaker
)
echo "==> Checking for internal tools..."
for tool in "${tools[@]}"; do
check_tool internal "$tool"
done
}
# Install tools.
install() {
install_internal
install_external
}
# Check tools.
check() {
check_internal
check_external
}
main() {
case $1 in
install-external)
install_external
;;
install-internal)
install_internal
;;
check-external)
check_external
;;
check-internal)
check_internal
;;
install)
install
;;
check)
check
;;
*)
echo "unknown sub-command" >&2
exit 1
;;
esac
}
main "$@"