Skip to content

Commit

Permalink
docker fixes for explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvsadana committed Jan 11, 2024
1 parent fc1f456 commit 5e32a8e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
17 changes: 13 additions & 4 deletions src/cli/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use rand::Rng;

use std::collections::HashMap;

use crate::utils::docker::run_docker_image;
use crate::utils::docker::{container_exists, kill_container, run_docker_image};

pub fn explorer() {
pub async fn explorer() {
let random_string: String = (0..64).map(|_| rand::thread_rng().sample(Alphanumeric).to_string()).collect();
let secret_key_base = format!("SECRET_KEY_BASE={}", random_string);

Expand All @@ -29,10 +29,19 @@ pub fn explorer() {

let host_config = HostConfig { port_bindings: Some(port_bindings), ..Default::default() };

const CONTAINER_NAME: &str = "madara-explorer";

if container_exists(CONTAINER_NAME).await {
// TODO: handle error
let _ = kill_container(CONTAINER_NAME).await;
}

run_docker_image(
"ghcr.io/lambdaclass/stark_compass_explorer:v0.2.34.2",
"madara-explorer",
CONTAINER_NAME,
Some(env),
Some(host_config),
);
)
.await;
log::info!("🧭 Explorer is running on http://localhost:4000");
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn main() {
Some(Commands::Init) => cli::init::init(),
Some(Commands::List) => cli::list::list(),
Some(Commands::Run) => cli::run::run().await,
Some(Commands::Explorer) => cli::explorer::explorer(),
Some(Commands::Explorer) => cli::explorer::explorer().await,
None => log::info!("Use --help to see the complete list of available commands"),
}
}
59 changes: 48 additions & 11 deletions src/utils/docker.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
use bollard::container::{Config, CreateContainerOptions};
use bollard::container::{Config, CreateContainerOptions, ListContainersOptions};
use bollard::image::CreateImageOptions;
use bollard::models::HostConfig;
use bollard::Docker;
use futures_util::TryStreamExt;

pub fn run_docker_image(image: &str, container_name: &str, env: Option<Vec<&str>>, host_config: Option<HostConfig>) {
is_docker_installed();
match pull_and_start_docker_image(image, container_name, env, host_config) {
// TODO: fix unwraps and handle errors

pub async fn run_docker_image(
image: &str,
container_name: &str,
env: Option<Vec<&str>>,
host_config: Option<HostConfig>,
) {
is_docker_installed().await;
log::info!("🐳 Running docker image: {}", image);
match pull_and_start_docker_image(image, container_name, env, host_config).await {
Ok(..) => {
log::info!("Successfully ran {}", container_name);
log::debug!("Successfully ran {}", container_name);
}
Err(err) => {
log::error!("Error: {} running image: {}", err, image);
}
};
}

#[tokio::main]
async fn is_docker_installed() -> bool {
async fn is_docker_installed() {
let docker = Docker::connect_with_local_defaults().unwrap();
return match docker.version().await {
match docker.version().await {
Ok(_) => {
log::info!("Docker running!");
log::debug!("Docker is installed!");
true
}
Err(_) => {
Expand All @@ -30,8 +37,38 @@ async fn is_docker_installed() -> bool {
};
}

#[tokio::main]
async fn pull_and_start_docker_image(
pub async fn container_exists(container_name: &str) -> bool {
let docker = Docker::connect_with_local_defaults().unwrap();
let mut list_container_options = ListContainersOptions::default();
list_container_options.all = true;
match docker.list_containers::<String>(Some(list_container_options)).await {
Ok(containers) => {
for container in containers {
if let Some(names) = container.names {
if names.contains(&format!("/{}", &container_name.to_string())) {
log::debug!("✅ Container {} exists!", container_name);
return true;
}
}
}
log::debug!("❌ Container {} does not exist!", container_name);
false
}
Err(_) => {
panic!("Failed to fetch containers, panicking");
}
}
}

pub async fn kill_container(container_name: &str) -> eyre::Result<()> {
let docker = Docker::connect_with_local_defaults().unwrap();
// TODO: handle the error
let _ = docker.kill_container::<String>(container_name, None).await;
docker.remove_container(container_name, None).await?;
Ok(())
}

pub async fn pull_and_start_docker_image(
image: &str,
container_name: &str,
env: Option<Vec<&str>>,
Expand Down

0 comments on commit 5e32a8e

Please sign in to comment.