Skip to main content

isothrum_runner/run/
retire.rs

1//! Taking a machine out of a running run ahead of the update that drops it: what that update
2//! wouldn't remove itself. The detach of an on-demand access machine takes it out the same way.
3
4use isoloom_manifest::{Machine, Native};
5
6use super::Running;
7use super::operation::Done;
8use crate::Result;
9use crate::error::Error;
10use crate::events::{Emitter, Events};
11use crate::executor::Cx;
12use crate::options::RunOptions;
13use crate::state::Op;
14
15impl Running<'_> {
16    /// Takes machine `machine` out of the running run, ahead of its removal from the manifest
17    /// (then an update), or of its move out of the run's start (back to an on-demand machine):
18    /// what the next update wouldn't remove itself. A Vagrant VM is destroyed (Vagrant forgets a
19    /// define the Vagrantfile no longer has); a Compose service is stopped and removed with the
20    /// services in its namespace, on this computer or inside the run's VM (an update leaves a
21    /// service outside the default start running). A machine a Terraform module makes is left to
22    /// the update's apply, which removes what the module no longer makes: the retire runs nothing
23    /// and succeeds.
24    ///
25    /// # Errors
26    ///
27    /// Fails with `machine_unknown` for a machine the run doesn't have; `unsupported` for the VM
28    /// the run's containers run in (`docker-vm`'s guest: every container would go with it), and
29    /// for a machine the run has nothing to take out of (one whose manifest doesn't say how it
30    /// runs, a Compose service with no VM to run in, an on-demand machine on a server or cloud);
31    /// and as the other operations do for a step or the run's state.
32    pub async fn retire(&self, machine: &str, opts: &RunOptions, events: &dyn Events) -> Result<()> {
33        self.operate(Op::Retire, opts, events, async |connected: &RunOptions, e: &Emitter<'_>| {
34            let target = self.machine_named(machine)?;
35            if self.manifest().guest.as_ref().is_some_and(|g| g.machine == target.name) {
36                return Err(Error::Unsupported {
37                    what: format!("retiring `{machine}`, the VM the run's containers run in"),
38                });
39            }
40            let (_, cx) = self.existing(connected, e).await?;
41            self.take_out(&cx, target).await?;
42            Ok(Done {
43                value: (),
44                ok: true,
45                data: serde_json::json!({ "machine": machine }),
46                state: None,
47            })
48        })
49        .await
50    }
51
52    /// Takes `target` out of the running run: a Compose service in the run's VM there
53    /// ([`crate::in_vm::retire`]), anything else through its driver.
54    pub(crate) async fn take_out(&self, cx: &Cx<'_>, target: &Machine) -> Result<()> {
55        match (&self.manifest().guest, &target.native) {
56            (Some(guest), Some(Native::Compose { service })) => crate::in_vm::retire(cx, &self.driver, guest, service.as_str()).await,
57            _ => self.driver.retire(cx, target).await,
58        }
59    }
60}