use super::{Provider, READ_LIMIT, RECONCILE_LIMIT, Result, ToolCx, Vagrant, Verb, WarningCode, call, dot, tool_error, vboxmanage};
impl Vagrant {
pub(super) async fn preamble(&self, cx: &ToolCx<'_>, provider: Provider) -> Result<()> {
self.reconcile_provider(cx, provider).await?;
self.discard_aborted_saved(cx, provider).await
}
pub(super) async fn reconcile_provider(&self, cx: &ToolCx<'_>, provider: Provider) -> Result<()> {
let dot = dot(cx);
if !dot.recorded()?.iter().any(|r| r.provider != provider.id()) {
return Ok(());
}
(cx.warn)(WarningCode::OtherProvider, "this run last ran on another provider: clearing that run first");
if let Err(e) = self.act(cx, Verb::Destroy, &[], RECONCILE_LIMIT).await {
(cx.warn)(WarningCode::CleanupFailed, &format!("the run on the other provider couldn't be destroyed: {e}"));
}
dot.reset()
}
pub(super) async fn discard_aborted_saved(&self, cx: &ToolCx<'_>, provider: Provider) -> Result<()> {
if provider != Provider::Virtualbox {
return Ok(());
}
let call = call(cx, READ_LIMIT);
for r in dot(cx).recorded()?.into_iter().filter(|r| r.provider == provider.id()) {
match vboxmanage::vm_state(&call, &r.id).await {
Ok(vboxmanage::VmState::AbortedSaved) => {
(cx.warn)(
WarningCode::SavedStateDiscarded,
&format!("{}: its saved state was cut off; it boots fresh", r.machine),
);
vboxmanage::discard_state(&call, &r.id).await.map_err(|e| tool_error("discard-saved", e))?;
}
Ok(vboxmanage::VmState::Other(_)) => {}
Err(e) => tracing::debug!(vm = %r.machine, error = %e, "the VM's state couldn't be read: its saved state, if any, is left as it is"),
}
}
Ok(())
}
}