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 4cec522f89
No known key found for this signature in database
18 changed files with 1238 additions and 51 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"
] }
varlink = "11.0.1"
systemd-ipc = { path = "../systemd-ipc" }
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

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,79 @@
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 = "0.0.0.0: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()?;
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;

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

@ -0,0 +1,46 @@
use dropshot::{endpoint, HttpError, HttpResponseOk, RequestContext};
use schemars::JsonSchema;
use serde::Serialize;
use trace_request::trace_request;
use systemd_ipc::addrs::SYSTEMD_HOSTNAME;
use systemd_ipc::io_systemd_hostname::{self, VarlinkClientInterface};
use std::sync::Arc;
use std::thread;
use crate::context::ControllerContext;
/// Version and build information
#[derive(Serialize, JsonSchema)]
struct MachineInfo {
machine_id: String,
}
/// Fetch machine id
#[endpoint {
method = GET,
path = "/machine_id",
}]
#[trace_request]
pub async fn describe(
rqctx: RequestContext<Arc<ControllerContext>>,
) -> Result<HttpResponseOk<MachineInfo>, HttpError> {
// Connect to systemd.Hostname
// Make it tokio task blocking
tokio::task::block_in_place(move || {
let conn = varlink::Connection::with_address(SYSTEMD_HOSTNAME).unwrap();
let mut sd = io_systemd_hostname::VarlinkClient::new(conn);
let machine_id = sd.describe().call().unwrap().MachineID;
tracing::info_span!("Hello, span hostd!");
tracing::info!(monotonic_counter.version_calls = 1);
let machine_info = MachineInfo { machine_id };
thread::sleep(std::time::Duration::from_millis(10000));
Ok(HttpResponseOk(machine_info))
})
}