From 1951cf2ebb3a1fb11db61627c1910911fb1948ae Mon Sep 17 00:00:00 2001 From: Donal Byrne Date: Mon, 12 Aug 2024 12:16:53 +0200 Subject: [PATCH] Refactor logs --- src/logs.rs | 61 ++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/src/logs.rs b/src/logs.rs index bc7b623..b3b510e 100644 --- a/src/logs.rs +++ b/src/logs.rs @@ -28,6 +28,21 @@ pub struct LogArgs { var_args: Vec, } +impl LogArgs { + pub fn to_podman_log_args(&self) -> Vec { + let mut cmd: Vec<_> = ["sudo", "podman", "pod", "logs", "--names"].map(String::from).to_vec(); + + if self.follow { + cmd.push("--follow".to_string()); + } + if self.tail > 0 { + let tail = format!("--tail {}", &self.tail); + cmd.push(tail); + } + return cmd + } +} + pub async fn logs(args: LogArgs) -> Result<(), Box> { let config = Config::load(Some(args.config.skateconfig.clone()))?; let (conns, errors) = ssh::cluster_connections(config.current_cluster()?).await; @@ -69,21 +84,13 @@ pub async fn logs(args: LogArgs) -> Result<(), Box> { log_daemonset(&conns, name, ns, &args).await } _ => { - Err("Unknown resource type".into()) + Err(format!("Unexpected resource type {}", resource_type).into()) } } } pub async fn log_pod(conns: &ssh::SshClients, name: &str, ns: String, args: &LogArgs) -> Result<(), Box> { - let mut cmd: Vec<_> = ["sudo", "podman", "pod", "logs", "--names"].map(String::from).to_vec(); - - if args.follow { - cmd.push("--follow".to_string()); - } - if args.tail > 0 { - let tail = format!("--tail {}", &args.tail); - cmd.push(tail); - } + let mut cmd = args.to_podman_log_args(); cmd.push(name.to_string()); @@ -93,6 +100,10 @@ pub async fn log_pod(conns: &ssh::SshClients, name: &str, ns: String, args: &Log let result: Vec<_> = fut.collect().await; + if result.iter().all(|r| r.is_err()) { + return Err(format!("{:?}", result.into_iter().map(|r| r.err().unwrap().to_string()).collect::>()).into()) + } + for res in result { match res { Err(e) => eprintln!("{}", e), @@ -104,15 +115,7 @@ pub async fn log_pod(conns: &ssh::SshClients, name: &str, ns: String, args: &Log } pub async fn log_deployment(conns: &ssh::SshClients, name: &str, ns: String, args: &LogArgs) -> Result<(), Box> { - let mut cmd: Vec<_> = ["sudo", "podman", "pod", "logs", "--names"].map(String::from).to_vec(); - - if args.follow { - cmd.push("--follow".to_string()); - } - if args.tail > 0 { - let tail = format!("--tail {}", &args.tail); - cmd.push(tail); - } + let mut cmd = args.to_podman_log_args(); cmd.push(format!("$(sudo podman pod ls --filter label=skate.io/deployment={} --filter label=skate.io/namespace={} -q)", name, ns)); @@ -123,6 +126,10 @@ pub async fn log_deployment(conns: &ssh::SshClients, name: &str, ns: String, arg let result: Vec<_> = fut.collect().await; + if result.iter().all(|r| r.is_err()) { + return Err(format!("{:?}", result.into_iter().map(|r| r.err().unwrap().to_string()).collect::>()).into()) + } + for res in result { match res { Err(e) => eprintln!("{}", e), @@ -134,16 +141,7 @@ pub async fn log_deployment(conns: &ssh::SshClients, name: &str, ns: String, arg } pub async fn log_daemonset(conns: &ssh::SshClients, name: &str, ns: String, args: &LogArgs) -> Result<(), Box> { - let mut cmd: Vec<_> = ["sudo", "podman", "pod", "logs", "--names"].map(String::from).to_vec(); - - if args.follow { - cmd.push("--follow".to_string()); - } - if args.tail > 0 { - - let tail = format!("--tail {}", &args.tail); - cmd.push(tail); - } + let mut cmd = args.to_podman_log_args(); cmd.push(format!("$(sudo podman pod ls --filter label=skate.io/daemonset={} --filter label=skate.io/namespace={} -q)", name, ns)); @@ -151,8 +149,13 @@ pub async fn log_daemonset(conns: &ssh::SshClients, name: &str, ns: String, args let fut: FuturesUnordered<_> = conns.clients.iter().map(|c| c.execute_stdout(&cmd)).collect(); + let result: Vec<_> = fut.collect().await; + if result.iter().all(|r| r.is_err()) { + return Err(format!("{:?}", result.into_iter().map(|r| r.err().unwrap().to_string()).collect::>()).into()) + } + for res in result { match res { Err(e) => eprintln!("{}", e),