generated from Patagia/template-nix
Compare commits
1 commit
0d822dfa44
...
bb277591e2
Author | SHA1 | Date | |
---|---|---|---|
bb277591e2 |
5 changed files with 60 additions and 8 deletions
|
@ -5,11 +5,13 @@ use std::sync::Arc;
|
|||
|
||||
use crate::context::ControllerContext;
|
||||
use crate::machine;
|
||||
use crate::sysupdate;
|
||||
|
||||
type ControllerApiDescription = ApiDescription<Arc<ControllerContext>>;
|
||||
|
||||
pub fn api() -> Result<ControllerApiDescription> {
|
||||
let mut api = ControllerApiDescription::new();
|
||||
api.register(machine::describe)?;
|
||||
api.register(sysupdate::list_versions)?;
|
||||
Ok(api)
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
use zbus::Connection;
|
||||
|
||||
pub struct ControllerContext {
|
||||
pub dbus: Connection,
|
||||
pub dbus: zbus::Connection,
|
||||
}
|
||||
|
||||
impl ControllerContext {
|
||||
pub fn new(dbus: Connection) -> ControllerContext {
|
||||
pub fn new(dbus: zbus::Connection) -> ControllerContext {
|
||||
ControllerContext { dbus }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
pub mod api;
|
||||
pub mod context;
|
||||
pub mod machine;
|
||||
pub mod sysupdate;
|
||||
|
|
|
@ -29,7 +29,9 @@ pub async fn describe(
|
|||
let machine_id = hostnamed
|
||||
.machine_id()
|
||||
.await
|
||||
.unwrap()
|
||||
.map_err(|e| match e {
|
||||
err => HttpError::for_internal_error(format!("Error: {}", err)),
|
||||
})?
|
||||
// convert bytes to hex string
|
||||
.iter()
|
||||
.map(|&b| format!("{:02x}", b))
|
||||
|
@ -37,8 +39,5 @@ pub async fn describe(
|
|||
|
||||
let machine_info = MachineInfo { machine_id };
|
||||
|
||||
tracing::info_span!("Hello, span hostd!");
|
||||
|
||||
tracing::info!(monotonic_counter.version_calls = 1);
|
||||
Ok(HttpResponseOk(machine_info))
|
||||
}
|
||||
|
|
52
hostd/src/sysupdate.rs
Normal file
52
hostd/src/sysupdate.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use dropshot::{endpoint, HttpError, HttpResponseOk, RequestContext};
|
||||
use schemars::JsonSchema;
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
use trace_request::trace_request;
|
||||
|
||||
use crate::context::ControllerContext;
|
||||
|
||||
const SYSUPDATE_HOST_PATH: &str = "/org/freedesktop/sysupdate1/target/host";
|
||||
|
||||
#[derive(Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct SysUpdate {
|
||||
current_version: String,
|
||||
versions: Vec<String>,
|
||||
}
|
||||
|
||||
#[endpoint {
|
||||
method = GET,
|
||||
path = "/list_versions",
|
||||
}]
|
||||
#[trace_request]
|
||||
pub async fn list_versions(
|
||||
rqctx: RequestContext<Arc<ControllerContext>>,
|
||||
) -> Result<HttpResponseOk<SysUpdate>, HttpError> {
|
||||
let sysupdate_target = zbus_systemd::sysupdate1::TargetProxy::builder(&rqctx.context().dbus)
|
||||
.path(SYSUPDATE_HOST_PATH)
|
||||
.unwrap()
|
||||
.build()
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
err => HttpError::for_internal_error(format!("Error: {}", err)),
|
||||
})?;
|
||||
|
||||
let versions = sysupdate_target.list(0).await.map_err(|e| match e {
|
||||
err => {
|
||||
println!("Error: {}", err);
|
||||
HttpError::for_internal_error(format!("Error: {}", err))
|
||||
}
|
||||
})?;
|
||||
|
||||
let current_version = sysupdate_target.get_version().await.map_err(|e| match e {
|
||||
err => HttpError::for_internal_error(format!("Error: {}", err)),
|
||||
})?;
|
||||
|
||||
let sysupdate = SysUpdate {
|
||||
versions,
|
||||
current_version,
|
||||
};
|
||||
|
||||
Ok(HttpResponseOk(sysupdate))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue