Skip to content

Commit

Permalink
feat: add optional data dir to blueprint manager (#342)
Browse files Browse the repository at this point in the history
* feat: add optional data dir to blueprint manager

* chore: fix Clippy warning

* chore: fmt
  • Loading branch information
Serial-ATA authored Oct 9, 2024
1 parent a90f3d6 commit f2cf1b8
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 172 deletions.
4 changes: 2 additions & 2 deletions blueprint-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish = false

[[bin]]
name = "blueprint-manager"
path = "src/cli.rs"
path = "src/main.rs"

[dependencies]
sp-core = { workspace = true }
Expand All @@ -18,7 +18,7 @@ structopt = { workspace = true }
tangle-subxt = { workspace = true }
toml = { workspace = true }
hex = { workspace = true }
tokio = { workspace = true, features = ["process", "io-util", "signal"] }
tokio = { workspace = true, features = ["process", "io-util", "signal", "macros"] }
reqwest = { workspace = true }
sha2 = { workspace = true }
futures = { workspace = true }
Expand Down
47 changes: 0 additions & 47 deletions blueprint-manager/src/cli.rs

This file was deleted.

7 changes: 5 additions & 2 deletions blueprint-manager/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ use structopt::StructOpt;
)]
pub struct BlueprintManagerConfig {
/// The path to the gadget configuration file
#[structopt(parse(from_os_str), short = "s", long = "gadget-config")]
#[structopt(parse(from_os_str), short = "s", long)]
pub gadget_config: Option<PathBuf>,
/// The path to the keystore
#[structopt(short = "k", long = "keystore-uri")]
#[structopt(short = "k", long)]
pub keystore_uri: String,
/// The directory in which all gadgets will store their data
#[structopt(long, short = "d", parse(from_os_str))]
pub data_dir: Option<PathBuf>,
/// The verbosity level, can be used multiple times
#[structopt(long, short = "v", parse(from_occurrences))]
pub verbose: i32,
Expand Down
10 changes: 10 additions & 0 deletions blueprint-manager/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ pub async fn run_blueprint_manager<F: SendFuture<'static, ()>>(
let _span = span.enter();
info!("Starting blueprint manager ... waiting for start signal ...");

if let Some(data_dir) = &blueprint_manager_config.data_dir {
if !data_dir.exists() {
info!(
"Data directory does not exist, creating it at `{}`",
data_dir.display()
);
std::fs::create_dir_all(data_dir)?;
}
}

let (tangle_key, ecdsa_key) = {
let keystore = GenericKeyStore::<parking_lot::RawRwLock>::Fs(FilesystemKeystore::open(
&gadget_config.keystore_uri,
Expand Down
46 changes: 46 additions & 0 deletions blueprint-manager/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use blueprint_manager::config::BlueprintManagerConfig;
use blueprint_manager::run_blueprint_manager;
use blueprint_manager::sdk;
use blueprint_manager::sdk::utils::msg_to_error;
use gadget_io::GadgetConfig;
use sdk::entry;
use structopt::StructOpt;

#[tokio::main]
#[allow(clippy::needless_return)]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
let mut blueprint_manager_config = BlueprintManagerConfig::from_args();

if let Some(data_dir) = blueprint_manager_config.data_dir.as_mut() {
*data_dir = std::path::absolute(&data_dir)?;
}

entry::setup_blueprint_manager_logger(
blueprint_manager_config.verbose,
blueprint_manager_config.pretty,
"gadget",
)?;

let Some(gadget_config) = blueprint_manager_config.gadget_config.as_ref() else {
return Err(msg_to_error(
"Gadget config file is required when running the blueprint manager in CLI mode"
.to_string(),
));
};

let gadget_config_settings = std::fs::read_to_string(gadget_config)?;
let gadget_config: GadgetConfig =
toml::from_str(&gadget_config_settings).map_err(|err| msg_to_error(err.to_string()))?;

// Allow CTRL-C to shutdown this CLI application instance
let shutdown_signal = async move {
let _ = tokio::signal::ctrl_c().await;
};

let handle =
run_blueprint_manager(blueprint_manager_config, gadget_config, shutdown_signal).await?;
handle.await?;

Ok(())
}
48 changes: 0 additions & 48 deletions blueprint-manager/src/sdk/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::config::BlueprintManagerConfig;
use crate::protocols::resolver::NativeGithubMetadata;
use gadget_io::GadgetConfig;
use gadget_sdk::config::Protocol;
use gadget_sdk::{info, warn};
use sha2::Digest;
use std::path::Path;
Expand Down Expand Up @@ -38,51 +35,6 @@ pub fn bounded_string_to_string(string: BoundedString) -> Result<String, FromUtf
String::from_utf8(bytes.clone())
}

pub fn generate_process_arguments(
gadget_config: &GadgetConfig,
opt: &BlueprintManagerConfig,
blueprint_id: u64,
service_id: u64,
protocol: Protocol,
) -> color_eyre::Result<Vec<String>> {
let mut arguments = vec![];
arguments.push("run".to_string());

if opt.test_mode {
arguments.push("--test-mode".to_string());
}

for bootnode in &gadget_config.bootnodes {
arguments.push(format!("--bootnodes={}", bootnode));
}

arguments.extend([
format!("--bind-addr={}", gadget_config.bind_addr),
format!("--bind-port={}", gadget_config.bind_port),
format!("--url={}", gadget_config.url),
format!("--keystore-uri={}", gadget_config.keystore_uri),
format!("--chain={}", gadget_config.chain),
format!("--verbose={}", opt.verbose),
format!("--pretty={}", opt.pretty),
format!("--blueprint-id={}", blueprint_id),
format!("--service-id={}", service_id),
format!("--protocol={}", protocol),
format!(
"--log-id=Blueprint-{blueprint_id}-Service-{service_id}-{}",
opt.instance_id.clone().unwrap_or_else(|| format!(
"{}-{}",
gadget_config.bind_addr, gadget_config.bind_port
))
),
]);

if let Some(keystore_password) = &gadget_config.keystore_password {
arguments.push(format!("--keystore-password={}", keystore_password));
}

Ok(arguments)
}

pub fn hash_bytes_to_hex<T: AsRef<[u8]>>(input: T) -> String {
let mut hasher = sha2::Sha256::default();
hasher.update(input);
Expand Down
Loading

0 comments on commit f2cf1b8

Please sign in to comment.