Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit d45c1f3

Browse files
authored
Merge branch 'main' into container-retention-policy
2 parents 386aabb + e3c4a40 commit d45c1f3

File tree

6 files changed

+103
-18
lines changed

6 files changed

+103
-18
lines changed

src/ApiService/ApiService/Functions/Jobs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private async Task<HttpResponseData> Post(HttpRequestData req, FunctionContext c
8383
"job");
8484
}
8585

86-
await _context.Events.SendEvent(new EventJobCreated(job.JobId, job.Config, job.UserInfo));
86+
await _context.Events.SendEvent(new EventJobCreated(job.JobId, job.Config, job.UserInfo, _context.ServiceConfiguration.OneFuzzVersion));
8787
return await RequestHandling.Ok(req, JobResponse.ForJob(job, taskInfo: null));
8888
}
8989

src/ApiService/ApiService/OneFuzzTypes/Events.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ TaskConfig Config
124124
public record EventJobCreated(
125125
Guid JobId,
126126
JobConfig Config,
127-
StoredUserInfo? UserInfo
127+
StoredUserInfo? UserInfo,
128+
string OneFuzzVersion
128129
) : BaseEvent();
129130

130131

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use std::process::Stdio;
2+
3+
use anyhow::Result;
4+
use serde_json::Value;
5+
6+
pub fn run(onefuzz_built_version: &str) -> Result<()> {
7+
// Find onefuzz cli
8+
let common_names = ["onefuzz", "onefuzz.exe", "onefuzz.cmd"];
9+
let mut valid_commands: Vec<_> = common_names
10+
.into_iter()
11+
.map(|name| {
12+
(
13+
name,
14+
std::process::Command::new(name)
15+
.stderr(Stdio::null())
16+
.stdout(Stdio::null())
17+
.arg("-h")
18+
.spawn(),
19+
)
20+
})
21+
.filter_map(|(name, child)| child.ok().map(|c| (name, c)))
22+
.collect();
23+
24+
if valid_commands.is_empty() {
25+
bail!(
26+
"Could not find any of the following common names for the onefuzz-cli: {:?}",
27+
common_names
28+
);
29+
}
30+
31+
let (name, child) = valid_commands
32+
.first_mut()
33+
.expect("Expected valid_commands to not be empty");
34+
35+
info!("Found the onefuzz cli at: {}", name);
36+
37+
// We just used this to check if it exists, we'll invoke it again later
38+
let _ = child.kill();
39+
40+
// Run onefuzz info get
41+
let output = std::process::Command::new(&name)
42+
.args(["info", "get"])
43+
.output()?;
44+
45+
if !output.status.success() {
46+
bail!(
47+
"Failed to run command `{} info get`. stderr: {:?}, stdout: {:?}",
48+
name,
49+
String::from_utf8(output.stderr),
50+
String::from_utf8(output.stdout)
51+
)
52+
}
53+
54+
let stdout = String::from_utf8(output.stdout)?;
55+
let info: Value = serde_json::from_str(&stdout)?;
56+
57+
if let Some(onefuzz_service_version) = info["versions"]["onefuzz"]["version"].as_str() {
58+
if onefuzz_service_version == onefuzz_built_version {
59+
println!("You are up to date!");
60+
} else {
61+
println!(
62+
"Version mismatch. onefuzz-task version: {} | onefuzz service version: {}",
63+
onefuzz_built_version, onefuzz_service_version
64+
);
65+
println!(
66+
"To update, please run the following command: {} tools get .",
67+
name
68+
);
69+
println!("Then extract the onefuzz-task binary from the appropriate OS folder");
70+
}
71+
return Ok(());
72+
}
73+
74+
bail!(
75+
"Failed to get onefuzz service version from cli response: {}",
76+
stdout
77+
)
78+
}

src/agent/onefuzz-task/src/local/template.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@ impl RunContext {
136136
name: impl AsRef<str>,
137137
path: impl AsRef<Path>,
138138
) -> Result<SyncedDir> {
139-
if !path.as_ref().exists() {
140-
std::fs::create_dir_all(&path)?;
141-
}
142-
143139
self.to_sync_dir(name, path)?
144140
.monitor_count(&self.event_sender)
145141
}
146142

