-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdocker.rs
More file actions
121 lines (103 loc) · 3.22 KB
/
docker.rs
File metadata and controls
121 lines (103 loc) · 3.22 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
use std::{
path::{Path, PathBuf},
process::Stdio,
};
use serde::{Deserialize, Serialize};
use tokio::{
io::{AsyncBufReadExt, BufReader},
process::Command,
task,
};
#[derive(Debug, Serialize, Deserialize)]
pub enum ContainerSource {
#[serde(rename = "image")]
Image(String),
#[serde(rename = "build")]
Build { name: String, path: String },
}
impl ContainerSource {
pub async fn resolve(&self, platform: Option<String>) -> anyhow::Result<()> {
match self {
Self::Image(image) => pull_image(image, platform).await,
Self::Build { name, path } => build_image(name, path).await,
}
}
pub fn image(&self) -> &str {
match self {
Self::Image(image) => image,
Self::Build { name, .. } => name,
}
}
}
async fn run_command(command: &str, args: &[&str]) -> anyhow::Result<()> {
let mut child = Command::new(command)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let stdout = child.stdout.take().expect("failed to capture stdout");
let stderr = child.stderr.take().expect("failed to capture stderr");
let mut stdout_reader = BufReader::new(stdout).lines();
let mut stderr_reader = BufReader::new(stderr).lines();
let stdout_task = task::spawn(async move {
while let Some(line) = stdout_reader.next_line().await.unwrap_or(None) {
log::info!("{}", line);
}
});
let stderr_task = task::spawn(async move {
while let Some(line) = stderr_reader.next_line().await.unwrap_or(None) {
// docker logs to stderr ... -.-
log::info!("{}", line);
}
});
let status = child.wait().await?;
stdout_task.await?;
stderr_task.await?;
if status.success() {
Ok(())
} else {
Err(anyhow::anyhow!("command failed with status: {:?}", status))
}
}
pub(crate) async fn pull_image(image: &str, platform: Option<String>) -> anyhow::Result<()> {
let result = run_command(
"sh",
&[
"-c",
&format!(
"docker images -q '{image}' | grep -q . || docker pull {}'{image}'",
if let Some(platform) = platform {
format!("--platform '{}' ", platform)
} else {
"".to_string()
}
),
],
)
.await;
if let Err(e) = result {
log::error!("Docker pull encountered an error: {}", e);
}
Ok(())
}
pub(crate) async fn build_image(name: &str, path: &str) -> anyhow::Result<()> {
let dockerfile = PathBuf::from(path);
if !dockerfile.exists() {
return Err(anyhow::anyhow!("dockerfile '{}' does not exist", path));
} else if !dockerfile.is_file() {
return Err(anyhow::anyhow!("path '{}' is not a dockerfile", path));
}
log::info!("building image '{}' from '{}'", name, dockerfile.display());
run_command(
"sh",
&[
"-c",
&format!(
"docker build -f '{}' -t '{name}' --quiet '{}'",
dockerfile.display(),
dockerfile.parent().unwrap_or(Path::new(".")).display(),
),
],
)
.await
}