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

Commit 81de102

Browse files
authored
Added machine_id to config_path and failure path of the agent (#2731)
1 parent 7227856 commit 81de102

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
lines changed

src/agent/onefuzz-agent/src/config.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ pub struct DynamicConfig {
209209
}
210210

211211
impl DynamicConfig {
212-
pub async fn save(&self) -> Result<()> {
213-
let path = Self::save_path()?;
212+
pub async fn save(&self, machine_id: Uuid) -> Result<()> {
213+
let path = Self::save_path(machine_id)?;
214214
let dir = path
215215
.parent()
216216
.ok_or(anyhow!("invalid dynamic config path"))?;
@@ -223,8 +223,8 @@ impl DynamicConfig {
223223
Ok(())
224224
}
225225

226-
pub async fn load() -> Result<Self> {
227-
let path = Self::save_path()?;
226+
pub async fn load(machine_id: Uuid) -> Result<Self> {
227+
let path = Self::save_path(machine_id)?;
228228
let data = fs::read(&path)
229229
.await
230230
.with_context(|| format!("unable to load dynamic config: {}", path.display()))?;
@@ -233,10 +233,10 @@ impl DynamicConfig {
233233
Ok(ctx)
234234
}
235235

236-
fn save_path() -> Result<PathBuf> {
236+
fn save_path(machine_id: Uuid) -> Result<PathBuf> {
237237
Ok(onefuzz::fs::onefuzz_root()?
238238
.join("etc")
239-
.join("dynamic-config.json"))
239+
.join(format!("dynamic-config-{}.json", machine_id)))
240240
}
241241
}
242242

@@ -294,7 +294,7 @@ impl Registration {
294294
match response.error_for_status_with_body().await {
295295
Ok(response) => {
296296
let dynamic_config: DynamicConfig = response.json().await?;
297-
dynamic_config.save().await?;
297+
dynamic_config.save(machine_id).await?;
298298
return Ok(Self {
299299
config,
300300
dynamic_config,
@@ -317,8 +317,8 @@ impl Registration {
317317
}
318318

319319
pub async fn load_existing(config: StaticConfig) -> Result<Self> {
320-
let dynamic_config = DynamicConfig::load().await?;
321320
let machine_id = config.machine_identity.machine_id;
321+
let dynamic_config = DynamicConfig::load(machine_id).await?;
322322
let registration = Self {
323323
config,
324324
dynamic_config,
@@ -355,7 +355,7 @@ impl Registration {
355355
.context("Registration.renew request body")?;
356356

357357
let dynamic_config: DynamicConfig = response.json().await?;
358-
dynamic_config.save().await?;
358+
dynamic_config.save(self.machine_id).await?;
359359

360360
Ok(Self {
361361
dynamic_config,

src/agent/onefuzz-agent/src/failure.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ use std::{
44
fs,
55
path::{Path, PathBuf},
66
};
7+
use uuid::Uuid;
78

8-
const FAILURE_FILE: &str = "onefuzz-agent-failure.txt";
9-
10-
pub fn failure_path() -> Result<PathBuf> {
11-
Ok(onefuzz_root()?.join(FAILURE_FILE))
9+
pub fn failure_path(machine_id: Uuid) -> Result<PathBuf> {
10+
Ok(onefuzz_root()?.join(format!("onefuzz-agent-failure-{}.txt", machine_id)))
1211
}
1312

14-
pub fn save_failure(err: &Error) -> Result<()> {
13+
pub fn save_failure(err: &Error, machine_id: Uuid) -> Result<()> {
1514
error!("saving failure: {:?}", err);
16-
let path = failure_path()?;
15+
let path = failure_path(machine_id)?;
1716
let message = format!("{:?}", err);
1817
fs::write(&path, message)
1918
.with_context(|| format!("unable to write failure log: {}", path.display()))
2019
}
2120

22-
pub fn read_failure() -> Result<String> {
23-
let path = failure_path()?;
21+
pub fn read_failure(machine_id: Uuid) -> Result<String> {
22+
let path = failure_path(machine_id)?;
2423
read_file_lossy(&path)
2524
}
2625

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ fn redirect(opt: RunOpt) -> Result<()> {
143143
cmd.arg("--config").arg(path);
144144
}
145145

146+
if let Some(machine_id) = opt.machine_id {
147+
cmd.arg("--machine_id").arg(machine_id.to_string());
148+
}
149+
150+
if let Some(machine_name) = opt.machine_name {
151+
cmd.arg("--machine_name").arg(machine_name);
152+
}
153+
154+
if opt.reset_node_lock {
155+
cmd.arg("--reset_lock");
156+
}
157+
146158
let exit_status: ExitStatus = cmd
147159
.spawn()
148160
.context("unable to start child onefuzz-agent")?
@@ -181,13 +193,14 @@ fn run(opt: RunOpt) -> Result<()> {
181193
}
182194

183195
let config = config?;
196+
let machine_id = config.machine_identity.machine_id;
184197

185198
if reset_lock {
186-
done::remove_done_lock(config.machine_identity.machine_id)?;
187-
} else if done::is_agent_done(config.machine_identity.machine_id)? {
199+
done::remove_done_lock(machine_id)?;
200+
} else if done::is_agent_done(machine_id)? {
188201
debug!(
189202
"agent is done, remove lock ({}) to continue",
190-
done::done_path(config.machine_identity.machine_id)?.display()
203+
done::done_path(machine_id)?.display()
191204
);
192205
return Ok(());
193206
}
@@ -196,7 +209,7 @@ fn run(opt: RunOpt) -> Result<()> {
196209

197210
if let Err(err) = &result {
198211
error!("error running supervisor agent: {:?}", err);
199-
if let Err(err) = failure::save_failure(err) {
212+
if let Err(err) = failure::save_failure(err, machine_id) {
200213
error!("unable to save failure log: {:?}", err);
201214
}
202215
}
@@ -234,7 +247,7 @@ async fn check_existing_worksets(coordinator: &mut coordinator::Coordinator) ->
234247

235248
if let Some(work) = WorkSet::load_from_fs_context(coordinator.get_machine_id()).await? {
236249
warn!("onefuzz-agent unexpectedly identified an existing workset on start");
237-
let failure = match failure::read_failure() {
250+
let failure = match failure::read_failure(coordinator.get_machine_id()) {
238251
Ok(value) => format!("onefuzz-agent failed: {}", value),
239252
Err(failure_err) => {
240253
warn!("unable to read failure: {:?}", failure_err);
@@ -306,7 +319,7 @@ async fn run_agent(config: StaticConfig, reset_node: bool) -> Result<()> {
306319
let mut coordinator = coordinator::Coordinator::new(registration.clone()).await?;
307320
debug!("initialized coordinator");
308321

309-
let reboot = reboot::Reboot;
322+
let reboot = reboot::Reboot::new(config.machine_identity.machine_id);
310323
let reboot_context = reboot.load_context().await?;
311324
if reset_node {
312325
WorkSet::remove_context(config.machine_identity.machine_id).await?;

src/agent/onefuzz-agent/src/panic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{panic, sync::Once};
44

55
fn panic_hook(info: &panic::PanicInfo) {
66
let err = anyhow!("supervisor panicked: {}\n{:?}", info, Backtrace::new());
7-
if let Err(err) = save_failure(&err) {
7+
if let Err(err) = save_failure(&err, uuid::Uuid::nil()) {
88
error!("unable to write panic log: {:?}", err);
99
}
1010
}

src/agent/onefuzz-agent/src/reboot.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::process::Command;
77
use anyhow::{Context, Result};
88
use downcast_rs::Downcast;
99
use tokio::fs;
10+
use uuid::Uuid;
1011

1112
use crate::work::*;
1213

@@ -36,11 +37,17 @@ impl IReboot for Reboot {
3637
}
3738
}
3839

39-
pub struct Reboot;
40+
pub struct Reboot {
41+
machine_id: Uuid,
42+
}
4043

4144
impl Reboot {
45+
pub fn new(machine_id: Uuid) -> Self {
46+
Self { machine_id }
47+
}
48+
4249
pub async fn save_context(&self, ctx: RebootContext) -> Result<()> {
43-
let path = reboot_context_path()?;
50+
let path = reboot_context_path(self.machine_id)?;
4451

4552
info!("saving reboot context to: {}", path.display());
4653

@@ -56,7 +63,7 @@ impl Reboot {
5663

5764
pub async fn load_context(&self) -> Result<Option<RebootContext>> {
5865
use std::io::ErrorKind;
59-
let path = reboot_context_path()?;
66+
let path = reboot_context_path(self.machine_id)?;
6067

6168
info!("checking for saved reboot context: {}", path.display());
6269

@@ -127,8 +134,8 @@ impl RebootContext {
127134
}
128135
}
129136

130-
fn reboot_context_path() -> Result<PathBuf> {
131-
Ok(onefuzz::fs::onefuzz_root()?.join("reboot_context.json"))
137+
fn reboot_context_path(machine_id: Uuid) -> Result<PathBuf> {
138+
Ok(onefuzz::fs::onefuzz_root()?.join(format!("reboot_context_{}.json", machine_id)))
132139
}
133140

134141
#[cfg(test)]

0 commit comments

Comments
 (0)