Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6269,6 +6269,7 @@ version = "0.0.0"
dependencies = [
"guid",
"jiff",
"tracing",
]

[[package]]
Expand Down
3 changes: 0 additions & 3 deletions flowey/flowey_lib_hvlite/src/run_prep_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl SimpleFlowNode for Node {
let vms = powershell_builder::PowerShellBuilder::new()
.cmdlet("Get-VM")
.finish()
.build()
.output()?;
log::info!(
"removing any existing VMs: {}",
Expand All @@ -60,7 +59,6 @@ impl SimpleFlowNode for Node {
.cmdlet("Stop-VM")
.flag("TurnOff")
.finish()
.build()
.output()?;

powershell_builder::PowerShellBuilder::new()
Expand All @@ -69,7 +67,6 @@ impl SimpleFlowNode for Node {
.cmdlet("Remove-VM")
.flag("Force")
.finish()
.build()
.output()?;
}

Expand Down
36 changes: 33 additions & 3 deletions petri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,44 @@ pub enum CommandError {
/// Run a command on the host and return the output
pub async fn run_host_cmd(mut cmd: Command) -> Result<String, CommandError> {
cmd.stderr(Stdio::piped()).stdin(Stdio::null());

let cmd_debug = format!("{cmd:?}");
::tracing::debug!(cmd = cmd_debug, "executing command");

let start = Timestamp::now();
let output = blocking::unblock(move || cmd.output()).await?;
let time_elapsed = Timestamp::now() - start;
log_and_check(&cmd_debug, start, output)
}

/// Run a PowerShell command on the host and return the output. Transient
/// Windows PowerShell 5.1 startup crashes are retried automatically by
/// [`powershell_builder::PowerShellBuilder::output_with`].
#[cfg(windows)]
pub async fn run_host_ps(
builder: powershell_builder::PowerShellBuilder,
) -> Result<String, CommandError> {
let start = Timestamp::now();
// The `Command` is owned by the builder, so capture its debug
// representation from inside the configure closure.
let (cmd_debug, output) = blocking::unblock(move || {
let mut cmd_debug = String::new();
let output = builder.output_with(|cmd| {
cmd.stderr(Stdio::piped()).stdin(Stdio::null());
cmd_debug = format!("{cmd:?}");
::tracing::debug!(cmd = cmd_debug, "executing command");
})?;
std::io::Result::Ok((cmd_debug, output))
})
.await?;
log_and_check(&cmd_debug, start, output)
}

/// Log the result of a host command run and map its exit status to
/// [`CommandError::Command`] on failure.
fn log_and_check(
cmd_debug: &str,
start: Timestamp,
output: std::process::Output,
) -> Result<String, CommandError> {
let time_elapsed = Timestamp::now() - start;
let stdout_str = String::from_utf8_lossy(&output.stdout).to_string();
let stderr_str = String::from_utf8_lossy(&output.stderr).to_string();
::tracing::debug!(
Expand Down
Loading
Loading