Skip to content

Commit f06a51f

Browse files
tevoineachkeita
authored andcommitted
Tevoinea/add version checking in local tasks (microsoft#3517)
* Compare task version to service version * Swallow output when looking for appropriate name
1 parent 32a6531 commit f06a51f

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed
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/main.rs

+12-2
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
}

0 commit comments

Comments
 (0)