Skip to content

Commit bf69825

Browse files
committed
bubble async up to avoid nested tokio runtime panics
Signed-off-by: Robert Detjens <github@detjens.dev>
1 parent a22b9e2 commit bf69825

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

src/builder/docker.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub struct ContainerInfo {
2828
id: String,
2929
}
3030

31-
#[tokio::main(flavor = "current_thread")] // make this a sync function
3231
pub async fn build_image(context: &Path, options: &BuildObject, tag: &str) -> Result<String> {
3332
trace!("building image in directory {context:?} to tag {tag:?}");
3433
let client = docker().await?;
@@ -84,7 +83,7 @@ pub async fn build_image(context: &Path, options: &BuildObject, tag: &str) -> Re
8483
Ok(tag.to_string())
8584
}
8685

87-
#[tokio::main(flavor = "current_thread")] // make this a sync function
86+
// #[tokio::main(flavor = "current_thread")] // make this a sync function
8887
pub async fn push_image(image_tag: &str, creds: &UserPass) -> Result<String> {
8988
info!("pushing image {image_tag:?} to registry");
9089
let client = docker().await?;

src/builder/mod.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,25 @@ pub enum TagWithSource {
4747
}
4848

4949
/// Build all enabled challenges for the given profile. Returns tags built
50-
pub fn build_challenges(
50+
pub async fn build_challenges(
5151
profile_name: &str,
5252
push: bool,
5353
extract_artifacts: bool,
5454
) -> Result<Vec<(&ChallengeConfig, BuildResult)>> {
55-
enabled_challenges(profile_name)?
56-
.into_iter()
57-
.map(|chal| build_challenge(profile_name, chal, push, extract_artifacts).map(|r| (chal, r)))
58-
.collect::<Result<_>>()
55+
try_join_all(
56+
enabled_challenges(profile_name)?
57+
.into_iter()
58+
.map(|chal| async move {
59+
build_challenge(profile_name, chal, push, extract_artifacts)
60+
.await
61+
.map(|r| (chal, r))
62+
}),
63+
)
64+
.await
5965
}
6066

6167
/// Build all images from given challenge, optionally pushing image or extracting artifacts
62-
fn build_challenge(
68+
async fn build_challenge(
6369
profile_name: &str,
6470
chal: &ChallengeConfig,
6571
push: bool,
@@ -73,27 +79,28 @@ fn build_challenge(
7379
assets: vec![],
7480
};
7581

76-
built.tags = chal
77-
.pods
78-
.iter()
79-
.map(|p| match &p.image_source {
82+
built.tags = try_join_all(chal.pods.iter().map(|p| async {
83+
match &p.image_source {
8084
Image(tag) => Ok(TagWithSource::Upstream(tag.to_string())),
8185
// build any pods that need building
8286
Build(build) => {
8387
let tag = chal.container_tag_for_pod(profile_name, &p.name)?;
8488

85-
let res = docker::build_image(&chal.directory, build, &tag).with_context(|| {
86-
format!(
87-
"error building image {} for chal {}",
88-
p.name,
89-
chal.directory.to_string_lossy()
90-
)
91-
});
89+
let res = docker::build_image(&chal.directory, build, &tag)
90+
.await
91+
.with_context(|| {
92+
format!(
93+
"error building image {} for chal {}",
94+
p.name,
95+
chal.directory.to_string_lossy()
96+
)
97+
});
9298
// map result tag string into enum
9399
res.map(TagWithSource::Built)
94100
}
95-
})
96-
.collect::<Result<_>>()?;
101+
}
102+
}))
103+
.await?;
97104

98105
if push {
99106
// only need to push tags we actually built
@@ -112,13 +119,12 @@ fn build_challenge(
112119
chal.directory
113120
);
114121

115-
tags_to_push
116-
.iter()
117-
.map(|tag| {
118-
docker::push_image(tag, &config.registry.build)
119-
.with_context(|| format!("error pushing image {tag}"))
120-
})
121-
.collect::<Result<Vec<_>>>()?;
122+
try_join_all(tags_to_push.iter().map(|tag| async move {
123+
docker::push_image(tag, &config.registry.build)
124+
.await
125+
.with_context(|| format!("error pushing image {tag}"))
126+
}))
127+
.await?;
122128
}
123129

124130
if extract_artifacts {

src/commands/build.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use std::process::exit;
55
use crate::builder::build_challenges;
66
use crate::configparser::{get_config, get_profile_config};
77

8-
pub fn run(profile_name: &str, push: &bool, extract: &bool) {
8+
#[tokio::main(flavor = "current_thread")] // make this a sync function
9+
pub async fn run(profile_name: &str, push: &bool, extract: &bool) {
910
info!("building images...");
1011

11-
let results = match build_challenges(profile_name, *push, *extract) {
12+
let results = match build_challenges(profile_name, *push, *extract).await {
1213
Ok(results) => results,
1314
Err(e) => {
1415
error!("{e:?}");

src/commands/deploy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub async fn run(profile_name: &str, no_build: &bool, _dry_run: &bool) {
2323
}
2424

2525
info!("building challenges...");
26-
let build_results = match build_challenges(profile_name, true, true) {
26+
let build_results = match build_challenges(profile_name, true, true).await {
2727
Ok(result) => result,
2828
Err(e) => {
2929
error!("{e:?}");

0 commit comments

Comments
 (0)