Skip to content

Commit 827d7ea

Browse files
committed
[testing] Replace script-based tool installation with nix
Previously, binary tools like promtail and prometheus (enabling log and metrics collection) and kube and kind (enabling local kube clusters) were installed using bash scripts. A script-based approach, while simple, requires tedious and error-prone copying and committing to other repos (e.g. subnet-evm and hypersdk) that need the same functionality. Possible to rewrite the scripts to golang which allows usage via `go run` or `go install`. Simple enough if the versions never change, but still requires retrieving binaries, verifying their hashes for security, and potentially dealing with tar archives. Still more complicated if it becomes necessary to ensure the desired version is the one in the path. Switching to [nix](https://nixos.org/), on the other hand, is an established way of solving the problem that doesn't involve maintaining code. Installing nix is a one-time operation not too different from homebrew, except that the result has more guarantees of reproducibility across linux and macos.
1 parent 44ac37e commit 827d7ea

File tree

9 files changed

+150
-97
lines changed

9 files changed

+150
-97
lines changed

.envrc

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use flake
2+
13
# Repo-local commands like ginkgo and tmpnetctl
24
PATH_add bin
35

.github/actions/run-monitored-tmpnet-cmd/action.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ runs:
4040
# Only run for the original repo; a forked repo won't have access to the monitoring credentials
4141
if: (inputs.prometheus_username != '')
4242
shell: bash
43-
run: bash -x ./scripts/run_prometheus.sh
43+
# Assumes calling project has a nix flake that ensures a compatible prometheus
44+
run: nix develop --impure --command bash -x ./scripts/run_prometheus.sh
4445
env:
4546
PROMETHEUS_USERNAME: ${{ inputs.prometheus_username }}
4647
PROMETHEUS_PASSWORD: ${{ inputs.prometheus_password }}
4748
- name: Start promtail
4849
if: (inputs.prometheus_username != '')
4950
shell: bash
50-
run: bash -x ./scripts/run_promtail.sh
51+
# Assumes calling project has a nix flake that ensures a compatible promtail
52+
run: nix develop --impure --command bash -x ./scripts/run_promtail.sh
5153
env:
5254
LOKI_USERNAME: ${{ inputs.loki_username }}
5355
LOKI_PASSWORD: ${{ inputs.loki_password }}

.github/workflows/ci.yml

+18-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ jobs:
5151
steps:
5252
- uses: actions/checkout@v4
5353
- uses: ./.github/actions/setup-go-for-project
54+
- uses: cachix/install-nix-action@v30
55+
with:
56+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
57+
# TODO(marun) Maybe figure out how to use `nix build` somehow i.e. make the default shell double as the default package
58+
- run: nix develop --command echo "dependencies installed"
5459
- name: Build AvalancheGo Binary
5560
shell: bash
5661
run: ./scripts/build.sh -r
@@ -72,6 +77,10 @@ jobs:
7277
steps:
7378
- uses: actions/checkout@v4
7479
- uses: ./.github/actions/setup-go-for-project
80+
- uses: cachix/install-nix-action@v30
81+
with:
82+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
83+
- run: nix develop --command echo "dependencies installed"
7584
- name: Build AvalancheGo Binary
7685
shell: bash
7786
run: ./scripts/build.sh -r
@@ -93,6 +102,10 @@ jobs:
93102
steps:
94103
- uses: actions/checkout@v4
95104
- uses: ./.github/actions/setup-go-for-project
105+
- uses: cachix/install-nix-action@v30
106+
with:
107+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
108+
- run: nix develop --command echo "dependencies installed"
96109
- name: Build AvalancheGo Binary
97110
shell: bash
98111
run: ./scripts/build.sh
@@ -230,6 +243,10 @@ jobs:
230243
steps:
231244
- uses: actions/checkout@v4
232245
- uses: ./.github/actions/setup-go-for-project
246+
- uses: cachix/install-nix-action@v30
247+
with:
248+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
249+
- run: nix develop --command echo "dependencies installed"
233250
- name: Run e2e tests
234251
shell: bash
235-
run: bash -x ./scripts/tests.e2e.bootstrap_monitor.sh
252+
run: nix develop --command bash -x ./scripts/tests.e2e.bootstrap_monitor.sh

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ tests/upgrade/upgrade.test
6262
vendor
6363

6464
**/testdata
65+
66+
.direnv

flake.lock

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
# To use:
3+
# - install nix: https://github.com/DeterminateSystems/nix-installer?tab=readme-ov-file#install-nix
4+
# - run `nix develop` or use direnv (https://direnv.net/)
5+
# - for quieter direnv output, set `export DIRENV_LOG_FORMAT=`
6+
7+
description = "AvalancheGo development environment";
8+
9+
# Flake inputs
10+
inputs = {
11+
nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2405.*.tar.gz";
12+
};
13+
14+
# Flake outputs
15+
outputs = { self, nixpkgs }:
16+
let
17+
# Systems supported
18+
allSystems = [
19+
"x86_64-linux" # 64-bit Intel/AMD Linux
20+
"aarch64-linux" # 64-bit ARM Linux
21+
"x86_64-darwin" # 64-bit Intel macOS
22+
"aarch64-darwin" # 64-bit ARM macOS
23+
];
24+
25+
# Helper to provide system-specific attributes
26+
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
27+
pkgs = import nixpkgs { inherit system; };
28+
});
29+
in
30+
{
31+
# Development environment output
32+
devShells = forAllSystems ({ pkgs }: {
33+
default = pkgs.mkShell {
34+
# The Nix packages provided in the environment
35+
packages = with pkgs; [
36+
# Monitoring tools
37+
promtail # Loki log shipper
38+
prometheus # Metrics collector
39+
40+
# Kube tools
41+
kubectl # Kubernetes CLI
42+
kind # Kubernetes-in-Docker
43+
kubernetes-helm # Helm CLI (Kubernetes package manager)
44+
self.packages.${system}.kind-with-registry # Script installing kind configured with a local registry
45+
];
46+
};
47+
});
48+
49+
# Package to install the kind-with-registry script
50+
packages = forAllSystems ({ pkgs }: {
51+
kind-with-registry = pkgs.stdenv.mkDerivation {
52+
pname = "kind-with-registry";
53+
version = "1.0.0";
54+
55+
src = pkgs.fetchurl {
56+
url = "https://raw.githubusercontent.com/kubernetes-sigs/kind/7cb9e6be25b48a0e248097eef29d496ab1a044d0/site/static/examples/kind-with-registry.sh";
57+
sha256 = "0gri0x0ygcwmz8l4h6zzsvydw8rsh7qa8p5218d4hncm363i81hv";
58+
};
59+
60+
phases = [ "installPhase" ];
61+
62+
installPhase = ''
63+
mkdir -p $out/bin
64+
install -m755 $src $out/bin/kind-with-registry.sh
65+
'';
66+
67+
meta = with pkgs.lib; {
68+
description = "Script to set up kind with a local registry";
69+
license = licenses.mit;
70+
maintainers = with maintainers; [ "maru-ava" ];
71+
};
72+
};
73+
});
74+
};
75+
}

