Skip to content

Commit 4339fb4

Browse files
committed
refactor: separate system toml config from service management logic
Signed-off-by: James Rhodes <jarhodes314@gmail.com>
1 parent 7c0b549 commit 4339fb4

File tree

20 files changed

+121
-107
lines changed

20 files changed

+121
-107
lines changed

crates/common/tedge_config/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ pub mod system_services;
44
pub mod tedge_config_cli;
55
pub use sudo::SudoCommandBuilder;
66
pub mod cli;
7+
mod system_toml;
78

9+
pub use self::system_toml::*;
810
pub use self::tedge_config_cli::config_setting::*;
911
pub use self::tedge_config_cli::error::*;
1012
pub use self::tedge_config_cli::models::*;

crates/common/tedge_config/src/system_services/error.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ pub enum SystemServiceError {
2121
Service manager '{name}' is not available on the system or elevated permissions have not been granted.")]
2222
ServiceManagerUnavailable { cmd: String, name: String },
2323

24-
#[error("Toml syntax error in the system config file '{path}': {reason}")]
25-
SystemConfigInvalidToml { path: Utf8PathBuf, reason: String },
26-
2724
#[error(
2825
"Syntax error in the system config file for '{cmd}': {reason}\n\
2926
Check '{path}' file."
@@ -33,7 +30,4 @@ pub enum SystemServiceError {
3330
cmd: String,
3431
path: Utf8PathBuf,
3532
},
36-
37-
#[error("Invalid log level: {name:?}, supported levels are info, warn, error and debug")]
38-
InvalidLogLevel { name: String },
3933
}

crates/common/tedge_config/src/system_services/manager.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use camino::Utf8Path;
22

3-
use crate::system_services::*;
3+
use super::*;
4+
use crate::SystemTomlError;
45
use std::fmt::Debug;
56
use std::sync::Arc;
67

