summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorinitard <solo@softwareag.com>2022-02-25 13:13:46 +0000
committerinitard <solo@softwareag.com>2022-03-04 10:48:34 +0000
commit1139e7bd1f5774fe426b880fb57cf7938225bacf (patch)
tree2575c603c8a8f959be975510e10b33c9f114178a
parent15e59352f97fc79be3544155a7089f16c26a8656 (diff)
configurable run path (#858)
- added logs.path to `tedge` command - can now use `tedge config set run.path /some/run/path` - agent and mapper use the run config Signed-off-by: initard <solo@softwareag.com>
-rw-r--r--crates/common/flockfile/src/unix.rs9
-rw-r--r--crates/common/tedge_config/src/models/file_path.rs6
-rw-r--r--crates/common/tedge_config/src/settings.rs13
-rw-r--r--crates/common/tedge_config/src/tedge_config.rs25
-rw-r--r--crates/common/tedge_config/src/tedge_config_defaults.rs6
-rw-r--r--crates/common/tedge_config/src/tedge_config_dto.rs3
-rw-r--r--crates/common/tedge_config/tests/test_tedge_config.rs1
-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
11 files changed, 98 insertions, 16 deletions
diff --git a/crates/common/flockfile/src/unix.rs b/crates/common/flockfile/src/unix.rs
index 28646601..fe2053a0 100644
--- a/crates/common/flockfile/src/unix.rs
+++ b/crates/common/flockfile/src/unix.rs
@@ -104,9 +104,12 @@ impl AsRef<Path> for Flockfile {
}
}
-/// Check /run/lock/ for a lock file of a given `app_name`
-pub fn check_another_instance_is_not_running(app_name: &str) -> Result<Flockfile, FlockfileError> {
- match Flockfile::new_lock(format!("{}.lock", app_name)) {
+/// Check `run_dir`/lock/ for a lock file of a given `app_name`
+pub fn check_another_instance_is_not_running(
+ app_name: &str,
+ run_dir: &PathBuf,
+) -> Result<Flockfile, FlockfileError> {
+ match Flockfile::new_lock(run_dir.join(format!("{}.lock", app_name))) {
Ok(file) => Ok(file),
Err(err) => {
return match &err {
diff --git a/crates/common/tedge_config/src/models/file_path.rs b/crates/common/tedge_config/src/models/file_path.rs
index 29f8a415..62ef5723 100644
--- a/crates/common/tedge_config/src/models/file_path.rs
+++ b/crates/common/tedge_config/src/models/file_path.rs
@@ -44,3 +44,9 @@ impl std::fmt::Display for FilePath {
write!(f, "{}", self.0.display())
}
}
+
+impl Into<PathBuf> for FilePath {
+ fn into(self) -> PathBuf {
+ PathBuf::from(self.0)
+ }
+}
diff --git a/crates/common/tedge_config/src/settings.rs b/crates/common/tedge_config/src/settings.rs
index c2a12956..42a3f0f3 100644
--- a/crates/common/tedge_config/src/settings.rs
+++ b/crates/common/tedge_config/src/settings.rs
@@ -311,3 +311,16 @@ impl ConfigSetting for LogPathDefaultSetting {
type Value = FilePath;
}
+
+pub struct RunPathDefaultSetting;
+
+impl ConfigSetting for RunPathDefaultSetting {
+ const KEY: &'static str = "run.path";
+
+ const DESCRIPTION: &'static str = concat!(
+ "The default path to be used for runtime information",
+ "Example: /run"
+ );
+
+ type Value = FilePath;
+}
diff --git a/crates/common/tedge_config/src/tedge_config.rs b/crates/common/tedge_config/src/tedge_config.rs
index f79e3875..f3567d28 100644
--- a/crates/common/tedge_config/src/tedge_config.rs
+++ b/crates/common/tedge_config/src/tedge_config.rs
@@ -558,3 +558,28 @@ impl ConfigSettingAccessor<LogPathDefaultSetting> for TEdgeConfig {
Ok(())
}
}
+
+impl ConfigSettingAccessor<RunPathDefaultSetting> for TEdgeConfig {
+ fn query(&self, _setting: RunPathDefaultSetting) -> ConfigSettingResult<FilePath> {
+ Ok(self
+ .data
+ .run
+ .dir_path
+ .clone()
+ .unwrap_or_else(|| self.config_defaults.default_logs_path.clone()))
+ }
+
+ fn update(
+ &mut self,
+ _setting: RunPathDefaultSetting,
+ value: FilePath,
+ ) -> ConfigSettingResult<()> {
+ self.data.run.dir_path = Some(value);
+ Ok(())
+ }
+
+ fn unset(&mut self, _setting: RunPathDefaultSetting) -> ConfigSettingResult<()> {
+ self.data.run.dir_path = None;
+ Ok(())
+ }
+}
diff --git a/crates/common/tedge_config/src/tedge_config_defaults.rs b/crates/common/tedge_config/src/tedge_config_defaults.rs
index d950fe5e..b035445c 100644
--- a/crates/common/tedge_config/src/tedge_config_defaults.rs
+++ b/crates/common/tedge_config/src/tedge_config_defaults.rs
@@ -49,6 +49,9 @@ pub struct TEdgeConfigDefaults {
/// Default log path
pub default_logs_path: FilePath,
+ /// Default run path
+ pub default_run_path: FilePath,
+
/// Default device type
pub default_device_type: String,
@@ -61,6 +64,7 @@ impl From<&TEdgeConfigLocation> for TEdgeConfigDefaults {
let system_cert_path = Path::new(DEFAULT_ETC_PATH).join("ssl").join("certs");
let tmp_path = Path::new(DEFAULT_TMP_PATH);
let logs_path = Path::new(DEFAULT_LOG_PATH);
+ let run_path = Path::new(DEFAULT_RUN_PATH);
Self {
default_device_cert_path: config_location
.tedge_config_root_path()
@@ -78,6 +82,7 @@ impl From<&TEdgeConfigLocation> for TEdgeConfigDefaults {
default_mqtt_port: Port(DEFAULT_PORT),
default_tmp_path: tmp_path.into(),
default_logs_path: logs_path.into(),
+ default_run_path: run_path.into(),
default_device_type: DEFAULT_DEVICE_TYPE.into(),
default_mqtt_bind_address: IpAddress::default(),
}
@@ -104,6 +109,7 @@ fn test_from_tedge_config_location() {
default_mqtt_port: Port(DEFAULT_PORT),
default_tmp_path: FilePath::from("/tmp"),
default_logs_path: FilePath::from("/var/log"),
+ default_run_path: FilePath::from("/run"),
default_device_type: DEFAULT_DEVICE_TYPE.into(),
default_mqtt_bind_address: IpAddress::default(),
}
diff --git a/crates/common/tedge_config/src/tedge_config_dto.rs b/crates/common/tedge_config/src/tedge_config_dto.rs
index 1bb1fb20..28cbfcbd 100644
--- a/crates/common/tedge_config/src/tedge_config_dto.rs
+++ b/crates/common/tedge_config/src/tedge_config_dto.rs
@@ -28,6 +28,9 @@ pub(crate) struct TEdgeConfigDto {
#[serde(default)]
pub(crate) logs: PathConfigDto,
+
+ #[serde(default)]
+ pub(crate) run: PathConfigDto,
}
/// Represents the device specific configurations defined in the [device] section
diff --git a/crates/common/tedge_config/tests/test_tedge_config.rs b/crates/common/tedge_config/tests/test_tedge_config.rs
index ef3f445b..61e59ac4 100644
--- a/crates/common/tedge_config/tests/test_tedge_config.rs
+++ b/crates/common/tedge_config/tests/test_tedge_config.rs
@@ -899,6 +899,7 @@ fn dummy_tedge_config_defaults() -> TEdgeConfigDefaults {
default_mqtt_port: Port(1883),
default_tmp_path: FilePath::from("/tmp"),
default_logs_path: FilePath::from("/var/log"),
+ default_run_path: FilePath::from("/run"),
default_device_type: String::from("test"),
default_mqtt_bind_address: IpAddress(IpAddr::V4(Ipv4Addr::LOCALHOST)),
}
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();