-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rs
376 lines (334 loc) · 16 KB
/
client.rs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright © 2022 David Caldwell <david@porkrind.org>
use std::error::Error;
use std::ffi::OsString;
use std::os::unix::process::ExitStatusExt;
use reqwest::{header,Url};
use reqwest::header::{CONTENT_TYPE, ACCEPT};
use crate::serve;
use crate::maybe_utf8::MaybeUTF8;
#[derive(Debug)]
pub struct Job {
id: String,
timeout: Option<std::time::Duration>,
cmd: Command,
api: Api,
}
impl Job {
pub async fn new(server_url: Url, user: &str, name: &str, id: Option<&str>, timeout: Option<std::time::Duration>, cmd: &Command) -> Result<Job, Box<dyn Error>> {
let mut env=vec![];
for (k, v) in std::env::vars_os() {
env.push((MaybeUTF8::new(k),MaybeUTF8::new(v)));
}
let api = Api::new(server_url)?;
let resp: serve::CreateRunResp = serde_json::from_str(&api.post("/run/create", &serde_json::to_string(&serve::CreateRunReq{ user:user.to_string(), name:name.to_string(), id:id.map(|i|i.to_string()), cmd:cmd.to_string(), env:env })?.as_bytes()).await?)?;
Ok(Job { id:resp.id, timeout:timeout, cmd:cmd.to_owned(), api: api })
}
pub async fn run(&self) -> Result<(), Box<dyn Error>> {
let mut child = self.cmd.create()
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()?;
trace!("Spawned child {:?}", child);
let outpiper = Job::copy_output(self.api.clone(), child.stdout.take().unwrap(), self.id.clone(), serve::OutKind::Stdout);
let errpiper = Job::copy_output(self.api.clone(), child.stderr.take().unwrap(), self.id.clone(), serve::OutKind::Stderr);
let pipers = async { tokio::join!(outpiper, errpiper) };
let heartbeat = async {//|| -> Result<(), ()> {
let now = std::time::Instant::now();
loop {
trace!("Waiting 1 second");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
if self.timeout.is_some() && now.elapsed() > self.timeout.unwrap() {
trace!("Timed out");
return Err(now.elapsed());
}
trace!("Sending Hearbeat");
let _resp = self.api.post(&format!("/run/{}/heartbeat", self.id), &[]).await;
}
#[allow(unreachable_code)] Ok(()) // if I remove this, it errs.
};
let mut timeout = false;
loop {
tokio::select! {
_ = pipers => { break; },
Err(elapsed) = heartbeat => {
trace!("Timeout reached after {:?}! Killing child {:?}", elapsed, child);
timeout = true;
child.kill().await?;
break;
},
}
}
let exitcode = child.wait().await?;
let status = match (exitcode.code(), exitcode.signal(), exitcode.core_dumped(), timeout) {
(_, _, _, true) => serve::ExitStatus::ClientTimeout,
(Some(code), _, _, _) => serve::ExitStatus::Exited(code),
(_, Some(sig), false, _) => serve::ExitStatus::Signal(sig),
(_, Some(sig), true, _) => serve::ExitStatus::CoreDump(sig),
(None, None, _, _) => panic!("Can't happen"),
};
self.api.post(&format!("/run/{}/complete", self.id), &serde_json::to_string(&status)?.as_bytes()).await?;
Ok(())
}
async fn copy_output<T: tokio::io::AsyncRead+Unpin >(api: Api, mut from: T, id: String, kind: serve::OutKind)/* -> Result<(),()> */{
use tokio::io::AsyncReadExt;
let mut buffer = [0; 4096];
loop {
if let Ok(read) = from.read(&mut buffer).await {
if read == 0 { break }
let _resp = api.post(&format!("/run/{}/{}", id, kind), &buffer[0..read]).await;
} else { break }
}
}
}
#[derive(Debug,Clone)]
pub struct Api {
server: Url,
ua: reqwest::Client,
}
impl Api {
fn new(server_url: Url) -> Result<Api, Box<dyn Error>> {
let mut fake_browser_headers = header::HeaderMap::new();
fake_browser_headers.insert(ACCEPT, header::HeaderValue::from_static("application/json"));
let client = reqwest::Client::builder()
.connection_verbose(true)
.default_headers(fake_browser_headers)
.build()?;
Ok(Api {
server: server_url,
ua: client,
})
}
pub async fn post(&self, path: &str, body: &[u8]) -> Result<String, Box<dyn Error>> {
let bod: Vec<u8> = body.into();
let resp = self.ua.post(self.server.join(path)?)
.header(CONTENT_TYPE, "application/json")
.body(bod)
.send()
.await?;
resp.error_for_status_ref()?;
use std::os::unix::ffi::OsStringExt;
trace!("API: {} <- {:?}", self.server.join(path)?, OsString::from_vec(body.to_vec()).to_string_lossy());
Ok(resp.text().await?)
}
pub async fn get(&self, path: &str) -> Result<String, Box<dyn Error>> {
let resp = self.ua.get(self.server.join(path)?)
.send()
.await?;
resp.error_for_status_ref()?;
let resp_str = resp.text().await?;
trace!("API: {} -> {}", self.server.join(path)?, resp_str);
Ok(resp_str)
}
}
pub async fn fallback_run(timeout: Option<std::time::Duration>, cmd: &Command) -> Result<(), Box<dyn Error>> {
// This is largely a copy+paste of Job::run(), above, but I don't know that it's worth it to abstract and de-duplicate.
let mut child = cmd.create()
.stdin(std::process::Stdio::null())
.spawn()?;
trace!("Spawned child in fallback mode{:?}", child);
let heartbeat = async {
let now = std::time::Instant::now();
loop {
trace!("Waiting 1 second");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
if timeout.is_some() && now.elapsed() > timeout.unwrap() {
trace!("Timed out");
return Err(now.elapsed());
}
}
#[allow(unreachable_code)] Ok(()) // if I remove this, it errs.
};
loop {
tokio::select! {
status = child.wait() => {
match status {
Err(e) => {
error!("Child process failed: {}", e);
Err(e)?;
break;
},
Ok(status) => {
trace!("Child exited with {}", status);
if let Some(code) = status.code() {
std::process::exit(code); // pass it on
}
break;
}
}
},
Err(elapsed) = heartbeat => {
error!("Timeout reached after {:?}! Killing child {:?}", elapsed, child);
child.kill().await?;
break;
},
}
}
Ok(())
}
#[derive(Clone, Debug)]
pub enum Command {
Shell(String),
Exec(Vec<String>),
}
impl Command {
pub fn create(&self) -> tokio::process::Command {
let (exe, args) = match self {
Command::Exec(args) => (args[0].clone(), args[1..].iter().map(|a| OsString::from(&a)).collect::<Vec<_>>()),
Command::Shell(cmd) => {
let shell = match (std::env::var("SYNCRON_SHELL"), std::env::var("SHELL"), std::env::args().nth(0)) {
(Ok(sh), _, _) => sh,
(_, Ok(sh), Some(me)) if sh != me => sh, // Prevent recursion
(_, Ok(sh), None) => sh, // Probably will never happen
(_, _, _) => "/bin/sh".to_string(),
};
(shell, vec![OsString::from("-c"), OsString::from(&cmd)])
},
};
let mut c = tokio::process::Command::new(exe);
c.args(args);
c
}
pub fn to_string(&self) -> String {
match self {
Command::Shell(cmd) => cmd.clone(),
Command::Exec(args) => {
let mut cmd = String::new();
for arg in args.iter() {
match (arg.contains(&['$', '`', '\\', '!', '"']), // Things that need single quotes
arg.contains('\''),
arg.contains(&[' ', '<', '>', '#', '^', ';', '*', '&', '|', '(', ')', '{', '}', '\n'])) // things that only need double quotes
{
(true, false, _) => cmd.extend(["'", arg, "' "]),
(false, true, _) |
(false, false, true) => cmd.extend(["\"", arg, "\" "]),
(true, true, _) => cmd.extend(["'", &arg.replace('\'', "'\"'\"'"), "' "]),
(false, false, false) => cmd.extend([arg, " "]),
}
}
cmd.trim_end().into()
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::db;
use chrono::Datelike;
async fn test_db() -> (db::Db, tempfile::TempDir) {
let db_path = tempfile::Builder::new().prefix("syncron-test").tempdir().unwrap();
let db = db::Db::new(&db_path.path()).await.expect(&format!("Create db in {}", db_path.path().to_string_lossy()));
(db, db_path)
}
impl PartialEq for db::Job {
fn eq(&self, a: &db::Job) -> bool {
self.user == a.user &&
self.id == a.id &&
self.name == a.name &&
self.job_id == a.job_id
}
}
impl PartialEq for db::Run {
fn eq(&self, a: &db::Run) -> bool {
self.job == a.job &&
self.date == a.date &&
self.run_id == a.run_id &&
self.run_db_id == a.run_db_id &&
self.client_id == a.client_id &&
self.log_path == a.log_path
}
}
macro_rules! assert_file_eq {
( $path:expr, $contents:expr ) => {
let b = std::fs::read($path).expect(&$path.to_string_lossy());
assert_eq!(String::from_utf8_lossy(&b), $contents);
}
}
#[test]
fn init_logging() {
simple_logger::SimpleLogger::new().init().unwrap();
}
#[tokio::test]
async fn basic() {
let (db, db_path) = test_db().await;
let cmd = "echo a simple test";
let env = vec![
(MaybeUTF8::new(OsString::from("PATH")), MaybeUTF8::new(OsString::from("Something:something_else/"))),
(MaybeUTF8::new(OsString::from("HOME")), MaybeUTF8::new(OsString::from("/home/my-home-dir"))),
(MaybeUTF8::new(OsString::from("SOMETHING WACKY")), MaybeUTF8::new(OsString::from("Oh\nDear"))),
];
let run = db::Run::create(&db, "test-user", "David's The _absolute_ Greatest", None, cmd.to_string(), env).await.expect("db::Run create worked");
assert_eq!(run.job.id, "david-s-the-absolute-greatest");
let id = run.client_id.expect("got client_id");
let mut run2 = db::Run::from_client_id(&db, id).await.expect("db::Run::from_client_id()");
assert!(run.date - run2.date < chrono::Duration::milliseconds(1000), "Dates are less close than expected {} vs {}", run.date, run2.date);
run2.date = run.date;
assert_eq!(run, run2);
run2.add_stdout("Some text. ").expect("text added");
run2.add_stdout("Some more text.\n").expect("more text added");
run2.add_stdout("Even more text.\n").expect("even more text added");
run2.complete(serve::ExitStatus::Exited(0)).await.expect("completed with no errors");
assert_file_eq!(&db_path.path().join("jobs").join("test-user").join("david-s-the-absolute-greatest")
.join(run2.date.year().to_string()).join(run2.date.month().to_string()).join(run2.date.day().to_string())
.join(&run.run_id).join("log"), "Some text. Some more text.\nEven more text.\n");
let run3 = db::Run::from_client_id(&db, id).await;
assert!(run3.is_err(), "db::Run::from_client_id() returned was {:?}", run3);
}
#[tokio::test]
async fn heartbeat_timeout() {
let (db, db_path) = test_db().await;
let cmd = "echo a simple test";
let run = db::Run::create(&db, "test-user", "David's The _absolute_ Greatest", None, cmd.to_string(), vec![]).await.expect("db::Run create worked");
sqlx::query!("UPDATE run SET heartbeat = 0 WHERE run_id = ?", run.run_db_id).execute(db.sql()).await.expect("update run set heartbeat");
let info = run.info().await.expect("got info");
assert_eq!(info.status, Some(serve::ExitStatus::ServerTimeout));
}
#[tokio::test]
async fn integration() {
let (db, db_path) = test_db().await;
let cmd = Command::Shell("echo a simple test".into());
std::env::set_var("MY_ENV_VAR", "some value");
let db_path = db_path.path().to_path_buf();
let _serve = tokio::spawn({ let db = db.clone(); async move { serve::serve(32923, &db, true).await.unwrap(); }});
let _client = tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(100)).await; // HACK
let job = crate::client::Job::new("http://127.0.0.1:32923/".parse().unwrap(), "test-user", "My Job", Some("my-id"), None, &cmd).await.unwrap();
let log_path = sqlx::query!("SELECT log FROM run WHERE client_id = ?", job.id).fetch_one(db.sql()).await.expect("SELECT log FROM run").log;
job.run().await.expect("job ran");
assert_file_eq!(&db_path.join(&log_path), "a simple test\n");
let jobs: Vec<serve::JobInfo> = serde_json::from_str(&job.api.get("/jobs").await.expect("GET /jobs")).expect("GET /jobs parse");
assert_eq!(jobs.len(), 1);
assert_eq!(jobs[0].id, "my-id");
assert_eq!(jobs[0].user, "test-user");
assert_eq!(jobs[0].name, "My Job");
let runs: Vec<serve::RunInfo> = serde_json::from_str(&job.api.get(&jobs[0].runs_url).await.expect("GET runs")).expect("GET runs parse");
assert_eq!(runs.len(), 1);
println!("runs: {:?}", runs);
assert_eq!(runs[0].status, Some(serve::ExitStatus::Exited(0)));
let run: serve::RunInfoFull = serde_json::from_str(&job.api.get(&runs[0].url.as_ref().expect("runs[0].url")).await.expect("GET run")).expect("GET run parse");
assert_eq!(run.cmd, cmd.to_string());
assert!(run.env.contains(&(MaybeUTF8::new(OsString::from("MY_ENV_VAR")), MaybeUTF8::new(OsString::from("some value")))));
assert_eq!(run.log.expect("run.log"), "a simple test\n");
job.api.post("/shutdown", &[]).await.expect("POST /shutdown");
}).await.unwrap();
_serve.await.unwrap();
}
#[tokio::test]
async fn timeout() {
trace!("Testing");
let (db, db_path) = test_db().await;
let cmd = Command::Exec(vec!["sleep".into(), "10".into()]);
let db_path = db_path.path().to_path_buf();
let _serve = tokio::spawn({let db = db.clone(); async move { serve::serve(32924, &db, true).await.unwrap(); }});
let _client = tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(100)).await; // HACK
let job = crate::client::Job::new("http://127.0.0.1:32924/".parse().unwrap(), "test-user", "My Bad Job", None, Some(std::time::Duration::from_millis(1500)), &cmd).await.unwrap();
let log_path = sqlx::query!("SELECT log FROM run WHERE client_id = ?", job.id).fetch_one(db.sql()).await.expect("SELECT log FROM run").log;
job.run().await.expect("job ran");
assert_eq!(db_path.join(&log_path).exists(), false);
job.api.post("/shutdown", &[]).await.expect("POST /shutdown");
}).await.unwrap();
_serve.await.unwrap();
}
}