@@ -34,6 +35,6 @@ pub trait SystemServiceManager: Debug {
3435

3536
pub fn service_manager(
3637
config_root: &Utf8Path,
37-
) -> Result<Arc<dyn SystemServiceManager>, SystemServiceError> {
38+
) -> Result<Arc<dyn SystemServiceManager>, SystemTomlError> {
3839
Ok(Arc::new(GeneralServiceManager::try_new(config_root)?))
3940
}

crates/common/tedge_config/src/system_services/managers/general_manager.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use crate::system_services::CommandBuilder;
22
use crate::system_services::SystemService;
33
use crate::system_services::SystemServiceError;
44
use crate::system_services::SystemServiceManager;
5+
use crate::SystemTomlError;
56
use camino::Utf8Path;
67
use camino::Utf8PathBuf;
78

8-
use super::config::InitConfig;
9-
use super::config::SystemConfig;
10-
use super::config::SERVICE_CONFIG_FILE;
9+
use crate::system_toml::InitConfig;
10+
use crate::system_toml::SystemConfig;
11+
use crate::system_toml::SYSTEM_CONFIG_FILE;
1112
use std::fmt;
1213
use std::process::ExitStatus;
1314

@@ -18,10 +19,10 @@ pub struct GeneralServiceManager {
1819
}
1920

2021
impl GeneralServiceManager {
21-
pub fn try_new(config_root: &Utf8Path) -> Result<Self, SystemServiceError> {
22+
pub fn try_new(config_root: &Utf8Path) -> Result<Self, SystemTomlError> {
2223
let init_config = SystemConfig::try_new(config_root)?.init;
2324

24-
let config_path = config_root.join(SERVICE_CONFIG_FILE);
25+
let config_path = config_root.join(SYSTEM_CONFIG_FILE);
2526

2627
Ok(Self {
2728
init_config,
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
mod config;
21
mod general_manager;
32

4-
pub use self::config::*;
53
pub use self::general_manager::*;

crates/common/tedge_config/src/system_services/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
1111
mod command_builder;
1212
mod error;
13-
mod log_config;
1413
mod manager;
1514
mod manager_ext;
1615
mod managers;
1716
mod services;
1817

1918
pub use self::command_builder::*;
2019
pub use self::error::*;
21-
pub use self::log_config::*;
2220
pub use self::manager::*;
2321
pub use self::manager_ext::*;
2422
pub use self::managers::*;

crates/common/tedge_config/src/system_services/log_config.rs renamed to crates/common/tedge_config/src/system_toml/log_level.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use camino::Utf8Path;
22

3+
use super::SystemConfig;
4+
use super::SystemTomlError;
35
use crate::cli::LogConfigArgs;
4-
use crate::system_services::SystemConfig;
5-
use crate::system_services::SystemServiceError;
66
use std::io::IsTerminal;
77
use std::str::FromStr;
88

@@ -18,7 +18,7 @@ pub fn log_init(
1818
sname: &str,
1919
flags: &LogConfigArgs,
2020
config_dir: &Utf8Path,
21-
) -> Result<(), SystemServiceError> {
21+
) -> Result<(), SystemTomlError> {
2222
let subscriber = tracing_subscriber::fmt()
2323
.with_writer(std::io::stderr)
2424
.with_ansi(std::io::stderr().is_terminal())
@@ -51,11 +51,11 @@ pub fn log_init(
5151
pub fn get_log_level(
5252
sname: &str,
5353
config_dir: &Utf8Path,
54-
) -> Result<tracing::Level, SystemServiceError> {
54+
) -> Result<tracing::Level, SystemTomlError> {
5555
let loglevel = SystemConfig::try_new(config_dir)?.log;
5656
match loglevel.get(sname) {
5757
Some(ll) => tracing::Level::from_str(&ll.to_uppercase()).map_err(|_| {
58-
SystemServiceError::InvalidLogLevel {
58+
SystemTomlError::InvalidLogLevel {
5959
name: ll.to_string(),
6060
}
6161
}),

crates/common/tedge_config/src/system_services/managers/config.rs renamed to crates/common/tedge_config/src/system_toml/mod.rs

Lines changed: 34 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,51 @@
1-
use crate::system_services::SystemServiceError;
2-
use camino::Utf8Path;
3-
use serde::Deserialize;
41
use std::collections::HashMap;
5-
use std::fs;
62
use std::time::Duration;
73

8-
pub const SERVICE_CONFIG_FILE: &str = "system.toml";
4+
use serde::Deserialize;
5+
6+
mod log_level;
7+
mod services;
8+
9+
pub use self::log_level::*;
10+
pub use self::services::*;
11+
use camino::Utf8Path;
12+
use camino::Utf8PathBuf;
13+
use std::fs;
14+
15+
pub const SYSTEM_CONFIG_FILE: &str = "system.toml";
916
const REBOOT_COMMAND: &[&str] = &["init", "6"];
1017

18+
#[derive(thiserror::Error, Debug)]
19+
pub enum SystemTomlError {
20+
#[error("Toml syntax error in the system config file '{path}': {reason}")]
21+
InvalidSyntax { path: Utf8PathBuf, reason: String },
22+
23+
#[error("Invalid log level: {name:?}, supported levels are info, warn, error and debug")]
24+
InvalidLogLevel { name: String },
25+
}
26+
1127
#[derive(Deserialize, Debug, Default, Eq, PartialEq)]
1228
pub struct SystemConfig {
1329
#[serde(default)]
14-
pub init: InitConfig,
30+
pub init: services::InitConfig,
1531
#[serde(default)]
1632
pub log: HashMap<String, String>,
1733
#[serde(default)]
1834
pub system: SystemSpecificCommands,
1935
}
2036

21-
#[derive(Deserialize, Debug, Eq, PartialEq)]
22-
#[serde(from = "InitConfigToml")]
23-
pub struct InitConfig {
24-
pub name: String,
25-
pub is_available: Vec<String>,
26-
pub restart: Vec<String>,
27-
pub stop: Vec<String>,
28-
pub start: Vec<String>,
29-
pub enable: Vec<String>,
30-
pub disable: Vec<String>,
31-
pub is_active: Vec<String>,
32-
}
33-
34-
#[derive(Deserialize, Debug, Eq, PartialEq)]
35-
#[serde(deny_unknown_fields)]
36-
struct InitConfigToml {
37-
name: String,
38-
is_available: Vec<String>,
39-
restart: Vec<String>,
40-
stop: Vec<String>,
41-
start: Option<Vec<String>>,
42-
enable: Vec<String>,
43-
disable: Vec<String>,
44-
is_active: Vec<String>,
45-
}
37+
impl SystemConfig {
38+
pub fn try_new(config_root: &Utf8Path) -> Result<Self, SystemTomlError> {
39+
let config_path = config_root.join(SYSTEM_CONFIG_FILE);
4640

47-
impl From<InitConfigToml> for InitConfig {
48-
fn from(value: InitConfigToml) -> Self {
49-
Self {
50-
name: value.name,
51-
is_available: value.is_available,
52-
start: value.start.unwrap_or(value.restart.clone()),
53-
restart: value.restart,
54-
stop: value.stop,
55-
enable: value.enable,
56-
disable: value.disable,
57-
is_active: value.is_active,
41+
match fs::read_to_string(config_path.clone()) {
42+
Ok(contents) => {
43+
toml::from_str(contents.as_str()).map_err(|e| SystemTomlError::InvalidSyntax {
44+
path: config_path,
45+
reason: e.to_string(),
46+
})
47+
}
48+
Err(_) => Ok(Self::default()),
5849
}
5950
}
6051
}
@@ -94,37 +85,6 @@ impl Default for SystemSpecificCommands {
9485
}
9586
}
9687

97-
impl Default for InitConfig {
98-
fn default() -> Self {
99-
Self {
100-
name: "systemd".to_string(),
101-
is_available: vec!["/bin/systemctl".into(), "--version".into()],
102-
restart: vec!["/bin/systemctl".into(), "restart".into(), "{}".into()],
103-
stop: vec!["/bin/systemctl".into(), "stop".into(), "{}".into()],
104-
start: vec!["/bin/systemctl".into(), "start".into(), "{}".into()],
105-
enable: vec!["/bin/systemctl".into(), "enable".into(), "{}".into()],
106-
disable: vec!["/bin/systemctl".into(), "disable".into(), "{}".into()],
107-
is_active: vec!["/bin/systemctl".into(), "is-active".into(), "{}".into()],
108-
}
109-
}
110-
}
111-
112-
impl SystemConfig {
113-
pub fn try_new(config_root: &Utf8Path) -> Result<Self, SystemServiceError> {
114-
let config_path = config_root.join(SERVICE_CONFIG_FILE);
115-
116-
match fs::read_to_string(config_path.clone()) {
117-
Ok(contents) => toml::from_str(contents.as_str()).map_err(|e| {
118-
SystemServiceError::SystemConfigInvalidToml {
119-
path: config_path,
120-
reason: e.to_string(),
121-
}
122-
}),
123-
Err(_) => Ok(Self::default()),
124-
}
125-
}
126-
}
127-
12888
#[cfg(test)]
12989
mod tests {
13090
use super::*;
@@ -235,7 +195,7 @@ mod tests {
235195
fn create_temp_system_config(content: &str) -> std::io::Result<(TempDir, Utf8PathBuf)> {
236196
let temp_dir = TempDir::new()?;
237197
let config_root = Utf8Path::from_path(temp_dir.path()).unwrap().to_owned();
238-
let config_file_path = config_root.join(SERVICE_CONFIG_FILE);
198+
let config_file_path = config_root.join(SYSTEM_CONFIG_FILE);
239199
let mut file = std::fs::File::create(config_file_path.as_path())?;
240200
file.write_all(content.as_bytes())?;
241201
Ok((temp_dir, config_root))
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use serde::Deserialize;
2+
3+
#[derive(Deserialize, Debug, Eq, PartialEq)]
4+
#[serde(from = "InitConfigToml")]
5+
pub struct InitConfig {
6+
pub name: String,
7+
pub is_available: Vec<String>,
8+
pub restart: Vec<String>,
9+
pub stop: Vec<String>,
10+
pub start: Vec<String>,
11+
pub enable: Vec<String>,
12+
pub disable: Vec<String>,
13+
pub is_active: Vec<String>,
14+
}
15+
16+
#[derive(Deserialize, Debug, Eq, PartialEq)]
17+
#[serde(deny_unknown_fields)]
18+
struct InitConfigToml {
19+
name: String,
20+
is_available: Vec<String>,
21+
restart: Vec<String>,
22+
stop: Vec<String>,
23+
start: Option<Vec<String>>,
24+
enable: Vec<String>,
25+
disable: Vec<String>,
26+
is_active: Vec<String>,
27+
}
28+
29+
impl From<InitConfigToml> for InitConfig {
30+
fn from(value: InitConfigToml) -> Self {
31+
Self {
32+
name: value.name,
33+
is_available: value.is_available,
34+
start: value.start.unwrap_or(value.restart.clone()),
35+
restart: value.restart,
36+
stop: value.stop,
37+
enable: value.enable,
38+
disable: value.disable,
39+
is_active: value.is_active,
40+
}
41+
}
42+
}
43+
44+
impl Default for InitConfig {
45+
fn default() -> Self {
46+
Self {
47+
name: "systemd".to_string(),
48+
is_available: vec!["/bin/systemctl".into(), "--version".into()],
49+
restart: vec!["/bin/systemctl".into(), "restart".into(), "{}".into()],
50+
stop: vec!["/bin/systemctl".into(), "stop".into(), "{}".into()],
51+
start: vec!["/bin/systemctl".into(), "start".into(), "{}".into()],
52+
enable: vec!["/bin/systemctl".into(), "enable".into(), "{}".into()],
53+
disable: vec!["/bin/systemctl".into(), "disable".into(), "{}".into()],
54+
is_active: vec!["/bin/systemctl".into(), "is-active".into(), "{}".into()],
55+
}
56+
}
57+
}

crates/core/tedge/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub enum TEdgeError {
2727
#[error(transparent)]
2828
FromSystemServiceError(#[from] tedge_config::system_services::SystemServiceError),
2929

30+
#[error(transparent)]
31+
FromSystemToml(#[from] tedge_config::SystemTomlError),
32+
3033
#[error(transparent)]
3134
FromTEdgeConfigRead(#[from] tedge_config::ReadError),
3235

0 commit comments

Comments
 (0)