Skip to content

Commit

Permalink
Merge pull request #5 from karnotxyz/explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvsadana authored Jan 4, 2024
2 parents abe0cf8 + cb35166 commit 3b23597
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 0 deletions.
162 changes: 162 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
6 changes: 6 additions & 0 deletions src/cli/explorer.rs
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 2 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ pub mod prompt;
pub mod list;

pub mod run;

pub mod explorer;
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ enum Commands {
List,
/// Runs the App Chain using Madara
Run,
/// Runs the L2 explorer
Explorer,
}

fn main() {
Expand All @@ -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"),
}
}
55 changes: 55 additions & 0 deletions src/utils/docker.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error + 'static>> {
let docker = Docker::connect_with_local_defaults().unwrap();

docker
.create_image(Some(CreateImageOptions { from_image: image, ..Default::default() }), None, None)
.try_collect::<Vec<_>>()
.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::<String>(&id, None).await?;

Ok(())
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pub mod errors;
pub mod toml;

pub mod constants;

pub mod docker;

0 comments on commit 3b23597

Please sign in to comment.