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
use ntex::channel::mpsc::Receiver;
use nanocl_error::http::HttpResult;
use nanocl_error::http_client::HttpClientResult;
use bollard_next::exec::{CreateExecResults, StartExecOptions};
use bollard_next::service::ExecInspectResponse;
use nanocl_stubs::cargo::CreateExecOptions;
use nanocl_stubs::generic::GenericNspQuery;
use nanocl_stubs::process::OutputLog;
use super::http_client::NanocldClient;
impl NanocldClient {
/// ## Default path for exec commands
const EXEC_PATH: &'static str = "/exec";
/// Create exec command inside a cargo
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
/// use nanocld_client::models::cargo_config::CreateExecOptions;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let exec = CreateExecOptions {
/// cmd: vec!["echo".into(), "hello".into()],
/// ..Default::default()
/// };
/// let result = client.create_exec("my-cargo", exec, None).await.unwrap();
/// println!("{}", result);
/// ```
pub async fn create_exec(
&self,
name: &str,
exec: &CreateExecOptions,
namespace: Option<&str>,
) -> HttpClientResult<CreateExecResults> {
let res = self
.send_post(
&format!("/cargoes/{name}/exec"),
Some(exec),
Some(GenericNspQuery::new(namespace)),
)
.await?;
Self::res_json(res).await
}
/// Inspect an exec command inside a cargo instance.
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
/// use nanocld_client::models::cargo_config::{CreateExecOptions, StartExecOptions};
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let exec = CreateExecOptions {
/// cmd: Some(vec!["echo".into(), "hello".into()]),
/// ..Default::default()
/// };
/// let result = client.create_exec("my-cargo", exec, None).await.unwrap();
/// let mut rx = client
/// .start_exec(&result.id, StartExecOptions::default())
/// .await
/// .unwrap();
/// while let Some(_out) = rx.next().await {}
///
/// client.inspect_exec(&result.id).await.unwrap();
/// let result = client.inspect_exec("my-cargo", exec, None).await.unwrap();
/// println!("{}", result);
/// ```
pub async fn inspect_exec(
&self,
id: &str,
) -> HttpClientResult<ExecInspectResponse> {
let res = self
.send_get(&format!("{}/{id}/cargo/inspect", Self::EXEC_PATH), Some(()))
.await?;
Self::res_json(res).await
}
/// Run an command inside a cargo
///
/// ## Example
///
/// ```no_run,ignore
/// use futures::StreamExt;
/// use nanocld_client::NanocldClient;
/// use nanocld_client::models::cargo_config::CreateExecOptions;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let exec = CreateExecOptions {
/// cmd: vec!["echo".into(), "hello".into()],
/// ..Default::default()
/// };
/// let result = client.create_exec("my-cargo", exec, None).await.unwrap();
/// let mut rx = client.start_exec(&result.id, StartExec::default(), None).await.unwrap();
/// while let Some(output) = rx.next().await {
/// println!("{}", output);
/// };
/// ```
pub async fn start_exec(
&self,
id: &str,
exec: &StartExecOptions,
) -> HttpClientResult<Receiver<HttpResult<OutputLog>>> {
let res = self
.send_post(
&format!("{}/{id}/cargo/start", &Self::EXEC_PATH),
Some(exec),
Some(()),
)
.await?;
Ok(Self::res_stream(res).await)
}
}
#[cfg(test)]
mod tests {
use bollard_next::exec::{CreateExecOptions, StartExecOptions};
use futures::StreamExt;
use crate::{ConnectOpts, NanocldClient};
#[ntex::test]
async fn exec_cargo() {
let client = NanocldClient::connect_to(&ConnectOpts {
url: "http://nanocl.internal:8585".into(),
..Default::default()
})
.expect("Failed to create a nanocl client");
let exec = CreateExecOptions {
cmd: Some(vec!["echo".into(), "hello".into()]),
..Default::default()
};
let result = client
.create_exec("nstore", &exec, Some("system"))
.await
.unwrap();
let mut rx = client
.start_exec(&result.id, &StartExecOptions::default())
.await
.unwrap();
while let Some(_out) = rx.next().await {}
client.inspect_exec(&result.id).await.unwrap();
}
}