Skip to content

Commit

Permalink
Fetch configs in configuration package build script (#173)
Browse files Browse the repository at this point in the history
* feature: build.rs fetches json from github pages

* feature: parallelize reqs in build.rs

* fix: handle failed http reqwests

* chore: changelog

* lint: remove unused import

* feature: config fetching on NomadConfig type

* refactor: make build.rs cleaner :)

* feature: re-run if configs are changed

* drive-by: clippy lints in tests

* feature: fetch from any URL

* fix: rename and relock

* drive-by: clippy lints in tests
  • Loading branch information
prestwich authored Jul 1, 2022
1 parent 27dea0b commit b2206e2
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 2,468 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion agents/relayer/src/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ mod test {
<Relayer as nomad_base::NomadAgent>::run(agent.build_channel("moonbeam"))
.await
.expect("Couldn't join relayer's run task");

assert!(run_result.is_err(), "Must have returned error");

let run_report_error_task = agent
Expand Down
1 change: 1 addition & 0 deletions configuration/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Unreleased

- feature: pull config files from github pages instead of local versions
- add environment variable overrides for agent configuration
- add tests for agent environment variable overrides
- remove `enabled` flag from agents project-wide
Expand Down
6 changes: 6 additions & 0 deletions configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ crate-type = ["cdylib", "rlib"]

[build-dependencies]
eyre = "0.6.6"
reqwest = "0.11.10"
tokio = { version = "1.18.2", features = ["rt", "macros"] }

[dependencies]
affix = "0.1.2"
Expand All @@ -29,6 +31,10 @@ serde_json = "1.0.78"
serde_yaml = "0.8.23"
nomad-types = { path = "../nomad-types" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest = "0.11.10"


[target.'cfg(target_arch = "wasm32")'.dependencies]
wee_alloc = "0.4.5"
js-sys = "0.3.56"
Expand Down
94 changes: 84 additions & 10 deletions configuration/build.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,66 @@
use std::io::Write;
use std::{fs, io::Write, path::PathBuf};

const DEFINITIONS: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/data/definitions.ts"));
const TYPEDEFS: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/data/types.rs"));

const OUTPUT_FILE: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/src/wasm/types.rs");

fn main() -> eyre::Result<()> {
println!(
"cargo:rerun-if-changed={}",
concat!(env!("CARGO_MANIFEST_DIR"), "/data/definitions.ts")
);
println!(
"cargo:rerun-if-changed={}",
concat!(env!("CARGO_MANIFEST_DIR"), "/data/types.rs")
const CONFIG_BASE_URI: &str = "https://nomad-xyz.github.io/config";
const ENVS: &[&str] = &["development", "staging", "production"];
const CONFIG_BASE_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/configs");

fn env_json(env: &str) -> String {
format!("{}.json", env)
}

fn config_uri(env: &str) -> String {
format!("{}/{}", CONFIG_BASE_URI, env_json(env))
}

fn config_path(env: &str) -> PathBuf {
PathBuf::from(CONFIG_BASE_DIR).join(env_json(env))
}

async fn fetch_config(env: &str) -> eyre::Result<String> {
let uri = config_uri(env);
Ok(reqwest::get(uri).await?.text().await?)
}

fn store_config(env: &str, contents: &str) -> eyre::Result<()> {
let mut f = fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(config_path(env))
.unwrap();

f.write_all(contents.as_ref())?;
Ok(())
}

async fn get_configs() -> eyre::Result<()> {
let (first, second, third) = tokio::join!(
fetch_config(ENVS[0]),
fetch_config(ENVS[1]),
fetch_config(ENVS[2]),
);

let mut f = std::fs::OpenOptions::new()
// We do it this way so that if fetches fail we update all possible
// However, if disk access is erroring, we error
if let Ok(first) = first {
store_config(ENVS[0], &first)?
}
if let Ok(second) = second {
store_config(ENVS[1], &second)?
}
if let Ok(third) = third {
store_config(ENVS[2], &third)?;
}
Ok(())
}

fn gen_wasm_bindgen() -> eyre::Result<()> {
let mut f = fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(OUTPUT_FILE)
Expand All @@ -40,3 +85,32 @@ const _: &'static str = r#""###

Ok(())
}

#[tokio::main]
async fn main() -> eyre::Result<()> {
println!(
"cargo:rerun-if-changed={}",
concat!(env!("CARGO_MANIFEST_DIR"), "/data/definitions.ts")
);
println!(
"cargo:rerun-if-changed={}",
concat!(env!("CARGO_MANIFEST_DIR"), "/data/types.rs")
);
println!(
"cargo:rerun-if-changed={}",
concat!(env!("CARGO_MANIFEST_DIR"), "/configs/production.json")
);
println!(
"cargo:rerun-if-changed={}",
concat!(env!("CARGO_MANIFEST_DIR"), "/configs/development.json")
);
println!(
"cargo:rerun-if-changed={}",
concat!(env!("CARGO_MANIFEST_DIR"), "/configs/staging.json")
);

gen_wasm_bindgen()?;
get_configs().await?;

Ok(())
}
2 changes: 2 additions & 0 deletions configuration/configs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.json
!test.json
Loading

0 comments on commit b2206e2

Please sign in to comment.