From 31d4d1e233c1572bd5715bde8c58477d8bfaa285 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Fri, 5 Apr 2024 10:49:31 +0200 Subject: [PATCH] env_config from PR #6125 --- control_plane/src/background_process.rs | 14 +++++++- libs/utils/src/env_config.rs | 48 +++++++++++++++++++++++++ libs/utils/src/lib.rs | 1 + libs/utils/src/logging.rs | 8 ++++- 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 libs/utils/src/env_config.rs diff --git a/control_plane/src/background_process.rs b/control_plane/src/background_process.rs index 2fced7d778..fbd739ac9e 100644 --- a/control_plane/src/background_process.rs +++ b/control_plane/src/background_process.rs @@ -86,7 +86,10 @@ where .stdout(process_log_file) .stderr(same_file_for_stderr) .args(args); - let filled_cmd = fill_remote_storage_secrets_vars(fill_rust_env_vars(background_command)); + + let filled_cmd = fill_env_vars_prefixed_neon(fill_remote_storage_secrets_vars( + fill_rust_env_vars(background_command), + )); filled_cmd.envs(envs); let pid_file_to_check = match &initial_pid_file { @@ -268,6 +271,15 @@ fn fill_remote_storage_secrets_vars(mut cmd: &mut Command) -> &mut Command { cmd } +fn fill_env_vars_prefixed_neon(mut cmd: &mut Command) -> &mut Command { + for (var, val) in std::env::vars() { + if var.starts_with("NEON_") { + cmd = cmd.env(var, val); + } + } + cmd +} + /// Add a `pre_exec` to the cmd that, inbetween fork() and exec(), /// 1. Claims a pidfile with a fcntl lock on it and /// 2. Sets up the pidfile's file descriptor so that it (and the lock) diff --git a/libs/utils/src/env_config.rs b/libs/utils/src/env_config.rs new file mode 100644 index 0000000000..823a49796c --- /dev/null +++ b/libs/utils/src/env_config.rs @@ -0,0 +1,48 @@ +use std::{fmt::Display, str::FromStr}; + +pub fn var(varname: &str, default: D) -> V +where + V: FromStr, + E: Display, + D: FnOnce() -> V, +{ + match std::env::var(varname) { + Ok(s) => s + .parse() + .map_err(|e| format!("failed to parse env var {varname}: {e:#}")) + .unwrap(), + Err(std::env::VarError::NotPresent) => default(), + Err(std::env::VarError::NotUnicode(_)) => { + panic!("env var {varname} is not unicode") + } + } +} + +pub struct Bool(bool); + +impl Bool { + pub const fn new_const() -> Self { + Bool(V) + } +} + +impl FromStr for Bool { + type Err = String; + + fn from_str(s: &str) -> Result { + if let Ok(b) = s.parse() { + return Ok(Bool(b)); + } + Ok(Bool(match s { + "0" => false, + "1" => true, + _ => return Err(format!("not a bool, accepting 0|1|{}|{}", false, true)), + })) + } +} + +impl Into for Bool { + fn into(self) -> bool { + self.0 + } +} diff --git a/libs/utils/src/lib.rs b/libs/utils/src/lib.rs index 04ce0626c8..bd04eeea05 100644 --- a/libs/utils/src/lib.rs +++ b/libs/utils/src/lib.rs @@ -27,6 +27,7 @@ pub mod auth; pub mod id; mod hex; +pub mod env_config; pub use hex::Hex; // http endpoint utils diff --git a/libs/utils/src/logging.rs b/libs/utils/src/logging.rs index f7b73dc984..d9531d0438 100644 --- a/libs/utils/src/logging.rs +++ b/libs/utils/src/logging.rs @@ -1,10 +1,16 @@ -use std::str::FromStr; +use std::{ + io::BufWriter, + str::FromStr, + sync::{Arc, Mutex}, +}; use anyhow::Context; use metrics::{IntCounter, IntCounterVec}; use once_cell::sync::Lazy; use strum_macros::{EnumString, EnumVariantNames}; +use super::env_config; + #[derive(EnumString, EnumVariantNames, Eq, PartialEq, Debug, Clone, Copy)] #[strum(serialize_all = "snake_case")] pub enum LogFormat {