2024-03-06 12:05:01 +00:00
|
|
|
use std::{net::SocketAddr, path::PathBuf, str::FromStr};
|
|
|
|
|
2024-03-05 11:35:25 +00:00
|
|
|
use anyhow::Result;
|
2024-03-06 12:05:01 +00:00
|
|
|
use control::RuntimeControlService;
|
2024-03-06 15:57:56 +00:00
|
|
|
use event::{DaemonEventContext, DaemonEventGenerator};
|
2024-03-06 12:05:01 +00:00
|
|
|
use krata::{control::control_service_server::ControlServiceServer, dial::ControlDialAddress};
|
2024-03-07 18:04:22 +00:00
|
|
|
use kratart::Runtime;
|
2024-03-06 12:05:01 +00:00
|
|
|
use log::info;
|
2024-03-06 15:57:56 +00:00
|
|
|
use tokio::{net::UnixListener, task::JoinHandle};
|
2024-03-06 12:05:01 +00:00
|
|
|
use tokio_stream::wrappers::UnixListenerStream;
|
|
|
|
use tonic::transport::{Identity, Server, ServerTlsConfig};
|
2024-03-05 11:35:25 +00:00
|
|
|
|
2024-03-06 12:05:01 +00:00
|
|
|
pub mod control;
|
2024-03-06 15:57:56 +00:00
|
|
|
pub mod event;
|
2024-03-05 11:35:25 +00:00
|
|
|
|
|
|
|
pub struct Daemon {
|
2024-03-06 12:05:01 +00:00
|
|
|
store: String,
|
2024-03-05 11:35:25 +00:00
|
|
|
runtime: Runtime,
|
2024-03-06 15:57:56 +00:00
|
|
|
events: DaemonEventContext,
|
|
|
|
task: JoinHandle<()>,
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Daemon {
|
2024-03-06 12:05:01 +00:00
|
|
|
pub async fn new(store: String, runtime: Runtime) -> Result<Self> {
|
2024-03-06 15:57:56 +00:00
|
|
|
let runtime_for_events = runtime.dupe().await?;
|
|
|
|
let (events, generator) = DaemonEventGenerator::new(runtime_for_events).await?;
|
|
|
|
Ok(Self {
|
|
|
|
store,
|
|
|
|
runtime,
|
|
|
|
events,
|
|
|
|
task: generator.launch().await?,
|
|
|
|
})
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 12:05:01 +00:00
|
|
|
pub async fn listen(&mut self, addr: ControlDialAddress) -> Result<()> {
|
2024-03-06 15:57:56 +00:00
|
|
|
let control_service = RuntimeControlService::new(self.events.clone(), self.runtime.clone());
|
2024-03-06 12:05:01 +00:00
|
|
|
|
|
|
|
let mut server = Server::builder();
|
|
|
|
|
|
|
|
if let ControlDialAddress::Tls {
|
|
|
|
host: _,
|
|
|
|
port: _,
|
|
|
|
insecure,
|
|
|
|
} = &addr
|
|
|
|
{
|
|
|
|
let mut tls_config = ServerTlsConfig::new();
|
|
|
|
if !insecure {
|
|
|
|
let certificate_path = format!("{}/tls/daemon.pem", self.store);
|
|
|
|
let key_path = format!("{}/tls/daemon.key", self.store);
|
|
|
|
tls_config = tls_config.identity(Identity::from_pem(certificate_path, key_path));
|
|
|
|
}
|
|
|
|
server = server.tls_config(tls_config)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let server = server.add_service(ControlServiceServer::new(control_service));
|
|
|
|
info!("listening on address {}", addr);
|
|
|
|
match addr {
|
|
|
|
ControlDialAddress::UnixSocket { path } => {
|
|
|
|
let path = PathBuf::from(path);
|
|
|
|
if path.exists() {
|
|
|
|
tokio::fs::remove_file(&path).await?;
|
|
|
|
}
|
|
|
|
let listener = UnixListener::bind(path)?;
|
|
|
|
let stream = UnixListenerStream::new(listener);
|
|
|
|
server.serve_with_incoming(stream).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlDialAddress::Tcp { host, port } => {
|
|
|
|
let address = format!("{}:{}", host, port);
|
|
|
|
server.serve(SocketAddr::from_str(&address)?).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlDialAddress::Tls {
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
insecure: _,
|
|
|
|
} => {
|
|
|
|
let address = format!("{}:{}", host, port);
|
|
|
|
server.serve(SocketAddr::from_str(&address)?).await?;
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 11:35:25 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2024-03-06 15:57:56 +00:00
|
|
|
|
|
|
|
impl Drop for Daemon {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.task.abort();
|
|
|
|
}
|
|
|
|
}
|