Skip to content

Commit

Permalink
Merge 9551eae into 3392064
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Mar 5, 2024
2 parents 3392064 + 9551eae commit 8f8305b
Show file tree
Hide file tree
Showing 14 changed files with 386 additions and 37 deletions.
1 change: 1 addition & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/agama-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ thiserror = "1.0.39"
tokio = { version = "1.33.0", features = ["macros", "rt-multi-thread"] }
tokio-stream = "0.1.14"
url = "2.5.0"
utoipa = "4.2.0"
zbus = { version = "3", default-features = false, features = ["tokio"] }
2 changes: 1 addition & 1 deletion rust/agama-lib/src/product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ mod proxies;
mod settings;
mod store;

pub use client::ProductClient;
pub use client::{Product, ProductClient};
pub use settings::ProductSettings;
pub use store::ProductStore;
3 changes: 2 additions & 1 deletion rust/agama-lib/src/product/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use zbus::Connection;
use super::proxies::RegistrationProxy;

/// Represents a software product
#[derive(Debug, Serialize)]
#[derive(Default, Debug, Serialize, utoipa::ToSchema)]
pub struct Product {
/// Product ID (eg., "ALP", "Tumbleweed", etc.)
pub id: String,
Expand All @@ -19,6 +19,7 @@ pub struct Product {
}

/// D-Bus client for the software service
#[derive(Clone)]
pub struct ProductClient<'a> {
product_proxy: SoftwareProductProxy<'a>,
registration_proxy: RegistrationProxy<'a>,
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-lib/src/software.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ pub mod proxies;
mod settings;
mod store;

pub use client::SoftwareClient;
pub use client::{Pattern, SelectedBy, SoftwareClient};
pub use settings::SoftwareSettings;
pub use store::SoftwareStore;
50 changes: 47 additions & 3 deletions rust/agama-lib/src/software/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use super::proxies::Software1Proxy;
use crate::error::ServiceError;
use serde::Serialize;
use std::collections::HashMap;
use zbus::Connection;

/// Represents a software product
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct Pattern {
/// Pattern ID (eg., "aaa_base", "gnome")
pub id: String,
Expand All @@ -20,7 +21,29 @@ pub struct Pattern {
pub order: String,
}

/// Represents the reason why a pattern is selected.
#[derive(Clone, Copy, Debug, Serialize)]
pub enum SelectedBy {
/// The pattern was selected by the user.
User = 0,
/// The pattern was selected automatically.
Auto = 1,
/// The pattern has not be selected.
None = 2,
}

impl From<u8> for SelectedBy {
fn from(value: u8) -> Self {
match value {
0 => Self::User,
1 => Self::Auto,
_ => Self::None,
}
}
}

/// D-Bus client for the software service
#[derive(Clone)]
pub struct SoftwareClient<'a> {
software_proxy: Software1Proxy<'a>,
}
Expand Down Expand Up @@ -55,18 +78,27 @@ impl<'a> SoftwareClient<'a> {

/// Returns the ids of patterns selected by user
pub async fn user_selected_patterns(&self) -> Result<Vec<String>, ServiceError> {
const USER_SELECTED: u8 = 0;
let patterns: Vec<String> = self
.software_proxy
.selected_patterns()
.await?
.into_iter()
.filter(|(_id, reason)| *reason == USER_SELECTED)
.filter(|(_id, reason)| *reason == SelectedBy::User as u8)
.map(|(id, _reason)| id)
.collect();
Ok(patterns)
}

/// Returns the selected pattern and the reason each one selected.
pub async fn selected_patterns(&self) -> Result<HashMap<String, SelectedBy>, ServiceError> {
let patterns = self.software_proxy.selected_patterns().await?;
let patterns = patterns
.into_iter()
.map(|(id, reason)| (id, reason.into()))
.collect();
Ok(patterns)
}

/// Selects patterns by user
pub async fn select_patterns(&self, patterns: &[String]) -> Result<(), ServiceError> {
let patterns: Vec<&str> = patterns.iter().map(AsRef::as_ref).collect();
Expand All @@ -80,4 +112,16 @@ impl<'a> SoftwareClient<'a> {
Ok(())
}
}

/// Returns the required space for installing the selected patterns.
///
/// It returns a formatted string including the size and the unit.
pub async fn used_disk_space(&self) -> Result<String, ServiceError> {
Ok(self.software_proxy.used_disk_space().await?)
}

/// Starts the process to read the repositories data.
pub async fn probe(&self) -> Result<(), ServiceError> {
Ok(self.software_proxy.probe().await?)
}
}
37 changes: 24 additions & 13 deletions rust/agama-server/src/agama-web-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@ use agama_dbus_server::{
l10n::helpers,
web::{self, run_monitor},
};
use clap::{Parser, Subcommand};
use agama_lib::connection_to;
use clap::{Args, Parser, Subcommand};
use tokio::sync::broadcast::channel;
use tracing_subscriber::prelude::*;
use utoipa::OpenApi;

#[derive(Subcommand, Debug)]
enum Commands {
/// Start the API server.
Serve {
// Address to listen on (":::3000" listens for both IPv6 and IPv4
// connections unless manually disabled in /proc/sys/net/ipv6/bindv6only)
#[arg(long, default_value = ":::3000")]
address: String,
},
Serve(ServeArgs),
/// Display the API documentation in OpenAPI format.
Openapi,
}

#[derive(Debug, Args)]
pub struct ServeArgs {
// Address to listen on (":::3000" listens for both IPv6 and IPv4
// connections unless manually disabled in /proc/sys/net/ipv6/bindv6only)
#[arg(long, default_value = ":::3000")]
address: String,
// Agama D-Bus address
#[arg(long, default_value = "unix:path=/run/agama/bus")]
dbus_address: String,
}

#[derive(Parser, Debug)]
#[command(
version,
Expand All @@ -33,22 +40,26 @@ struct Cli {
}

/// Start serving the API.
async fn serve_command(address: &str) -> anyhow::Result<()> {
///
/// `args`: command-line arguments.
async fn serve_command(args: ServeArgs) -> anyhow::Result<()> {
let journald = tracing_journald::layer().expect("could not connect to journald");
tracing_subscriber::registry().with(journald).init();

let listener = tokio::net::TcpListener::bind(address)
let listener = tokio::net::TcpListener::bind(&args.address)
.await
.unwrap_or_else(|_| panic!("could not listen on {}", address));
.unwrap_or_else(|_| panic!("could not listen on {}", &args.address));

let (tx, _) = channel(16);
run_monitor(tx.clone()).await?;

let config = web::ServiceConfig::load().unwrap();
let service = web::service(config, tx);
let config = web::ServiceConfig::load()?;
let dbus = connection_to(&args.dbus_address).await?;
let service = web::service(config, tx, dbus).await?;
axum::serve(listener, service)
.await
.expect("could not mount app on listener");

Ok(())
}

Expand All @@ -60,7 +71,7 @@ fn openapi_command() -> anyhow::Result<()> {

async fn run_command(cli: Cli) -> anyhow::Result<()> {
match cli.command {
Commands::Serve { address } => serve_command(&address).await,
Commands::Serve(args) => serve_command(args).await,
Commands::Openapi => openapi_command(),
}
}
Expand Down
1 change: 1 addition & 0 deletions rust/agama-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ pub mod error;
pub mod l10n;
pub mod network;
pub mod questions;
pub mod software;
pub mod web;
pub use web::service;
2 changes: 2 additions & 0 deletions rust/agama-server/src/software.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod web;
pub use web::{software_service, software_stream};

0 comments on commit 8f8305b

Please sign in to comment.