Skip to content

Commit

Permalink
feat(zkstack): Run containers with either chain or ecosystem (#3345)
Browse files Browse the repository at this point in the history
## What ❔

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zkstack dev fmt` and `zkstack dev
lint`.
  • Loading branch information
matias-gonz authored Nov 29, 2024
1 parent cfffd44 commit 8256433
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
25 changes: 19 additions & 6 deletions zkstack_cli/crates/config/src/zkstack_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use anyhow::bail;
use xshell::Shell;

Expand All @@ -9,14 +11,18 @@ pub enum ZkStackConfig {
}

impl ZkStackConfig {
fn from_file(shell: &Shell) -> anyhow::Result<ZkStackConfig> {
pub fn from_file(shell: &Shell) -> anyhow::Result<ZkStackConfig> {
if let Ok(ecosystem) = EcosystemConfig::from_file(shell) {
Ok(ZkStackConfig::EcosystemConfig(ecosystem))
} else {
let chain_internal = ChainConfigInternal::from_file(shell)?;
let chain = ChainConfig::from_internal(chain_internal, shell.clone())?;
Ok(ZkStackConfig::ChainConfig(chain))
return Ok(ZkStackConfig::EcosystemConfig(ecosystem));
}

if let Ok(chain_internal) = ChainConfigInternal::from_file(shell) {
if let Ok(chain) = ChainConfig::from_internal(chain_internal, shell.clone()) {
return Ok(ZkStackConfig::ChainConfig(chain));
}
}

bail!("Missing ZkStackConfig. Failed to find ecosystem or chain");
}

pub fn current_chain(shell: &Shell) -> anyhow::Result<ChainConfig> {
Expand All @@ -32,4 +38,11 @@ impl ZkStackConfig {
Err(e) => bail!(e),
}
}

pub fn link_to_code(&self) -> PathBuf {
match self {
ZkStackConfig::EcosystemConfig(ecosystem) => ecosystem.link_to_code.clone(),
ZkStackConfig::ChainConfig(chain) => chain.link_to_code.clone(),
}
}
}
18 changes: 7 additions & 11 deletions zkstack_cli/crates/zkstack/src/commands/containers.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
use std::path::PathBuf;

use anyhow::{anyhow, Context};
use anyhow::anyhow;
use common::{docker, logger, spinner::Spinner};
use config::{
zkstack_config::ZkStackConfig, EcosystemConfig, DOCKER_COMPOSE_FILE,
ERA_OBSERVABILITY_COMPOSE_FILE,
};
use config::{zkstack_config::ZkStackConfig, DOCKER_COMPOSE_FILE, ERA_OBSERVABILITY_COMPOSE_FILE};
use xshell::Shell;

use super::args::ContainersArgs;
use crate::{
commands::ecosystem::setup_observability,
messages::{
MSG_CONTAINERS_STARTED, MSG_FAILED_TO_FIND_ECOSYSTEM_ERR,
MSG_RETRY_START_CONTAINERS_PROMPT, MSG_STARTING_CONTAINERS,
MSG_CONTAINERS_STARTED, MSG_RETRY_START_CONTAINERS_PROMPT, MSG_STARTING_CONTAINERS,
MSG_STARTING_DOCKER_CONTAINERS_SPINNER,
},
};

pub fn run(shell: &Shell, args: ContainersArgs) -> anyhow::Result<()> {
let args = args.fill_values_with_prompt();
let ecosystem = ZkStackConfig::ecosystem(shell).context(MSG_FAILED_TO_FIND_ECOSYSTEM_ERR)?;
let link_to_code = ZkStackConfig::from_file(shell)?.link_to_code();

initialize_docker(shell, &ecosystem)?;
initialize_docker(shell, link_to_code)?;

logger::info(MSG_STARTING_CONTAINERS);

Expand All @@ -38,9 +34,9 @@ pub fn run(shell: &Shell, args: ContainersArgs) -> anyhow::Result<()> {
Ok(())
}

pub fn initialize_docker(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> {
pub fn initialize_docker(shell: &Shell, link_to_code: PathBuf) -> anyhow::Result<()> {
if !shell.path_exists(DOCKER_COMPOSE_FILE) {
copy_dockerfile(shell, ecosystem.link_to_code.clone())?;
copy_dockerfile(shell, link_to_code)?;
};

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn create(args: EcosystemCreateArgs, shell: &Shell) -> anyhow::Result<()> {

if args.start_containers {
let spinner = Spinner::new(MSG_STARTING_CONTAINERS_SPINNER);
initialize_docker(shell, &ecosystem_config)?;
initialize_docker(shell, ecosystem_config.link_to_code)?;
start_containers(shell, false)?;
spinner.finish();
}
Expand Down
1 change: 0 additions & 1 deletion zkstack_cli/crates/zkstack/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ pub(super) const MSG_STARTING_DOCKER_CONTAINERS_SPINNER: &str =
pub(super) const MSG_CONTAINERS_STARTED: &str = "Containers started successfully";
pub(super) const MSG_RETRY_START_CONTAINERS_PROMPT: &str =
"Failed to start containers. Make sure there is nothing running on default ports for Ethereum node l1 and postgres. Want to try again?";
pub(super) const MSG_FAILED_TO_FIND_ECOSYSTEM_ERR: &str = "Failed to find ecosystem folder.";
pub(super) const MSG_OBSERVABILITY_RUN_PROMPT: &str = "Do you want to run observability?";

/// Server related messages
Expand Down

0 comments on commit 8256433

Please sign in to comment.