Skip to content

Commit

Permalink
Refactor logs
Browse files Browse the repository at this point in the history
  • Loading branch information
byrnedo committed Aug 12, 2024
1 parent e2b2a22 commit 1951cf2
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ pub struct LogArgs {
var_args: Vec<String>,
}

impl LogArgs {
pub fn to_podman_log_args(&self) -> Vec<String> {
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<dyn Error>> {
let config = Config::load(Some(args.config.skateconfig.clone()))?;
let (conns, errors) = ssh::cluster_connections(config.current_cluster()?).await;
Expand Down Expand Up @@ -69,21 +84,13 @@ pub async fn logs(args: LogArgs) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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());

Expand All @@ -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::<Vec<String>>()).into())
}

for res in result {
match res {
Err(e) => eprintln!("{}", e),
Expand All @@ -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<dyn Error>> {
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));

Expand All @@ -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::<Vec<String>>()).into())
}

for res in result {
match res {
Err(e) => eprintln!("{}", e),
Expand All @@ -134,25 +141,21 @@ 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<dyn Error>> {
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));

let cmd = cmd.join(" ");

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::<Vec<String>>()).into())
}

for res in result {
match res {
Err(e) => eprintln!("{}", e),
Expand Down

0 comments on commit 1951cf2

Please sign in to comment.