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
use crate::{
conn::{tty, Headers, Payload},
opts, Result, Value,
};
use containers_api::url;
#[derive(Debug)]
/// [Api Reference](https://docs.podman.io/en/latest/_static/api.html?version=v4.3.1#tag/Exec)
pub struct Exec {
podman: crate::Podman,
id: crate::Id,
is_tty: bool,
is_unchecked: bool,
}
impl Exec {
///Exports an interface exposing operations against a Exec instance with TTY
pub(crate) fn new_tty(podman: crate::Podman, id: impl Into<crate::Id>) -> Self {
Exec {
podman,
id: id.into(),
is_tty: true,
is_unchecked: false,
}
}
///Exports an interface exposing operations against a Exec instance without TTY
pub(crate) fn new_raw(podman: crate::Podman, id: impl Into<crate::Id>) -> Self {
Exec {
podman,
id: id.into(),
is_tty: false,
is_unchecked: false,
}
}
///Exports an interface exposing operations against a Exec instance with unchecked TTY state
pub(crate) fn new_unchecked(podman: crate::Podman, id: impl Into<crate::Id>) -> Self {
Exec {
podman,
id: id.into(),
is_tty: false,
is_unchecked: true,
}
}
///A getter for Exec id
pub fn id(&self) -> &crate::Id {
&self.id
}
}
#[derive(Debug)]
/// Handle for Podman Execs.
pub struct Execs {
podman: crate::Podman,
}
impl Execs {
///Exports an interface for interacting with Podman Execs.
pub fn new(podman: crate::Podman) -> Self {
Execs { podman }
}
///Returns a reference to a set of operations available to a specific Exec.
pub fn get(&self, id: impl Into<crate::Id>) -> Exec {
Exec::new_unchecked(self.podman.clone(), id)
}
}
impl Exec {
api_doc! {
Exec => StartLibpod
|
/// Starts a previously set up exec instance. If `detach` is true, this endpoint returns
/// immediately after starting the command. Otherwise, it sets up an interactive session
/// with the command.
///
/// To create an exec instance use [`Container::create_exec`](crate::api::Container::create_exec).
///
/// Examples:
///
/// ```no_run
/// async {
/// use podman_api::Podman;
/// use futures_util::StreamExt;
/// let podman = Podman::unix("/run/user/1000/podman/podman.sock");
/// let container = podman.containers().get("451b27c6b9d3");
///
/// let exec = container
/// .create_exec(
/// &podman_api::opts::ExecCreateOpts::builder()
/// .command(["cat", "/some/path/in/container"])
/// .build(),
/// )
/// .await
/// .unwrap();
///
/// let opts = Default::default();
/// let mut stream = exec.start(&opts).await.unwrap().unwrap();
///
/// while let Some(chunk) = stream.next().await {
/// println!("{:?}", chunk.unwrap());
/// }
/// };
/// ```
pub async fn start<'exec>(
&'exec self,
opts: &'exec opts::ExecStartOpts,
) -> Result<Option<tty::Multiplexer>> {
if self.is_unchecked {
return Err(crate::Error::UncheckedExec);
}
let ep = format!("/libpod/exec/{}/start", &self.id);
let payload = Payload::Json(
opts.serialize()
.map_err(|e| crate::conn::Error::Any(Box::new(e)))?,
);
let detach = opts.params.get("Detach").and_then(|value| value.as_bool()).unwrap_or(false);
if !detach {
self.podman.clone().post_upgrade_stream(ep, payload).await.map(|x| {
if self.is_tty {
Some(tty::Multiplexer::new(x, tty::decode_raw))
} else {
Some(tty::Multiplexer::new(x, tty::decode_chunk))
}
})
} else {
self.podman.post(ep, payload, None).await.map(|_| None)
}
}}
api_doc! {
Exec => InspectLibpod
|
/// Returns low-level information about an exec instance.
///
/// Examples:
///
/// ```no_run
/// async {
/// use podman_api::Podman;
/// use futures_util::StreamExt;
/// let podman = Podman::unix("/run/user/1000/podman/podman.sock");
/// let container = podman.containers().get("451b27c6b9d3");
///
/// let exec = container
/// .create_exec(
/// &podman_api::opts::ExecCreateOpts::builder()
/// .command(["cat", "/some/path/in/container"])
/// .build(),
/// )
/// .await
/// .unwrap();
///
/// match exec.inspect().await {
/// Ok(info) => println!("{:?}", info),
/// Err(e) => eprintln!("{}", e)
/// }
/// };
/// ```
pub async fn inspect(&self) -> Result<Value> {
let ep = format!("/libpod/exec/{}/json", &self.id);
self.podman.get_json(&ep).await
}}
api_doc! {
Exec => ResizeLibpod
|
/// Resize the TTY session used by an exec instance. This endpoint only works if
/// tty was specified as part of creating and starting the exec instance.
///
/// Examples:
///
/// ```no_run
/// use futures_util::StreamExt;
/// async {
/// use podman_api::Podman;
/// let podman = Podman::unix("/run/user/1000/podman/podman.sock");
/// let container = podman.containers().get("451b27c6b9d3");
///
/// let exec = container
/// .create_exec(
/// &podman_api::opts::ExecCreateOpts::builder()
/// .command(["cat", "/some/path/in/container"])
/// .build(),
/// )
/// .await
/// .unwrap();
///
/// if let Err(e) = exec.resize(1280, 720).await {
/// eprintln!("{}", e);
/// }
/// };
/// ```
pub async fn resize(&self, width: usize, heigth: usize) -> Result<()> {
let ep = url::construct_ep(
format!("/libpod/exec/{}/resize", &self.id),
Some(url::encoded_pairs([
("h", heigth.to_string()),
("w", width.to_string()),
])),
);
self.podman.post(&ep, Payload::None::<&str>, Headers::none()).await.map(|_| ())
}}
}