Skip to content

Commit 3a72d7f

Browse files
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 <tiagolobocastro@gmail.com>
1 parent 6c66a33 commit 3a72d7f

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed

nix/overlay.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ self: super: rec {
1414
ms-buildenv = super.callPackage ./pkgs/ms-buildenv { };
1515
nvme-cli = super.callPackage ./pkgs/nvme-cli { };
1616
nvmet-cli = super.callPackage ./pkgs/nvmet-cli { };
17-
units = (super.callPackage ./pkgs/io-engine/units.nix { });
17+
units = (super.callPackage ./pkgs/io-engine/units.nix { inherit tag sourcer; });
1818
}

nix/pkgs/io-engine/cargo-package.nix

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ let
9595
};
9696
in
9797
{
98+
cargoDeps = rustPlatform.importCargoLock {
99+
lockFile = ../../../Cargo.lock;
100+
};
98101
release = rustPlatform.buildRustPackage (buildProps // {
99102
cargoBuildFlags = "--bin io-engine --bin io-engine-client --bin casperf";
100103
buildType = "release";

nix/pkgs/io-engine/units.nix

+5-22
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
11
{ stdenv
2-
, clang
3-
, dockerTools
4-
, e2fsprogs
52
, lib
6-
, libaio
7-
, libspdk
8-
, libspdk-dev
9-
, udev
10-
, liburing
11-
, makeRustPlatform
12-
, numactl
13-
, openssl
14-
, pkg-config
15-
, protobuf
16-
, sources
17-
, xfsprogs
18-
, utillinux
19-
, llvmPackages
20-
, targetPackages
21-
, buildPackages
22-
, targetPlatform
233
, pkgs
4+
, git
5+
, tag
246
, sourcer
257
}:
268
let
279
versionDrv = import ../../lib/version.nix { inherit lib stdenv git tag sourcer; };
2810
versions = {
29-
"version" = version;
11+
"version" = "${versionDrv}";
3012
"long" = builtins.readFile "${versionDrv.long}";
3113
"tag_or_long" = builtins.readFile "${versionDrv.tag_or_long}";
3214
};
33-
project-builder = { cargoBuildFlags }: pkgs.callPackage ./cargo-package.nix { inherit versions cargoBuildFlags; };
15+
project-builder = { cargoBuildFlags ? [ ] }: pkgs.callPackage ./cargo-package.nix { inherit versions cargoBuildFlags; };
3416
components = { build }: {
3517
io-engine = (project-builder { cargoBuildFlags = [ "--bin io-engine" ]; }).${build};
3618
io-engine-cli = (project-builder { cargoBuildFlags = [ "--bin io-engine-cli" ]; }).${build};
@@ -39,6 +21,7 @@ let
3921
};
4022
in
4123
{
24+
cargoDeps = (project-builder { }).cargoDeps;
4225
release = components { build = "release"; };
4326
debug = components { build = "debug"; };
4427
}

scripts/release.sh

+40-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,39 @@ nix_experimental() {
3030
echo -n " "
3131
fi
3232
}
33+
pre_fetch_cargo_deps() {
34+
local nixAttrPath=$1
35+
local project=$2
36+
local maxAttempt=$3
37+
38+
local outLink="--no-out-link"
39+
local cargoVendorMsg=""
40+
if [ -n "$CARGO_VENDOR_DIR" ]; then
41+
if [ "$(realpath -s "$CARGO_VENDOR_DIR")" = "$(realpath -s "$SCRIPTDIR/..")" ]; then
42+
cargoVendorDir="$CARGO_VENDOR_DIR/$GIT_BRANCH"
43+
else
44+
cargoVendorDir="$CARGO_VENDOR_DIR/$project/$GIT_BRANCH"
45+
fi
46+
47+
outLink="--out-link "$cargoVendorDir""
48+
cargoVendorMsg="into $(realpath -s "$cargoVendorDir") "
49+
fi
50+
51+
for (( attempt=1; attempt<=maxAttempt; attempt++ )); do
52+
if $NIX_BUILD $outLink -A "$nixAttrPath"; then
53+
echo "Cargo vendored dependencies pre-fetched "$cargoVendorMsg"after $attempt attempt(s)"
54+
return 0
55+
fi
56+
sleep 1
57+
done
58+
if [ "$attempt" = "1" ]; then
59+
echo "Cargo vendor pre-fetch is disabled"
60+
return 0
61+
fi
62+
63+
echo "Failed to pre-fetch the cargo vendored dependencies in $maxAttempt attempts"
64+
exit 1
65+
}
3366

3467
help() {
3568
cat <<EOF
@@ -59,8 +92,8 @@ RM="rm"
5992
SCRIPTDIR=$(dirname "$0")
6093
TAG=`get_tag`
6194
HASH=`get_hash`
62-
BRANCH=`git rev-parse --abbrev-ref HEAD`
63-
BRANCH=${BRANCH////-}
95+
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD`
96+
BRANCH=${GIT_BRANCH////-}
6497
IMAGES=
6598
UPLOAD=
6699
SKIP_PUBLISH=
@@ -69,6 +102,8 @@ REGISTRY=
69102
ALIAS=
70103
DEBUG=
71104
OVERRIDE_COMMIT_HASH=
105+
CARGO_VENDOR_DIR=${CARGO_VENDOR_DIR:-}
106+
CARGO_VENDOR_ATTEMPTS=${CARGO_VENDOR_ATTEMPTS:-25}
72107

73108
# Check if all needed tools are installed
74109
curl --version >/dev/null
@@ -140,6 +175,9 @@ done
140175

141176
cd $SCRIPTDIR/..
142177

178+
# pre-fetch build dependencies with a number of attempts to harden against flaky networks
179+
pre_fetch_cargo_deps units.cargoDeps "mayastor-io-engine" "$CARGO_VENDOR_ATTEMPTS"
180+
143181
if [ -z "$IMAGES" ]; then
144182
if [ -z "$DEBUG" ]; then
145183
IMAGES="mayastor-io-engine mayastor-fio-spdk mayastor-casperf"

0 commit comments

Comments
 (0)