diff --git a/Cargo.lock b/Cargo.lock index 23c30bd..2921d42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -443,6 +443,46 @@ dependencies = [ "byte-tools", ] +[[package]] +name = "bollard" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f03db470b3c0213c47e978da93200259a1eb4dae2e5512cba9955e2b540a6fc6" +dependencies = [ + "base64 0.21.5", + "bollard-stubs", + "bytes", + "futures-core", + "futures-util", + "hex", + "http", + "hyper", + "hyperlocal", + "log", + "pin-project-lite", + "serde", + "serde_derive", + "serde_json", + "serde_repr", + "serde_urlencoded", + "thiserror", + "tokio", + "tokio-util", + "url", + "winapi", +] + +[[package]] +name = "bollard-stubs" +version = "1.43.0-rc.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b58071e8fd9ec1e930efd28e3a90c1251015872a2ce49f81f36421b86466932e" +dependencies = [ + "serde", + "serde_repr", + "serde_with", +] + [[package]] name = "bounded-collections" version = "0.1.9" @@ -519,6 +559,7 @@ dependencies = [ "android-tzdata", "iana-time-zone", "num-traits", + "serde", "windows-targets 0.48.5", ] @@ -709,6 +750,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", + "serde", +] + [[package]] name = "derivative" version = "2.2.0" @@ -1286,6 +1337,19 @@ dependencies = [ "tokio-native-tls", ] +[[package]] +name = "hyperlocal" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fafdf7b2b2de7c9784f76e02c0935e65a8117ec3b768644379983ab333ac98c" +dependencies = [ + "futures-util", + "hex", + "hyper", + "pin-project", + "tokio", +] + [[package]] name = "iana-time-zone" version = "0.1.59" @@ -1367,6 +1431,7 @@ checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" dependencies = [ "equivalent", "hashbrown 0.14.3", + "serde", ] [[package]] @@ -1605,9 +1670,11 @@ dependencies = [ name = "madara-cli" version = "0.1.0" dependencies = [ + "bollard", "clap", "dirs", "env_logger", + "futures-util", "git2", "hex", "inquire", @@ -1619,6 +1686,7 @@ dependencies = [ "strum", "strum_macros", "thiserror", + "tokio", "toml", ] @@ -1931,6 +1999,26 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "pin-project" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.46", +] + [[package]] name = "pin-project-lite" version = "0.2.13" @@ -1949,6 +2037,12 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -2419,6 +2513,17 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_repr" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b2e6b945e9d3df726b65d6ee24060aff8e3533d431f677a9695db04eff9dfdb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.46", +] + [[package]] name = "serde_spanned" version = "0.6.5" @@ -2440,6 +2545,22 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64cd236ccc1b7a29e7e2739f27c0b2dd199804abc4290e32f59f3b68d6405c23" +dependencies = [ + "base64 0.21.5", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.1.0", + "serde", + "serde_json", + "time", +] + [[package]] name = "sha2" version = "0.8.2" @@ -2894,6 +3015,35 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f657ba42c3f86e7680e53c8cd3af8abbe56b5491790b46e22e19c0d57463583e" +dependencies = [ + "deranged", + "itoa", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26197e33420244aeb70c3e8c78376ca46571bc4e701e4791c2cd9f57dcb3a43f" +dependencies = [ + "time-core", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -2922,9 +3072,21 @@ dependencies = [ "num_cpus", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys 0.48.0", ] +[[package]] +name = "tokio-macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.46", +] + [[package]] name = "tokio-native-tls" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index a2ae3a8..58f8117 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,9 +10,11 @@ path = "src/main.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +bollard = "0.15.0" clap = { version = "4.4.11", features = ["derive"] } dirs = "5.0.1" env_logger = "0.10.1" +futures-util = "0.3.30" git2 = "0.18.1" hex = { version = "0.4.3", features = [] } inquire = "0.6.2" @@ -24,4 +26,5 @@ sp-core = "27.0.0" strum = { version = "0.25.0", features = ["derive"] } strum_macros = { version = "0.25.3", features = [] } thiserror = "1.0.52" +tokio = { version = "1.35.1", features = ["rt", "rt-multi-thread", "macros"] } toml = "0.8.8" diff --git a/src/cli/explorer.rs b/src/cli/explorer.rs new file mode 100644 index 0000000..01fda4b --- /dev/null +++ b/src/cli/explorer.rs @@ -0,0 +1,6 @@ +use crate::utils::docker::run_docker_image; + +pub fn explorer() { + let env_vars = vec!["RPC_API_HOST=\"http://127.0.0.1:9944\""]; + run_docker_image("ghcr.io/lambdaclass/stark_compass_explorer:v0.2.34.2", "madara-explorer", env_vars); +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 2ba0144..48a38f2 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -5,3 +5,5 @@ pub mod prompt; pub mod list; pub mod run; + +pub mod explorer; diff --git a/src/main.rs b/src/main.rs index 63388b2..c458311 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,8 @@ enum Commands { List, /// Runs the App Chain using Madara Run, + /// Runs the L2 explorer + Explorer, } fn main() { @@ -28,6 +30,7 @@ fn main() { Some(Commands::Init) => cli::init::init(), Some(Commands::List) => cli::list::list(), Some(Commands::Run) => cli::run::run(), + Some(Commands::Explorer) => cli::explorer::explorer(), None => log::info!("Use --help to see the complete list of available commands"), } } diff --git a/src/utils/docker.rs b/src/utils/docker.rs new file mode 100644 index 0000000..ff4b096 --- /dev/null +++ b/src/utils/docker.rs @@ -0,0 +1,55 @@ +use bollard::container::{Config, CreateContainerOptions}; +use bollard::Docker; + +use bollard::image::CreateImageOptions; +use futures_util::TryStreamExt; + +pub fn run_docker_image(image: &str, container_name: &str, env_vars: Vec<&str>) { + is_docker_installed(); + match pull_and_start_docker_image(image, container_name, env_vars) { + Ok(..) => { + log::info!("Successfully ran {}", container_name); + } + Err(err) => { + log::error!("Error: {} running image: {}", err, image); + } + }; +} + +#[tokio::main] +async fn is_docker_installed() -> bool { + let docker = Docker::connect_with_local_defaults().unwrap(); + return match docker.version().await { + Ok(_) => { + log::info!("Docker running!"); + true + } + Err(_) => { + panic!("Please check docker installation, panicking"); + } + }; +} + +#[tokio::main] +async fn pull_and_start_docker_image( + image: &str, + container_name: &str, + env_vars: Vec<&str>, +) -> Result<(), Box> { + let docker = Docker::connect_with_local_defaults().unwrap(); + + docker + .create_image(Some(CreateImageOptions { from_image: image, ..Default::default() }), None, None) + .try_collect::>() + .await?; + + let config = Config { image: Some(image), tty: Some(true), env: Some(env_vars), ..Default::default() }; + + let container_option = Some(CreateContainerOptions { name: container_name, ..Default::default() }); + + let id = docker.create_container::<&str, &str>(container_option, config).await?.id; + + docker.start_container::(&id, None).await?; + + Ok(()) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 341d72e..ca30982 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -11,3 +11,5 @@ pub mod errors; pub mod toml; pub mod constants; + +pub mod docker;