-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask-submit.rs
84 lines (71 loc) · 2.4 KB
/
task-submit.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
//! Submits an example task to an execution service.
//!
//! You can run this with the following command:
//!
//! ```bash
//! export USER="<USER>"
//! export PASSWORD="<PASSWORD>"
//! export RUST_LOG="tes=debug"
//!
//! cargo run --release --features=client,serde --example task-submit <URL>
//! ```
use anyhow::Context;
use anyhow::Result;
use base64::prelude::*;
use tes::v1::client;
use tes::v1::types::Task;
use tes::v1::types::task::Executor;
use tes::v1::types::task::Resources;
use tracing_subscriber::EnvFilter;
/// The environment variable for a basic auth username.
const USER_ENV: &str = "USER";
/// The environment variable for a basic auth password.
const PASSWORD_ENV: &str = "PASSWORD";
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
let url = std::env::args().nth(1).expect("url to be present");
let mut builder = client::Builder::default()
.url_from_string(url)
.expect("url could not be parsed");
let username = std::env::var(USER_ENV).ok();
let password = std::env::var(PASSWORD_ENV).ok();
if (username.is_some() && password.is_none()) || (username.is_none() && password.is_some()) {
panic!("${USER_ENV} and ${PASSWORD_ENV} must both be set to use basic auth");
}
if let Some(username) = username {
let credentials = format!("{}:{}", username, password.unwrap());
let encoded = BASE64_STANDARD.encode(credentials);
builder = builder.insert_header("Authorization", format!("Basic {}", encoded));
}
let client = builder.try_build().expect("could not build client");
let task = Task {
name: Some(String::from("my-task")),
description: Some(String::from("A description.")),
resources: Some(Resources {
cpu_cores: Some(4),
preemptible: Some(true),
..Default::default()
}),
executors: vec![Executor {
image: String::from("ubuntu:latest"),
command: vec![
String::from("/bin/bash"),
String::from("-c"),
String::from("echo 'hello, world!'"),
],
..Default::default()
}],
..Default::default()
};
println!(
"{:#?}",
client
.create_task(task)
.await
.context("submitting a task")?
);
Ok(())
}