scripts/run_prometheus.sh

+8-33
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ if ! [[ "$0" =~ scripts/run_prometheus.sh ]]; then
2020
exit 255
2121
fi
2222

23+
CMD=prometheus
24+
25+
if ! command -v "${CMD}" &> /dev/null; then
26+
echo "prometheus not found, have you run 'nix develop'?"
27+
echo "To install nix: https://github.com/DeterminateSystems/nix-installer?tab=readme-ov-file#install-nix"
28+
exit 1
29+
fi
30+
2331
PROMETHEUS_WORKING_DIR="${HOME}/.tmpnet/prometheus"
2432
PIDFILE="${PROMETHEUS_WORKING_DIR}"/run.pid
2533

@@ -48,39 +56,6 @@ if [[ -z "${PROMETHEUS_PASSWORD}" ]]; then
4856
exit 1
4957
fi
5058

51-
# This was the LTS version when this script was written. Probably not
52-
# much reason to update it unless something breaks since the usage
53-
# here is only to collect metrics from temporary networks.
54-
VERSION="2.45.3"
55-
56-
# Ensure the prometheus command is locally available
57-
CMD=prometheus
58-
if ! command -v "${CMD}" &> /dev/null; then
59-
# Try to use a local version
60-
CMD="${PWD}/bin/prometheus"
61-
if ! command -v "${CMD}" &> /dev/null; then
62-
echo "prometheus not found, attempting to install..."
63-
64-
GOOS="$(go env GOOS)"
65-
GOARCH="$(go env GOARCH)"
66-
if [[ "${GOOS}" == "darwin" && "${GOARCH}" == "arm64" ]]; then
67-
echo "On macos, only amd64 binaries are available so rosetta is required on apple silicon machines."
68-
echo "To avoid using rosetta, install via homebrew: brew install prometheus"
69-
fi
70-
if [[ "${GOOS}" == "linux" && "${GOARCH}" != "amd64" ]]; then
71-
echo "On linux, only amd64 binaries are available. Manual installation of prometheus is required."
72-
exit 1
73-
fi
74-
75-
# Install the specified release
76-
PROMETHEUS_FILE="prometheus-${VERSION}.${GOOS}-amd64"
77-
URL="https://github.com/prometheus/prometheus/releases/download/v${VERSION}/${PROMETHEUS_FILE}.tar.gz"
78-
curl -s -L "${URL}" | tar zxv -C /tmp > /dev/null
79-
mkdir -p "$(dirname "${CMD}")"
80-
cp /tmp/"${PROMETHEUS_FILE}/prometheus" "${CMD}"
81-
fi
82-
fi
83-
8459
# Configure prometheus
8560
FILE_SD_PATH="${PROMETHEUS_WORKING_DIR}/file_sd_configs"
8661
mkdir -p "${FILE_SD_PATH}"

scripts/run_promtail.sh

