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
use std::{collections::BTreeMap, env::var, fmt::Debug, time::Duration};
use super::ports::Ports;
pub trait Image
where
Self: Sized,
Self::Args: ImageArgs + Clone + Debug,
{
type Args;
fn name(&self) -> String;
fn tag(&self) -> String;
fn ready_conditions(&self) -> Vec<WaitFor>;
fn env_vars(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
Box::new(std::iter::empty())
}
fn volumes(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
Box::new(std::iter::empty())
}
fn entrypoint(&self) -> Option<String> {
None
}
fn expose_ports(&self) -> Vec<u16> {
Default::default()
}
#[allow(unused_variables)]
fn exec_after_start(&self, cs: ContainerState) -> Vec<ExecCommand> {
Default::default()
}
}
#[derive(Default, Debug)]
pub struct ExecCommand {
pub cmd: String,
pub ready_conditions: Vec<WaitFor>,
}
#[derive(Debug)]
pub struct ContainerState {
ports: Ports,
}
impl ContainerState {
pub fn new(ports: Ports) -> Self {
Self { ports }
}
#[deprecated(
since = "0.13.1",
note = "Use `host_port_ipv4()` or `host_port_ipv6()` instead."
)]
pub fn host_port(&self, internal_port: u16) -> u16 {
self.host_port_ipv4(internal_port)
}
pub fn host_port_ipv4(&self, internal_port: u16) -> u16 {
self.ports
.map_to_host_port_ipv4(internal_port)
.unwrap_or_else(|| {
panic!(
"Container does not have a mapped port for {}",
internal_port
)
})
}
pub fn host_port_ipv6(&self, internal_port: u16) -> u16 {
self.ports
.map_to_host_port_ipv6(internal_port)
.unwrap_or_else(|| {
panic!(
"Container does not have a mapped port for {}",
internal_port
)
})
}
}
pub trait ImageArgs {
fn into_iterator(self) -> Box<dyn Iterator<Item = String>>;
}
impl ImageArgs for () {
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
Box::new(vec![].into_iter())
}
}
#[must_use]
#[derive(Debug)]
pub struct RunnableImage<I: Image> {
image: I,
image_args: I::Args,
image_tag: Option<String>,
container_name: Option<String>,
network: Option<String>,
env_vars: BTreeMap<String, String>,
volumes: BTreeMap<String, String>,
ports: Option<Vec<Port>>,
}
impl<I: Image> RunnableImage<I> {
pub fn inner(&self) -> &I {
&self.image
}
pub fn args(&self) -> &I::Args {
&self.image_args
}
pub fn network(&self) -> &Option<String> {
&self.network
}
pub fn container_name(&self) -> &Option<String> {
&self.container_name
}
pub fn env_vars(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
Box::new(self.image.env_vars().chain(self.env_vars.iter()))
}
pub fn volumes(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
Box::new(self.image.volumes().chain(self.volumes.iter()))
}
pub fn ports(&self) -> &Option<Vec<Port>> {
&self.ports
}
pub fn entrypoint(&self) -> Option<String> {
self.image.entrypoint()
}
pub fn descriptor(&self) -> String {
if let Some(tag) = &self.image_tag {
format!("{}:{}", self.image.name(), tag)
} else {
format!("{}:{}", self.image.name(), self.image.tag())
}
}
pub fn ready_conditions(&self) -> Vec<WaitFor> {
self.image.ready_conditions()
}
pub fn expose_ports(&self) -> Vec<u16> {
self.image.expose_ports()
}
pub fn exec_after_start(&self, cs: ContainerState) -> Vec<ExecCommand> {
self.image.exec_after_start(cs)
}
}
impl<I: Image> RunnableImage<I> {
pub fn with_tag(self, tag: impl Into<String>) -> Self {
Self {
image_tag: Some(tag.into()),
..self
}
}
pub fn with_container_name(self, name: impl Into<String>) -> Self {
Self {
container_name: Some(name.into()),
..self
}
}
pub fn with_network(self, network: impl Into<String>) -> Self {
Self {
network: Some(network.into()),
..self
}
}
pub fn with_env_var(self, (key, value): (impl Into<String>, impl Into<String>)) -> Self {
let mut env_vars = self.env_vars;
env_vars.insert(key.into(), value.into());
Self { env_vars, ..self }
}
pub fn with_volume(self, (orig, dest): (impl Into<String>, impl Into<String>)) -> Self {
let mut volumes = self.volumes;
volumes.insert(orig.into(), dest.into());
Self { volumes, ..self }
}
pub fn with_mapped_port<P: Into<Port>>(self, port: P) -> Self {
let mut ports = self.ports.unwrap_or_default();
ports.push(port.into());
Self {
ports: Some(ports),
..self
}
}
}
impl<I> From<I> for RunnableImage<I>
where
I: Image,
I::Args: Default,
{
fn from(image: I) -> Self {
Self::from((image, I::Args::default()))
}
}
impl<I: Image> From<(I, I::Args)> for RunnableImage<I> {
fn from((image, image_args): (I, I::Args)) -> Self {
Self {
image,
image_args,
image_tag: None,
container_name: None,
network: None,
env_vars: BTreeMap::default(),
volumes: BTreeMap::default(),
ports: None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Port {
pub local: u16,
pub internal: u16,
}
#[derive(Debug, PartialEq, Clone)]
pub enum WaitFor {
Nothing,
StdOutMessage { message: String },
StdErrMessage { message: String },
Duration { length: Duration },
Healthcheck,
}
impl WaitFor {
pub fn message_on_stdout<S: Into<String>>(message: S) -> WaitFor {
WaitFor::StdOutMessage {
message: message.into(),
}
}
pub fn message_on_stderr<S: Into<String>>(message: S) -> WaitFor {
WaitFor::StdErrMessage {
message: message.into(),
}
}
pub fn seconds(length: u64) -> WaitFor {
WaitFor::Duration {
length: Duration::from_secs(length),
}
}
pub fn millis(length: u64) -> WaitFor {
WaitFor::Duration {
length: Duration::from_millis(length),
}
}
pub fn millis_in_env_var(name: &'static str) -> WaitFor {
let additional_sleep_period = var(name).map(|value| value.parse());
(|| {
let length = additional_sleep_period.ok()?.ok()?;
Some(WaitFor::Duration {
length: Duration::from_millis(length),
})
})()
.unwrap_or(WaitFor::Nothing)
}
}
impl From<(u16, u16)> for Port {
fn from((local, internal): (u16, u16)) -> Self {
Port { local, internal }
}
}