From 52a4680ed26e755b860e3b97c79618a0c20cb696 Mon Sep 17 00:00:00 2001 From: Danil Date: Mon, 8 Jul 2024 10:50:34 +0200 Subject: [PATCH] feat(zk_toolbox): Clean command (#2387) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What ❔ ## Why ❔ ## Checklist - [ ] 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 `zk fmt` and `zk lint`. --------- Signed-off-by: Danil --- .../zk_supervisor/src/commands/clean/mod.rs | 73 +++++++++++++++++++ .../crates/zk_supervisor/src/commands/mod.rs | 1 + zk_toolbox/crates/zk_supervisor/src/main.rs | 8 +- .../crates/zk_supervisor/src/messages.rs | 10 +++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/clean/mod.rs diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/clean/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/clean/mod.rs new file mode 100644 index 000000000000..0c5d2f52682a --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/clean/mod.rs @@ -0,0 +1,73 @@ +use anyhow::Context; +use clap::Subcommand; +use common::{docker, logger}; +use config::{EcosystemConfig, DOCKER_COMPOSE_FILE}; +use xshell::Shell; + +use crate::messages::{ + MSG_CONTRACTS_CLEANING, MSG_CONTRACTS_CLEANING_FINISHED, MSG_DOCKER_COMPOSE_CLEANED, + MSG_DOCKER_COMPOSE_DOWN, MSG_DOCKER_COMPOSE_REMOVE_VOLUMES, +}; + +#[derive(Subcommand, Debug)] +pub enum CleanCommands { + All, + Containers, + ContractsCache, +} + +pub fn run(shell: &Shell, args: CleanCommands) -> anyhow::Result<()> { + let ecosystem = EcosystemConfig::from_file(shell)?; + match args { + CleanCommands::All => { + containers(shell)?; + contracts(shell, &ecosystem)?; + } + CleanCommands::Containers => containers(shell)?, + CleanCommands::ContractsCache => contracts(shell, &ecosystem)?, + } + Ok(()) +} + +pub fn containers(shell: &Shell) -> anyhow::Result<()> { + logger::info(MSG_DOCKER_COMPOSE_DOWN); + docker::down(shell, DOCKER_COMPOSE_FILE)?; + logger::info(MSG_DOCKER_COMPOSE_REMOVE_VOLUMES); + shell.remove_path("volumes")?; + logger::info(MSG_DOCKER_COMPOSE_CLEANED); + Ok(()) +} + +pub fn contracts(shell: &Shell, ecosystem_config: &EcosystemConfig) -> anyhow::Result<()> { + let path_to_foundry = ecosystem_config.path_to_foundry(); + logger::info(MSG_CONTRACTS_CLEANING); + shell + .remove_path(path_to_foundry.join("artifacts")) + .context("artifacts")?; + shell + .remove_path(path_to_foundry.join("cache")) + .context("cache")?; + shell + .remove_path(path_to_foundry.join("cache-forge")) + .context("cache-forge")?; + shell + .remove_path(path_to_foundry.join("out")) + .context("out")?; + shell + .remove_path(path_to_foundry.join("typechain")) + .context("typechain")?; + shell + .remove_path(path_to_foundry.join("script-config")) + .context("remove script-config")?; + shell + .create_dir(path_to_foundry.join("script-config")) + .context("create script-config")?; + shell + .remove_path(path_to_foundry.join("script-out")) + .context("remove script-out")?; + shell + .create_dir(path_to_foundry.join("script-out")) + .context("create script-out")?; + logger::info(MSG_CONTRACTS_CLEANING_FINISHED); + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs index 90da1b288d48..b2c6df6a4864 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs @@ -1,2 +1,3 @@ +pub mod clean; pub mod database; pub mod test; diff --git a/zk_toolbox/crates/zk_supervisor/src/main.rs b/zk_toolbox/crates/zk_supervisor/src/main.rs index 79c28511c0a1..17ad5c577996 100644 --- a/zk_toolbox/crates/zk_supervisor/src/main.rs +++ b/zk_toolbox/crates/zk_supervisor/src/main.rs @@ -8,10 +8,13 @@ use common::{ }; use config::EcosystemConfig; use messages::{ - msg_global_chain_does_not_exist, MSG_SUBCOMMAND_DATABASE_ABOUT, MSG_SUBCOMMAND_TESTS_ABOUT, + msg_global_chain_does_not_exist, MSG_SUBCOMMAND_CLEAN, MSG_SUBCOMMAND_DATABASE_ABOUT, + MSG_SUBCOMMAND_TESTS_ABOUT, }; use xshell::Shell; +use crate::commands::clean::CleanCommands; + mod commands; mod dals; mod messages; @@ -31,6 +34,8 @@ enum SupervisorSubcommands { Database(DatabaseCommands), #[command(subcommand, about = MSG_SUBCOMMAND_TESTS_ABOUT)] Test(TestCommands), + #[command(subcommand, about = MSG_SUBCOMMAND_CLEAN)] + Clean(CleanCommands), } #[derive(Parser, Debug)] @@ -80,6 +85,7 @@ async fn run_subcommand(args: Supervisor, shell: &Shell) -> anyhow::Result<()> { match args.command { SupervisorSubcommands::Database(command) => commands::database::run(shell, command).await?, SupervisorSubcommands::Test(command) => commands::test::run(shell, command)?, + SupervisorSubcommands::Clean(command) => commands::clean::run(shell, command)?, } Ok(()) } diff --git a/zk_toolbox/crates/zk_supervisor/src/messages.rs b/zk_toolbox/crates/zk_supervisor/src/messages.rs index 97d30baf1d9e..3275523ed96e 100644 --- a/zk_toolbox/crates/zk_supervisor/src/messages.rs +++ b/zk_toolbox/crates/zk_supervisor/src/messages.rs @@ -8,6 +8,7 @@ pub(super) fn msg_global_chain_does_not_exist(chain: &str, available_chains: &st // Subcommands help pub(super) const MSG_SUBCOMMAND_DATABASE_ABOUT: &str = "Database related commands"; pub(super) const MSG_SUBCOMMAND_TESTS_ABOUT: &str = "Run tests"; +pub(super) const MSG_SUBCOMMAND_CLEAN: &str = "Clean artifacts"; // Database related messages pub(super) const MSG_NO_DATABASES_SELECTED: &str = "No databases selected"; @@ -94,3 +95,12 @@ pub(super) const MSG_INTEGRATION_TESTS_BUILDING_CONTRACTS: &str = "Building test pub(super) const MSG_REVERT_TEST_ENABLE_CONSENSUS_HELP: &str = "Enable consensus"; pub(super) const MSG_REVERT_TEST_RUN_INFO: &str = "Running revert and restart test"; pub(super) const MSG_REVERT_TEST_RUN_SUCCESS: &str = "Revert and restart test ran successfully"; + +// Cleaning related messages +pub(super) const MSG_DOCKER_COMPOSE_DOWN: &str = "docker compose down"; +pub(super) const MSG_DOCKER_COMPOSE_REMOVE_VOLUMES: &str = "docker compose remove volumes"; +pub(super) const MSG_DOCKER_COMPOSE_CLEANED: &str = "docker compose network cleaned"; +pub(super) const MSG_CONTRACTS_CLEANING: &str = + "Removing contracts building and deployment artifacts"; +pub(super) const MSG_CONTRACTS_CLEANING_FINISHED: &str = + "Contracts building and deployment artifacts are cleaned up";