From 1746470367bd149ff9b3b4ec7ea3fa5374bcbd70 Mon Sep 17 00:00:00 2001 From: Kinesin Data Technologies Incorporated <93931750+kinesintech@users.noreply.github.com> Date: Thu, 29 Sep 2022 14:07:01 -0300 Subject: [PATCH] Adding in inherited environment variables to local executor --- src/executors/local_executor.rs | 38 ++++++++++++++++++++++++++++----- src/varmap.rs | 4 ++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/executors/local_executor.rs b/src/executors/local_executor.rs index 38a1411..5efd5a7 100644 --- a/src/executors/local_executor.rs +++ b/src/executors/local_executor.rs @@ -11,6 +11,8 @@ use tokio::time::{sleep, Duration}; use futures::StreamExt; use tokio::io::AsyncReadExt; +type Environment = HashMap>; + #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(untagged)] enum Cmd { @@ -37,7 +39,7 @@ struct LocalTaskDetail { /// Environment variables to set #[serde(default)] - environment: HashMap>, + environment: Environment, /// Timeout in seconds #[serde(default)] @@ -104,6 +106,7 @@ async fn run_task( mut stop_rx: oneshot::Receiver<()>, output_options: TaskOutputOptions, varmap: VarMap, + mut env: Environment, ) -> Result { let mut details = extract_details(&task).unwrap(); let mut attempt = TaskAttempt::new(); @@ -117,9 +120,10 @@ async fn run_task( command.stderr(Stdio::piped()); command.args(args); - // Need to convert optional - let cmd_env: HashMap = details - .environment + // Build out environment. This takes the initial environment, and will + // upsert it with the task details. + env.extend(details.environment); + let cmd_env: HashMap = env .iter() .filter(|(_, v)| v.is_some()) .map(|(k, v)| (k.clone(), varmap.apply_to(&v.clone().unwrap()))) @@ -218,6 +222,29 @@ pub async fn start_local_executor( ) { let mut running = FuturesUnordered::new(); + /* + Inherited environment vars + */ + + let default_vars = [ + "LANG", + "HOSTNAME", + "LOGNAME", + "USER", + "PATH", + "HOME", + "XDG_CONFIG_HOME", + "ALL_PROXY", + "FTP_PROXY", + "HTTPS_PROXY", + "HTTP_PROXY", + "NO_PROXY", + ]; + let inherited_env: Environment = default_vars + .iter() + .map(|envvar| (envvar.to_string(), std::env::var(envvar).ok())) + .collect(); + while let Some(msg) = exe_msgs.recv().await { use ExecutorMessage::{ExecuteTask, Stop, ValidateTask}; match msg { @@ -237,8 +264,9 @@ pub async fn start_local_executor( if running.len() == max_parallel { running.next().await; } + let env = inherited_env.clone(); running.push(tokio::spawn(async move { - let attempt = match run_task(details, kill, output_options, varmap).await { + let attempt = match run_task(details, kill, output_options, varmap, env).await { Ok(attempt) => attempt, Err(e) => TaskAttempt { succeeded: false, diff --git a/src/varmap.rs b/src/varmap.rs index c601493..b2e1013 100644 --- a/src/varmap.rs +++ b/src/varmap.rs @@ -18,6 +18,10 @@ impl DerefMut for VarMap { } impl VarMap { + pub fn new() -> Self { + VarMap(HashMap::new()) + } + // Derive variables from a given interval pub fn from_interval(int: &Interval, tz: Tz) -> Self { let start = int.start.with_timezone(&tz);