Skip to content

Commit ca742f3

Browse files
committed
feat: implement deploy and run
1 parent 8738d5d commit ca742f3

17 files changed

Lines changed: 593 additions & 114 deletions

File tree

Cargo.lock

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2024"
66
[dependencies]
77
aho-corasick = "1.1.3"
88
async-tempfile = "0.7.0"
9+
change-case = "0.2.0"
910
clap = { version = "4.5.45", features = ["derive"] }
1011
docker_credential = "1.3.2"
1112
futures = "0.3.31"

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ Platform-specific builds:
174174
```yaml
175175
build:
176176
multi-arch:
177-
type: bazel
178177
platforms:
179178
linux/amd64: //platforms:linux_amd64
180179
linux/arm64: //platforms:linux_arm64

src/builder/bazel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ impl Builder for BazelBuilder {
8383

8484
async fn build(
8585
self,
86-
mut progress: prodash::tree::Item,
8786
Context {
8887
service_name,
8988
platform,
90-
input,
91-
}: Context<Self::Input>,
89+
mut progress,
90+
}: Context,
91+
input: Self::Input,
9292
) -> Result<Output, Self::Error> {
9393
progress.set_name(&service_name);
9494
progress.message(MessageLevel::Info, "starting builder");

src/builder/docker.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tokio::process::Command;
88
use crate::{
99
builder::{Builder, Context, Output},
1010
config::Docker,
11-
exec::{self, ExitError},
11+
exec::{self, CmdBuilder, ExitError},
1212
image,
1313
};
1414

@@ -103,12 +103,12 @@ impl Builder for DockerBuilder {
103103

104104
async fn build(
105105
self,
106-
mut progress: prodash::tree::Item,
107106
Context {
108107
service_name,
109108
platform,
110-
input,
111-
}: Context<Self::Input>,
109+
mut progress,
110+
}: Context,
111+
input: Self::Input,
112112
) -> Result<Output, Self::Error> {
113113
progress.set_name(&service_name);
114114
progress.message(MessageLevel::Info, "starting builder");
@@ -136,26 +136,23 @@ impl Builder for DockerBuilder {
136136
progress.message(MessageLevel::Info, "using existing buildkit builder");
137137
}
138138

139-
let mut custom_args = vec![];
139+
let mut cmd = CmdBuilder::new(&self.binary);
140+
cmd.arg("buildx").arg("build");
141+
140142
let build_args = fmt_map(input.build_args, '=');
141143
let hosts = fmt_map(input.hosts, ':');
142144

143145
for entry in build_args.iter() {
144-
custom_args.push("--build-arg");
145-
custom_args.push(entry);
146+
cmd.flag("--build-arg", entry);
146147
}
147148

148149
for entry in hosts.iter() {
149-
custom_args.push("--add-host");
150-
custom_args.push(entry);
150+
cmd.flag("--add-host", entry);
151151
}
152152

153153
let dest = TempDir::new_with_name(&service_name).await?;
154154
let status = exec::run_with_progress(
155-
Command::new(&self.binary)
156-
.arg("build")
157-
.args(custom_args)
158-
.arg("--builder")
155+
cmd.arg("--builder")
159156
.arg("steiger")
160157
.arg("--platform")
161158
.arg(&platform)

src/builder/ko.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::{path::PathBuf, process::ExitStatus};
33
use async_tempfile::TempDir;
44
use gix::progress::MessageLevel;
55
use miette::Diagnostic;
6-
use prodash::tree::Item;
76
use tokio::process::Command;
87

98
use crate::{
@@ -47,12 +46,12 @@ impl Builder for KoBuilder {
4746

4847
async fn build(
4948
self,
50-
mut progress: Item,
5149
Context {
5250
service_name,
5351
platform,
54-
input,
55-
}: Context<Self::Input>,
52+
mut progress,
53+
}: Context,
54+
input: Self::Input,
5655
) -> Result<Output, Self::Error> {
5756
progress.set_name(&service_name);
5857
progress.message(MessageLevel::Info, "starting builder");

src/builder/mod.rs

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use futures::TryFutureExt;
21
use miette::Diagnostic;
32
use prodash::{messages::MessageLevel, tree::Item};
43
use tokio::{task::JoinSet, time::Instant};
@@ -43,26 +42,18 @@ impl Output {
4342
}
4443
}
4544

46-
pub struct Context<T> {
45+
pub struct Context {
4746
pub service_name: String,
4847
pub platform: String,
49-
pub input: T,
48+
pub progress: Item,
5049
}
5150

52-
impl<T> Context<T> {
53-
pub fn new(service_name: String, platform: String, input: T) -> Self {
51+
impl Context {
52+
pub fn new(service_name: String, platform: String, progress: Item) -> Self {
5453
Self {
5554
service_name,
5655
platform,
57-
input,
58-
}
59-
}
60-
61-
pub fn with<I>(self, input: I) -> Context<I> {
62-
Context {
63-
input,
64-
platform: self.platform,
65-
service_name: self.service_name,
56+
progress,
6657
}
6758
}
6859
}
@@ -74,22 +65,18 @@ pub trait Builder: Clone {
7465
fn try_init() -> Result<Self, Self::Error>
7566
where
7667
Self: Sized;
77-
async fn build(
78-
self,
79-
progress: Item,
80-
input: Context<Self::Input>,
81-
) -> Result<Output, Self::Error>;
68+
async fn build(self, ctx: Context, input: Self::Input) -> Result<Output, Self::Error>;
8269
}
8370

8471
type ErrorOf<T> = <T as Builder>::Error;
8572

86-
use std::{collections::HashMap, sync::Arc};
73+
use std::collections::HashMap;
8774

8875
fn run_builder<B>(
8976
var: &mut Option<B>,
90-
progress: Item,
91-
ctx: Context<B::Input>,
92-
) -> Result<impl Future<Output = Result<Output, <B as Builder>::Error>> + use<B>, BuildError>
77+
ctx: Context,
78+
input: B::Input,
79+
) -> Result<impl Future<Output = Result<Output, BuildError>> + use<B>, BuildError>
9380
where
9481
B: Builder,
9582
BuildError: From<<B as Builder>::Error>,
@@ -103,19 +90,19 @@ where
10390
}
10491
}?;
10592

106-
Ok(builder.build(progress, ctx))
93+
Ok(async { Ok(builder.build(ctx, input).await?) })
10794
}
10895

10996
pub struct MetaBuild {
110-
config: Arc<Config>,
97+
config: Config,
11198
ko: Option<KoBuilder>,
11299
bazel: Option<BazelBuilder>,
113100
docker: Option<DockerBuilder>,
114101
nix: Option<NixBuilder>,
115102
}
116103

117104
impl MetaBuild {
118-
pub fn new(config: Arc<Config>) -> Self {
105+
pub fn new(config: Config) -> Self {
119106
Self {
120107
config,
121108
ko: None,
@@ -127,40 +114,27 @@ impl MetaBuild {
127114

128115
pub async fn build(mut self, mut pb: Item, platform: &str) -> Result<Output, BuildError> {
129116
let instant = Instant::now();
130-
let config = Arc::clone(&self.config);
131117
let mut set = JoinSet::default();
132118

133-
pb.init(Some(config.build.len()), None);
119+
pb.init(Some(self.config.build.len()), None);
134120
pb.message(MessageLevel::Info, format!("detected platform: {platform}"));
135121

136-
for (name, build) in config.build.iter() {
137-
let progress = pb.add_child(name);
138-
let ctx = Context::new(name.clone(), platform.to_string(), ());
122+
for (name, build) in self.config.build {
123+
let progress = pb.add_child(&name);
124+
let ctx = Context::new(name, platform.to_string(), progress);
139125

140-
match &build {
126+
match build {
141127
Build::Ko(ko) => {
142-
set.spawn(
143-
run_builder(&mut self.ko, progress, ctx.with(ko.clone()))?
144-
.map_err(BuildError::Ko),
145-
);
128+
set.spawn(run_builder(&mut self.ko, ctx, ko)?);
146129
}
147130
Build::Bazel(bazel) => {
148-
set.spawn(
149-
run_builder(&mut self.bazel, progress, ctx.with(bazel.clone()))?
150-
.map_err(BuildError::Bazel),
151-
);
131+
set.spawn(run_builder(&mut self.bazel, ctx, bazel)?);
152132
}
153133
Build::Docker(docker) => {
154-
set.spawn(
155-
run_builder(&mut self.docker, progress, ctx.with(docker.clone()))?
156-
.map_err(BuildError::Docker),
157-
);
134+
set.spawn(run_builder(&mut self.docker, ctx, docker)?);
158135
}
159136
Build::Nix(nix) => {
160-
set.spawn(
161-
run_builder(&mut self.nix, progress, ctx.with(nix.clone()))?
162-
.map_err(BuildError::Nix),
163-
);
137+
set.spawn(run_builder(&mut self.nix, ctx, nix)?);
164138
}
165139
};
166140
}

src/builder/nix.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ pub struct EvalResult {
161161

162162
impl EvalResult {
163163
async fn build(
164-
self,
164+
mut self,
165165
nix_binary: Arc<PathBuf>,
166166
mut progress: Item,
167167
) -> Result<OutPaths, NixError> {
168-
if let Some(error) = self.error {
169-
progress.message(MessageLevel::Failure, error.clone());
168+
if let Some(error) = self.error.take() {
169+
progress.message(MessageLevel::Failure, &error);
170170
return Err(NixError::Eval(error));
171171
}
172172

@@ -262,7 +262,7 @@ impl NixBuilder {
262262

263263
if packages.values().any(|v| v == &attr_path) {
264264
progress.init(Some(set.len() + 1), None);
265-
let binary = self.nix_binary.clone();
265+
let binary = Arc::clone(&self.nix_binary);
266266
let progress = progress.add_child(format!("{attr_path} › nix"));
267267
set.spawn(drv.build(binary, progress));
268268
}
@@ -303,12 +303,12 @@ impl Builder for NixBuilder {
303303

304304
async fn build(
305305
self,
306-
mut progress: Item,
307306
Context {
308307
service_name,
309308
platform,
310-
input,
311-
}: Context<Self::Input>,
309+
mut progress,
310+
}: Context,
311+
input: Self::Input,
312312
) -> Result<Output, Self::Error> {
313313
progress.set_name(&service_name);
314314
progress.info("starting builder".to_string());

0 commit comments

Comments
 (0)