use std::path::Path;
use crate::config::{GuestOs, Result, VmProfile};
use crate::ssh::VmSession;
use crate::winrm::WinRM;
pub fn capture(profile: &VmProfile, session: &mut VmSession, winrm: Option<&WinRM>, output: &Path) -> Result<()> {
let vm_path = "/tmp/testbed-screenshot.png";
match profile.os {
GuestOs::Linux | GuestOs::MacOS => capture_linux(session, vm_path)?,
GuestOs::Windows => capture_windows(session, winrm, vm_path)?,
}
crate::ssh::download(session, vm_path, output)?;
crate::ssh::exec(session, &format!("rm -f {vm_path}"))?;
Ok(())
}
fn capture_linux(session: &mut VmSession, vm_path: &str) -> Result<()> {
crate::ssh::exec(
session,
&format!("DISPLAY=:99 scrot --overwrite {vm_path}"),
)?;
Ok(())
}
fn capture_windows(session: &mut VmSession, winrm: Option<&WinRM>, _vm_path: &str) -> Result<()> {
if let Some(winrm) = winrm {
let output = Path::new(_vm_path);
crate::bootstrap::windows::screenshot_windows(winrm, session, output)?;
return Ok(());
}
crate::ssh::exec(session, r#"
Add-Type -AssemblyName System.Windows.Forms
$bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$bmp = New-Object System.Drawing.Bitmap $bounds.Width, $bounds.Height
$graphics = [System.Drawing.Graphics]::FromImage($bmp)
$graphics.CopyFromScreen(0, 0, 0, 0, $bmp.Size)
$bmp.Save('/tmp/testbed-screenshot.png', [System.Drawing.Imaging.ImageFormat]::Png)
$graphics.Dispose()
$bmp.Dispose()
"#)?;
Ok(())
}