mithril_aggregator/commands/
tools_command.rsuse anyhow::Context;
use clap::{Parser, Subcommand};
use config::{builder::DefaultState, ConfigBuilder};
use mithril_common::StdResult;
use mithril_persistence::sqlite::{SqliteCleaner, SqliteCleaningTask};
use slog::{debug, Logger};
use std::sync::Arc;
use crate::{
database::repository::{CertificateRepository, SignedEntityStore},
dependency_injection::DependenciesBuilder,
tools::CertificatesHashMigrator,
Configuration,
};
#[derive(Parser, Debug, Clone)]
pub struct ToolsCommand {
#[clap(subcommand)]
pub genesis_subcommand: ToolsSubCommand,
}
impl ToolsCommand {
pub async fn execute(
&self,
root_logger: Logger,
config_builder: ConfigBuilder<DefaultState>,
) -> StdResult<()> {
self.genesis_subcommand
.execute(root_logger, config_builder)
.await
}
}
#[derive(Debug, Clone, Subcommand)]
pub enum ToolsSubCommand {
RecomputeCertificatesHash(RecomputeCertificatesHashCommand),
}
impl ToolsSubCommand {
pub async fn execute(
&self,
root_logger: Logger,
config_builder: ConfigBuilder<DefaultState>,
) -> StdResult<()> {
match self {
Self::RecomputeCertificatesHash(cmd) => cmd.execute(root_logger, config_builder).await,
}
}
}
#[derive(Parser, Debug, Clone)]
pub struct RecomputeCertificatesHashCommand {}
impl RecomputeCertificatesHashCommand {
pub async fn execute(
&self,
root_logger: Logger,
config_builder: ConfigBuilder<DefaultState>,
) -> StdResult<()> {
let config: Configuration = config_builder
.build()
.with_context(|| "configuration build error")?
.try_deserialize()
.with_context(|| "configuration deserialize error")?;
debug!(root_logger, "RECOMPUTE CERTIFICATES HASH command"; "config" => format!("{config:?}"));
println!("Recomputing all certificate hash",);
let mut dependencies_builder =
DependenciesBuilder::new(root_logger.clone(), config.clone());
let connection = dependencies_builder
.get_sqlite_connection()
.await
.with_context(|| "Dependencies Builder can not get sqlite connection")?;
let migrator = CertificatesHashMigrator::new(
CertificateRepository::new(connection.clone()),
Arc::new(SignedEntityStore::new(connection.clone())),
root_logger,
);
migrator
.migrate()
.await
.with_context(|| "recompute-certificates-hash: database migration error")?;
SqliteCleaner::new(&connection)
.with_tasks(&[SqliteCleaningTask::Vacuum])
.run()
.with_context(|| "recompute-certificates-hash: database vacuum error")?;
Ok(())
}
}