Skip to content

Commit

Permalink
Merged logs http client into manager http client
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Nov 4, 2024
1 parent 79e4595 commit a116aba
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 83 deletions.
4 changes: 2 additions & 2 deletions rust/agama-cli/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// find current contact information at www.suse.com.

use agama_lib::base_http_client::BaseHTTPClient;
use agama_lib::logs::http_client::HTTPClient;
use agama_lib::logs::set_archive_permissions;
use agama_lib::manager::http_client::ManagerHTTPClient as HTTPClient;
use clap::Subcommand;
use std::io;
use std::path::PathBuf;
Expand All @@ -41,7 +41,7 @@ pub enum LogsCommands {

/// Main entry point called from agama CLI main loop
pub async fn run(client: BaseHTTPClient, subcommand: LogsCommands) -> anyhow::Result<()> {
let client = HTTPClient::new(client)?;
let client = HTTPClient::new(client);

match subcommand {
LogsCommands::Store { destination } => {
Expand Down
2 changes: 0 additions & 2 deletions rust/agama-lib/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ use std::process::Command;
use tempfile::TempDir;
use utoipa::ToSchema;

pub mod http_client;

const DEFAULT_COMMANDS: [(&str, &str); 3] = [
// (<command to be executed>, <file name used for storing result of the command>)
("journalctl -u agama", "agama"),
Expand Down
79 changes: 0 additions & 79 deletions rust/agama-lib/src/logs/http_client.rs

This file was deleted.

48 changes: 48 additions & 0 deletions rust/agama-lib/src/manager/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.

use crate::logs::LogsLists;
use crate::{base_http_client::BaseHTTPClient, error::ServiceError};
use reqwest::header::CONTENT_ENCODING;
use std::io::Cursor;
use std::path::{Path, PathBuf};

pub struct ManagerHTTPClient {
client: BaseHTTPClient,
Expand All @@ -34,4 +38,48 @@ impl ManagerHTTPClient {
// so we pass () which is rendered as `null`
self.client.post_void("/manager/probe_sync", &()).await
}

/// Downloads package of logs from the backend
///
/// For now the path is path to a destination file without an extension. Extension
/// will be added according to the compression type found in the response
///
/// Returns path to logs
pub async fn store(&self, path: &Path) -> Result<PathBuf, ServiceError> {
// 1) response with logs
let response = self.client.get_raw("/manager/logs/store").await?;

// 2) find out the destination file name
let ext =
&response
.headers()
.get(CONTENT_ENCODING)
.ok_or(ServiceError::CannotGenerateLogs(String::from(
"Invalid response",
)))?;
let mut destination = path.to_path_buf();

destination.set_extension(
ext.to_str()
.map_err(|_| ServiceError::CannotGenerateLogs(String::from("Invalid response")))?,
);

// 3) store response's binary content (logs) in a file
let mut file = std::fs::File::create(destination.as_path()).map_err(|_| {
ServiceError::CannotGenerateLogs(String::from("Cannot store received response"))
})?;
let mut content = Cursor::new(response.bytes().await?);

std::io::copy(&mut content, &mut file).map_err(|_| {
ServiceError::CannotGenerateLogs(String::from("Cannot store received response"))
})?;

Ok(destination)
}

/// Asks backend for lists of log files and commands used for creating logs archive returned by
/// store (/logs/store) backed HTTP API command
pub async fn list(&self) -> Result<LogsLists, ServiceError> {
Ok(self.client.get("/manager/logs/list").await?)
}
}

0 comments on commit a116aba

Please sign in to comment.