Skip to content

Commit

Permalink
Added manager route for downloading logs
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed May 15, 2024
1 parent 3ef5965 commit 46ec817
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions rust/agama-server/src/manager/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
//! * `manager_service` which returns the Axum service.
//! * `manager_stream` which offers an stream that emits the manager events coming from D-Bus.

use std::pin::Pin;

use agama_lib::{
error::ServiceError,
manager::{InstallationPhase, ManagerClient},
proxies::Manager1Proxy,
};
use axum::{
extract::State,
extract::{Request, State},
http::StatusCode,
response::IntoResponse,
routing::{get, post},
Json, Router,
};
use rand::distributions::{Alphanumeric, DistString};
use serde::Serialize;
use std::{pin::Pin, process::Command};
use tokio_stream::{Stream, StreamExt};
use tower_http::services::ServeFile;

use crate::{
error::Error,
Expand Down Expand Up @@ -90,6 +93,7 @@ pub async fn manager_service(dbus: zbus::Connection) -> Result<Router, ServiceEr
.route("/install", post(install_action))
.route("/finish", post(finish_action))
.route("/installer", get(installer_status))
.route("/logs", get(download_logs))
.merge(status_router)
.merge(progress_router)
.with_state(state))
Expand Down Expand Up @@ -163,3 +167,36 @@ async fn installer_status(
};
Ok(Json(status))
}

/// Returns agama logs
#[utoipa::path(get, path = "/api/manager/logs", responses(
(status = 200, description = "Download logs blob.")
))]

pub async fn download_logs() -> impl IntoResponse {
let path = generate_logs().await;
let Ok(path) = path else {
return (StatusCode::NOT_FOUND).into_response();
};

match ServeFile::new(path)
.try_call(Request::new(axum::body::Body::empty()))
.await
{
Ok(res) => res.into_response(),
Err(_) => (StatusCode::NOT_FOUND).into_response(),
}
}

async fn generate_logs() -> Result<String, Error> {
let random_name: String = Alphanumeric.sample_string(&mut rand::thread_rng(), 8);
let path = format!("/run/agama/logs_{random_name}");

Command::new("agama")
.args(["logs", "store", "-d", path.as_str()])
.status()
.expect("Cannot generate logs");

let full_path = format!("{path}.tar.bz2");
Ok(full_path)
}

0 comments on commit 46ec817

Please sign in to comment.