From 61422edb0f7fe342b93998cb9786f78c3bb1f86e Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Thu, 7 Nov 2024 08:37:11 +0100 Subject: [PATCH 01/11] Added command for collecting agama-web-server logs. And removed some which are not existing anymore. Like /var/log/zypper/ dir. --- rust/agama-lib/src/logs.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rust/agama-lib/src/logs.rs b/rust/agama-lib/src/logs.rs index beed88c96..ee06464bb 100644 --- a/rust/agama-lib/src/logs.rs +++ b/rust/agama-lib/src/logs.rs @@ -32,19 +32,18 @@ use std::process::Command; use tempfile::TempDir; use utoipa::ToSchema; -const DEFAULT_COMMANDS: [(&str, &str); 3] = [ +const DEFAULT_COMMANDS: [(&str, &str); 4] = [ // (, ) ("journalctl -u agama", "agama"), ("journalctl -u agama-auto", "agama-auto"), + ("journalctl -u agama-web-server", "agama-web-server"), ("journalctl --dmesg", "dmesg"), ]; -const DEFAULT_PATHS: [&str; 14] = [ +const DEFAULT_PATHS: [&str; 12] = [ // logs "/var/log/YaST2", "/var/log/zypper.log", - "/var/log/zypper/history*", - "/var/log/zypper/pk_backend_zypp", "/var/log/pbl.log", "/var/log/linuxrc.log", "/var/log/wickedd.log", From 8cf14ab5b7de85cfca5ce354e897c4d56b01b3b9 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Mon, 11 Nov 2024 09:03:25 +0100 Subject: [PATCH 02/11] A command for listing agama related packages with version --- rust/agama-lib/src/logs.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/rust/agama-lib/src/logs.rs b/rust/agama-lib/src/logs.rs index ee06464bb..c596a5ec0 100644 --- a/rust/agama-lib/src/logs.rs +++ b/rust/agama-lib/src/logs.rs @@ -32,15 +32,16 @@ use std::process::Command; use tempfile::TempDir; use utoipa::ToSchema; -const DEFAULT_COMMANDS: [(&str, &str); 4] = [ +const DEFAULT_COMMANDS: [(&str, &str); 5] = [ // (, ) ("journalctl -u agama", "agama"), ("journalctl -u agama-auto", "agama-auto"), ("journalctl -u agama-web-server", "agama-web-server"), ("journalctl --dmesg", "dmesg"), + ("rpm -qa", "rpm-qa" ), ]; -const DEFAULT_PATHS: [&str; 12] = [ +const DEFAULT_PATHS: [&str; 13] = [ // logs "/var/log/YaST2", "/var/log/zypper.log", @@ -55,6 +56,7 @@ const DEFAULT_PATHS: [&str; 12] = [ "/etc/install.inf", "/etc/os-release", "/linuxrc.config", + "/.packages.root", ]; const DEFAULT_RESULT: &str = "/run/agama/agama-logs"; @@ -142,7 +144,22 @@ impl LogItem for LogPath { let options = CopyOptions::new(); // fs_extra's own Error doesn't implement From trait so ? operator is unusable match copy_items(&[self.src_path.as_str()], dst_path, &options) { - Ok(_p) => Ok(()), + Ok(_p) => { + // turns "invisible" files to visible ones (.packages.root -> packages.root) + if let Some(fname) = dst_file.file_name() { + if let Some(name) = fname.to_str().and_then(|n| { + if n.starts_with(".") { + Some(n.trim_start_matches(".")) + } else { + None + } + }) { + let _ = fs::rename(dst_file.clone(), dst_file.with_file_name(name)); + } + } + + Ok(()) + } Err(_e) => Err(io::Error::new( io::ErrorKind::Other, "Copying of a file failed", From e8b0efe9fcd863e97aba4fed4185b5f0948d866a Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 13 Nov 2024 16:48:13 +0100 Subject: [PATCH 03/11] Do not store command output(s) when empty --- rust/agama-lib/src/logs.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/rust/agama-lib/src/logs.rs b/rust/agama-lib/src/logs.rs index c596a5ec0..4e28045d0 100644 --- a/rust/agama-lib/src/logs.rs +++ b/rust/agama-lib/src/logs.rs @@ -188,11 +188,17 @@ impl LogItem for LogCmd { let output = Command::new(cmd_parts[0]) .args(cmd_parts[1..].iter()) .output()?; - let mut file_stdout = File::create(format!("{}.out.log", file_path.display()))?; - let mut file_stderr = File::create(format!("{}.err.log", file_path.display()))?; - file_stdout.write_all(&output.stdout)?; - file_stderr.write_all(&output.stderr)?; + if output.stdout.len() > 0 { + let mut file_stdout = File::create(format!("{}.out.log", file_path.display()))?; + + file_stdout.write_all(&output.stdout)?; + } + if output.stderr.len() > 0 { + let mut file_stderr = File::create(format!("{}.err.log", file_path.display()))?; + + file_stderr.write_all(&output.stderr)?; + } Ok(()) } From 2846105a504e2443089b4f2d1894e6cdea8eeb5d Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 13 Nov 2024 16:53:22 +0100 Subject: [PATCH 04/11] No need to explicitly set archive root permissions on client side --- rust/agama-cli/src/logs.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index cc9a12da0..525e695a2 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -19,7 +19,6 @@ // find current contact information at www.suse.com. use agama_lib::base_http_client::BaseHTTPClient; -use agama_lib::logs::set_archive_permissions; use agama_lib::manager::http_client::ManagerHTTPClient as HTTPClient; use clap::Subcommand; use std::io; @@ -53,9 +52,6 @@ pub async fn run(client: BaseHTTPClient, subcommand: LogsCommands) -> anyhow::Re .await .map_err(|_| anyhow::Error::msg("Downloading of logs failed"))?; - set_archive_permissions(result.clone()) - .map_err(|_| anyhow::Error::msg("Cannot store the logs"))?; - println!("{}", result.clone().display()); Ok(()) From 82e475abe8b7e132bcafd3246f21f67a321f882c Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Wed, 13 Nov 2024 18:23:40 +0100 Subject: [PATCH 05/11] Do not mask backend errors in "agama logs store" --- rust/agama-cli/src/logs.rs | 2 +- rust/agama-lib/src/base_http_client.rs | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 525e695a2..31afef3f6 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -50,7 +50,7 @@ pub async fn run(client: BaseHTTPClient, subcommand: LogsCommands) -> anyhow::Re let result = client .store(dst_file.as_path()) .await - .map_err(|_| anyhow::Error::msg("Downloading of logs failed"))?; + .map_err(|e| anyhow::Error::new(e))?; println!("{}", result.clone().display()); diff --git a/rust/agama-lib/src/base_http_client.rs b/rust/agama-lib/src/base_http_client.rs index 2b1d54b47..5a868ae3a 100644 --- a/rust/agama-lib/src/base_http_client.rs +++ b/rust/agama-lib/src/base_http_client.rs @@ -229,11 +229,18 @@ impl BaseHTTPClient { /// Returns raw reqwest::Response. Use e.g. in case when response content is not /// JSON body but e.g. binary data pub async fn get_raw(&self, path: &str) -> Result { - self.client + let raw: Result<_, ServiceError> = self.client .get(self.url(path)) .send() .await - .map_err(|e| e.into()) + .map_err(|e| e.into()); + let response = raw?; + + if response.status().is_success() { + Ok(response) + } else { + Err(self.build_backend_error(response).await) + } } /// POST/PUT/PATCH an object to a given path and returns server response. From ec59615fa69ccfc41803001042ec3fb425aec4e2 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Thu, 14 Nov 2024 10:14:33 +0100 Subject: [PATCH 06/11] Formatting is almost perfect now --- rust/agama-lib/src/base_http_client.rs | 3 ++- rust/agama-lib/src/logs.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/agama-lib/src/base_http_client.rs b/rust/agama-lib/src/base_http_client.rs index 5a868ae3a..bb29e5bf4 100644 --- a/rust/agama-lib/src/base_http_client.rs +++ b/rust/agama-lib/src/base_http_client.rs @@ -229,7 +229,8 @@ impl BaseHTTPClient { /// Returns raw reqwest::Response. Use e.g. in case when response content is not /// JSON body but e.g. binary data pub async fn get_raw(&self, path: &str) -> Result { - let raw: Result<_, ServiceError> = self.client + let raw: Result<_, ServiceError> = self + .client .get(self.url(path)) .send() .await diff --git a/rust/agama-lib/src/logs.rs b/rust/agama-lib/src/logs.rs index 4e28045d0..415df032f 100644 --- a/rust/agama-lib/src/logs.rs +++ b/rust/agama-lib/src/logs.rs @@ -38,7 +38,7 @@ const DEFAULT_COMMANDS: [(&str, &str); 5] = [ ("journalctl -u agama-auto", "agama-auto"), ("journalctl -u agama-web-server", "agama-web-server"), ("journalctl --dmesg", "dmesg"), - ("rpm -qa", "rpm-qa" ), + ("rpm -qa", "rpm-qa"), ]; const DEFAULT_PATHS: [&str; 13] = [ From 0872ac989c8dce04e7fadcf5ea931558025ded30 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Fri, 22 Nov 2024 15:48:31 +0100 Subject: [PATCH 07/11] Agama's d-bus bus monitoring and error logging --- rust/agama-lib/src/logs.rs | 3 ++- service/share/agama-dbus-monitor.service | 13 +++++++++++++ setup-services.sh | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 service/share/agama-dbus-monitor.service diff --git a/rust/agama-lib/src/logs.rs b/rust/agama-lib/src/logs.rs index 415df032f..9064c143b 100644 --- a/rust/agama-lib/src/logs.rs +++ b/rust/agama-lib/src/logs.rs @@ -41,7 +41,7 @@ const DEFAULT_COMMANDS: [(&str, &str); 5] = [ ("rpm -qa", "rpm-qa"), ]; -const DEFAULT_PATHS: [&str; 13] = [ +const DEFAULT_PATHS: [&str; 14] = [ // logs "/var/log/YaST2", "/var/log/zypper.log", @@ -52,6 +52,7 @@ const DEFAULT_PATHS: [&str; 13] = [ "/var/log/messages", "/var/log/boot.msg", "/var/log/udev.log", + "/run/agama/dbus.log", // config "/etc/install.inf", "/etc/os-release", diff --git a/service/share/agama-dbus-monitor.service b/service/share/agama-dbus-monitor.service new file mode 100644 index 000000000..c2f9d19d0 --- /dev/null +++ b/service/share/agama-dbus-monitor.service @@ -0,0 +1,13 @@ +[Unit] +Description=Agama's D-Bus bus monitor +After=agama.service + +[Service] +Type=exec +ExecStart=/usr/bin/busctl --address unix:path=/run/agama/bus --quiet --match type=error monitor +StandardOutput=file:/run/agama/dbus.log +Restart=always +User=root + +[Install] +WantedBy=default.target diff --git a/setup-services.sh b/setup-services.sh index d41575235..ae6312485 100755 --- a/setup-services.sh +++ b/setup-services.sh @@ -32,6 +32,7 @@ sudosed() { # if agama is already running -> stop it $SUDO systemctl list-unit-files agama.service &>/dev/null && $SUDO systemctl stop agama.service +$SUDO systemctl list-unit-files agama-dbus-monitor.service &>/dev/null && $SUDO systemctl stop agama-dbus-monitor.service $SUDO systemctl list-unit-files agama-web-server.service &>/dev/null && $SUDO systemctl stop agama-web-server.service # Ruby services @@ -190,6 +191,10 @@ $SUDO cp -v $MYDIR/service/share/dbus.conf /usr/share/dbus-1/agama.conf $SUDO cp -f agama.pam /usr/lib/pam.d/agama ) +# copy D-Bus monitor service +$SUDO cp -vf $MYDIR/service/share/agama-dbus-monitor.service /usr/lib/systemd/system/agama-dbus-monitor.service +$SUDO chmod 0600 /usr/lib/systemd/system/agama-dbus-monitor.service + # copy the product files $SUDO mkdir -p /usr/share/agama/products.d $SUDO cp -f $MYDIR/products.d/*.yaml /usr/share/agama/products.d From b62b3c32d36cc2c37431a92a3b4b9d059c3e9832 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Sun, 24 Nov 2024 18:13:27 +0100 Subject: [PATCH 08/11] Updated changelog --- rust/package/agama.changes | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rust/package/agama.changes b/rust/package/agama.changes index 5e14dc65e..e36c9d4b5 100644 --- a/rust/package/agama.changes +++ b/rust/package/agama.changes @@ -1,3 +1,17 @@ +------------------------------------------------------------------- +Fri Nov 29 08:57:19 UTC 2024 - Michal Filka + +- several improvements for CLI. agama logs store: + - collects logs of agama-web-server service + - collects list of installed packages (e.g. .packages.root or + rpm -qa) + - do not add into the archive empty files when log command + returns nothing + - do not explicitly set archive permissions on client side + - do not hide backend errors (e.g. in case of invalid auth token) +- added service for monitoring Agama's D-Bus bus which stores + errors into a log. + ------------------------------------------------------------------- Thu Nov 28 15:58:59 UTC 2024 - Ladislav Slezák From 842d25d0822266aa65fddaaa28ba3ce62ca57455 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Tue, 26 Nov 2024 10:25:03 +0100 Subject: [PATCH 09/11] Updated rpm spec --- rust/package/agama.spec | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/package/agama.spec b/rust/package/agama.spec index 4ac788432..bec4b0b81 100644 --- a/rust/package/agama.spec +++ b/rust/package/agama.spec @@ -149,6 +149,7 @@ install -D -m 0644 %{_builddir}/agama/share/agama-web-server.service %{buildroot install -D -d -m 0755 %{buildroot}%{_libexecdir} install -D -m 0755 %{_builddir}/agama/share/agama-scripts.sh %{buildroot}%{_libexecdir}/agama-scripts.sh install -D -m 0644 %{_builddir}/agama/share/agama-scripts.service %{buildroot}%{_unitdir}/agama-scripts.service +install -D -m 0644 %{_builddir}/agama/share/agama-dbus-monitor.service %{buildroot}%{_unitdir}/agama-dbus-monitor.service # install manpages mkdir -p %{buildroot}%{_mandir}/man1 @@ -174,24 +175,28 @@ echo $PATH %pre %service_add_pre agama-web-server.service +%service_add_pre agama-dbus-monitor.service %pre -n agama-scripts %service_add_pre agama-scripts.service %post %service_add_post agama-web-server.service +%service_add_post agama-dbus-monitor.service %post -n agama-scripts %service_add_post agama-scripts.service %preun %service_del_preun agama-web-server.service +%service_del_preun agama-dbus-monitor.service %preun -n agama-scripts %service_del_preun agama-scripts.service %postun %service_del_postun_with_restart agama-web-server.service +%service_del_postun_with_restart agama-dbus-monitor.service %postun -n agama-scripts %service_del_postun_with_restart agama-scripts.service @@ -204,6 +209,7 @@ echo $PATH %{_datadir}/dbus-1/agama-services %{_pam_vendordir}/agama %{_unitdir}/agama-web-server.service +%{_unitdir}/agama-dbus-monitor.service %files -n agama-cli %{_bindir}/agama From 2e30e9b22546cebe9ebc88eb0537f5399e5e6324 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Thu, 28 Nov 2024 09:45:57 +0100 Subject: [PATCH 10/11] Refactoring according to the review --- rust/agama-lib/src/logs.rs | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/rust/agama-lib/src/logs.rs b/rust/agama-lib/src/logs.rs index 9064c143b..4e9351c50 100644 --- a/rust/agama-lib/src/logs.rs +++ b/rust/agama-lib/src/logs.rs @@ -143,29 +143,16 @@ impl LogItem for LogPath { fs::create_dir_all(dst_path)?; let options = CopyOptions::new(); - // fs_extra's own Error doesn't implement From trait so ? operator is unusable - match copy_items(&[self.src_path.as_str()], dst_path, &options) { - Ok(_p) => { - // turns "invisible" files to visible ones (.packages.root -> packages.root) - if let Some(fname) = dst_file.file_name() { - if let Some(name) = fname.to_str().and_then(|n| { - if n.starts_with(".") { - Some(n.trim_start_matches(".")) - } else { - None - } - }) { - let _ = fs::rename(dst_file.clone(), dst_file.with_file_name(name)); - } - } - - Ok(()) - } - Err(_e) => Err(io::Error::new( - io::ErrorKind::Other, - "Copying of a file failed", - )), + + copy_items(&[self.src_path.as_str()], dst_path, &options) + .map_err(|_| io::Error::new(io::ErrorKind::Other, "Copying of a file failed"))?; + + if let Some(name) = dst_file.file_name().and_then(|fname| fname.to_str()) { + let dst_name = name.trim_start_matches("."); + let _ = fs::rename(dst_file.clone(), dst_file.with_file_name(dst_name)); } + + Ok(()) } } From cc2b7e3f50df9d77df3121a69f1a5d41f0953b3c Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Thu, 28 Nov 2024 09:53:41 +0100 Subject: [PATCH 11/11] Store journalctl logs even for new agama-dbus-monitor --- rust/agama-lib/src/logs.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust/agama-lib/src/logs.rs b/rust/agama-lib/src/logs.rs index 4e9351c50..8d1c2e029 100644 --- a/rust/agama-lib/src/logs.rs +++ b/rust/agama-lib/src/logs.rs @@ -32,11 +32,12 @@ use std::process::Command; use tempfile::TempDir; use utoipa::ToSchema; -const DEFAULT_COMMANDS: [(&str, &str); 5] = [ +const DEFAULT_COMMANDS: [(&str, &str); 6] = [ // (, ) ("journalctl -u agama", "agama"), ("journalctl -u agama-auto", "agama-auto"), ("journalctl -u agama-web-server", "agama-web-server"), + ("journalctl -u agama-dbus-monitor", "agama-dbus-monitor"), ("journalctl --dmesg", "dmesg"), ("rpm -qa", "rpm-qa"), ];