|
| 1 | +use std::io; |
| 2 | +use std::process::{Child, Command, Output, Stdio}; |
| 3 | +use std::thread::sleep; |
| 4 | +use std::time::{Duration, Instant}; |
| 5 | + |
| 6 | +pub struct Docker {} |
| 7 | + |
| 8 | +impl Docker { |
| 9 | + /// Builds a Docker image from a given Dockerfile. |
| 10 | + /// |
| 11 | + /// # Errors |
| 12 | + /// |
| 13 | + /// Will fail if the docker build command fails. |
| 14 | + pub fn build(dockerfile: &str, tag: &str) -> io::Result<()> { |
| 15 | + let status = Command::new("docker") |
| 16 | + .args(["build", "-f", dockerfile, "-t", tag, "."]) |
| 17 | + .status()?; |
| 18 | + |
| 19 | + if status.success() { |
| 20 | + Ok(()) |
| 21 | + } else { |
| 22 | + Err(io::Error::new(io::ErrorKind::Other, "Failed to build Docker image")) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /// Runs a Docker container from a given image. |
| 27 | + /// |
| 28 | + /// # Errors |
| 29 | + /// |
| 30 | + /// Will fail if the docker run command fails. |
| 31 | + pub fn run(image: &str, name: &str) -> io::Result<Output> { |
| 32 | + let output = Command::new("docker") |
| 33 | + .args(["run", "--detach", "--name", name, image]) |
| 34 | + .output()?; |
| 35 | + |
| 36 | + if output.status.success() { |
| 37 | + Ok(output) |
| 38 | + } else { |
| 39 | + Err(io::Error::new(io::ErrorKind::Other, "Failed to run Docker container")) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Runs a Docker container from a given image in the background. |
| 44 | + /// |
| 45 | + /// # Errors |
| 46 | + /// |
| 47 | + /// Will fail if the docker run command fails to start. |
| 48 | + pub fn run_spawned(image: &str, name: &str) -> io::Result<Child> { |
| 49 | + let child = Command::new("docker") |
| 50 | + .args(["run", "--name", name, image]) |
| 51 | + .stdin(Stdio::null()) // Ignore stdin |
| 52 | + .stdout(Stdio::null()) // Ignore stdout |
| 53 | + .stderr(Stdio::null()) // Ignore stderr |
| 54 | + .spawn()?; |
| 55 | + |
| 56 | + Ok(child) |
| 57 | + } |
| 58 | + |
| 59 | + /// Stops a Docker container. |
| 60 | + /// |
| 61 | + /// # Errors |
| 62 | + /// |
| 63 | + /// Will fail if the docker stop command fails. |
| 64 | + pub fn stop(name: &str) -> io::Result<()> { |
| 65 | + let status = Command::new("docker").args(["stop", name]).status()?; |
| 66 | + |
| 67 | + if status.success() { |
| 68 | + Ok(()) |
| 69 | + } else { |
| 70 | + Err(io::Error::new(io::ErrorKind::Other, "Failed to stop Docker container")) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// Removes a Docker container. |
| 75 | + /// |
| 76 | + /// # Errors |
| 77 | + /// |
| 78 | + /// Will fail if the docker rm command fails. |
| 79 | + pub fn remove(name: &str) -> io::Result<()> { |
| 80 | + let status = Command::new("docker").args(["rm", "-f", name]).status()?; |
| 81 | + |
| 82 | + if status.success() { |
| 83 | + Ok(()) |
| 84 | + } else { |
| 85 | + Err(io::Error::new(io::ErrorKind::Other, "Failed to remove Docker container")) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Fetches logs from a Docker container. |
| 90 | + /// |
| 91 | + /// # Errors |
| 92 | + /// |
| 93 | + /// Will fail if the docker logs command fails. |
| 94 | + pub fn logs(container_name: &str) -> io::Result<String> { |
| 95 | + let output = Command::new("docker").args(["logs", container_name]).output()?; |
| 96 | + |
| 97 | + if output.status.success() { |
| 98 | + Ok(String::from_utf8_lossy(&output.stdout).to_string()) |
| 99 | + } else { |
| 100 | + Err(io::Error::new( |
| 101 | + io::ErrorKind::Other, |
| 102 | + "Failed to fetch logs from Docker container", |
| 103 | + )) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// Checks if a Docker container is healthy. |
| 108 | + #[must_use] |
| 109 | + pub fn wait_until_is_healthy(name: &str, timeout: Duration) -> bool { |
| 110 | + let start = Instant::now(); |
| 111 | + |
| 112 | + while start.elapsed() < timeout { |
| 113 | + let Ok(output) = Command::new("docker") |
| 114 | + .args(["ps", "-f", &format!("name={name}"), "--format", "{{.Status}}"]) |
| 115 | + .output() |
| 116 | + else { |
| 117 | + return false; |
| 118 | + }; |
| 119 | + |
| 120 | + let output_str = String::from_utf8_lossy(&output.stdout); |
| 121 | + |
| 122 | + if output_str.contains("(healthy)") { |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + sleep(Duration::from_secs(1)); |
| 127 | + } |
| 128 | + |
| 129 | + false |
| 130 | + } |
| 131 | +} |
0 commit comments