Skip to content

Commit ada8bf8

Browse files
committed
Add fallback when specifing user for docker commands and cp scripts to scripts_dir
1 parent 5c2ac9f commit ada8bf8

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

crates/orchestrator/src/network/node.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use anyhow::anyhow;
1010
use fancy_regex::Regex;
1111
use glob_match::glob_match;
1212
use prom_metrics_parser::MetricMap;
13-
use provider::DynNode;
13+
use provider::{
14+
types::{ExecutionResult, RunScriptOptions},
15+
DynNode,
16+
};
1417
use serde::{Deserialize, Serialize, Serializer};
1518
use subxt::{backend::rpc::RpcClient, OnlineClient};
1619
use support::net::{skip_err_while_waiting, wait_ws_ready};
@@ -281,8 +284,8 @@ impl NetworkNode {
281284
/// Returns `Ok(stdout)` on success, or `Err((exit_status, stderr))` on failure.
282285
pub async fn run_script(
283286
&self,
284-
options: provider::types::RunScriptOptions,
285-
) -> Result<provider::types::ExecutionResult, anyhow::Error> {
287+
options: RunScriptOptions,
288+
) -> Result<ExecutionResult, anyhow::Error> {
286289
self.inner
287290
.run_script(options)
288291
.await

crates/provider/src/docker/client.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -311,28 +311,31 @@ impl DockerClient {
311311
where
312312
S: Into<String> + std::fmt::Debug + Send + Clone,
313313
{
314-
let mut cmd = self.client_command();
315-
cmd.arg("exec");
316-
317-
if let Some(env) = env {
318-
for env_var in env {
319-
cmd.args(["-e", &format!("{}={}", env_var.0.into(), env_var.1.into())]);
320-
}
321-
}
314+
// Try with specified user first
315+
let result = self
316+
.exec_with_user(name, command.clone(), env.clone(), as_user.clone())
317+
.await?;
322318

323-
if let Some(user) = as_user {
324-
cmd.args(["-u", user.into().as_ref()]);
319+
// If command failed and we specified a user, retry without user
320+
if result.is_err() && as_user.is_some() {
321+
trace!("Command failed with user, retrying without user specification");
322+
return self.exec_with_user(name, command, env, None).await;
325323
}
326324

327-
cmd.arg(name);
328-
329-
cmd.args(
330-
command
331-
.clone()
332-
.into_iter()
333-
.map(|s| <S as Into<String>>::into(s)),
334-
);
325+
Ok(result)
326+
}
335327

328+
async fn exec_with_user<S>(
329+
&self,
330+
name: &str,
331+
command: Vec<S>,
332+
env: Option<Vec<(S, S)>>,
333+
as_user: Option<S>,
334+
) -> Result<ExecutionResult>
335+
where
336+
S: Into<String> + std::fmt::Debug + Send + Clone,
337+
{
338+
let mut cmd = self.build_exec_command(name, command.clone(), env, as_user);
336339
trace!("cmd is : {:?}", cmd);
337340

338341
let result = cmd.output().await.map_err(|err| {
@@ -357,6 +360,35 @@ impl DockerClient {
357360
Ok(Ok(String::from_utf8_lossy(&result.stdout).to_string()))
358361
}
359362

363+
fn build_exec_command<S>(
364+
&self,
365+
name: &str,
366+
command: Vec<S>,
367+
env: Option<Vec<(S, S)>>,
368+
as_user: Option<S>,
369+
) -> Command
370+
where
371+
S: Into<String> + std::fmt::Debug + Send + Clone,
372+
{
373+
let mut cmd = self.client_command();
374+
cmd.arg("exec");
375+
376+
if let Some(env) = env {
377+
for env_var in env {
378+
cmd.args(["-e", &format!("{}={}", env_var.0.into(), env_var.1.into())]);
379+
}
380+
}
381+
382+
if let Some(user) = as_user {
383+
cmd.args(["-u", user.into().as_ref()]);
384+
}
385+
386+
cmd.arg(name);
387+
cmd.args(command.into_iter().map(|s| <S as Into<String>>::into(s)));
388+
389+
cmd
390+
}
391+
360392
pub async fn container_cp(
361393
&self,
362394
name: &str,

crates/provider/src/docker/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ where
372372
&self.container_name,
373373
vec!["mkdir", "-p", &remote_dir.to_string_lossy()],
374374
None,
375-
None,
375+
Some("root"),
376376
)
377377
.await
378378
.map_err(|err| {
@@ -511,7 +511,7 @@ where
511511
))
512512
.to_string_lossy();
513513

514-
let remote_script_path = PathBuf::from(format!("/tmp/{file_name}"));
514+
let remote_script_path = self.scripts_dir.join(Path::new(file_name.as_ref()));
515515

516516
// Upload the script to the container and make it executable
517517
self.send_file(&options.local_script_path, &remote_script_path, "0755")

0 commit comments

Comments
 (0)