Skip to content

Commit

Permalink
add: env vars for explorer, branch based pull (#6)
Browse files Browse the repository at this point in the history
* add: env vars for explorer, branch based pull
  • Loading branch information
anshalshukla authored Jan 7, 2024
1 parent 3b23597 commit 2dd4694
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 51 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ git2 = "0.18.1"
hex = { version = "0.4.3", features = [] }
inquire = "0.6.2"
log = "0.4.20"
rand = "0.8.5"
reqwest = { version = "0.11.23", features = ["json", "blocking"] }
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.109"
Expand Down
9 changes: 0 additions & 9 deletions keypair.json

This file was deleted.

4 changes: 0 additions & 4 deletions src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ use crate::da::da_layers::DALayer;
pub struct AppChainConfig {
pub app_chain: String,
pub base_path: String,
pub chain_id: String,
pub mode: RollupMode,
pub da_layer: DALayer,
pub block_time: u64,
pub disable_fees: bool,
pub fee_token: String,
/// Stores commit hash of madara app chain build
pub madara_version: String,
/// Maintains version of config, will help in handling edge
Expand Down
36 changes: 34 additions & 2 deletions src/cli/explorer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
use bollard::models::{HostConfig, PortBinding};
use rand::distributions::Alphanumeric;
use rand::Rng;

use std::collections::HashMap;

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);
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);

let env = vec![
"RPC_API_HOST=http://host.docker.internal:9944",
"DB_TYPE=sqlite",
"DISABLE_MAINNET_SYNC=false",
"DISABLE_TESTNET_SYNC=true",
"TESTNET_RPC_API_HOST=http://host.docker.internal:9944",
"DATABASE_PATH=/use/exp.db",
"PHX_HOST=localhost",
&secret_key_base,
];

let mut port_bindings = HashMap::new();
port_bindings.insert(
"4000/tcp".to_string(),
Some(vec![PortBinding { host_ip: Some("0.0.0.0".to_string()), host_port: Some("4000".to_string()) }]),
);

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

run_docker_image(
"ghcr.io/lambdaclass/stark_compass_explorer:v0.2.34.2",
"madara-explorer",
Some(env),
Some(host_config),
);
}
20 changes: 2 additions & 18 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use inquire::InquireError;
use strum::IntoEnumIterator;
use thiserror::Error;

use super::prompt::{get_boolean_input, get_custom_input, get_option, get_text_input};
use super::prompt::{get_option, get_text_input};
use crate::app::config::{AppChainConfig, ConfigVersion, RollupMode};
use crate::da::da_layers::{DAFactory, DALayer};
use crate::utils::constants::{APP_CONFIG_NAME, MADARA_REPO_NAME, MADARA_REPO_ORG};
Expand Down Expand Up @@ -51,13 +51,8 @@ fn generate_config() -> Result<AppChainConfig, InitError> {
let default_base_path = binding.to_str().unwrap_or("madara-data");

let base_path = get_text_input("Enter base path for data directory of your app chain:", Some(default_base_path))?;
let chain_id = get_text_input("Enter chain id for your app chain:", Some("MADARA"))?;
let mode = get_option("Select mode for your app chain:", RollupMode::iter().collect::<Vec<_>>())?;
let da_layer = get_option("Select DA layer for your app chain:", DALayer::iter().collect::<Vec<_>>())?;
let block_time =
get_custom_input::<u64>("Enter block time of chain:", Some(1000), Some("Time in ms (e.g, 1000, 2000)."))?;
let disable_fees = get_boolean_input("Do you want to disable fees for your app chain:", Some(false))?;
let fee_token = get_text_input("Enter fee token:", Some("STRK"))?;
let madara_version = get_latest_commit_hash(MADARA_REPO_ORG, MADARA_REPO_NAME)?;
let config_version = ConfigVersion::Version1;

Expand All @@ -69,18 +64,7 @@ fn generate_config() -> Result<AppChainConfig, InitError> {
}
};

Ok(AppChainConfig {
app_chain,
base_path,
chain_id,
mode,
da_layer,
block_time,
disable_fees,
fee_token,
madara_version,
config_version,
})
Ok(AppChainConfig { app_chain, base_path, mode, da_layer, madara_version, config_version })
}

