1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! `msb exec` command — execute a command in a sandbox.
use std::io::{IsTerminal, Write};
use std::time::Duration;
use clap::Args;
use microsandbox::sandbox::ExecOutput;
use tokio::io::AsyncReadExt;
use crate::ui;
//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------
/// Run a command in a running sandbox.
#[derive(Debug, Args)]
pub struct ExecArgs {
/// Sandbox to run the command in.
pub name: String,
/// Set an environment variable (KEY=value).
#[arg(short, long)]
pub env: Vec<String>,
/// Set the working directory for the command.
#[arg(short, long)]
pub workdir: Option<String>,
/// Run the command as the specified guest user.
#[arg(short = 'u', long)]
pub user: Option<String>,
/// Allocate a pseudo-terminal (enables colors, line editing).
#[arg(short = 't', long)]
pub tty: bool,
/// Kill the command after this duration (e.g. 30s, 5m, 1h).
#[arg(long)]
pub timeout: Option<String>,
/// Set a POSIX resource limit (e.g. nofile=1024, nproc=64, as=1073741824).
#[arg(long)]
pub rlimit: Vec<String>,
/// Suppress progress output.
#[arg(short, long)]
pub quiet: bool,
/// Command to run inside the sandbox (after --).
/// When omitted in interactive mode, attaches to the default shell.
#[arg(last = true)]
pub command: Vec<String>,
}
//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------
/// Execute the `msb exec` command.
pub async fn run(args: ExecArgs) -> anyhow::Result<()> {
let sandbox = super::resolve_and_start(&args.name, args.quiet).await?;
// Build exec options.
let env_pairs: Vec<(String, String)> = args
.env
.iter()
.map(|s| ui::parse_env(s).map_err(anyhow::Error::msg))
.collect::<anyhow::Result<Vec<_>>>()?;
let workdir = args.workdir;
let interactive = std::io::stdin().is_terminal();
// Read piped stdin upfront so it can be forwarded into the sandbox.
let piped_stdin = if !interactive {
let mut buf = Vec::new();
tokio::io::stdin().read_to_end(&mut buf).await.ok();
Some(buf)
} else {
None
};
// Resolve the command using the same OCI-aware logic as `msb run`:
// user command > entrypoint [+ cmd] > cmd > config.shell > /bin/sh.
let (cmd, cmd_args) =
match super::common::resolve_command(sandbox.config(), args.command, interactive)? {
(Some(cmd), cmd_args) => (cmd, cmd_args),
(None, _) => {
super::maybe_stop(&sandbox).await;
std::process::exit(0);
}
};
// Parse rlimits.
let rlimits: Vec<_> = args
.rlimit
.iter()
.map(|s| super::common::parse_rlimit(s))
.collect::<anyhow::Result<Vec<_>>>()?;
// Parse timeout.
let timeout = match &args.timeout {
Some(t) => Some(Duration::from_secs(super::common::parse_duration_secs(t)?)),
None => None,
};
if interactive {
// Interactive mode with TTY — use attach.
let exit_code = sandbox
.attach_with(cmd, |a| {
let mut a = a.args(cmd_args);
for (k, v) in &env_pairs {
a = a.env(k, v);
}
if let Some(ref cwd) = workdir {
a = a.cwd(cwd);
}
if let Some(ref user) = args.user {
a = a.user(user);
}
for &(resource, soft, hard) in &rlimits {
a = a.rlimit_range(resource, soft, hard);
}
a
})
.await?;
super::maybe_stop(&sandbox).await;
if exit_code != 0 {
std::process::exit(exit_code);
}
} else {
// Non-interactive: exec and capture output.
let output: ExecOutput = sandbox
.exec_with(cmd, |e| {
let mut e = e
.args(cmd_args)
.stdin_bytes(piped_stdin.unwrap_or_default());
for (k, v) in &env_pairs {
e = e.env(k, v);
}
if let Some(ref cwd) = workdir {
e = e.cwd(cwd);
}
if let Some(ref user) = args.user {
e = e.user(user);
}
if args.tty {
e = e.tty(true);
}
if let Some(t) = timeout {
e = e.timeout(t);
}
for &(resource, soft, hard) in &rlimits {
e = e.rlimit_range(resource, soft, hard);
}
e
})
.await?;
std::io::stdout().write_all(output.stdout_bytes())?;
std::io::stderr().write_all(output.stderr_bytes())?;
super::maybe_stop(&sandbox).await;
if !output.status().success {
std::process::exit(output.status().code);
}
}
Ok(())
}