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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::process::Stdio;
use std::time::Duration;
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
const COMMAND_TIMEOUT: Duration = Duration::from_secs(60);
#[derive(Clone, Debug)]
pub struct Runner {
ssh: Option<String>,
sudo: bool,
kubectl: String,
}
impl Runner {
pub fn new(ssh: Option<String>, sudo: bool, kubectl: Option<String>) -> Self {
Self {
ssh,
sudo,
kubectl: kubectl.unwrap_or_else(|| "kubectl".to_string()),
}
}
pub fn kubectl_path(&self) -> &str {
&self.kubectl
}
pub fn ssh_host(&self) -> Option<&str> {
self.ssh.as_deref()
}
pub async fn kubectl(&self, args: &[String], stdin: Option<&str>) -> Result<String, String> {
let mut command = Vec::with_capacity(args.len() + 1);
command.push(self.kubectl.clone());
command.extend(args.iter().cloned());
self.run(&command, stdin).await
}
pub async fn shell(&self, script: &str) -> Result<String, String> {
self.shell_with_stdin(script, None).await
}
pub async fn shell_with_stdin(
&self,
script: &str,
stdin: Option<&str>,
) -> Result<String, String> {
let command = vec!["sh".to_string(), "-c".to_string(), script.to_string()];
self.run(&command, stdin).await
}
/// Spawns `kubectl <args>` with stdout piped for streaming (watches).
/// The child is killed when dropped.
pub fn spawn_kubectl_stream(&self, args: &[String]) -> Result<tokio::process::Child, String> {
let mut argv = Vec::with_capacity(args.len() + 1);
argv.push(self.kubectl.clone());
argv.extend(args.iter().cloned());
let mut command = self.command(&argv);
command
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
command
.spawn()
.map_err(|error| format!("failed to spawn {:?}: {error}", argv[0]))
}
/// The local or `ssh` command for `args`, with `sudo -n` when enabled.
fn command(&self, args: &[String]) -> Command {
let command = match &self.ssh {
Some(host) => {
let mut remote = Vec::with_capacity(args.len() + 2);
if self.sudo {
remote.push("sudo".to_string());
remote.push("-n".to_string());
}
remote.extend(args.iter().cloned());
let script = remote
.iter()
.map(|argument| shell_quote(argument))
.collect::<Vec<_>>()
.join(" ");
let mut command = Command::new("ssh");
command
.arg("-o")
.arg("BatchMode=yes")
.arg("-o")
.arg("ConnectTimeout=10")
.arg(host)
.arg(script);
command
}
None => {
let (program, rest) = args.split_first().expect("checked non-empty");
let mut command = if self.sudo {
let mut command = Command::new("sudo");
command.arg("-n").arg(program);
command
} else {
Command::new(program)
};
command.args(rest);
command
}
};
command
}
pub async fn run(&self, args: &[String], stdin: Option<&str>) -> Result<String, String> {
if args.is_empty() {
return Err("refusing to run an empty command".to_string());
}
let mut command = self.command(args);
command
.stdin(if stdin.is_some() {
Stdio::piped()
} else {
Stdio::null()
})
.stdout(Stdio::piped())
.stderr(Stdio::piped());
let mut child = command
.spawn()
.map_err(|error| format!("failed to spawn {:?}: {error}", args[0]))?;
if let Some(input) = stdin {
let mut pipe = child
.stdin
.take()
.ok_or_else(|| "failed to open command stdin".to_string())?;
pipe.write_all(input.as_bytes())
.await
.map_err(|error| format!("failed to write command stdin: {error}"))?;
pipe.shutdown()
.await
.map_err(|error| format!("failed to close command stdin: {error}"))?;
}
let output = tokio::time::timeout(COMMAND_TIMEOUT, child.wait_with_output())
.await
.map_err(|_| {
format!(
"command timed out after {}s: {}",
COMMAND_TIMEOUT.as_secs(),
args.join(" ")
)
})?
.map_err(|error| format!("command failed: {error}"))?;
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
if output.status.success() {
return Ok(stdout);
}
let stderr = String::from_utf8_lossy(&output.stderr);
let detail = if stderr.trim().is_empty() {
stdout
} else {
stderr.to_string()
};
Err(format!(
"command exited with {}: {}",
output.status,
detail.trim()
))
}
}
pub fn shell_quote(value: &str) -> String {
if value
.chars()
.all(|character| character.is_ascii_alphanumeric() || "-_./:=@".contains(character))
{
return value.to_string();
}
format!("'{}'", value.replace('\'', "'\\''"))
}
#[cfg(test)]
mod tests {
use super::shell_quote;
#[test]
fn quotes_only_what_the_remote_shell_needs() {
assert_eq!(shell_quote("kubectl"), "kubectl");
assert_eq!(
shell_quote("/var/lib/kcp/admin.kubeconfig"),
"/var/lib/kcp/admin.kubeconfig"
);
assert_eq!(
shell_quote("*kcp/admin.kubeconfig"),
"'*kcp/admin.kubeconfig'"
);
assert_eq!(shell_quote("a b"), "'a b'");
assert_eq!(shell_quote("it's"), "'it'\\''s'");
}
}