mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-07 18:24:36 +00:00
feat: Enable core to use local config files while being managed via the web (#1540)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use std::sync::Arc;
|
||||
use std::{collections::HashSet, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
common::config::ConfigLoader,
|
||||
common::config::{ConfigFileControl, ConfigFilePermission, ConfigLoader},
|
||||
instance_manager::NetworkInstanceManager,
|
||||
proto::{
|
||||
api::manage::*,
|
||||
api::{config::GetConfigRequest, manage::*},
|
||||
rpc_types::{self, controller::BaseController},
|
||||
},
|
||||
};
|
||||
@@ -46,11 +46,59 @@ impl WebClientService for InstanceManageRpcService {
|
||||
if let Some(inst_id) = req.inst_id {
|
||||
cfg.set_id(inst_id.into());
|
||||
}
|
||||
self.manager.run_network_instance(cfg, true)?;
|
||||
println!("instance {} started", id);
|
||||
Ok(RunNetworkInstanceResponse {
|
||||
let resp = RunNetworkInstanceResponse {
|
||||
inst_id: Some(id.into()),
|
||||
})
|
||||
};
|
||||
|
||||
let mut control = if let Some(control) = self.manager.get_instance_config_control(&id) {
|
||||
if !req.overwrite {
|
||||
return Ok(resp);
|
||||
}
|
||||
if control.is_read_only() {
|
||||
return Err(
|
||||
anyhow::anyhow!("instance {} is read-only, cannot be overwritten", id).into(),
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(path) = control.path.as_ref() {
|
||||
let real_control = ConfigFileControl::from_path(path.clone()).await;
|
||||
if real_control.is_read_only() {
|
||||
return Err(anyhow::anyhow!(
|
||||
"config file {} is read-only, cannot be overwritten",
|
||||
path.display()
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
|
||||
self.manager.delete_network_instance(vec![id])?;
|
||||
|
||||
control.clone()
|
||||
} else if let Some(config_dir) = self.manager.get_config_dir() {
|
||||
ConfigFileControl::new(
|
||||
Some(config_dir.join(format!("{}.toml", id))),
|
||||
ConfigFilePermission::default(),
|
||||
)
|
||||
} else {
|
||||
ConfigFileControl::new(None, ConfigFilePermission::default())
|
||||
};
|
||||
|
||||
if !control.is_read_only() {
|
||||
if let Some(config_file) = control.path.as_ref() {
|
||||
if let Err(e) = std::fs::write(config_file, cfg.dump()) {
|
||||
tracing::warn!(
|
||||
"failed to write config file {}: {}",
|
||||
config_file.display(),
|
||||
e
|
||||
);
|
||||
control.set_read_only(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.manager.run_network_instance(cfg, true, control)?;
|
||||
println!("instance {} started", id);
|
||||
Ok(resp)
|
||||
}
|
||||
|
||||
async fn retain_network_instance(
|
||||
@@ -124,12 +172,79 @@ impl WebClientService for InstanceManageRpcService {
|
||||
_: BaseController,
|
||||
req: DeleteNetworkInstanceRequest,
|
||||
) -> Result<DeleteNetworkInstanceResponse, rpc_types::error::Error> {
|
||||
let remain_inst_ids = self
|
||||
let inst_ids: HashSet<uuid::Uuid> = req.inst_ids.into_iter().map(Into::into).collect();
|
||||
let inst_ids = self
|
||||
.manager
|
||||
.delete_network_instance(req.inst_ids.into_iter().map(Into::into).collect())?;
|
||||
.iter()
|
||||
.filter(|v| inst_ids.contains(v.key()))
|
||||
.filter(|v| v.get_config_file_control().is_deletable())
|
||||
.map(|v| *v.key())
|
||||
.collect::<Vec<_>>();
|
||||
let config_files = inst_ids
|
||||
.iter()
|
||||
.filter_map(|id| {
|
||||
self.manager
|
||||
.get_instance_config_control(id)
|
||||
.and_then(|control| control.path.clone())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let remain_inst_ids = self.manager.delete_network_instance(inst_ids)?;
|
||||
println!("instance {:?} retained", remain_inst_ids);
|
||||
for config_file in config_files {
|
||||
if let Err(e) = std::fs::remove_file(&config_file) {
|
||||
tracing::warn!(
|
||||
"failed to remove config file {}: {}",
|
||||
config_file.display(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(DeleteNetworkInstanceResponse {
|
||||
remain_inst_ids: remain_inst_ids.into_iter().map(Into::into).collect(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_network_instance_config(
|
||||
&self,
|
||||
_: BaseController,
|
||||
req: GetNetworkInstanceConfigRequest,
|
||||
) -> Result<GetNetworkInstanceConfigResponse, rpc_types::error::Error> {
|
||||
let inst_id: uuid::Uuid = req
|
||||
.inst_id
|
||||
.ok_or_else(|| anyhow::anyhow!("instance id is required"))?
|
||||
.into();
|
||||
let config = self
|
||||
.manager
|
||||
.get_instance_service(&inst_id)
|
||||
.ok_or_else(|| anyhow::anyhow!("instance service not found"))?
|
||||
.get_config_service()
|
||||
.get_config(BaseController::default(), GetConfigRequest::default())
|
||||
.await?
|
||||
.config;
|
||||
Ok(GetNetworkInstanceConfigResponse { config })
|
||||
}
|
||||
|
||||
async fn list_network_instance_meta(
|
||||
&self,
|
||||
_: BaseController,
|
||||
req: ListNetworkInstanceMetaRequest,
|
||||
) -> Result<ListNetworkInstanceMetaResponse, rpc_types::error::Error> {
|
||||
let mut metas = Vec::with_capacity(req.inst_ids.len());
|
||||
for inst_id in req.inst_ids {
|
||||
let inst_id: uuid::Uuid = (inst_id).into();
|
||||
let Some(control) = self.manager.get_instance_config_control(&inst_id) else {
|
||||
continue;
|
||||
};
|
||||
let Some(name) = self.manager.get_network_instance_name(&inst_id) else {
|
||||
continue;
|
||||
};
|
||||
let meta = NetworkMeta {
|
||||
inst_id: Some(inst_id.into()),
|
||||
network_name: name,
|
||||
config_permission: control.permission.into(),
|
||||
};
|
||||
metas.push(meta);
|
||||
}
|
||||
Ok(ListNetworkInstanceMetaResponse { metas })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,18 +79,23 @@ fn get_instance_service(
|
||||
let id = if let Some(api::instance::instance_identifier::Selector::Id(id)) = selector {
|
||||
(*id).into()
|
||||
} else {
|
||||
let ids = instance_manager.filter_network_instance(|_, i| {
|
||||
if let Some(api::instance::instance_identifier::Selector::InstanceSelector(selector)) =
|
||||
selector
|
||||
{
|
||||
if let Some(name) = selector.name.as_ref() {
|
||||
if i.get_inst_name() != *name {
|
||||
return false;
|
||||
let ids = instance_manager
|
||||
.iter()
|
||||
.filter(|v| {
|
||||
if let Some(api::instance::instance_identifier::Selector::InstanceSelector(
|
||||
selector,
|
||||
)) = selector
|
||||
{
|
||||
if let Some(name) = selector.name.as_ref() {
|
||||
if v.get_inst_name() != *name {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
});
|
||||
true
|
||||
})
|
||||
.map(|v| *v.key())
|
||||
.collect::<Vec<_>>();
|
||||
match ids.len() {
|
||||
0 => return Err(anyhow::anyhow!("No instance matches the selector")),
|
||||
1 => ids[0],
|
||||
|
||||
@@ -40,6 +40,7 @@ where
|
||||
&self,
|
||||
identify: T,
|
||||
config: NetworkConfig,
|
||||
save: bool,
|
||||
) -> Result<(), RemoteClientError<E>> {
|
||||
let client = self
|
||||
.get_rpc_client(identify.clone())
|
||||
@@ -50,18 +51,21 @@ where
|
||||
RunNetworkInstanceRequest {
|
||||
inst_id: None,
|
||||
config: Some(config.clone()),
|
||||
overwrite: true,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
self.get_storage()
|
||||
.insert_or_update_user_network_config(
|
||||
identify,
|
||||
resp.inst_id.unwrap_or_default().into(),
|
||||
config,
|
||||
)
|
||||
.await
|
||||
.map_err(RemoteClientError::PersistentError)?;
|
||||
if save {
|
||||
self.get_storage()
|
||||
.insert_or_update_user_network_config(
|
||||
identify,
|
||||
resp.inst_id.unwrap_or_default().into(),
|
||||
config,
|
||||
)
|
||||
.await
|
||||
.map_err(RemoteClientError::PersistentError)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -156,13 +160,17 @@ where
|
||||
let client = self
|
||||
.get_rpc_client(identify.clone())
|
||||
.ok_or(RemoteClientError::ClientNotFound)?;
|
||||
|
||||
let cfg = self
|
||||
.get_storage()
|
||||
.update_network_config_state(identify, inst_id, disabled)
|
||||
.await
|
||||
.map_err(RemoteClientError::PersistentError)?;
|
||||
.handle_get_network_config(identify.clone(), inst_id)
|
||||
.await?;
|
||||
|
||||
if disabled {
|
||||
self.get_storage()
|
||||
.insert_or_update_user_network_config(identify.clone(), inst_id, cfg.clone())
|
||||
.await
|
||||
.map_err(RemoteClientError::PersistentError)?;
|
||||
|
||||
client
|
||||
.delete_network_instance(
|
||||
BaseController::default(),
|
||||
@@ -177,15 +185,18 @@ where
|
||||
BaseController::default(),
|
||||
RunNetworkInstanceRequest {
|
||||
inst_id: Some(inst_id.into()),
|
||||
config: Some(
|
||||
cfg.get_network_config()
|
||||
.map_err(RemoteClientError::PersistentError)?,
|
||||
),
|
||||
config: Some(cfg),
|
||||
overwrite: true,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
self.get_storage()
|
||||
.update_network_config_state(identify, inst_id, disabled)
|
||||
.await
|
||||
.map_err(RemoteClientError::PersistentError)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -196,14 +207,38 @@ where
|
||||
) -> Result<GetNetworkMetasResponse, RemoteClientError<E>> {
|
||||
let mut metas = std::collections::HashMap::new();
|
||||
|
||||
if let Some(client) = self.get_rpc_client(identify.clone()) {
|
||||
if let Ok(resp) = client
|
||||
.list_network_instance_meta(
|
||||
BaseController::default(),
|
||||
ListNetworkInstanceMetaRequest {
|
||||
inst_ids: inst_ids.iter().cloned().map(|id| id.into()).collect(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
for meta in resp.metas {
|
||||
if let Some(inst_id) = meta.inst_id.as_ref() {
|
||||
let inst_id: uuid::Uuid = (*inst_id).into();
|
||||
metas.insert(inst_id, meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for instance_id in inst_ids {
|
||||
if metas.contains_key(&instance_id) {
|
||||
continue;
|
||||
}
|
||||
let config = self
|
||||
.handle_get_network_config(identify.clone(), instance_id)
|
||||
.await?;
|
||||
metas.insert(
|
||||
instance_id,
|
||||
NetworkMeta {
|
||||
instance_name: config.network_name.unwrap_or_default(),
|
||||
inst_id: Some(instance_id.into()),
|
||||
network_name: config.network_name.unwrap_or_default(),
|
||||
config_permission: 0,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -233,6 +268,22 @@ where
|
||||
identify: T,
|
||||
inst_id: uuid::Uuid,
|
||||
) -> Result<NetworkConfig, RemoteClientError<E>> {
|
||||
if let Some(client) = self.get_rpc_client(identify.clone()) {
|
||||
if let Ok(resp) = client
|
||||
.get_network_instance_config(
|
||||
BaseController::default(),
|
||||
GetNetworkInstanceConfigRequest {
|
||||
inst_id: Some(inst_id.into()),
|
||||
},
|
||||
)
|
||||
.await
|
||||
{
|
||||
if let Some(config) = resp.config {
|
||||
return Ok(config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let inst_id = inst_id.to_string();
|
||||
|
||||
let db_row = self
|
||||
@@ -277,11 +328,6 @@ pub struct ListNetworkInstanceIdsJsonResp {
|
||||
disabled_inst_ids: Vec<crate::proto::common::Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct NetworkMeta {
|
||||
instance_name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct GetNetworkMetasResponse {
|
||||
metas: std::collections::HashMap<uuid::Uuid, NetworkMeta>,
|
||||
@@ -312,7 +358,7 @@ where
|
||||
identify: T,
|
||||
network_inst_id: Uuid,
|
||||
disabled: bool,
|
||||
) -> Result<C, E>;
|
||||
) -> Result<(), E>;
|
||||
|
||||
async fn list_network_configs(&self, identify: T, props: ListNetworkProps)
|
||||
-> Result<Vec<C>, E>;
|
||||
|
||||
Reference in New Issue
Block a user