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

capture azcopy log file #945

Merged
merged 5 commits into from
Jun 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/agent/onefuzz/src/az_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@

use anyhow::{Context, Result};
use backoff::{self, future::retry_notify, ExponentialBackoff};
use std::ffi::OsStr;
use std::fmt;
use std::process::Stdio;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use std::{
ffi::OsStr,
fmt,
path::Path,
process::Stdio,
sync::atomic::{AtomicUsize, Ordering},
time::Duration,
};
use tempfile::tempdir;
use tokio::fs;
use tokio::process::Command;

const RETRY_INTERVAL: Duration = Duration::from_secs(5);
Expand All @@ -29,15 +34,35 @@ impl fmt::Display for Mode {
}
}

// NOTE, this is intended to read a single file in a tempdir managed by the
// caller, rather than the default AZCOPY log location.
async fn read_azcopy_log_file(path: &Path) -> Result<String> {
bmc-msft marked this conversation as resolved.
Show resolved Hide resolved
let mut entries = fs::read_dir(path).await?;
// there should be only up to one file in azcopy_log dir
if let Some(file) = entries.next_entry().await? {
fs::read_to_string(file.path())
.await
.with_context(|| format!("unable to read file: {}", file.path().display()))
} else {
bail!("no log file in path: {}", path.display());
}
}

async fn az_impl(mode: Mode, src: &OsStr, dst: &OsStr, args: &[&str]) -> Result<()> {
let temp_dir = tempdir()?;

// https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-configure#change-the-location-of-log-files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let mut cmd = Command::new("azcopy");
cmd.kill_on_drop(true)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.env("AZCOPY_LOG_LOCATION", temp_dir.path())
.arg(mode.to_string())
.arg(&src)
.arg(&dst)
.arg("--log-level")
.arg("ERROR")
.args(args);

let output = cmd
Expand All @@ -49,13 +74,17 @@ async fn az_impl(mode: Mode, src: &OsStr, dst: &OsStr, args: &[&str]) -> Result<
if !output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let logfile = read_azcopy_log_file(temp_dir.path())
.await
.unwrap_or_else(|e| format!("unable to read azcopy log file from: {:?}", e));
anyhow::bail!(
"azcopy {} failed src:{:?} dst:{:?} stdout:{:?} stderr:{:?}",
"azcopy {} failed src:{:?} dst:{:?} stdout:{:?} stderr:{:?} log:{:?}",
mode,
src,
dst,
stdout,
stderr
stderr,
logfile,
);
}

Expand Down