Skip to content

Commit

Permalink
feat(core): nurse clean up the global repository
Browse files Browse the repository at this point in the history
Adding a new case for the coffe nurse command to clean up
the global repository directory and move it insied the network
subdirectory.

Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Feb 13, 2024
1 parent 472d9f6 commit 1272fa5
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 43 deletions.
4 changes: 4 additions & 0 deletions coffee_cmd/src/coffee_term/command_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ pub fn show_nurse_result(
NurseStatus::RepositoryLocallyRemoved(_) => {
"Removed from local storage".to_string()
}
NurseStatus::MovingGlobalRepostoryTo(_) => {
"Moving Global repository directory".to_string()
}
};
let repos_str = match status {
NurseStatus::RepositoryLocallyRestored(repos)
| NurseStatus::RepositoryLocallyRemoved(repos) => repos.join(", "),
NurseStatus::MovingGlobalRepostoryTo(network) => network.to_owned(),
};

table.push([
Expand Down
15 changes: 14 additions & 1 deletion coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Coffee mod implementation
use std::collections::HashMap;
use std::fmt::Debug;
use std::path::Path;
use std::vec::Vec;
use tokio::fs;

use async_trait::async_trait;
use clightningrpc_common::client::Client;
Expand All @@ -12,6 +12,7 @@ use log;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::fs;
use tokio::process::Command;

use coffee_github::repository::Github;
Expand All @@ -20,6 +21,7 @@ use coffee_lib::plugin_manager::PluginManager;
use coffee_lib::repository::Repository;
use coffee_lib::types::response::*;
use coffee_lib::url::URL;
use coffee_lib::utils::{copy_dir_if_exist, rm_dir_if_exist};
use coffee_lib::{commit_id, error, get_repo_info, sh};
use coffee_storage::model::repository::{Kind, Repository as RepositoryInfo};
use coffee_storage::nosql_db::NoSQlStorage;
Expand Down Expand Up @@ -497,6 +499,17 @@ impl PluginManager for CoffeeManager {
let mut actions = self.patch_repository_locally_absent(repos.to_vec()).await?;
nurse_actions.append(&mut actions);
}
Defect::CoffeeGlobalrepoCleanup(networks) => {
let global_repo = format!("{}/repositories", self.config.root_path);
for (network, path) in networks {
if !Path::exists(Path::new(&path)) {
copy_dir_if_exist(&global_repo, path).await?;
}
nurse_actions
.push(NurseStatus::MovingGlobalRepostoryTo(network.to_owned()));
}
rm_dir_if_exist(&global_repo).await?;
}
}
}
let mut nurse = CoffeeNurse {
Expand Down
6 changes: 4 additions & 2 deletions coffee_core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use std::env;

use crate::CoffeeOperation;
use coffee_lib::utils::{check_dir_or_make_if_missing, move_dir_if_exist};
use coffee_lib::utils::{check_dir_or_make_if_missing, copy_dir_if_exist};
use coffee_lib::{errors::CoffeeError, plugin::Plugin};

use crate::CoffeeArgs;
Expand Down Expand Up @@ -66,7 +66,9 @@ impl CoffeeConf {
check_dir_or_make_if_missing(format!("{def_path}/{}", coffee.network)).await?;
check_dir_or_make_if_missing(format!("{def_path}/{}/plugins", coffee.network)).await?;
let repo_dir = format!("{def_path}/{}/repositories", coffee.network);
move_dir_if_exist(&format!("{def_path}/repositories"), &repo_dir).await?;
// older version of coffee has a repository inside the directory
copy_dir_if_exist(&format!("{def_path}/repositories"), &repo_dir).await?;
// FIXME: nurse should clean up the `{def_path}/repositories`.
check_dir_or_make_if_missing(repo_dir).await?;
// after we know all the information regarding
// the configuration we try to see if there is
Expand Down
7 changes: 5 additions & 2 deletions coffee_core/src/nurse/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use async_trait::async_trait;
use coffee_lib::errors::CoffeeError;
use coffee_lib::types::response::{ChainOfResponsibilityStatus, Defect};

use super::strategy::GitRepositoryLocallyAbsentStrategy;
use super::strategy::{CoffeeRepositoryDirCleanUp, GitRepositoryLocallyAbsentStrategy};
use crate::coffee::CoffeeManager;

#[async_trait]
Expand All @@ -52,7 +52,10 @@ impl RecoveryChainOfResponsibility {
/// Create a new instance of the chain of responsibility
pub async fn new() -> Result<Self, CoffeeError> {
Ok(Self {
handlers: vec![Arc::new(GitRepositoryLocallyAbsentStrategy)],
handlers: vec![
Arc::new(GitRepositoryLocallyAbsentStrategy),
Arc::new(CoffeeRepositoryDirCleanUp),
],
})
}

Expand Down
31 changes: 31 additions & 0 deletions coffee_core/src/nurse/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,34 @@ impl Handler for GitRepositoryLocallyAbsentStrategy {
}
}
}

/// Stategy for migration of the repository global directory
/// to a local directory for each network, see [1]
///
/// This is a strategy tht return the list of network that
/// needs to be migrated to to the new repository configuration.
///
/// [1] https://github.com/coffee-tools/coffee/issues/234
pub struct CoffeeRepositoryDirCleanUp;

#[async_trait]
impl Handler for CoffeeRepositoryDirCleanUp {
async fn can_be_applied(
self: Arc<Self>,
coffee: &CoffeeManager,
) -> Result<Option<Defect>, CoffeeError> {
let networks = ["testnet", "signet", "bitcoin", "liquid"];
// Check whether there exists a network-specific repositories folder for each network.
let mut directory_moving = vec![];
for network in networks {
let subpath_repo = format!("{}/{network}/repositories", coffee.config.root_path);
if !Path::exists(Path::new(&subpath_repo)) {
directory_moving.push((network.to_string(), subpath_repo));
}
}
if directory_moving.is_empty() {
return Ok(None);
}
Ok(Some(Defect::CoffeeGlobalrepoCleanup(directory_moving)))
}
}
20 changes: 20 additions & 0 deletions coffee_lib/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ pub mod response {
// A patch operation when a git repository is present in the coffee configuration
// but is absent from the local storage.
RepositoryLocallyAbsent(Vec<String>),
/// (Affected network, path)
CoffeeGlobalrepoCleanup(Vec<(String, String)>),
// TODO: Add more patch operations
}

Expand Down Expand Up @@ -166,6 +168,16 @@ pub mod response {
write!(f, " {}", repo)?;
}
}
Defect::CoffeeGlobalrepoCleanup(networks) => {
writeln!(
f,
"Global repository migration completed for the networks: {}",
networks
.iter()
.map(|(network, _)| network.to_owned())
.collect::<String>()
)?;
}
}
}
Ok(())
Expand All @@ -180,6 +192,7 @@ pub mod response {
pub enum NurseStatus {
RepositoryLocallyRestored(Vec<String>),
RepositoryLocallyRemoved(Vec<String>),
MovingGlobalRepostoryTo(String),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -206,6 +219,7 @@ pub mod response {
NurseStatus::RepositoryLocallyRestored(repos) => {
repositories_locally_restored.append(&mut repos.clone())
}
NurseStatus::MovingGlobalRepostoryTo(_) => {}
}
}
if !repositories_locally_removed.is_empty() {
Expand All @@ -231,6 +245,12 @@ pub mod response {
NurseStatus::RepositoryLocallyRemoved(val) => {
write!(f, "Repositories removed locally: {}", val.join(" "))
}
NurseStatus::MovingGlobalRepostoryTo(net) => {
write!(
f,
"Global repository directory moved to subdirectory for network `{net}`"
)
}
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions coffee_lib/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::macros::error;
use std::path::Path;

use tokio::fs::create_dir;
use tokio::fs::rename;
use tokio::fs;

use crate::errors::CoffeeError;

Expand All @@ -23,16 +22,24 @@ pub fn get_plugin_info_from_path(path: &Path) -> Result<(String, String), Coffee

pub async fn check_dir_or_make_if_missing(path: String) -> Result<(), CoffeeError> {
if !Path::exists(Path::new(&path.to_owned())) {
create_dir(path.clone()).await?;
fs::create_dir(path.clone()).await?;
log::debug!("created dir {path}");
}
Ok(())
}

pub async fn move_dir_if_exist(origin: &str, destination: &str) -> Result<(), CoffeeError> {
pub async fn copy_dir_if_exist(origin: &str, destination: &str) -> Result<(), CoffeeError> {
if Path::exists(Path::new(&origin)) {
rename(origin, destination).await?;
log::debug!("move dir from {origin} to {destination}");
fs::copy(origin, destination).await?;
log::debug!("copying dir from {origin} to {destination}");
}
Ok(())
}

pub async fn rm_dir_if_exist(origin: &str) -> Result<(), CoffeeError> {
if Path::exists(Path::new(&origin)) {
fs::remove_dir_all(origin).await?;
log::debug!("rm dir from {origin}");
}
Ok(())
}
Expand Down
32 changes: 0 additions & 32 deletions git-bugreport-2024-02-07-1353.txt

This file was deleted.

0 comments on commit 1272fa5

Please sign in to comment.