-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathengine.rs
268 lines (238 loc) · 7.96 KB
/
engine.rs
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
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::config::bool_from_envvar;
use crate::extensions::CommandExt;
use crate::shell::MessageInfo;
use crate::{errors::*, OutputExt};
use super::{Architecture, ContainerOs};
pub const DOCKER: &str = "docker";
pub const PODMAN: &str = "podman";
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum EngineType {
Docker,
Podman,
PodmanRemote,
Nerdctl,
Other,
}
impl EngineType {
/// Returns `true` if the engine type is [`Podman`](Self::Podman) or [`PodmanRemote`](Self::PodmanRemote).
#[must_use]
pub const fn is_podman(&self) -> bool {
matches!(self, Self::Podman | Self::PodmanRemote)
}
/// Returns `true` if the engine type is [`Docker`](EngineType::Docker).
#[must_use]
pub const fn is_docker(&self) -> bool {
matches!(self, Self::Docker)
}
/// Returns `true` if the build command supports the `--output` flag.
#[must_use]
pub const fn supports_output_flag(&self) -> bool {
!matches!(self, Self::Other)
}
/// Returns `true` if the build command supports the `--pull` flag.
#[must_use]
pub const fn supports_pull_flag(&self) -> bool {
!matches!(self, Self::Nerdctl | Self::Other)
}
/// Returns `true` if the build command supports the `--cache-from type=` key.
///
/// Some container engines, especially podman, do not support the `type`
/// key of `--cache-from` during the image build steps. They also do
/// not support any tags for the `--cache-from` steps either. See:
/// https://docs.podman.io/en/latest/markdown/podman-build.1.html#cache-from
#[must_use]
pub const fn supports_cache_from_type(&self) -> bool {
matches!(self, Self::Docker | Self::Nerdctl)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Engine {
pub kind: EngineType,
pub path: PathBuf,
pub in_docker: bool,
pub arch: Option<Architecture>,
pub os: Option<ContainerOs>,
pub is_remote: bool,
}
impl Engine {
pub const CROSS_CONTAINER_ENGINE_NO_BUILDKIT_ENV: &'static str =
"CROSS_CONTAINER_ENGINE_NO_BUILDKIT";
pub fn new(
in_docker: Option<bool>,
is_remote: Option<bool>,
msg_info: &mut MessageInfo,
) -> Result<Engine> {
#[allow(clippy::map_err_ignore)]
let path = get_container_engine()
.map_err(|_| eyre::eyre!("no container engine found"))
.with_suggestion(|| "is docker or podman installed?")?;
Self::from_path(path, in_docker, is_remote, msg_info)
}
pub fn from_path(
path: PathBuf,
in_docker: Option<bool>,
is_remote: Option<bool>,
msg_info: &mut MessageInfo,
) -> Result<Engine> {
let in_docker = match in_docker {
Some(v) => v,
None => Self::in_docker(msg_info)?,
};
let (kind, arch, os) = get_engine_info(&path, msg_info)?;
let is_remote = is_remote.unwrap_or_else(Self::is_remote);
Ok(Engine {
path,
kind,
in_docker,
arch,
os,
is_remote,
})
}
#[must_use]
pub fn needs_remote(&self) -> bool {
self.is_remote && self.kind == EngineType::Podman
}
pub fn in_docker(msg_info: &mut MessageInfo) -> Result<bool> {
Ok(
if let Ok(value) = env::var("CROSS_CONTAINER_IN_CONTAINER") {
if env::var("CROSS_DOCKER_IN_DOCKER").is_ok() {
msg_info.warn(
"using both `CROSS_CONTAINER_IN_CONTAINER` and `CROSS_DOCKER_IN_DOCKER`.",
)?;
}
bool_from_envvar(&value)
} else if let Ok(value) = env::var("CROSS_DOCKER_IN_DOCKER") {
// FIXME: remove this when we deprecate CROSS_DOCKER_IN_DOCKER.
bool_from_envvar(&value)
} else {
false
},
)
}
#[must_use]
pub fn is_remote() -> bool {
env::var("CROSS_REMOTE")
.map(|s| bool_from_envvar(&s))
.unwrap_or_default()
}
#[must_use]
pub fn has_buildkit() -> bool {
!env::var(Self::CROSS_CONTAINER_ENGINE_NO_BUILDKIT_ENV)
.map(|x| bool_from_envvar(&x))
.unwrap_or_default()
}
}
// determine if the container engine is docker. this fixes issues with
// any aliases (#530), and doesn't fail if an executable suffix exists.
fn get_engine_info(
ce: &Path,
msg_info: &mut MessageInfo,
) -> Result<(EngineType, Option<Architecture>, Option<ContainerOs>)> {
let stdout_help = Command::new(ce)
.arg("--help")
.run_and_get_stdout(msg_info)?
.to_lowercase();
let kind = if stdout_help.contains("podman-remote") {
EngineType::PodmanRemote
} else if stdout_help.contains("podman") {
EngineType::Podman
} else if stdout_help.contains("nerdctl") {
EngineType::Nerdctl
} else if stdout_help.contains("docker") && !stdout_help.contains("emulate") {
EngineType::Docker
} else {
EngineType::Other
};
// this can fail: podman can give partial output
// linux,,,Error: template: version:1:15: executing "version" at <.Arch>:
// can't evaluate field Arch in type *define.Version
let os_arch_server = engine_info(
ce,
&["version", "-f", "{{ .Server.Os }},,,{{ .Server.Arch }}"],
",,,",
msg_info,
);
let (os_arch_other, os_arch_server_result) = match os_arch_server {
Ok(Some(os_arch)) => (Ok(Some(os_arch)), None),
result => {
if kind.is_podman() {
(get_podman_info(ce, msg_info), result.err())
} else {
(get_custom_info(ce, msg_info), result.err())
}
}
};
let os_arch = match (os_arch_other, os_arch_server_result) {
(Ok(os_arch), _) => os_arch,
(Err(e), Some(server_err)) => return Err(server_err.to_section_report().with_error(|| e)),
(Err(e), None) => return Err(e.to_section_report()),
};
let (os, arch) = os_arch.map_or(<_>::default(), |(os, arch)| (Some(os), Some(arch)));
Ok((kind, arch, os))
}
#[derive(Debug, thiserror::Error)]
pub enum EngineInfoError {
#[error(transparent)]
Eyre(eyre::Report),
#[error("could not get os and arch")]
CommandError(#[from] CommandError),
}
impl EngineInfoError {
pub fn to_section_report(self) -> eyre::Report {
match self {
EngineInfoError::Eyre(e) => e,
EngineInfoError::CommandError(e) => {
e.to_section_report().wrap_err("could not get os and arch")
}
}
}
}
/// Get engine info
fn engine_info(
ce: &Path,
args: &[&str],
sep: &str,
msg_info: &mut MessageInfo,
) -> Result<Option<(ContainerOs, Architecture)>, EngineInfoError> {
let mut cmd = Command::new(ce);
cmd.args(args);
let out = cmd
.run_and_get_output(msg_info)
.map_err(EngineInfoError::Eyre)?;
cmd.status_result(msg_info, out.status, Some(&out))?;
out.stdout()?
.to_lowercase()
.trim()
.split_once(sep)
.map(|(os, arch)| -> Result<_> { Ok((ContainerOs::new(os)?, Architecture::new(arch)?)) })
.transpose()
.map_err(EngineInfoError::Eyre)
}
fn get_podman_info(
ce: &Path,
msg_info: &mut MessageInfo,
) -> Result<Option<(ContainerOs, Architecture)>, EngineInfoError> {
engine_info(ce, &["info", "-f", "{{ .Version.OsArch }}"], "/", msg_info)
}
fn get_custom_info(
ce: &Path,
msg_info: &mut MessageInfo,
) -> Result<Option<(ContainerOs, Architecture)>, EngineInfoError> {
engine_info(
ce,
&["version", "-f", "{{ .Client.Os }},,,{{ .Client.Arch }}"],
",,,",
msg_info,
)
}
pub fn get_container_engine() -> Result<PathBuf, which::Error> {
if let Ok(ce) = env::var("CROSS_CONTAINER_ENGINE") {
which::which(ce)
} else {
which::which(DOCKER).or_else(|_| which::which(PODMAN))
}
}