forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.rs
More file actions
234 lines (206 loc) · 6.75 KB
/
docker.rs
File metadata and controls
234 lines (206 loc) · 6.75 KB
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
//! Docker command wrapper.
use std::io;
use std::process::{Command, Output};
use std::thread::sleep;
use std::time::{Duration, Instant};
/// Docker command wrapper.
pub struct Docker {}
#[derive(Clone, Debug)]
pub struct RunningContainer {
pub image: String,
pub name: String,
pub output: Output,
}
impl Drop for RunningContainer {
/// Ensures that the temporary container is stopped when the struct goes out
/// of scope.
fn drop(&mut self) {
tracing::info!("Dropping running container: {}", self.name);
if Docker::is_container_running(&self.name) {
let _unused = Docker::stop(self);
}
}
}
/// `docker run` command options.
pub struct RunOptions {
pub env_vars: Vec<(String, String)>,
pub ports: Vec<String>,
}
impl Docker {
/// Builds a Docker image from a given Dockerfile.
///
/// # Errors
///
/// Will fail if the docker build command fails.
pub fn build(dockerfile: &str, tag: &str) -> io::Result<()> {
let status = Command::new("docker")
.args(["build", "-f", dockerfile, "-t", tag, "."])
.status()?;
if status.success() {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Failed to build Docker image from dockerfile {dockerfile}"),
))
}
}
/// Runs a Docker container from a given image with multiple environment variables.
///
/// # Arguments
///
/// * `image` - The Docker image to run.
/// * `container` - The name for the Docker container.
/// * `env_vars` - A slice of tuples, each representing an environment variable as ("KEY", "value").
///
/// # Errors
///
/// Will fail if the docker run command fails.
pub fn run(image: &str, container: &str, options: &RunOptions) -> io::Result<RunningContainer> {
let initial_args = vec![
"run".to_string(),
"--detach".to_string(),
"--name".to_string(),
container.to_string(),
];
// Add environment variables
let mut env_var_args: Vec<String> = vec![];
for (key, value) in &options.env_vars {
env_var_args.push("--env".to_string());
env_var_args.push(format!("{key}={value}"));
}
// Add port mappings
let mut port_args: Vec<String> = vec![];
for port in &options.ports {
port_args.push("--publish".to_string());
port_args.push(port.to_string());
}
let args = [initial_args, env_var_args, port_args, [image.to_string()].to_vec()].concat();
tracing::debug!("Docker run args: {:?}", args);
let output = Command::new("docker").args(args).output()?;
if output.status.success() {
Ok(RunningContainer {
image: image.to_owned(),
name: container.to_owned(),
output,
})
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Failed to run Docker image {image}"),
))
}
}
/// Stops a Docker container.
///
/// # Errors
///
/// Will fail if the docker stop command fails.
pub fn stop(container: &RunningContainer) -> io::Result<()> {
let status = Command::new("docker").args(["stop", &container.name]).status()?;
if status.success() {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Failed to stop Docker container {}", container.name),
))
}
}
/// Removes a Docker container.
///
/// # Errors
///
/// Will fail if the docker rm command fails.
pub fn remove(container: &str) -> io::Result<()> {
let status = Command::new("docker").args(["rm", "-f", container]).status()?;
if status.success() {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Failed to remove Docker container {container}"),
))
}
}
/// Fetches logs from a Docker container.
///
/// # Errors
///
/// Will fail if the docker logs command fails.
pub fn logs(container: &str) -> io::Result<String> {
let output = Command::new("docker").args(["logs", container]).output()?;
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).to_string())
} else {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Failed to fetch logs from Docker container {container}"),
))
}
}
/// Checks if a Docker container is healthy.
#[must_use]
pub fn wait_until_is_healthy(name: &str, timeout: Duration) -> bool {
let start = Instant::now();
while start.elapsed() < timeout {
let Ok(output) = Command::new("docker")
.args(["ps", "-f", &format!("name={name}"), "--format", "{{.Status}}"])
.output()
else {
return false;
};
let output_str = String::from_utf8_lossy(&output.stdout);
tracing::info!("Waiting until container is healthy: {:?}", output_str);
if output_str.contains("(healthy)") {
return true;
}
sleep(Duration::from_secs(1));
}
false
}
/// Checks if a Docker container is running.
///
/// # Arguments
///
/// * `container` - The name of the Docker container.
///
/// # Returns
///
/// `true` if the container is running, `false` otherwise.
#[must_use]
pub fn is_container_running(container: &str) -> bool {
match Command::new("docker")
.args(["ps", "-f", &format!("name={container}"), "--format", "{{.Names}}"])
.output()
{
Ok(output) => {
let output_str = String::from_utf8_lossy(&output.stdout);
output_str.contains(container)
}
Err(_) => false,
}
}
/// Checks if a Docker container exists.
///
/// # Arguments
///
/// * `container` - The name of the Docker container.
///
/// # Returns
///
/// `true` if the container exists, `false` otherwise.
#[must_use]
pub fn container_exist(container: &str) -> bool {
match Command::new("docker")
.args(["ps", "-a", "-f", &format!("name={container}"), "--format", "{{.Names}}"])
.output()
{
Ok(output) => {
let output_str = String::from_utf8_lossy(&output.stdout);
output_str.contains(container)
}
Err(_) => false,
}
}
}