-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.rs
More file actions
294 lines (252 loc) · 8.99 KB
/
build.rs
File metadata and controls
294 lines (252 loc) · 8.99 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
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
use std::{
fmt::{Debug, Display},
path::PathBuf,
str::FromStr,
};
use clap::{Args, ValueHint, value_parser};
use semver::Version;
use snafu::{ResultExt, Snafu, ensure};
use strum::EnumDiscriminants;
use url::Host;
use crate::core::{
docker::BuildArgument,
image::ImageSelector,
platform::{Architecture, TargetPlatform},
};
#[derive(Debug, Args)]
pub struct BuildArguments {
/// The image(s) which should be build. The format is name[=version,...].
#[arg(help_heading = "Image Options", required = true)]
pub images: Vec<ImageSelector>,
// NOTE (@Techassi): Should this maybe be renamed to vendor_version?
/// The image version being built.
#[arg(
short, long,
value_parser = BuildArguments::parse_image_version,
default_value_t = Self::default_image_version(),
help_heading = "Image Options"
)]
pub image_version: Version,
/// Target platform of the image.
#[arg(
short, long,
short_alias = 'a', alias = "architecture",
default_value_t = Self::default_architecture(),
help_heading = "Image Options"
)]
pub target_platform: TargetPlatform,
/// Image registry used in image manifests, URIs, and tags.
/// The format is host[:port].
#[arg(
short, long,
default_value_t = Self::default_registry(),
value_hint = ValueHint::Hostname,
help_heading = "Registry Options"
)]
pub registry: HostPort,
/// The namespace within the given registry.
#[arg(
short = 'n',
long = "registry-namespace",
alias = "organization",
default_value = "sdp",
help_heading = "Registry Options"
)]
pub registry_namespace: String,
/// Use 'localhost' as the registry instead of <REGISTRY> to avoid any accidental interactions
/// with remote registries.
///
/// This is especially useful in CI, which can re-tag the image before pushing it.
#[arg(long, help_heading = "Registry Options")]
pub use_localhost_registry: bool,
/// Override the target containerfile used, points to <IMAGE>/<TARGET_CONTAINERFILE>.
#[arg(
long,
default_value_os_t = Self::default_target_containerfile(),
value_hint = ValueHint::FilePath,
help_heading = "Build Options"
)]
pub target_containerfile: PathBuf,
/// Override build arguments, in key=value format. The key is case insensitive. This argument
/// can be supplied multiple times.
#[arg(
long = "build-argument",
alias = "build-arg",
value_name = "BUILD_ARGUMENT",
help_heading = "Build Options"
)]
pub build_arguments: Vec<BuildArgument>,
/// Load and override build arguments, in key=value format, each separated by a newline from the
/// specified file.
#[arg(long, alias = "build-args-file", help_heading = "Build Options")]
pub build_arguments_file: Option<PathBuf>,
/// Write target image tags to <EXPORT_FILE>. Useful for signing or other follow-up CI steps.
#[arg(
long,
alias = "export-tags-file",
help_heading = "Build Options",
value_name = "FILE",
value_hint = ValueHint::FilePath,
value_parser = value_parser!(PathBuf),
default_missing_value = "boil-target-tags",
num_args(0..=1)
)]
pub write_image_manifest_uris: Option<PathBuf>,
/// Strips the architecture from the image (index) manifest tag.
#[arg(long, help_heading = "Build Options")]
pub strip_architecture: bool,
/// Loads the image into the local image store.
///
/// DEPRECATED: Use -- --load instead.
#[arg(long, help_heading = "Build Options")]
#[deprecated(since = "0.1.7", note = "Use -- --load instead")]
pub load: bool,
/// Dry run. This does not build the image(s) but instead prints out the bakefile.
#[arg(short, long, alias = "dry")]
pub dry_run: bool,
/// Arguments passed after '--' which are directly passed to the Docker command.
///
/// Care needs to be taken, because these arguments can override/modify the behaviour defined
/// via the generated Bakefile. A few save arguments include but are not limited to: --load,
/// --no-cache, --progress.
#[arg(raw = true)]
pub rest: Vec<String>,
}
impl BuildArguments {
fn parse_image_version(input: &str) -> Result<Version, ParseImageVersionError> {
let version = Version::from_str(input).context(ParseVersionSnafu)?;
ensure!(version.build.is_empty(), ContainsBuildMetadataSnafu);
Ok(version)
}
fn default_image_version() -> Version {
"0.0.0-dev".parse().expect("must be a valid SemVer")
}
fn default_registry() -> HostPort {
HostPort {
host: Host::Domain(String::from("oci.stackable.tech")),
port: None,
}
}
// TODO: Auto-detect this
fn default_architecture() -> TargetPlatform {
TargetPlatform::Linux(Architecture::Amd64)
}
fn default_target_containerfile() -> PathBuf {
PathBuf::from("Dockerfile")
}
}
#[derive(Debug, Snafu)]
pub enum ParseImageVersionError {
#[snafu(display("failed to parse semantic version"))]
ParseVersion { source: semver::Error },
#[snafu(display("semantic version must not contain build metadata"))]
ContainsBuildMetadata,
}
#[derive(Debug, PartialEq, Snafu, EnumDiscriminants)]
pub enum ParseHostPortError {
#[snafu(display("unexpected empty input"))]
EmptyInput,
#[snafu(display("invalid format, expected host[:port]"))]
InvalidFormat,
#[snafu(display("failed to parse host"))]
InvalidHost { source: url::ParseError },
#[snafu(display("failed to parse port"))]
InvalidPort { source: std::num::ParseIntError },
}
#[derive(Clone, Debug)]
pub struct HostPort {
pub host: Host,
pub port: Option<u16>,
}
impl Display for HostPort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.port {
Some(port) => write!(f, "{host}:{port}", host = self.host),
None => Display::fmt(&self.host, f),
}
}
}
impl FromStr for HostPort {
type Err = ParseHostPortError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
ensure!(!input.is_empty(), EmptyInputSnafu);
let parts: Vec<_> = input.split(':').collect();
match parts[..] {
[host] => {
let host = Host::parse(host).context(InvalidHostSnafu)?;
Ok(Self { host, port: None })
}
[host, port] => {
let host = Host::parse(host).context(InvalidHostSnafu)?;
let port = u16::from_str(port).context(InvalidPortSnafu)?;
Ok(Self {
host,
port: Some(port),
})
}
_ => InvalidFormatSnafu.fail(),
}
}
}
impl HostPort {
pub fn localhost() -> Self {
HostPort {
host: Host::Domain(String::from("localhost")),
port: None,
}
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use strum::IntoDiscriminant;
use url::ParseError;
use super::*;
enum Either<L, R> {
Left(L),
Right(R),
}
impl<L, R> Either<L, R>
where
L: PartialEq,
R: PartialEq,
{
fn is_either(&self, left: &L, right: &R) -> bool {
match self {
Either::Left(l) => l.eq(left),
Either::Right(r) => r.eq(right),
}
}
}
#[rstest]
#[case("registry.example.org:65535")]
#[case("registry.example.org:8080")]
#[case("registry.example.org")]
#[case("example.org:8080")]
#[case("localhost:8080")]
#[case("example.org")]
#[case("localhost")]
fn valid_host_port(#[case] input: &str) {
let host_port = HostPort::from_str(input).expect("must parse");
assert_eq!(host_port.to_string(), input);
}
#[rustfmt::skip]
#[rstest]
// We use the discriminants here, because ParseIntErrors cannot be constructed outside of std.
// As such, it is impossible to fully qualify the error we expect in cases where port parsing
// fails.
#[case("localhost:65536", Either::Right(ParseHostPortErrorDiscriminants::InvalidPort))]
#[case("localhost:", Either::Right(ParseHostPortErrorDiscriminants::InvalidPort))]
// Other errors can be fully qualified.
#[case("with space:", Either::Left(ParseHostPortError::InvalidHost { source: ParseError::IdnaError }))]
#[case("with space", Either::Left(ParseHostPortError::InvalidHost { source: ParseError::IdnaError }))]
#[case(":", Either::Left(ParseHostPortError::InvalidHost { source: ParseError::EmptyHost }))]
#[case("", Either::Left(ParseHostPortError::EmptyInput))]
fn invalid_host_port(
#[case] input: &str,
#[case] expected_error: Either<ParseHostPortError, ParseHostPortErrorDiscriminants>,
) {
let error = HostPort::from_str(input).expect_err("must not parse");
assert!(expected_error.is_either(&error, &error.discriminant()));
}
}