Skip to content

Commit d0fdb17

Browse files
committed
Store artifact files in the jobs build directory
This will delete them once the job is completed.
1 parent cfbab52 commit d0fdb17

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

src/binaries.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use std::{collections::HashMap, io, path::PathBuf};
1+
use std::{
2+
collections::HashMap,
3+
io,
4+
path::{Path, PathBuf},
5+
};
26

37
use color_eyre::eyre::{Report, Result, WrapErr};
48
use futures_util::TryStreamExt;
59
use open_build_service_api as obs;
6-
use tempfile::NamedTempFile;
710
use tokio::fs::File as AsyncFile;
811
use tokio_util::compat::FuturesAsyncReadCompatExt;
912
use tracing::{info_span, instrument, Instrument};
@@ -13,6 +16,7 @@ use crate::retry::retry_request;
1316
#[instrument(skip(client))]
1417
pub async fn download_binaries(
1518
client: obs::Client,
19+
build_dir: &Path,
1620
project: &str,
1721
package: &str,
1822
repository: &str,
@@ -33,7 +37,11 @@ pub async fn download_binaries(
3337
let binary = binary.clone();
3438
let client = client.clone();
3539
async move {
36-
let (file, path) = NamedTempFile::new()?.keep()?;
40+
let (file, path) = tempfile::Builder::new()
41+
.prefix("obs-glr-")
42+
.suffix(".bin")
43+
.tempfile_in(build_dir)?
44+
.keep()?;
3745

3846
let stream = client
3947
.project(project.to_owned())
@@ -77,6 +85,10 @@ mod tests {
7785

7886
#[tokio::test]
7987
async fn test_build_results() {
88+
let build_dir = tempfile::Builder::new()
89+
.prefix("obs-glr-test-bin-")
90+
.tempdir()
91+
.unwrap();
8092
let test_file = "test.bin";
8193
let test_contents = "123980238";
8294

@@ -113,7 +125,15 @@ mod tests {
113125
let client = create_default_client(&mock);
114126

115127
let mut binaries = assert_ok!(
116-
download_binaries(client, TEST_PROJECT, TEST_PACKAGE_1, TEST_REPO, TEST_ARCH_1).await
128+
download_binaries(
129+
client,
130+
build_dir.path(),
131+
TEST_PROJECT,
132+
TEST_PACKAGE_1,
133+
TEST_REPO,
134+
TEST_ARCH_1
135+
)
136+
.await
117137
);
118138
assert_eq!(binaries.len(), 1);
119139

src/handler.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
fs::File,
55
hash::{Hash, Hasher},
66
io::SeekFrom,
7-
path::PathBuf,
7+
path::{Path, PathBuf},
88
};
99

1010
use async_trait::async_trait;
@@ -19,7 +19,6 @@ use gitlab_runner::{
1919
};
2020
use open_build_service_api as obs;
2121
use serde::{Deserialize, Serialize};
22-
use tempfile::NamedTempFile;
2322
use tokio::{fs::File as AsyncFile, io::AsyncSeekExt};
2423
use tokio_util::{compat::TokioAsyncReadCompatExt, io::ReaderStream};
2524
use tracing::{debug, error, instrument, warn};
@@ -242,9 +241,12 @@ impl UploadableFile for ObsArtifact {
242241

243242
impl ObsBuildInfo {
244243
#[instrument]
245-
fn save(&self) -> Result<PathBuf> {
246-
let mut temp_file =
247-
NamedTempFile::new().wrap_err("Failed to create file for build info")?;
244+
fn save(&self, dir: &Path) -> Result<PathBuf> {
245+
let mut temp_file = tempfile::Builder::new()
246+
.prefix("obs-glr-build-info-")
247+
.suffix(".yml")
248+
.tempfile_in(dir)
249+
.wrap_err("Failed to create file for build info")?;
248250
serde_yaml::to_writer(temp_file.as_file_mut(), self)
249251
.wrap_err("Failed to write build yaml to file")?;
250252
let (_file, path) = temp_file
@@ -373,7 +375,10 @@ impl ObsJobHandler {
373375
debug!("Saving initial build info: {:?}", build_info);
374376

375377
let build_info_2 = build_info.clone();
376-
let build_info_path = tokio::task::spawn_blocking(move || build_info_2.save()).await??;
378+
let build_info_path = {
379+
let build_dir = self.job.build_dir().to_owned();
380+
tokio::task::spawn_blocking(move || build_info_2.save(&build_dir)).await??
381+
};
377382

378383
self.artifacts.replace(ObsArtifact::new_file(
379384
args.build_info_out.clone(),
@@ -454,7 +459,10 @@ impl ObsJobHandler {
454459
};
455460
debug!("Saving complete build info: {:?}", build_info);
456461

457-
let build_info_path = tokio::task::spawn_blocking(move || build_info.save()).await??;
462+
let build_info_path = {
463+
let build_dir = self.job.build_dir().to_owned();
464+
tokio::task::spawn_blocking(move || build_info.save(&build_dir)).await??
465+
};
458466
self.artifacts
459467
.replace(ObsArtifact::new_file(args.build_info_out, build_info_path));
460468

@@ -524,7 +532,7 @@ impl ObsJobHandler {
524532
.await?;
525533
debug!("Completed with: {:?}", completion);
526534

527-
let log_file = monitor.download_build_log().await?;
535+
let log_file = monitor.download_build_log(self.job.build_dir()).await?;
528536
self.artifacts.replace(ObsArtifact::new_file(
529537
args.build_log_out.clone(),
530538
log_file.path.clone(),
@@ -575,6 +583,7 @@ impl ObsJobHandler {
575583
async fn run_download_binaries(&mut self, args: DownloadBinariesAction) -> Result<()> {
576584
let binaries = download_binaries(
577585
self.client.clone(),
586+
self.job.build_dir(),
578587
&args.project,
579588
&args.package,
580589
&args.repository,

src/monitor.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use std::{path::PathBuf, time::Duration};
1+
use std::{
2+
path::{Path, PathBuf},
3+
time::Duration,
4+
};
25

36
use color_eyre::eyre::{ensure, eyre, Context, Report, Result};
47
use derivative::*;
58
use futures_util::stream::StreamExt;
69
use gitlab_runner::outputln;
710
use open_build_service_api as obs;
8-
use tempfile::NamedTempFile;
911
use tokio::{
1012
fs::File as AsyncFile,
1113
io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
@@ -243,11 +245,14 @@ impl ObsMonitor {
243245
}
244246

245247
#[instrument]
246-
pub async fn download_build_log(&self) -> Result<LogFile> {
248+
pub async fn download_build_log(&self, dir: &Path) -> Result<LogFile> {
247249
const LOG_LEN_TO_CHECK_FOR_MD5: u64 = 2500;
248250

249251
let (mut file, path) = retry_request(|| async {
250-
let temp_file = NamedTempFile::new()?;
252+
let temp_file = tempfile::Builder::new()
253+
.prefix("obs-glr-build-log-")
254+
.suffix(".txt")
255+
.tempfile_in(dir)?;
251256
let (file, path) = temp_file.keep()?;
252257
let mut file = AsyncFile::from_std(file);
253258
let mut stream = self
@@ -371,6 +376,10 @@ mod tests {
371376

372377
#[tokio::test]
373378
async fn test_download_log() {
379+
let build_dir = tempfile::Builder::new()
380+
.prefix("obs-glr-test-mon-")
381+
.tempdir()
382+
.unwrap();
374383
let srcmd5 = random_md5();
375384
let log_content = format!(
376385
"srcmd5 '{}'\n
@@ -428,7 +437,7 @@ mod tests {
428437
},
429438
);
430439

431-
let log_file = assert_ok!(monitor.download_build_log().await);
440+
let log_file = assert_ok!(monitor.download_build_log(build_dir.path()).await);
432441
assert_eq!(log_file.len, log_content.len() as u64);
433442

434443
let mut log = "".to_owned();
@@ -450,7 +459,7 @@ mod tests {
450459
true,
451460
);
452461

453-
let err = assert_err!(monitor.download_build_log().await);
462+
let err = assert_err!(monitor.download_build_log(build_dir.path()).await);
454463
assert!(err.to_string().contains("unavailable"));
455464
}
456465

0 commit comments

Comments
 (0)