Skip to content

Commit

Permalink
tedge-agent must use new tedge-config
Browse files Browse the repository at this point in the history
Signed-off-by: Pradeep Kumar K J <[email protected]>
  • Loading branch information
PradeepKiruvale committed Jul 14, 2023
1 parent e86234d commit 91c1d08
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 60 deletions.
5 changes: 5 additions & 0 deletions crates/common/tedge_config/src/tedge_config_cli/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use tedge_config_macros::ConfigNotSet;

#[derive(thiserror::Error, Debug)]
pub enum TEdgeConfigError {
#[error("TOML parse error")]
Expand Down Expand Up @@ -29,6 +31,9 @@ pub enum TEdgeConfigError {

#[error(transparent)]
DirNotFound(#[from] tedge_utils::paths::PathsError),

#[error(transparent)]
ConfigNotSet(#[from] ConfigNotSet),
}

impl TEdgeConfigError {
Expand Down
36 changes: 12 additions & 24 deletions crates/core/tedge_agent/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ use flockfile::Flockfile;
use flockfile::FlockfileError;
use std::fmt::Debug;
use tedge_actors::Runtime;
use tedge_config::ConfigRepository;
use tedge_config::ConfigSettingAccessor;
use tedge_config::DataPathSetting;
use tedge_config::Flag;
use tedge_config::HttpBindAddressSetting;
use tedge_config::HttpPortSetting;
use tedge_config::LockFilesSetting;
use tedge_config::LogPathSetting;
use tedge_config::RunPathSetting;
use tedge_config::TEdgeConfigLocation;
use tedge_health_ext::HealthMonitorBuilder;
use tedge_mqtt_ext::MqttActorBuilder;
use tedge_mqtt_ext::MqttConfig;
Expand All @@ -39,18 +29,18 @@ pub struct AgentConfig {
pub sw_update_config: SoftwareManagerConfig,
pub config_dir: Utf8PathBuf,
pub run_dir: Utf8PathBuf,
pub use_lock: Flag,
pub use_lock: bool,
pub log_dir: Utf8PathBuf,
pub data_dir: Utf8PathBuf,
}

impl AgentConfig {
pub fn from_tedge_config(
tedge_config_location: &TEdgeConfigLocation,
tedge_config_location: &tedge_config::TEdgeConfigLocation,
) -> Result<Self, anyhow::Error> {
let config_repository =
tedge_config::TEdgeConfigRepository::new(tedge_config_location.clone());
let tedge_config = config_repository.load()?;
let tedge_config = config_repository.load_new()?;

let config_dir = tedge_config_location.tedge_config_root_path.clone();

Expand All @@ -60,13 +50,14 @@ impl AgentConfig {
.with_session_name(TEDGE_AGENT);

// HTTP config
let data_dir = tedge_config.query(DataPathSetting)?;
let http_bind_address = tedge_config.query(HttpBindAddressSetting)?;
let http_port = tedge_config.query(HttpPortSetting)?.0;
let data_dir = tedge_config.data.path.clone();
let http_bind_address = tedge_config.http.bind.address;
let http_port = tedge_config.http.bind.port;

let http_config = HttpConfig::default()
.with_data_dir(data_dir.clone())
.with_port(http_port)
.with_ip_address(http_bind_address.into());
.with_ip_address(http_bind_address);

// Restart config
let restart_config = RestartManagerConfig::from_tedge_config(tedge_config_location)?;
Expand All @@ -75,14 +66,11 @@ impl AgentConfig {
let sw_update_config = SoftwareManagerConfig::from_tedge_config(tedge_config_location)?;

// For flockfile
let run_dir = tedge_config.query(RunPathSetting)?;
let use_lock = tedge_config.query(LockFilesSetting)?;
let run_dir = tedge_config.run.path.clone();
let use_lock = tedge_config.run.lock_files;

// For agent specific
let log_dir = tedge_config
.query(LogPathSetting)?
.join("tedge")
.join("agent");
let log_dir = tedge_config.logs.path.join("tedge").join("agent");

Ok(Self {
mqtt_config,
Expand All @@ -107,7 +95,7 @@ pub struct Agent {
impl Agent {
pub fn try_new(name: &str, config: AgentConfig) -> Result<Self, FlockfileError> {
let mut flock = None;
if config.use_lock.is_set() {
if config.use_lock {
flock = Some(check_another_instance_is_not_running(
name,
config.run_dir.as_std_path(),
Expand Down
14 changes: 4 additions & 10 deletions crates/core/tedge_agent/src/restart_manager/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use camino::Utf8PathBuf;
use tedge_config::ConfigRepository;
use tedge_config::ConfigSettingAccessor;
use tedge_config::TEdgeConfigError;
use tedge_config::TEdgeConfigLocation;
use tedge_config::TmpPathSetting;

#[derive(Debug, Clone)]
pub struct RestartManagerConfig {
pub tmp_dir: Utf8PathBuf,
Expand All @@ -20,13 +14,13 @@ impl RestartManagerConfig {
}

pub fn from_tedge_config(
tedge_config_location: &TEdgeConfigLocation,
) -> Result<RestartManagerConfig, TEdgeConfigError> {
tedge_config_location: &tedge_config::TEdgeConfigLocation,
) -> Result<RestartManagerConfig, tedge_config::TEdgeConfigError> {
let config_repository =
tedge_config::TEdgeConfigRepository::new(tedge_config_location.clone());
let tedge_config = config_repository.load()?;
let tedge_config = config_repository.load_new()?;

let tmp_dir = tedge_config.query(TmpPathSetting)?;
let tmp_dir = tedge_config.tmp.path.clone();
let config_dir = tedge_config_location.tedge_config_root_path.clone();

Ok(Self::new(&tmp_dir, &config_dir))
Expand Down
10 changes: 3 additions & 7 deletions crates/core/tedge_agent/src/software_manager/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ use tedge_api::SoftwareRequestResponse;
use tedge_api::SoftwareType;
use tedge_api::SoftwareUpdateRequest;
use tedge_api::SoftwareUpdateResponse;
use tedge_config::ConfigRepository;
use tedge_config::ConfigSettingAccessorStringExt;
use tedge_config::SoftwarePluginDefaultSetting;
use tedge_config::TEdgeConfigLocation;
use tracing::error;
use tracing::log::warn;

Expand Down Expand Up @@ -227,10 +223,10 @@ impl SoftwareManagerActor {
}

fn get_default_plugin(
config_location: &TEdgeConfigLocation,
config_location: &tedge_config::TEdgeConfigLocation,
) -> Result<Option<SoftwareType>, SoftwareManagerError> {
let config_repository = tedge_config::TEdgeConfigRepository::new(config_location.clone());
let tedge_config = config_repository.load()?;
let tedge_config = config_repository.load_new()?;

Ok(tedge_config.query_string_optional(SoftwarePluginDefaultSetting)?)
Ok(tedge_config.software.plugin.default.clone().or_none().cloned())
}
32 changes: 14 additions & 18 deletions crates/core/tedge_agent/src/software_manager/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
use camino::Utf8PathBuf;
use tedge_config::ConfigRepository;
use tedge_config::ConfigSettingAccessor;
use tedge_config::ConfigSettingAccessorStringExt;
use tedge_config::LogPathSetting;
use tedge_config::SoftwarePluginDefaultSetting;
use tedge_config::TEdgeConfigError;
use tedge_config::TEdgeConfigLocation;
use tedge_config::TmpPathSetting;

#[derive(Debug, Clone)]
pub struct SoftwareManagerConfig {
pub tmp_dir: Utf8PathBuf,
Expand Down Expand Up @@ -39,24 +31,28 @@ impl SoftwareManagerConfig {

pub fn from_tedge_config(
tedge_config_location: &TEdgeConfigLocation,
) -> Result<SoftwareManagerConfig, TEdgeConfigError> {
) -> Result<SoftwareManagerConfig, tedge_config::TEdgeConfigError> {
let config_repository =
tedge_config::TEdgeConfigRepository::new(tedge_config_location.clone());
let tedge_config = config_repository.load()?;
let tedge_config = config_repository.load_new()?;

let config_dir = tedge_config_location.tedge_config_root_path.clone();

let tmp_dir = tedge_config.query(TmpPathSetting)?;
let tmp_dir = &tedge_config.tmp.path;
let sm_plugins_dir = config_dir.join("sm-plugins");
let log_dir = tedge_config
.query(LogPathSetting)?
.join("tedge")
.join("agent");
let default_plugin_type =
tedge_config.query_string_optional(SoftwarePluginDefaultSetting)?;
let log_dir = tedge_config.logs.path.join("tedge").join("agent");
let default_plugin_type = Some(
tedge_config
.software
.plugin
.default
.clone()
.or_config_not_set()?
.to_string(),
);

Ok(Self::new(
&tmp_dir,
tmp_dir,
&config_dir,
&sm_plugins_dir,
&log_dir,
Expand Down
3 changes: 3 additions & 0 deletions crates/core/tedge_agent/src/software_manager/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub enum SoftwareManagerError {

#[error(transparent)]
FromConfigSetting(#[from] tedge_config::ConfigSettingError),

#[error(transparent)]
ConfigNotSet(#[from] tedge_config::new::ConfigNotSet),
}

impl From<SoftwareManagerError> for tedge_actors::RuntimeError {
Expand Down
1 change: 0 additions & 1 deletion crates/core/tedge_agent/src/software_manager/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ async fn test_new_software_update_operation() -> Result<(), DynError> {

match converter_box.recv().await.unwrap() {
SoftwareResponse::SoftwareUpdateResponse(res) => {
dbg!(&res);
assert_eq!(res.response.status, OperationStatus::Executing);
}
SoftwareResponse::SoftwareListResponse(_) => {
Expand Down

0 comments on commit 91c1d08

Please sign in to comment.