From 2bfd274bf31ed74f690bb5d4087c6a433ab83a06 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Thu, 30 Oct 2025 20:03:37 +0000 Subject: fix: dont panic when unable to obtain hostname if we are unable to obtain the hostname of the local machine from the /etc/hostname file or HOSTNAME env var then an empty string is used as the hostname and a warning is shown instead of a panic. since the hostname is just used to determine if we are executing on the cluster, and the cluster machines all have their hostnames set, then using the empty string should not be a problem. --- src/context.rs | 19 +++++++++++-------- src/main.rs | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/context.rs b/src/context.rs index 89e2c3e..f67c302 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,4 +1,4 @@ -use eyre::{Context as _, Result}; +use eyre::Result; use crate::machine::Machine; @@ -60,7 +60,7 @@ impl Context { } async fn get_execution_node() -> Result { - let hostname = get_hostname().await?; + let hostname = get_hostname().await; let node = match hostname.as_str() { "frontend" => ExecutionNode::Frontend, _ => match Machine::from_hostname(&hostname) { @@ -71,11 +71,14 @@ async fn get_execution_node() -> Result { Ok(node) } -async fn get_hostname() -> Result { - if let Ok(hostname) = tokio::fs::read_to_string("/etc/hostname").await { - Ok(hostname) +async fn get_hostname() -> String { + let hostname = if let Ok(hostname) = tokio::fs::read_to_string("/etc/hostname").await { + hostname + } else if let Ok(hostname) = std::env::var("HOSTNAME") { + hostname } else { - std::env::var("HOSTNAME").context("reading HOSTNAME env var") - } - .map(|hostname| hostname.trim().to_string()) + tracing::warn!("unable to obtain hostname, using empty string"); + String::default() + }; + hostname.trim().to_string() } diff --git a/src/main.rs b/src/main.rs index da113c1..6e12d6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1033,7 +1033,8 @@ fn machine_generate_configs( AddressAllocationPolicy::Total(n) => { let mut counter = 0; while counter < *n { - let machine = machines[(counter as usize) % machines.len()]; + let machine = machines[(counter as usize) % machines.len()]; // TODO: proper error + // message for panic here let address = machine_address_for_idx(machine, counter / (machines.len() as u32)); addresses.push(address); counter += 1; -- cgit