Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit ca661af

Browse files
author
Adam Gleave
authored
Fetch dashboard release in build.rs (#1192)
* Fetch dashboard release in build script * Update CI and Dockerfile * Complete descriptions for BuildError variants * Make build deps optional * Fmt * Remove references to npm in documentation, update .dockerignore * Add better rerun conditions * Use reqwest::blocking::Client * Hardcode release URL/checksum * Fix build when FETCH_DASHBOARD does not exist * Update Cargo.toml * Use checksums crate * Roll back to sha2 crate, stream to hasher through std::io traits
1 parent 5a475dc commit ca661af

File tree

12 files changed

+193
-121
lines changed

12 files changed

+193
-121
lines changed

.dockerignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
target/
2-
3-
bee-node/src/plugins/dashboard/frontend/node_modules/
1+
target/

.github/workflows/ci.yml

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ jobs:
9595
${{ matrix.os }}-${{ matrix.rust }}-
9696
${{ matrix.os }}-
9797
98-
- name: Install dashboard frontend
99-
run: |
100-
git submodule init
101-
git submodule update --recursive --remote
102-
cd bee-node/src/plugins/dashboard/frontend/
103-
npm install
104-
npm run build-bee
105-
10698
- name: Build
10799
uses: actions-rs/cargo@v1
108100
with:
@@ -229,14 +221,6 @@ jobs:
229221
ubuntu-latest-beta-
230222
ubuntu-latest-
231223
232-
- name: Install dashboard frontend
233-
run: |
234-
git submodule init
235-
git submodule update --recursive --remote
236-
cd bee-node/src/plugins/dashboard/frontend/
237-
npm install
238-
npm run build-bee
239-
240224
- uses: actions-rs/clippy-check@v1
241225
with:
242226
token: ${{ secrets.GITHUB_TOKEN }}
@@ -270,14 +254,6 @@ jobs:
270254
ubuntu-latest-nightly-
271255
ubuntu-latest-
272256
273-
- name: Install dashboard frontend
274-
run: |
275-
git submodule init
276-
git submodule update --recursive --remote
277-
cd bee-node/src/plugins/dashboard/frontend/
278-
npm install
279-
npm run build-bee
280-
281257
- uses: actions-rs/cargo@v1
282258
with:
283259
command: install

.github/workflows/coverage.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ jobs:
3636
crate: rustfilt
3737
version: 0.2.1
3838

39-
- name: Install dashboard frontend
40-
run: |
41-
git submodule init
42-
git submodule update --recursive --remote
43-
cd bee-node/src/plugins/dashboard/frontend/
44-
npm install
45-
npm run build-bee
46-
4739
- name: Run test coverage
4840
run: bash .github/workflows/scripts/coverage.sh
4941

.gitmodules

Lines changed: 0 additions & 5 deletions
This file was deleted.

Cargo.lock

Lines changed: 68 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bee-node/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ tracing = { version = "0.1.29", default-features = false, optional = true }
5656
warp = { version = "0.3.1", default-features = false }
5757
warp-reverse-proxy = { version = "0.4.0", default-features = false, optional = true }
5858

59+
[build-dependencies]
60+
reqwest = { version = "0.11.5", default-features = false, features = [ "blocking", "default-tls", "json" ], optional = true }
61+
sha2 = { version = "0.9.6", default-features = false, optional = true }
62+
tempfile = { version = "3.3.0", default-features = false, optional = true }
63+
zip = { version = "0.5.13", default-features = false, features = [ "bzip2", "deflate" ], optional = true }
64+
5965
[lib]
6066
name = "bee_node"
6167
path = "src/lib.rs"
@@ -67,6 +73,6 @@ path = "src/main.rs"
6773
[features]
6874
default = [ "rocksdb" ]
6975

70-
dashboard = [ "cap", "mime_guess", "rust-embed", "serde_repr", "warp-reverse-proxy" ]
76+
dashboard = [ "cap", "mime_guess", "reqwest", "rust-embed", "serde_repr", "sha2", "tempfile", "warp-reverse-proxy", "zip" ]
7177
rocksdb = [ "bee-storage-rocksdb" ]
7278
sled = [ "bee-storage-sled" ]

bee-node/README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
```sh
1010
apt-get update
1111
apt-get upgrade
12-
apt-get install git npm build-essential cmake pkg-config librocksdb-dev llvm clang libclang-dev libssl-dev
12+
apt-get install git build-essential cmake pkg-config librocksdb-dev llvm clang libclang-dev libssl-dev
1313
```
1414

1515
### MacOS
1616

1717
```sh
1818
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
19-
brew install cmake npm
19+
brew install cmake
2020
```
2121

2222
### Windows
2323

2424
Open Powershell and execute the following commands:
2525
```sh
2626
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
27-
choco install git --params '/NoAutoCrlf' nodejs-lts cmake --installargs 'ADD_CMAKE_TO_PATH=System' llvm
27+
choco install git --params '/NoAutoCrlf' cmake --installargs 'ADD_CMAKE_TO_PATH=System' llvm
2828
```
2929
Restart Powershell
3030

@@ -59,11 +59,6 @@ cd bee/bee-node
5959
With dashboard
6060

6161
```sh
62-
git submodule update --init
63-
cd src/plugins/dashboard/frontend
64-
npm install
65-
npm run build-bee
66-
cd ../../../../
6762
cargo build --release --features dashboard
6863
```
6964

bee-node/build.rs

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,68 @@
1-
// Copyright 2020-2021 IOTA Stiftung
1+
// Copyright 2020-2022 IOTA Stiftung
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::process::Command;
4+
use std::{fmt, process::Command};
55

6+
#[allow(dead_code)]
67
#[derive(Debug)]
78
enum BuildError {
8-
GitCommit,
9+
DashboardDownload,
10+
DashboardInvalidArchive,
11+
DashboardInvalidChecksum,
12+
DashboardRequest(Option<u16>, String),
913
GitBranch,
14+
GitCommit,
15+
}
16+
17+
impl fmt::Display for BuildError {
18+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19+
match &self {
20+
Self::DashboardDownload => write!(f, "failed to download latest release archive"),
21+
Self::DashboardInvalidArchive => write!(f, "failed to open or extract release archive"),
22+
Self::DashboardInvalidChecksum => write!(
23+
f,
24+
"checksum of downloaded archive did not match that specified in the release"
25+
),
26+
Self::DashboardRequest(Some(code), url) => write!(f, "failed request to `{}`: status code {}", url, code),
27+
Self::DashboardRequest(_, url) => write!(f, "failed request to `{}`", url),
28+
Self::GitBranch => write!(f, "failed to retrieve git branch name"),
29+
Self::GitCommit => write!(f, "failed to retrieve git commit"),
30+
}
31+
}
1032
}
1133

