Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): consistent dynamic plugins install/remove #251

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Coffee mod implementation
use std::collections::HashMap;
use std::fmt::Debug;
use std::vec::Vec;

Check warning on line 4 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `Vec` is imported redundantly

Check warning on line 4 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `Vec` is imported redundantly
use tokio::fs;

use async_trait::async_trait;
use clightningrpc_common::client::Client;
use clightningrpc_common::json_utils;
use clightningrpc_conf::{CLNConf, SyncCLNConf};
use log;

Check warning on line 11 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `log` is imported redundantly

Check warning on line 11 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `log` is imported redundantly
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand Down Expand Up @@ -169,6 +169,17 @@
Ok(())
}

pub async fn stop_plugin(&self, path: &str) -> Result<(), CoffeeError> {
let mut payload = json_utils::init_payload();
json_utils::add_str(&mut payload, "subcommand", "stop");
json_utils::add_str(&mut payload, "plugin", path);
let response = self
.cln::<serde_json::Value, serde_json::Value>("plugin", payload)
.await?;
log::debug!("plugin stopped: {response}");
Ok(())
}

pub fn storage_info(&self) -> CoffeeStorageInfo {
CoffeeStorageInfo::from(self)
}
Expand Down Expand Up @@ -300,6 +311,8 @@
self.flush().await?;
self.update_conf().await?;
} else {
self.config.plugins.push(plugin);
self.flush().await?;
self.start_plugin(&path).await?;
}
return Ok(());
Expand Down Expand Up @@ -328,9 +341,19 @@
log::debug!("runnable plugin path: {exec_path}");
plugins.remove(index);
log::debug!("coffee cln config: {}", self.coffee_cln_config);
self.coffee_cln_config
.rm_conf("plugin", Some(&exec_path.to_owned()))
.map_err(|err| error!("{}", &err.cause))?;
let remove_config = self
.coffee_cln_config
.rm_conf("plugin", Some(&exec_path.to_owned()));
if let Err(err) = remove_config {
// if this is true, we are probably a dynamic plugin:
if err.cause.contains("field with `plugin` not present") {
if let Err(e) = self.stop_plugin(&exec_path).await {
log::warn!("{}", e);
};
} else {
return Err(error!("{}", &err.cause));
}
}
self.flush().await?;
self.update_conf().await?;
Ok(CoffeeRemove { plugin })
Expand Down
Loading