Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions rust/Cargo.lock

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

23 changes: 23 additions & 0 deletions rust/agama-bootloader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "agama-bootloader"
version = "0.1.0"
rust-version.workspace = true
edition.workspace = true

[dependencies]
agama-utils = { path = "../agama-utils" }
anyhow = "1.0.99"
async-trait = "0.1.89"
gettext-rs = { version = "0.7.2", features = ["gettext-system"] }
regex = "1.11.2"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = { version = "1.0.128", features = ["raw_value"] }
thiserror = "2.0.16"
tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread", "sync"] }
tokio-stream = "0.1.17"
tracing = "0.1.41"
zbus = "5.11.0"

[dev-dependencies]
test-context = "0.4.1"
tokio-test = "0.4.4"
84 changes: 84 additions & 0 deletions rust/agama-bootloader/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) [2024] SUSE LLC
//
// All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, contact SUSE LLC.
//
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.

//! Implements a client to access Agama's D-Bus API related to Bootloader management.

use agama_utils::api::bootloader::Config;
use async_trait::async_trait;
use zbus::Connection;

use crate::dbus::BootloaderProxy;

/// Errors that can occur when using the Bootloader client.
#[derive(thiserror::Error, Debug)]
pub enum Error {
/// Error originating from the D-Bus communication.
#[error("D-Bus service error: {0}")]
DBus(#[from] zbus::Error),
/// Error parsing or generating JSON data.
#[error("Passed json data is not correct: {0}")]
InvalidJson(#[from] serde_json::Error),
}

/// Trait defining the interface for the Bootloader client.
///
/// This trait abstracts the operations available for managing the bootloader configuration
/// via the Agama D-Bus API. For testing purpose it can be replaced by mock object.
#[async_trait]
pub trait BootloaderClient {
/// Retrieves the current bootloader configuration.
async fn get_config(&self) -> ClientResult<Config>;
/// Sets the bootloader configuration.
async fn set_config(&self, config: &Config) -> ClientResult<()>;
}

pub type ClientResult<T> = Result<T, Error>;

/// Client to connect to Agama's D-Bus API for Bootloader management.
#[derive(Clone)]
pub struct Client<'a> {
bootloader_proxy: BootloaderProxy<'a>,
}

impl<'a> Client<'a> {
pub async fn new(connection: Connection) -> ClientResult<Client<'a>> {
let bootloader_proxy = BootloaderProxy::new(&connection).await?;

Ok(Self { bootloader_proxy })
}
}

#[async_trait]
impl<'a> BootloaderClient for Client<'a> {
async fn get_config(&self) -> ClientResult<Config> {
let serialized_string = self.bootloader_proxy.get_config().await?;
let settings = serde_json::from_str(serialized_string.as_str())?;
Ok(settings)
}

async fn set_config(&self, config: &Config) -> ClientResult<()> {
// ignore return value as currently it does not fail and who knows what future brings
// but it should not be part of result and instead transformed to Issue
self.bootloader_proxy
.set_config(serde_json::to_string(config)?.as_str())
.await?;
Ok(())
}
}
35 changes: 35 additions & 0 deletions rust/agama-bootloader/src/dbus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! # D-Bus interface proxy for: `org.freedesktop.locale1`
//!
//! This code was generated by `zbus-xmlgen` `5.1.0` from D-Bus introspection data.
//! Source: `Interface '/org/freedesktop/locale1' from service 'org.freedesktop.locale1' on system bus`.
//!
//! You may prefer to adapt it, instead of using it verbatim.
//!
//! More information can be found in the [Writing a client proxy] section of the zbus
//! documentation.
//!
//! This type implements the [D-Bus standard interfaces], (`org.freedesktop.DBus.*`) for which the
//! following zbus API can be used:
//!
//! * [`zbus::fdo::PeerProxy`]
//! * [`zbus::fdo::IntrospectableProxy`]
//! * [`zbus::fdo::PropertiesProxy`]
//!
//! Consequently `zbus-xmlgen` did not generate code for the above interfaces.
//!
//! [Writing a client proxy]: https://dbus2.github.io/zbus/client.html
//! [D-Bus standard interfaces]: https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces,
use zbus::proxy;
#[proxy(
default_service = "org.opensuse.Agama.Storage1",
default_path = "/org/opensuse/Agama/Storage1",
interface = "org.opensuse.Agama.Storage1.Bootloader",
assume_defaults = true
)]
pub trait Bootloader {
/// GetConfig method
fn get_config(&self) -> zbus::Result<String>;

/// SetConfig method
fn set_config(&self, serialized_config: &str) -> zbus::Result<u32>;
}
44 changes: 44 additions & 0 deletions rust/agama-bootloader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) [2025] SUSE LLC
//
// All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, contact SUSE LLC.
//
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.