fn write_config(config: &AppChainConfig) -> Result<(), InitError> {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
pub const MADARA_REPO_ORG: &str = "keep-starknet-strange";
pub const MADARA_REPO_NAME: &str = "madara";

pub const KARNOT_REPO_ORG: &str = "karnotxyz";

pub const BRANCH_NAME: &str = "custom_events_page";

pub const APP_CONFIG_NAME: &str = "config.toml";
pub const APP_DA_CONFIG_NAME: &str = "da-config.json";
pub const APP_SECRET_PHRASE: &str = "secret-phrase.txt";
13 changes: 7 additions & 6 deletions src/utils/docker.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use bollard::container::{Config, CreateContainerOptions};
use bollard::Docker;

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_vars: Vec<&str>) {
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_vars) {
match pull_and_start_docker_image(image, container_name, env, host_config) {
Ok(..) => {
log::info!("Successfully ran {}", container_name);
}
Expand Down Expand Up @@ -34,7 +34,8 @@ async fn is_docker_installed() -> bool {
async fn pull_and_start_docker_image(
image: &str,
container_name: &str,
env_vars: Vec<&str>,
env: Option<Vec<&str>>,
host_config: Option<HostConfig>,
) -> Result<(), Box<dyn std::error::Error + 'static>> {
let docker = Docker::connect_with_local_defaults().unwrap();

Expand All @@ -43,7 +44,7 @@ async fn pull_and_start_docker_image(
.try_collect::<Vec<_>>()
.await?;

let config = Config { image: Some(image), tty: Some(true), env: Some(env_vars), ..Default::default() };
let config = Config { image: Some(image), tty: Some(true), env, host_config, ..Default::default() };

let container_option = Some(CreateContainerOptions { name: container_name, ..Default::default() });

Expand Down
19 changes: 10 additions & 9 deletions src/utils/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn get_latest_commit_hash(org: &str, repo: &str) -> Result<String, GithubErr
};
}

pub fn git_clone(url: &str, path: &PathBuf) -> Result<(), GithubError> {
pub fn git_clone(url: &str, path: &PathBuf, branch: Option<&str>) -> Result<(), GithubError> {
if let Ok(repo) = Repository::open(path) {
// Check if the repository is valid
if repo.is_empty() == Ok(false) {
Expand All @@ -51,14 +51,15 @@ pub fn git_clone(url: &str, path: &PathBuf) -> Result<(), GithubError> {
fs::remove_dir_all(path)?;
}

let output = Command::new("git")
.arg("clone")
.arg("--progress")
.arg(url)
.arg(path)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;
let mut cmd = Command::new("git");
cmd.arg("clone").arg("--progress").arg(url).arg(path).stdout(Stdio::inherit()).stderr(Stdio::inherit());

if let Some(branch) = branch {
let clone_branch = format!("--branch={}", branch);
cmd.arg(clone_branch);
}

let output = cmd.output()?;

let status = output.status;

Expand Down
6 changes: 3 additions & 3 deletions src/utils/madara.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::da::da_layers::DALayer;
use crate::utils::cmd::execute_cmd;
use crate::utils::constants::{APP_DA_CONFIG_NAME, MADARA_REPO_NAME, MADARA_REPO_ORG};
use crate::utils::constants::{APP_DA_CONFIG_NAME, BRANCH_NAME, KARNOT_REPO_ORG, MADARA_REPO_NAME};
use crate::utils::errors::MadaraError;
use crate::utils::github::git_clone;
use crate::utils::paths::{get_app_home, get_madara_home};
use crate::utils::toml::regenerate_app_config;
pub const GITHUB_BASE_URL: &str = "https://github.com";

pub fn clone_madara_and_build_repo() -> Result<(), MadaraError> {
let repo_url = format!("{}/{}/{}", GITHUB_BASE_URL, MADARA_REPO_ORG, MADARA_REPO_NAME);
let repo_url = format!("{}/{}/{}", GITHUB_BASE_URL, KARNOT_REPO_ORG, MADARA_REPO_NAME);
let madara_path = get_madara_home()?.join("madara");

match git_clone(&repo_url, &madara_path) {
match git_clone(&repo_url, &madara_path, Some(BRANCH_NAME)) {
Ok(_) => {
log::info!("Successfully cloned Madara repo");
}
Expand Down

0 comments on commit 2dd4694

Please sign in to comment.