cleanup local job_id directory upon clean exit (#738)

This commit is contained in:
bmc-msft
2021-03-27 16:39:09 -04:00
committed by GitHub
parent 19d19a1e0e
commit e8ce384bd9
7 changed files with 55 additions and 9 deletions

17
src/agent/Cargo.lock generated
View File

@ -1702,6 +1702,7 @@ dependencies = [
"anyhow", "anyhow",
"appinsights", "appinsights",
"async-trait", "async-trait",
"atexit",
"backoff", "backoff",
"clap", "clap",
"env_logger", "env_logger",
@ -1713,6 +1714,7 @@ dependencies = [
"onefuzz", "onefuzz",
"onefuzz-telemetry", "onefuzz-telemetry",
"path-absolutize", "path-absolutize",
"remove_dir_all 0.7.0",
"reqwest", "reqwest",
"reqwest-retry", "reqwest-retry",
"serde", "serde",
@ -2236,6 +2238,19 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "remove_dir_all"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "882f368737489ea543bc5c340e6f3d34a28c39980bd9a979e47322b26f60ac40"
dependencies = [
"libc",
"log",
"num_cpus",
"rayon",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.10.10" version = "0.10.10"
@ -2723,7 +2738,7 @@ dependencies = [
"libc", "libc",
"rand 0.8.3", "rand 0.8.3",
"redox_syscall", "redox_syscall",
"remove_dir_all", "remove_dir_all 0.5.3",
"winapi 0.3.9", "winapi 0.3.9",
] ]

View File

@ -25,6 +25,13 @@ pub fn exit_process(code: i32) -> ! {
ATEXIT.exit_process(code) ATEXIT.exit_process(code)
} }
/// Runs the registered functions but does *not* terminate the process
///
/// This function is not called automatically (e.g. via `drop`).
pub fn execute() {
ATEXIT.execute()
}
impl AtExit { impl AtExit {
fn new() -> Arc<Self> { fn new() -> Arc<Self> {
let result = Arc::new(AtExit { let result = Arc::new(AtExit {
@ -56,9 +63,13 @@ impl AtExit {
} }
fn exit_process(&self, code: i32) -> ! { fn exit_process(&self, code: i32) -> ! {
self.execute();
std::process::exit(code);
}
fn execute(&self) {
for function in self.functions.write().unwrap().iter_mut() { for function in self.functions.write().unwrap().iter_mut() {
function(); function();
} }
std::process::exit(code);
} }
} }

View File

@ -35,6 +35,8 @@ reqwest-retry = { path = "../reqwest-retry" }
onefuzz-telemetry = { path = "../onefuzz-telemetry" } onefuzz-telemetry = { path = "../onefuzz-telemetry" }
stacktrace-parser = { path = "../stacktrace-parser" } stacktrace-parser = { path = "../stacktrace-parser" }
path-absolutize = "3.0.6" path-absolutize = "3.0.6"
atexit = { path = "../atexit" }
remove_dir_all = "0.7"
[dev-dependencies] [dev-dependencies]
tempfile = "3.2" tempfile = "3.2"

View File

@ -1,10 +1,14 @@
use crate::tasks::config::CommonConfig; use crate::tasks::config::CommonConfig;
use crate::tasks::utils::parse_key_value; use crate::tasks::utils::parse_key_value;
use anyhow::Result; use anyhow::Result;
use backoff::{future::retry, Error as BackoffError, ExponentialBackoff};
use clap::{App, Arg, ArgMatches}; use clap::{App, Arg, ArgMatches};
use onefuzz::jitter::delay_with_jitter; use onefuzz::jitter::delay_with_jitter;
use onefuzz::{blob::BlobContainerUrl, monitor::DirectoryMonitor, syncdir::SyncedDir}; use onefuzz::{blob::BlobContainerUrl, monitor::DirectoryMonitor, syncdir::SyncedDir};
use path_absolutize::Absolutize;
use remove_dir_all::remove_dir_all;
use reqwest::Url; use reqwest::Url;
use std::task::Poll;
use std::{ use std::{
collections::HashMap, collections::HashMap,
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -12,10 +16,6 @@ use std::{
}; };
use uuid::Uuid; use uuid::Uuid;
use backoff::{future::retry, Error as BackoffError, ExponentialBackoff};
use path_absolutize::Absolutize;
use std::task::Poll;
pub const SETUP_DIR: &str = "setup_dir"; pub const SETUP_DIR: &str = "setup_dir";
pub const INPUTS_DIR: &str = "inputs_dir"; pub const INPUTS_DIR: &str = "inputs_dir";
pub const CRASHES_DIR: &str = "crashes_dir"; pub const CRASHES_DIR: &str = "crashes_dir";
@ -128,6 +128,12 @@ pub fn add_common_config(app: App<'static, 'static>) -> App<'static, 'static> {
.takes_value(true) .takes_value(true)
.required(false), .required(false),
) )
.arg(
Arg::with_name("keep_job_dir")
.long("keep_job_dir")
.required(false)
.help("keep the local directory created for running the task"),
)
} }
fn get_uuid(name: &str, args: &ArgMatches<'_>) -> Result<Uuid> { fn get_uuid(name: &str, args: &ArgMatches<'_>) -> Result<Uuid> {
@ -162,6 +168,14 @@ pub fn get_synced_dirs(
Ok(dirs?) Ok(dirs?)
} }
fn register_cleanup(job_id: Uuid) -> Result<()> {
let path = std::env::current_dir()?.join(job_id.to_string());
atexit::register(move || {
remove_dir_all(&path).expect("cleanup failed");
});
Ok(())
}
pub fn get_synced_dir( pub fn get_synced_dir(
name: &str, name: &str,
job_id: Uuid, job_id: Uuid,
@ -204,6 +218,10 @@ pub fn build_common_config(args: &ArgMatches<'_>, generate_task_id: bool) -> Res
PathBuf::default() PathBuf::default()
}; };
if !args.is_present("keep_job_dir") {
register_cleanup(job_id)?;
}
let config = CommonConfig { let config = CommonConfig {
job_id, job_id,
task_id, task_id,

View File

@ -20,7 +20,6 @@ use crate::{
}; };
use anyhow::Result; use anyhow::Result;
use clap::{App, SubCommand}; use clap::{App, SubCommand};
use onefuzz::utils::try_wait_all_join_handles; use onefuzz::utils::try_wait_all_join_handles;
use std::collections::HashSet; use std::collections::HashSet;
use tokio::task::spawn; use tokio::task::spawn;

View File

@ -57,7 +57,6 @@ pub fn build_coverage_config(
pub async fn run(args: &clap::ArgMatches<'_>) -> Result<()> { pub async fn run(args: &clap::ArgMatches<'_>) -> Result<()> {
let common = build_common_config(args, true)?; let common = build_common_config(args, true)?;
let config = build_coverage_config(args, false, None, common)?; let config = build_coverage_config(args, false, None, common)?;
let mut task = CoverageTask::new(config); let mut task = CoverageTask::new(config);
task.managed_run().await task.managed_run().await
} }

View File

@ -40,7 +40,9 @@ fn main() -> Result<()> {
let matches = app.get_matches(); let matches = app.get_matches();
let mut rt = tokio::runtime::Runtime::new()?; let mut rt = tokio::runtime::Runtime::new()?;
rt.block_on(run(matches)) let result = rt.block_on(run(matches));
atexit::execute();
result
} }
async fn run(args: ArgMatches<'_>) -> Result<()> { async fn run(args: ArgMatches<'_>) -> Result<()> {