env_config from PR #6125

This commit is contained in:
Christian Schwarz
2024-04-05 10:49:31 +02:00
parent 0c6367a732
commit 31d4d1e233
4 changed files with 69 additions and 2 deletions

View File

@@ -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)

View File

@@ -0,0 +1,48 @@
use std::{fmt::Display, str::FromStr};
pub fn var<V, E, D>(varname: &str, default: D) -> V
where
V: FromStr<Err = E>,
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<const V: bool>() -> Self {
Bool(V)
}
}
impl FromStr for Bool {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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<bool> for Bool {
fn into(self) -> bool {
self.0
}
}

View File

@@ -27,6 +27,7 @@ pub mod auth;
pub mod id;
mod hex;
pub mod env_config;
pub use hex::Hex;
// http endpoint utils

View File

@@ -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 {