//! This crate implements the support for localization handling in Agama.
//! It takes care of setting the locale, keymap and timezone for Agama itself
//! and the target system.
//!
//! From a technical point of view, it includes:
//!
//! * The [UserConfig] struct that defines the settings the user can
//! alter for the target system.
//! * The [Proposal] struct that describes how the system will look like after
//! the installation.
//! * The [SystemInfo] which includes information about the system
//! where Agama is running.
//! * An [specific event type](Event) for localization-related events.
//!
//! The service can be started by calling the [start_service] function, which
//! returns a [agama_utils::actors::ActorHandler] to interact with the system.

pub mod service;
pub use service::{Service, Starter};

pub mod client;
mod dbus;
pub mod message;
pub mod test_utils;
47 changes: 47 additions & 0 deletions rust/agama-bootloader/src/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) [2025] SUSE LLC
//
// All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, contact SUSE LLC.
//
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.

use agama_utils::{actor::Message, api::bootloader::Config};

pub struct GetConfig;

impl Message for GetConfig {
type Reply = Config;
}

pub struct SetConfig<T> {
pub config: Option<T>,
}

impl<T: Send + 'static> Message for SetConfig<T> {
type Reply = ();
}

impl<T> SetConfig<T> {
pub fn new(config: Option<T>) -> Self {
Self { config }
}

pub fn with(config: T) -> Self {
Self {
config: Some(config),
}
}
}
124 changes: 124 additions & 0 deletions rust/agama-bootloader/src/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (c) [2025] SUSE LLC
//
// All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, contact SUSE LLC.
//
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.

use agama_utils::{
actor::{self, Actor, Handler, MessageHandler},
api::{bootloader::Config, Issue},
issue,
};
use async_trait::async_trait;

use crate::{
client::{self, Client},
message,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Actor(#[from] actor::Error),
#[error(transparent)]
Client(#[from] client::Error),
}

/// Builds and spawns the bootloader service.
///
/// This struct allows to build a bootloader service. It allows replacing
/// the client for a custom one.
pub struct Starter {
connection: zbus::Connection,
client: Option<Box<dyn client::BootloaderClient + Send + 'static>>,
issues: Handler<issue::Service>,
}

impl Starter {
/// Creates a new starter.
///
/// * `connection`: connection to the D-Bus.
/// * `issues`: handler to the issues service.
pub fn new(connection: zbus::Connection, issues: Handler<issue::Service>) -> Self {
Self {
connection,
client: None,
issues,
}
}

/// Sets a custom client.
pub fn with_client(mut self, client: impl client::BootloaderClient + Send + 'static) -> Self {
self.client = Some(Box::new(client));
self
}

/// Starts the service and returns a handler to communicate with it.
pub async fn start(self) -> Result<Handler<Service>, Error> {
let client = match self.client {
Some(client) => client,
None => Box::new(Client::new(self.connection.clone()).await?),
};
let service = Service {
client: client,
issues: self.issues,
};
let handler = actor::spawn(service);
Ok(handler)
}
}

/// Bootloader service.
///
/// It is responsible for handling the bootloader configuration.
pub struct Service {
client: Box<dyn client::BootloaderClient + Send + 'static>,
issues: Handler<issue::Service>,
}

impl Service {
pub fn starter(connection: zbus::Connection, issues: Handler<issue::Service>) -> Starter {
Starter::new(connection, issues)
}

/// Returns configuration issues.
fn find_issues(&self) -> Vec<Issue> {
// TODO: get issues from bootloader proposal
vec![]
}
}

impl Actor for Service {
type Error = Error;
}

#[async_trait]
impl MessageHandler<message::GetConfig> for Service {
async fn handle(&mut self, _message: message::GetConfig) -> Result<Config, Error> {
Ok(self.client.get_config().await?)
}
}

#[async_trait]
impl MessageHandler<message::SetConfig<Config>> for Service {
async fn handle(&mut self, message: message::SetConfig<Config>) -> Result<(), Error> {
self.client
.set_config(&message.config.unwrap_or_default())
.await?;
Ok(())
}
}
Loading
Loading