summaryrefslogtreecommitdiffstats
path: root/crates/core
diff options
context:
space:
mode:
Diffstat (limited to 'crates/core')
-rw-r--r--crates/core/tedge/src/cli/config/config_key.rs1
-rw-r--r--crates/core/tedge_agent/src/agent.rs21
-rw-r--r--crates/core/tedge_agent/src/restart_operation_handler.rs24
-rw-r--r--crates/core/tedge_mapper/src/main.rs5
4 files changed, 38 insertions, 13 deletions
diff --git a/crates/core/tedge/src/cli/config/config_key.rs b/crates/core/tedge/src/cli/config/config_key.rs
index ba760614..064791a4 100644
--- a/crates/core/tedge/src/cli/config/config_key.rs
+++ b/crates/core/tedge/src/cli/config/config_key.rs
@@ -59,6 +59,7 @@ impl ConfigKey {
config_key!(SoftwarePluginDefaultSetting),
config_key!(TmpPathDefaultSetting),
config_key!(LogPathDefaultSetting),
+ config_key!(RunPathDefaultSetting),
]
}
}
diff --git a/crates/core/tedge_agent/src/agent.rs b/crates/core/tedge_agent/src/agent.rs
index 07a57dde..12d77135 100644
--- a/crates/core/tedge_agent/src/agent.rs
+++ b/crates/core/tedge_agent/src/agent.rs
@@ -43,6 +43,7 @@ pub struct SmAgentConfig {
pub response_topic_restart: Topic,
pub sm_home: PathBuf,
pub log_dir: PathBuf,
+ pub run_dir: PathBuf,
config_location: TEdgeConfigLocation,
pub download_dir: PathBuf,
}
@@ -79,6 +80,8 @@ impl Default for SmAgentConfig {
let log_dir = PathBuf::from(&format!("{DEFAULT_LOG_PATH}/tedge/agent"));
+ let run_dir = PathBuf::from(DEFAULT_RUN_PATH);
+
let config_location = TEdgeConfigLocation::default();
let download_dir = PathBuf::from("/tmp");
@@ -95,6 +98,7 @@ impl Default for SmAgentConfig {
response_topic_restart,
sm_home,
log_dir,
+ run_dir,
config_location,
download_dir,
}
@@ -120,13 +124,15 @@ impl SmAgentConfig {
let tedge_download_dir = tedge_config.query_string(TmpPathDefaultSetting)?.into();
let tedge_log_dir = tedge_config.query_string(LogPathDefaultSetting)?.into();
+ let tedge_run_dir = tedge_config.query_string(RunPathDefaultSetting)?.into();
Ok(SmAgentConfig::default()
.with_sm_home(tedge_config_path)
.with_mqtt_config(mqtt_config)
.with_config_location(tedge_config_location)
.with_download_directory(tedge_download_dir)
- .with_log_directory(tedge_log_dir))
+ .with_log_directory(tedge_log_dir)
+ .with_run_directory(tedge_run_dir))
}
pub fn with_sm_home(self, sm_home: PathBuf) -> Self {
@@ -160,6 +166,13 @@ impl SmAgentConfig {
..self
}
}
+
+ pub fn with_run_directory(self, tmp_dir: PathBuf) -> Self {
+ Self {
+ run_dir: tmp_dir,
+ ..self
+ }
+ }
}
#[derive(Debug)]
@@ -172,7 +185,7 @@ pub struct SmAgent {
impl SmAgent {
pub fn try_new(name: &str, mut config: SmAgentConfig) -> Result<Self, AgentError> {
- let flock = check_another_instance_is_not_running(name)?;
+ let flock = check_another_instance_is_not_running(name, &config.run_dir)?;
info!("{} starting", &name);
let persistance_store = AgentStateRepository::new(config.sm_home.clone());
@@ -482,7 +495,7 @@ impl SmAgent {
let () = responses
.publish(Message::new(topic, executing_response.to_bytes()?))
.await?;
- let () = restart_operation::create_slash_run_file()?;
+ let () = restart_operation::create_slash_run_file(&self.config.run_dir)?;
let _process_result = std::process::Command::new("sudo").arg("sync").status();
// state = "Restarting"
@@ -536,7 +549,7 @@ impl SmAgent {
StateStatus::Restart(RestartOperationStatus::Restarting) => {
let _state = self.persistance_store.clear().await?;
- if restart_operation::has_rebooted()? {
+ if restart_operation::has_rebooted(&self.config.run_dir)? {
info!("Device restart successful.");
status = OperationStatus::Successful;
}
diff --git a/crates/core/tedge_agent/src/restart_operation_handler.rs b/crates/core/tedge_agent/src/restart_operation_handler.rs
index 245d79d3..0873c43d 100644
--- a/crates/core/tedge_agent/src/restart_operation_handler.rs
+++ b/crates/core/tedge_agent/src/restart_operation_handler.rs
@@ -1,10 +1,16 @@
pub mod restart_operation {
use crate::error::AgentError;
- use std::{fs::File, fs::OpenOptions, io::Read, io::Write, path::Path};
+ use std::{
+ fs::File,
+ fs::OpenOptions,
+ io::Read,
+ io::Write,
+ path::{Path, PathBuf},
+ };
use time::OffsetDateTime;
- const SLASH_RUN_PATH_TEDGE_AGENT_RESTART: &str = "/run/tedge_agent/tedge_agent_restart";
+ const SLASH_RUN_PATH_TEDGE_AGENT_RESTART: &str = "tedge_agent/tedge_agent_restart";
const SLASH_PROC_UPTIME: &str = "/proc/uptime";
/// creates an empty file in /run
@@ -14,8 +20,9 @@ pub mod restart_operation {
/// ```
/// let () = RestartOperationHelper::create_slash_run_file()?;
/// ```
- pub fn create_slash_run_file() -> Result<(), AgentError> {
- let path = Path::new(SLASH_RUN_PATH_TEDGE_AGENT_RESTART);
+ pub fn create_slash_run_file(run_dir: &PathBuf) -> Result<(), AgentError> {
+ let path = &run_dir.join(SLASH_RUN_PATH_TEDGE_AGENT_RESTART);
+ let path = Path::new(path);
let mut file = match OpenOptions::new()
.create(true)
@@ -33,8 +40,9 @@ pub mod restart_operation {
Ok(())
}
- pub fn slash_run_file_exists() -> bool {
- std::path::Path::new(&SLASH_RUN_PATH_TEDGE_AGENT_RESTART.to_string()).exists()
+ pub fn slash_run_file_exists(run_dir: &PathBuf) -> bool {
+ let path = &run_dir.join(SLASH_RUN_PATH_TEDGE_AGENT_RESTART);
+ std::path::Path::new(path).exists()
}
/// returns the datetime of `SLASH_RUN_PATH_TEDGE_AGENT_RESTART` "modified at".
@@ -92,10 +100,10 @@ pub mod restart_operation {
}
/// checks if system rebooted by comparing dt of tedge_agent_restart with dt of system restart.
- pub fn has_rebooted() -> Result<bool, AgentError> {
+ pub fn has_rebooted(run_dir: &PathBuf) -> Result<bool, AgentError> {
// there is no slash run file after the reboot, so we assume success.
// this is true for most of the cases as "/run/" is normally cleared after a reboot.
- if !slash_run_file_exists() {
+ if !slash_run_file_exists(&run_dir) {
return Ok(true);
}
diff --git a/crates/core/tedge_mapper/src/main.rs b/crates/core/tedge_mapper/src/main.rs
index 04adf9c9..d6de0301 100644
--- a/crates/core/tedge_mapper/src/main.rs
+++ b/crates/core/tedge_mapper/src/main.rs
@@ -84,7 +84,10 @@ async fn main() -> anyhow::Result<()> {
tedge_config::TEdgeConfigLocation::from_custom_root(&mapper.config_dir);
let config = tedge_config::TEdgeConfigRepository::new(tedge_config_location.clone()).load()?;
// Run only one instance of a mapper
- let _flock = check_another_instance_is_not_running(&mapper.name.to_string())?;
+ let _flock = check_another_instance_is_not_running(
+ &mapper.name.to_string(),
+ &config.query(RunPathDefaultSetting)?.into(),
+ )?;
if mapper.init {
let mut mapper = CumulocityMapper::new();