diff options
| author | diogo464 <[email protected]> | 2025-10-30 20:03:37 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-10-30 20:03:37 +0000 |
| commit | 2bfd274bf31ed74f690bb5d4087c6a433ab83a06 (patch) | |
| tree | 6fb707941aac04aa9737b127bd6c666197713326 | |
| parent | 6e4b2e481305efdd7caf096ecca99a7060a9c0d1 (diff) | |
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.
| -rw-r--r-- | src/context.rs | 19 | ||||
| -rw-r--r-- | 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 @@ | |||
| 1 | use eyre::{Context as _, Result}; | 1 | use eyre::Result; |
| 2 | 2 | ||
| 3 | use crate::machine::Machine; | 3 | use crate::machine::Machine; |
| 4 | 4 | ||
| @@ -60,7 +60,7 @@ impl Context { | |||
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | async fn get_execution_node() -> Result<ExecutionNode> { | 62 | async fn get_execution_node() -> Result<ExecutionNode> { |
| 63 | let hostname = get_hostname().await?; | 63 | let hostname = get_hostname().await; |
| 64 | let node = match hostname.as_str() { | 64 | let node = match hostname.as_str() { |
| 65 | "frontend" => ExecutionNode::Frontend, | 65 | "frontend" => ExecutionNode::Frontend, |
| 66 | _ => match Machine::from_hostname(&hostname) { | 66 | _ => match Machine::from_hostname(&hostname) { |
| @@ -71,11 +71,14 @@ async fn get_execution_node() -> Result<ExecutionNode> { | |||
| 71 | Ok(node) | 71 | Ok(node) |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | async fn get_hostname() -> Result<String> { | 74 | async fn get_hostname() -> String { |
| 75 | if let Ok(hostname) = tokio::fs::read_to_string("/etc/hostname").await { | 75 | let hostname = if let Ok(hostname) = tokio::fs::read_to_string("/etc/hostname").await { |
| 76 | Ok(hostname) | 76 | hostname |
| 77 | } else if let Ok(hostname) = std::env::var("HOSTNAME") { | ||
| 78 | hostname | ||
| 77 | } else { | 79 | } else { |
| 78 | std::env::var("HOSTNAME").context("reading HOSTNAME env var") | 80 | tracing::warn!("unable to obtain hostname, using empty string"); |
| 79 | } | 81 | String::default() |
| 80 | .map(|hostname| hostname.trim().to_string()) | 82 | }; |
| 83 | hostname.trim().to_string() | ||
| 81 | } | 84 | } |
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( | |||
| 1033 | AddressAllocationPolicy::Total(n) => { | 1033 | AddressAllocationPolicy::Total(n) => { |
| 1034 | let mut counter = 0; | 1034 | let mut counter = 0; |
| 1035 | while counter < *n { | 1035 | while counter < *n { |
| 1036 | let machine = machines[(counter as usize) % machines.len()]; | 1036 | let machine = machines[(counter as usize) % machines.len()]; // TODO: proper error |
| 1037 | // message for panic here | ||
| 1037 | let address = machine_address_for_idx(machine, counter / (machines.len() as u32)); | 1038 | let address = machine_address_for_idx(machine, counter / (machines.len() as u32)); |
| 1038 | addresses.push(address); | 1039 | addresses.push(address); |
| 1039 | counter += 1; | 1040 | counter += 1; |