147143
pub fn to_sync_dir(&self, name: impl AsRef<str>, path: impl AsRef<Path>) -> Result<SyncedDir> {
148144
let path = path.as_ref();
145+
if !path.exists() {
146+
std::fs::create_dir_all(path)?;
147+
}
148+
149149
let name = name.as_ref();
150150
let current_dir = std::env::current_dir()?;
151151
if self.create_job_dir {

src/agent/onefuzz-task/src/main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,38 @@ extern crate onefuzz;
1111

1212
use anyhow::Result;
1313
use clap::{ArgMatches, Command};
14+
1415
use std::io::{stdout, Write};
1516

17+
mod check_for_update;
1618
mod local;
1719
mod managed;
1820
mod tasks;
1921

2022
const LICENSE_CMD: &str = "licenses";
2123
const LOCAL_CMD: &str = "local";
2224
const MANAGED_CMD: &str = "managed";
25+
const CHECK_FOR_UPDATE: &str = "check_for_update";
26+
27+
const ONEFUZZ_BUILT_VERSION: &str = env!("ONEFUZZ_VERSION");
2328

2429
fn main() -> Result<()> {
2530
let built_version = format!(
2631
"{} onefuzz:{} git:{}",
2732
crate_version!(),
28-
env!("ONEFUZZ_VERSION"),
33+
ONEFUZZ_BUILT_VERSION,
2934
env!("GIT_VERSION")
3035
);
3136

3237
let app = Command::new("onefuzz-task")
3338
.version(built_version)
3439
.subcommand(managed::cmd::args(MANAGED_CMD))
3540
.subcommand(local::cmd::args(LOCAL_CMD))
36-
.subcommand(Command::new(LICENSE_CMD).about("display third-party licenses"));
41+
.subcommand(Command::new(LICENSE_CMD).about("display third-party licenses"))
42+
.subcommand(
43+
Command::new(CHECK_FOR_UPDATE)
44+
.about("compares the version of onefuzz-task with the onefuzz service"),
45+
);
3746

3847
let matches = app.get_matches();
3948

@@ -55,6 +64,7 @@ async fn run(args: ArgMatches) -> Result<()> {
5564
Some((LICENSE_CMD, _)) => licenses(),
5665
Some((LOCAL_CMD, sub)) => local::cmd::run(sub.to_owned()).await,
5766
Some((MANAGED_CMD, sub)) => managed::cmd::run(sub).await,
67+
Some((CHECK_FOR_UPDATE, _)) => check_for_update::run(ONEFUZZ_BUILT_VERSION),
5868
_ => anyhow::bail!("No command provided. Run with 'help' to see available commands."),
5969
}
6070
}

src/agent/onefuzz-task/tests/template_integration.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
path::{Path, PathBuf},
55
};
66

7+
use path_absolutize::Absolutize;
78
use tokio::fs;
89

910
use anyhow::Result;
@@ -131,25 +132,20 @@ async fn create_test_directory(config: &Path, target_exe: &Path) -> Result<TestL
131132
test_directory = test_directory.canonicalize()?;
132133

133134
let mut inputs_directory = PathBuf::from(&test_directory).join("inputs");
134-
fs::create_dir(&inputs_directory).await?;
135-
inputs_directory = inputs_directory.canonicalize()?;
135+
inputs_directory = inputs_directory.absolutize()?.into();
136136

137137
let mut crashes_directory = PathBuf::from(&test_directory).join("crashes");
138-
fs::create_dir(&crashes_directory).await?;
139-
crashes_directory = crashes_directory.canonicalize()?;
138+
crashes_directory = crashes_directory.absolutize()?.into();
140139

141140
let mut crashdumps_directory = PathBuf::from(&test_directory).join("crashdumps");
142-
fs::create_dir(&crashdumps_directory).await?;
143-
crashdumps_directory = crashdumps_directory.canonicalize()?;
141+
crashdumps_directory = crashdumps_directory.absolutize()?.into();
144142

145143
let mut coverage_directory = PathBuf::from(&test_directory).join("coverage");
146-
fs::create_dir(&coverage_directory).await?;
147-
coverage_directory = coverage_directory.canonicalize()?;
144+
coverage_directory = coverage_directory.absolutize()?.into();
148145

149146
let mut regression_reports_directory =
150147
PathBuf::from(&test_directory).join("regression_reports");
151-
fs::create_dir(&regression_reports_directory).await?;
152-
regression_reports_directory = regression_reports_directory.canonicalize()?;
148+
regression_reports_directory = regression_reports_directory.absolutize()?.into();
153149

154150
let mut target_in_test = PathBuf::from(&test_directory).join("fuzz.exe");
155151
fs::copy(target_exe, &target_in_test).await?;

0 commit comments

Comments
 (0)