1234
fn main() -> Result<(), BuildError> {
35+
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_DASHBOARD");
36+
37+
parse_git_information()?;
38+
39+
#[cfg(feature = "dashboard")]
40+
{
41+
let dashboard_dir = std::env::var("OUT_DIR").unwrap() + "/dashboard";
42+
println!("cargo:rustc-env=DASHBOARD_DIR={}", dashboard_dir);
43+
let dashboard_dir = std::path::Path::new(&dashboard_dir);
44+
45+
// Rebuild if FETCH_DASHBOARD environment variable has changed.
46+
println!("cargo:rerun-if-env-changed=FETCH_DASHBOARD");
47+
// Rebuild if DASHBOARD_DIR has changed to a different path.
48+
println!("cargo:rerun-if-env-changed=DASHBOARD_DIR");
49+
50+
let should_fetch = std::env::var("FETCH_DASHBOARD").map(|val| val == "1").unwrap_or(false);
51+
52+
if should_fetch || !dashboard_dir.exists() {
53+
if dashboard_dir.exists() {
54+
// If the path already exists, we are re-downloading: remove the old files.
55+
std::fs::remove_dir_all(dashboard_dir).expect("could not remove existing dashboard");
56+
}
57+
58+
dashboard::fetch(dashboard_dir)?;
59+
}
60+
}
61+
62+
Ok(())
63+
}
64+
65+
fn parse_git_information() -> Result<(), BuildError> {
1366
match Command::new("git").args(&["rev-parse", "HEAD"]).output() {
1467
Ok(output) => {
1568
println!(
@@ -36,3 +89,57 @@ fn main() -> Result<(), BuildError> {
3689

3790
Ok(())
3891
}
92+
93+
#[cfg(feature = "dashboard")]
94+
mod dashboard {
95+
use super::*;
96+
97+
use sha2::{Digest, Sha256};
98+
use zip::ZipArchive;
99+
100+
use std::{io, path::Path};
101+
102+
const RELEASE_URL: &str =
103+
"https://github.com/iotaledger/node-dashboard/releases/download/v1.0.0/node-dashboard-bee-1.0.0.zip";
104+
const RELEASE_CHECKSUM: &str = "d61bce7d70b51ea88536b22c5341ccdc3c4651bf8e02e093a771312f3f046c30";
105+
106+
pub(super) fn fetch<P: AsRef<Path>>(dashboard_dir: P) -> Result<(), BuildError> {
107+
println!("downloading latest dashboard release from {}", RELEASE_URL);
108+
109+
let client = reqwest::blocking::Client::builder()
110+
.user_agent("bee-fetch-dashboard")
111+
.build()
112+
.expect("could not create client");
113+
114+
let mut tmp_file = tempfile::NamedTempFile::new().expect("could not create temp file");
115+
116+
client
117+
.get(RELEASE_URL)
118+
.send()
119+
.and_then(|resp| resp.error_for_status())
120+
.map_err(|e| BuildError::DashboardRequest(e.status().map(|code| code.as_u16()), RELEASE_URL.to_string()))?
121+
.copy_to(&mut tmp_file)
122+
.expect("copying failed");
123+
124+
let mut hasher = Sha256::new();
125+
io::copy(&mut tmp_file.reopen().expect("could not open temp file"), &mut hasher).expect("io error");
126+
let checksum = format!("{:x}", hasher.finalize());
127+
128+
println!("checksum: {}\noriginal: {}", checksum, RELEASE_CHECKSUM);
129+
130+
if checksum != RELEASE_CHECKSUM {
131+
return Err(BuildError::DashboardInvalidChecksum);
132+
}
133+
134+
println!("checksum ok");
135+
136+
let mut archive = ZipArchive::new(tmp_file).map_err(|_| BuildError::DashboardInvalidArchive)?;
137+
138+
println!("extracting release archive to {}", dashboard_dir.as_ref().display());
139+
archive
140+
.extract(dashboard_dir)
141+
.map_err(|_| BuildError::DashboardInvalidArchive)?;
142+
143+
Ok(())
144+
}
145+
}

0 commit comments

Comments
 (0)