feat(hostd): varlink interfaced host controller to manage machine configuration and boot mgmt

This commit is contained in:
Lars Sjöström 2025-01-08 11:09:34 +01:00
parent 8e99ab4555
commit 375bc34dac
No known key found for this signature in database
18 changed files with 1632 additions and 52 deletions

1
hostd/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

23
hostd/Cargo.toml Normal file
View file

@ -0,0 +1,23 @@
[package]
name = "hostd"
version.workspace = true
edition.workspace = true
[dependencies]
anyhow.workspace = true
tokio.workspace = true
sqlx = { version = "0.8.3", default-features = false, features = [
"macros", "migrate", "postgres", "runtime-tokio", "tls-rustls", "time", "uuid"
] }
dropshot.workspace = true
clap.workspace = true
slog.workspace = true
slog-async.workspace = true
tracing-slog.workspace = true
tracing.workspace = true
trace-request = { path = "../trace-request" }
schemars.workspace = true
serde.workspace = true
http.workspace = true
zbus_systemd = { version = "0.25701.0", features = ["hostname1", "sysupdate1", "network1", "portable1", "resolve1", "systemd1"] }
zbus = "5.4.0"

15
hostd/src/api.rs Normal file
View file

@ -0,0 +1,15 @@
use anyhow::Result;
use dropshot::ApiDescription;
use std::sync::Arc;
use crate::context::ControllerContext;
use crate::machine;
type ControllerApiDescription = ApiDescription<Arc<ControllerContext>>;
pub fn api() -> Result<ControllerApiDescription> {
let mut api = ControllerApiDescription::new();
api.register(machine::describe)?;
Ok(api)
}

View file

@ -0,0 +1,81 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use dropshot::{ConfigDropshot, ServerBuilder};
use slog::Drain;
// use sqlx::PgPool;
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
use tracing_slog::TracingSlogDrain;
use hostd::api;
use hostd::context::ControllerContext;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Cli {
#[arg(
long = "telemetry-otlp-endpoint",
default_value = "http://localhost:4317",
env = "OTEL_EXPORTER_OTLP_ENDPOINT"
)]
otlp_endpoint: Option<String>,
#[arg(
long = "log-stderr",
short = 'v',
default_value = "false",
env = "LOG_TO_STDERR"
)]
log_stderr: bool,
#[arg(
long = "listen-address",
default_value = "127.0.0.1:9478",
env = "LISTEN_ADDRESS"
)]
listen_address: String,
#[arg(
long = "database-url",
default_value = "postgresql://localhost/patagia",
env = "DATABASE_URL"
)]
database_url: Option<String>,
}
#[tokio::main]
async fn main() -> Result<()> {
let args = Cli::parse();
let config = ConfigDropshot {
bind_address: SocketAddr::from_str(&args.listen_address).unwrap(),
..Default::default()
};
let logger = {
let level_drain = slog::LevelFilter(TracingSlogDrain, slog::Level::Debug).fuse();
let async_drain = slog_async::Async::new(level_drain).build().fuse();
slog::Logger::root(async_drain, slog::o!())
};
// let database_url = args.database_url.unwrap();
// dont connect to database pass null
// let pg = PgPool::connect(&database_url).await?;
// sqlx::migrate!().run(&pg).await?;
let ctx = ControllerContext::new();
let api = api::api()?;
println!("Listening on http://{}", config.bind_address);
ServerBuilder::new(api, Arc::new(ctx), logger)
.config(config)
.start()
.map_err(|e| anyhow!("Error starting server: {:?}", e))?
.await
.map_err(|e| anyhow!(e))
}

11
hostd/src/context.rs Normal file
View file

@ -0,0 +1,11 @@
// use sqlx::postgres::PgPool;
pub struct ControllerContext {
// pub pg_pool: PgPool,
}
impl ControllerContext {
pub fn new() -> ControllerContext {
ControllerContext {}
}
}

3
hostd/src/lib.rs Normal file
View file

@ -0,0 +1,3 @@
pub mod api;
pub mod context;
pub mod machine;

45
hostd/src/machine.rs Normal file
View file

@ -0,0 +1,45 @@
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;
/// Machine information
#[derive(Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct MachineInfo {
machine_id: String,
}
/// Fetch machine info
#[endpoint {
method = GET,
path = "/machine_info",
}]
#[trace_request]
pub async fn describe(
rqctx: RequestContext<Arc<ControllerContext>>,
) -> Result<HttpResponseOk<MachineInfo>, HttpError> {
let conn = zbus::Connection::system().await.unwrap();
let manager = zbus_systemd::hostname1::HostnamedProxy::new(&conn)
.await
.unwrap();
let machine_id = manager
.machine_id()
.await
.unwrap()
// convert bytes to hex string
.iter()
.map(|&b| format!("{:02x}", b))
.collect();
let machine_info = MachineInfo { machine_id };
tracing::info_span!("Hello, span hostd!");
tracing::info!(monotonic_counter.version_calls = 1);
Ok(HttpResponseOk(machine_info))
}