From 3a72d7fc9a29a60d753ca6f5660747fbe1798ea2 Mon Sep 17 00:00:00 2001 From: Tiago Castro Date: Fri, 12 Apr 2024 10:37:07 +0100 Subject: [PATCH] build: harden build by pre-fetching dependencies Attempt to vendor the cargo deps for up to 25 times Supports linking the resulting derivation into a global location through env var CARGO_VENDOR_DIR, example: > CARGO_VENDOR_DIR=/tmp ./scripts/release.sh --image "" /nix/store/2x03mh7l1q0jx4xfsd3nw4h4ks0pxxya-cargo-vendor-dir Cargo vendored dependencies pre-fetched into /tmp/mayastor-io-engine/develop after 1 attempt(s) This can allow us to create a place holder in the jenkins nodes to keep the last dependencies for each main branch of each repo. Signed-off-by: Tiago Castro --- nix/overlay.nix | 2 +- nix/pkgs/io-engine/cargo-package.nix | 3 ++ nix/pkgs/io-engine/units.nix | 27 ++++-------------- scripts/release.sh | 42 ++++++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/nix/overlay.nix b/nix/overlay.nix index 8a92fea39..8ceec06f3 100644 --- a/nix/overlay.nix +++ b/nix/overlay.nix @@ -14,5 +14,5 @@ self: super: rec { ms-buildenv = super.callPackage ./pkgs/ms-buildenv { }; nvme-cli = super.callPackage ./pkgs/nvme-cli { }; nvmet-cli = super.callPackage ./pkgs/nvmet-cli { }; - units = (super.callPackage ./pkgs/io-engine/units.nix { }); + units = (super.callPackage ./pkgs/io-engine/units.nix { inherit tag sourcer; }); } diff --git a/nix/pkgs/io-engine/cargo-package.nix b/nix/pkgs/io-engine/cargo-package.nix index 0540b1af9..bf03e2518 100644 --- a/nix/pkgs/io-engine/cargo-package.nix +++ b/nix/pkgs/io-engine/cargo-package.nix @@ -95,6 +95,9 @@ let }; in { + cargoDeps = rustPlatform.importCargoLock { + lockFile = ../../../Cargo.lock; + }; release = rustPlatform.buildRustPackage (buildProps // { cargoBuildFlags = "--bin io-engine --bin io-engine-client --bin casperf"; buildType = "release"; diff --git a/nix/pkgs/io-engine/units.nix b/nix/pkgs/io-engine/units.nix index 4ee573624..09ca550aa 100644 --- a/nix/pkgs/io-engine/units.nix +++ b/nix/pkgs/io-engine/units.nix @@ -1,36 +1,18 @@ { stdenv -, clang -, dockerTools -, e2fsprogs , lib -, libaio -, libspdk -, libspdk-dev -, udev -, liburing -, makeRustPlatform -, numactl -, openssl -, pkg-config -, protobuf -, sources -, xfsprogs -, utillinux -, llvmPackages -, targetPackages -, buildPackages -, targetPlatform , pkgs +, git +, tag , sourcer }: let versionDrv = import ../../lib/version.nix { inherit lib stdenv git tag sourcer; }; versions = { - "version" = version; + "version" = "${versionDrv}"; "long" = builtins.readFile "${versionDrv.long}"; "tag_or_long" = builtins.readFile "${versionDrv.tag_or_long}"; }; - project-builder = { cargoBuildFlags }: pkgs.callPackage ./cargo-package.nix { inherit versions cargoBuildFlags; }; + project-builder = { cargoBuildFlags ? [ ] }: pkgs.callPackage ./cargo-package.nix { inherit versions cargoBuildFlags; }; components = { build }: { io-engine = (project-builder { cargoBuildFlags = [ "--bin io-engine" ]; }).${build}; io-engine-cli = (project-builder { cargoBuildFlags = [ "--bin io-engine-cli" ]; }).${build}; @@ -39,6 +21,7 @@ let }; in { + cargoDeps = (project-builder { }).cargoDeps; release = components { build = "release"; }; debug = components { build = "debug"; }; } diff --git a/scripts/release.sh b/scripts/release.sh index fa575db70..ad66753ac 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -30,6 +30,39 @@ nix_experimental() { echo -n " " fi } +pre_fetch_cargo_deps() { + local nixAttrPath=$1 + local project=$2 + local maxAttempt=$3 + + local outLink="--no-out-link" + local cargoVendorMsg="" + if [ -n "$CARGO_VENDOR_DIR" ]; then + if [ "$(realpath -s "$CARGO_VENDOR_DIR")" = "$(realpath -s "$SCRIPTDIR/..")" ]; then + cargoVendorDir="$CARGO_VENDOR_DIR/$GIT_BRANCH" + else + cargoVendorDir="$CARGO_VENDOR_DIR/$project/$GIT_BRANCH" + fi + + outLink="--out-link "$cargoVendorDir"" + cargoVendorMsg="into $(realpath -s "$cargoVendorDir") " + fi + + for (( attempt=1; attempt<=maxAttempt; attempt++ )); do + if $NIX_BUILD $outLink -A "$nixAttrPath"; then + echo "Cargo vendored dependencies pre-fetched "$cargoVendorMsg"after $attempt attempt(s)" + return 0 + fi + sleep 1 + done + if [ "$attempt" = "1" ]; then + echo "Cargo vendor pre-fetch is disabled" + return 0 + fi + + echo "Failed to pre-fetch the cargo vendored dependencies in $maxAttempt attempts" + exit 1 +} help() { cat </dev/null @@ -140,6 +175,9 @@ done cd $SCRIPTDIR/.. +# pre-fetch build dependencies with a number of attempts to harden against flaky networks +pre_fetch_cargo_deps units.cargoDeps "mayastor-io-engine" "$CARGO_VENDOR_ATTEMPTS" + if [ -z "$IMAGES" ]; then if [ -z "$DEBUG" ]; then IMAGES="mayastor-io-engine mayastor-fio-spdk mayastor-casperf"