diff options
author | initard <solo@softwareag.com> | 2022-05-09 10:31:16 +0100 |
---|---|---|
committer | initard <solo@softwareag.com> | 2022-05-20 16:09:23 +0100 |
commit | 8645e200799d40a2d0e7231c457bd096d926d7ec (patch) | |
tree | 7455c39da7fba38b7563f104633dad2dfff42f1a /crates/core | |
parent | 82f79d547f315cf482dca28c6454058c109a4871 (diff) |
c8y_log_plugin implementation #1077
- log plugin is now a daemon
- log file uses a configuration file to request logs from c8y UI
- log plugin uses inotify to automatically update c8y of new logs files
Signed-off-by: initard <solo@softwareag.com>
Diffstat (limited to 'crates/core')
-rw-r--r-- | crates/core/c8y_api/src/http_proxy.rs | 18 | ||||
-rw-r--r-- | crates/core/c8y_smartrest/src/smartrest_deserializer.rs | 29 | ||||
-rw-r--r-- | crates/core/tedge_mapper/src/c8y/converter.rs | 44 | ||||
-rw-r--r-- | crates/core/tedge_mapper/src/c8y/tests.rs | 12 |
4 files changed, 24 insertions, 79 deletions
diff --git a/crates/core/c8y_api/src/http_proxy.rs b/crates/core/c8y_api/src/http_proxy.rs index 78d8280b..b9f78e32 100644 --- a/crates/core/c8y_api/src/http_proxy.rs +++ b/crates/core/c8y_api/src/http_proxy.rs @@ -42,6 +42,7 @@ pub trait C8YHttpProxy: Send + Sync { async fn upload_log_binary( &mut self, + log_type: &str, log_content: &str, ) -> Result<String, SMCumulocityMapperError>; @@ -269,20 +270,6 @@ impl JwtAuthHttpProxy { Ok(internal_id) } - fn create_log_event(&self) -> C8yCreateEvent { - let c8y_managed_object = C8yManagedObject { - id: self.end_point.c8y_internal_id.clone(), - }; - - C8yCreateEvent::new( - Some(c8y_managed_object), - "c8y_Logfile".to_string(), - OffsetDateTime::now_utc(), - "software-management".to_string(), - HashMap::new(), - ) - } - fn create_event( &self, event_type: String, @@ -388,11 +375,12 @@ impl C8YHttpProxy for JwtAuthHttpProxy { async fn upload_log_binary( &mut self, + log_type: &str, log_content: &str, ) -> Result<String, SMCumulocityMapperError> { let token = self.get_jwt_token().await?; - let log_event = self.create_log_event(); + let log_event = self.create_event(log_type.to_string(), None, None); let event_response_id = self.send_event_internal(log_event).await?; let binary_upload_event_url = self .end_point diff --git a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs index ff5c177c..0d2fb0c5 100644 --- a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs +++ b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs @@ -1,4 +1,4 @@ -use crate::error::{SMCumulocityMapperError, SmartRestDeserializerError}; +use crate::error::SmartRestDeserializerError; use agent_interface::{SoftwareModule, SoftwareModuleUpdate, SoftwareUpdateRequest}; use csv::ReaderBuilder; use download::DownloadInfo; @@ -306,9 +306,7 @@ impl SmartRestJwtResponse { /// path.push("/path/to/file/with/date/in/path-2021-10-27T10:29:58Z"); /// let path_bufdate_time = get_datetime_from_file_path(&path).unwrap(); /// ``` -pub fn get_datetime_from_file_path( - log_path: &Path, -) -> Result<OffsetDateTime, SMCumulocityMapperError> { +pub fn get_datetime_from_file_path(log_path: &Path) -> Option<OffsetDateTime> { if let Some(stem_string) = log_path.file_stem().and_then(|s| s.to_str()) { // a typical file stem looks like this: software-list-2021-10-27T10:29:58Z. // to extract the date, rsplit string on "-" and take (last) 3 @@ -317,16 +315,17 @@ pub fn get_datetime_from_file_path( stem_string_vec.reverse(); // join on '-' to get the date string let date_string = stem_string_vec.join("-"); - let dt = OffsetDateTime::parse(&date_string, &format_description::well_known::Rfc3339)?; - - return Ok(dt); - } - match log_path.to_str() { - Some(path) => Err(SMCumulocityMapperError::InvalidDateInFileName( - path.to_string(), - )), - None => Err(SMCumulocityMapperError::InvalidUtf8Path), + let dt = OffsetDateTime::parse(&date_string, &format_description::well_known::Rfc3339); + match dt { + Ok(dt) => { + return Some(dt); + } + Err(_) => { + return None; + } + } } + None } #[cfg(test)] @@ -650,7 +649,7 @@ mod tests { // this should return an Ok Result. let path_buf = PathBuf::from_str(file_path).unwrap(); let path_buf_datetime = get_datetime_from_file_path(&path_buf); - assert!(path_buf_datetime.is_ok()); + assert!(path_buf_datetime.is_some()); } #[test_case("/path/to/software-list-2021-10-27-10:44:44Z.log")] @@ -662,6 +661,6 @@ mod tests { // this should return an err. let path_buf = PathBuf::from_str(file_path).unwrap(); let path_buf_datetime = get_datetime_from_file_path(&path_buf); - assert!(path_buf_datetime.is_err()); + assert!(path_buf_datetime.is_none()); } } diff --git a/crates/core/tedge_mapper/src/c8y/converter.rs b/crates/core/tedge_mapper/src/c8y/converter.rs index e830a7a4..dc407100 100644 --- a/crates/core/tedge_mapper/src/c8y/converter.rs +++ b/crates/core/tedge_mapper/src/c8y/converter.rs @@ -18,18 +18,17 @@ use c8y_smartrest::{ smartrest_serializer::{ CumulocitySupportedOperations, SmartRestGetPendingOperations, SmartRestSerializer, SmartRestSetOperationToExecuting, SmartRestSetOperationToFailed, - SmartRestSetOperationToSuccessful, SmartRestSetSupportedLogType, - SmartRestSetSupportedOperations, + SmartRestSetOperationToSuccessful, SmartRestSetSupportedOperations, }, }; use c8y_translator::json; use logged_command::LoggedCommand; use mqtt_channel::{Message, Topic, TopicFilter}; -use plugin_sm::operation_logs::{OperationLogs, OperationLogsError}; +use plugin_sm::operation_logs::OperationLogs; use std::{ collections::{hash_map::Entry, HashMap, HashSet}, - fs::{self, File}, + fs::File, io::Read, path::{Path, PathBuf}, }; @@ -333,9 +332,6 @@ where &self.device_type, )); - let supported_log_types_message = self.wrap_error(create_supported_log_types_message( - &self.operation_logs.log_dir, - )); let pending_operations_message = self.wrap_error(create_get_pending_operations_message()); let software_list_message = self.wrap_error(create_get_software_list_message()); @@ -343,7 +339,6 @@ where inventory_fragments_message, supported_operations_message, device_data_message, - supported_log_types_message, pending_operations_message, software_list_message, ]) @@ -556,39 +551,6 @@ fn create_get_pending_operations_message() -> Result<Message, ConversionError> { Ok(Message::new(&topic, payload)) } -fn get_supported_log_types(log_dir: &Path) -> Result<Vec<String>, ConversionError> { - let mut result = HashSet::new(); - - let paths = fs::read_dir(&log_dir).unwrap(); - for path in paths { - let path = path?; - if fs::metadata(path.path())?.is_file() { - let file_name = path.file_name(); - let file_name = file_name - .to_str() - .ok_or(OperationLogsError::FileFormatError)?; - - // FIXME: this is a hotfix to map "software-list" and "software-update" to "software-management" - // this should be fixed in https://github.com/thin-edge/thin-edge.io/issues/1077 - if file_name.starts_with("software-list") | file_name.starts_with("software-update") { - result.insert("software-management".to_string()); - } else { - let log_type = file_name.split('-').next(); - let log_type = log_type.ok_or(OperationLogsError::FileFormatError)?; - result.insert(log_type.to_string()); - } - } - } - Ok(Vec::from_iter(result)) -} - -fn create_supported_log_types_message(log_dir: &Path) -> Result<Message, ConversionError> { - let supported_operation_types = get_supported_log_types(log_dir)?; - let payload = SmartRestSetSupportedLogType::from(supported_operation_types).to_smartrest()?; - let topic = C8yTopic::SmartRestResponse.to_topic()?; - Ok(Message::new(&topic, payload)) -} - fn create_supported_operations_fragments_message() -> Result<Message, ConversionError> { let ops = Operations::try_new(SUPPORTED_OPERATIONS_DIRECTORY, C8Y_CLOUD)?; let ops = ops.get_operations_list(); diff --git a/crates/core/tedge_mapper/src/c8y/tests.rs b/crates/core/tedge_mapper/src/c8y/tests.rs index c15ce8f9..117bdced 100644 --- a/crates/core/tedge_mapper/src/c8y/tests.rs +++ b/crates/core/tedge_mapper/src/c8y/tests.rs @@ -57,13 +57,8 @@ async fn mapper_publishes_a_supported_operation_and_a_pending_operations_onto_c8 // Start SM Mapper let sm_mapper = start_c8y_mapper(broker.port).await; - // Expect both 118 and 500 messages has been received on `c8y/s/us`, if no msg received for the timeout the test fails. - mqtt_tests::assert_received_all_expected( - &mut messages, - TEST_TIMEOUT_MS, - &["software-management", "500\n"], - ) - .await; + // Expect 500 messages has been received on `c8y/s/us`, if no msg received for the timeout the test fails. + mqtt_tests::assert_received_all_expected(&mut messages, TEST_TIMEOUT_MS, &["500\n"]).await; sm_mapper.unwrap().abort(); } @@ -300,7 +295,7 @@ async fn mapper_fails_during_sw_update_recovers_and_process_response() -> Result mqtt_tests::assert_received_all_expected( &mut responses, TEST_TIMEOUT_MS, - &["software-management", "500\n", "503,c8y_SoftwareUpdate,\n"], + &["500\n", "503,c8y_SoftwareUpdate,\n"], ) .await; @@ -896,6 +891,7 @@ impl C8YHttpProxy for FakeC8YHttpProxy { async fn upload_log_binary( &mut self, + _log_type: &str, _log_content: &str, ) -> Result<String, SMCumulocityMapperError> { Ok("fake/upload/url".into()) |