nzr-api/config: try to guess cloud-init http addr

This commit is contained in:
snow flurry 2024-08-14 23:44:48 -07:00
parent 267b924d7f
commit 3d58c6c671
3 changed files with 15 additions and 4 deletions

View file

@ -49,10 +49,20 @@ pub struct DHCPConfig {
pub struct CloudConfig {
pub listen_addr: String,
pub port: u16,
pub http_addr: String,
pub http_addr: Option<String>,
pub admin_user: String,
}
impl CloudConfig {
pub fn http_addr(&self) -> String {
if let Some(http_addr) = &self.http_addr {
http_addr.clone()
} else {
format!("http://{}:{}", self.listen_addr, self.port)
}
}
}
/// Server<->Client RPC configuration.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RPCConfig {
@ -114,7 +124,7 @@ impl Default for Config {
cloud: CloudConfig {
listen_addr: "0.0.0.0".to_owned(),
port: 80,
http_addr: "http://192.0.2.1/".to_owned(),
http_addr: None,
admin_user: "admin".to_owned(),
},
}

View file

@ -126,7 +126,7 @@ pub async fn new_instance(
let mut system_map = InfoMap::new();
system_map.push(
"serial",
format!("ds=nocloud;s={}", ctx.config.cloud.http_addr),
format!("ds=nocloud;s={}", ctx.config.cloud.http_addr()),
);
sysinfo.system(system_map);
sysinfo

View file

@ -1,4 +1,4 @@
use std::net::{Ipv4Addr, SocketAddr};
use std::net::SocketAddr;
use axum::extract::{ConnectInfo, State};
use nzr_api::{
@ -24,6 +24,7 @@ async fn get_metadata() {
listen_addr: "0.0.0.0".into(),
port: 80,
admin_user: "admin".to_owned(),
http_addr: None,
},
..Default::default()
};