+8-27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ if ! [[ "$0" =~ scripts/run_promtail.sh ]]; then
1919
exit 255
2020
fi
2121

22+
CMD=promtail
23+
24+
if ! command -v "${CMD}" &> /dev/null; then
25+
echo "promtail not found, have you run 'nix develop'?"
26+
echo "To install nix: https://github.com/DeterminateSystems/nix-installer?tab=readme-ov-file#install-nix"
27+
exit 1
28+
fi
29+
2230
PROMTAIL_WORKING_DIR="${HOME}/.tmpnet/promtail"
2331
PIDFILE="${PROMTAIL_WORKING_DIR}"/run.pid
2432

@@ -47,33 +55,6 @@ if [[ -z "${LOKI_PASSWORD}" ]]; then
4755
exit 1
4856
fi
4957

50-
# Version as of this writing
51-
VERSION="v2.9.5"
52-
53-
# Ensure the promtail command is locally available
54-
CMD=promtail
55-
if ! command -v "${CMD}" &> /dev/null; then
56-
# Try to use a local version
57-
CMD="${PWD}/bin/promtail"
58-
if ! command -v "${CMD}" &> /dev/null; then
59-
echo "promtail not found, attempting to install..."
60-
61-
# Determine the platform
62-
GOOS="$(go env GOOS)"
63-
GOARCH="$(go env GOARCH)"
64-
DIST="${GOOS}-${GOARCH}"
65-
66-
# Install the specified release
67-
PROMTAIL_FILE="promtail-${DIST}"
68-
ZIP_PATH="/tmp/${PROMTAIL_FILE}.zip"
69-
BIN_DIR="$(dirname "${CMD}")"
70-
URL="https://github.com/grafana/loki/releases/download/${VERSION}/promtail-${DIST}.zip"
71-
curl -L -o "${ZIP_PATH}" "${URL}"
72-
unzip "${ZIP_PATH}" -d "${BIN_DIR}"
73-
mv "${BIN_DIR}/${PROMTAIL_FILE}" "${CMD}"
74-
fi
75-
fi
76-
7758
# Configure promtail
7859
FILE_SD_PATH="${PROMTAIL_WORKING_DIR}/file_sd_configs"
7960
mkdir -p "${FILE_SD_PATH}"

scripts/tests.e2e.bootstrap_monitor.sh

+8-34
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,14 @@ if ! [[ "$0" =~ scripts/tests.e2e.bootstrap_monitor.sh ]]; then
99
exit 255
1010
fi
1111

12-
GOOS="$(go env GOOS)"
13-
GOARCH="$(go env GOARCH)"
12+
CMD=kind-with-registry.sh
1413

15-
function ensure_command {
16-
local cmd=$1
17-
local install_uri=$2
18-
19-
if ! command -v "${cmd}" &> /dev/null; then
20-
# Try to use a local version
21-
local local_cmd="${PWD}/bin/${cmd}"
22-
mkdir -p "${PWD}/bin"
23-
if ! command -v "${local_cmd}" &> /dev/null; then
24-
echo "${cmd} not found, attempting to install..."
25-
curl -L -o "${local_cmd}" "${install_uri}"
26-
# TODO(marun) Optionally validate the binary against published checksum
27-
chmod +x "${local_cmd}"
28-
fi
29-
fi
30-
}
31-
32-
# Ensure the kubectl command is available
33-
KUBECTL_VERSION=v1.30.2
34-
ensure_command kubectl "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/${GOOS}/${GOARCH}/kubectl"
35-
36-
# Ensure the kind command is available
37-
KIND_VERSION=v0.23.0
38-
ensure_command kind "https://kind.sigs.k8s.io/dl/${KIND_VERSION}/kind-${GOOS}-${GOARCH}"
39-
40-
# Ensure the kind-with-registry command is available
41-
ensure_command "kind-with-registry.sh" "https://raw.githubusercontent.com/kubernetes-sigs/kind/7cb9e6be25b48a0e248097eef29d496ab1a044d0/site/static/examples/kind-with-registry.sh"
14+
if ! command -v "${CMD}" &> /dev/null; then
15+
echo "kind-with-registry.sh not found, have you run 'nix develop'?"
16+
echo "To install nix: https://github.com/DeterminateSystems/nix-installer?tab=readme-ov-file#install-nix"
17+
exit 1
18+
fi
4219

43-
# Deploy a kind cluster with a local registry. Include the local bin in the path to
44-
# ensure locally installed kind and kubectl are available since the script expects to
45-
# call them without a qualifying path.
46-
PATH="${PWD}/bin:$PATH" bash -x "${PWD}/bin/kind-with-registry.sh"
20+
"${CMD}"
4721

48-
KUBECONFIG="$HOME/.kube/config" PATH="${PWD}/bin:$PATH" ./bin/ginkgo -v ./tests/fixture/bootstrapmonitor/e2e
22+
KUBECONFIG="$HOME/.kube/config" ./bin/ginkgo -v ./tests/fixture/bootstrapmonitor/e2e

0 commit comments

Comments
 (0)