Skip to main content

isoloom_manifest/
identity.rs

1//! Which output a manifest is, the tools it needs, and how the runner finds what a run created
2//! without reading the generated files: the Compose project, the Vagrant providers, the
3//! Terraform module and state.
4
5use std::collections::BTreeSet;
6
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9
10use crate::argv::HostArgv;
11use crate::list::NonEmpty;
12use crate::names::{GoneText, PinnedImage, SnakeName, ToolName, Version};
13use crate::path::{PathTemplate, RelPath};
14use crate::template::Template;
15
16/// The target whose files the manifest sits next to.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
18#[serde(rename_all = "kebab-case")]
19pub enum Target {
20    /// Docker Compose on the host.
21    Docker,
22    /// Docker Compose in one VM.
23    DockerVm,
24    /// One VM per machine, through Vagrant.
25    Vagrant,
26    /// VMs on a Proxmox server, through Terraform.
27    Proxmox,
28    /// VMs on an ESXi server.
29    Esxi,
30    /// Docker Compose on a cloud VM.
31    CloudDocker,
32    /// One cloud VM per machine.
33    CloudVm,
34    /// A cloud's own services, through the environment's Terraform module.
35    CloudServices,
36}
37
38/// The cloud of a cloud target's module.
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
40#[serde(rename_all = "lowercase")]
41pub enum Cloud {
42    /// Amazon Web Services.
43    Aws,
44    /// Microsoft Azure.
45    Azure,
46    /// Google Cloud.
47    Gcp,
48    /// DigitalOcean.
49    Digitalocean,
50    /// Akamai Linode.
51    Linode,
52    /// Oracle Cloud Infrastructure.
53    Oci,
54}
55
56/// The runner driver that implements the manifest's actions, reads its status and clears its
57/// leftovers. The native commands themselves are in `lifecycle`.
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
59#[serde(rename_all = "lowercase")]
60pub enum Driver {
61    /// Docker Compose.
62    Compose,
63    /// Vagrant.
64    Vagrant,
65    /// Terraform (`terraform` on the PATH; OpenTofu is untested).
66    Terraform,
67}
68
69impl Driver {
70    /// The driver as the manifest writes it.
71    #[must_use]
72    pub fn id(self) -> &'static str {
73        match self {
74            Driver::Compose => "compose",
75            Driver::Vagrant => "vagrant",
76            Driver::Terraform => "terraform",
77        }
78    }
79}
80
81/// A host operating system.
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema)]
83#[serde(rename_all = "lowercase")]
84pub enum HostOs {
85    /// Linux.
86    Linux,
87    /// macOS.
88    Macos,
89    /// Windows.
90    Windows,
91}
92
93/// A Vagrant provider (`vagrant up --provider`).
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema)]
95#[serde(rename_all = "snake_case")]
96pub enum Provider {
97    /// VirtualBox.
98    Virtualbox,
99    /// VMware Workstation or Fusion.
100    VmwareDesktop,
101    /// Hyper-V.
102    Hyperv,
103    /// Parallels Desktop.
104    Parallels,
105    /// libvirt (KVM).
106    Libvirt,
107    /// QEMU.
108    Qemu,
109    /// UTM.
110    Utm,
111    /// An ESXi server.
112    VmwareEsxi,
113}
114
115impl Provider {
116    /// The provider as the manifest writes it (`vmware_desktop`), and `vagrant up --provider`.
117    #[must_use]
118    pub const fn id(self) -> &'static str {
119        match self {
120            Provider::Virtualbox => "virtualbox",
121            Provider::VmwareDesktop => "vmware_desktop",
122            Provider::Hyperv => "hyperv",
123            Provider::Parallels => "parallels",
124            Provider::Libvirt => "libvirt",
125            Provider::Qemu => "qemu",
126            Provider::Utm => "utm",
127            Provider::VmwareEsxi => "vmware_esxi",
128        }
129    }
130
131    /// The Vagrant plugin it needs, `None` when Vagrant has it built in (VirtualBox, Hyper-V):
132    /// what a manifest's provider names as its `plugin`.
133    ///
134    /// ```
135    /// use isoloom_manifest::Provider;
136    ///
137    /// assert_eq!(Provider::Libvirt.plugin(), Some("vagrant-libvirt"));
138    /// assert_eq!(Provider::Virtualbox.plugin(), None);
139    /// ```
140    #[must_use]
141    pub const fn plugin(self) -> Option<&'static str> {
142        match self {
143            Provider::Virtualbox | Provider::Hyperv => None,
144            Provider::VmwareDesktop => Some("vagrant-vmware-desktop"),
145            Provider::Parallels => Some("vagrant-parallels"),
146            Provider::Libvirt => Some("vagrant-libvirt"),
147            Provider::Qemu => Some("vagrant-qemu"),
148            Provider::Utm => Some("vagrant_utm"),
149            Provider::VmwareEsxi => Some("vagrant-vmware-esxi"),
150        }
151    }
152}
153
154/// A native tool the steps run, probed before `up`: a missing one fails with its name, not a raw
155/// spawn error.
156#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
157#[serde(deny_unknown_fields)]
158pub struct Tool {
159    /// The tool's name, as messages show it.
160    pub name: ToolName,
161    /// A read-only command that succeeds when the tool (and its daemon, for Docker) is usable.
162    /// Time-bounded by the runner.
163    pub probe: HostArgv,
164    /// The oldest version that works.
165    #[serde(default, skip_serializing_if = "Option::is_none")]
166    pub min_version: Option<Version>,
167    /// An image the runner may run the tool from instead of the host binary, pinned by digest.
168    #[serde(default, skip_serializing_if = "Option::is_none")]
169    pub container: Option<PinnedImage>,
170    /// The hosts that use `container`.
171    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
172    pub container_hosts: BTreeSet<HostOs>,
173    /// Only on these hosts (empty: every host).
174    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
175    pub hosts: BTreeSet<HostOs>,
176    /// Only with these providers (empty: every provider).
177    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
178    pub providers: BTreeSet<Provider>,
179}
180
181/// The Compose project of a run.
182#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
183#[serde(deny_unknown_fields)]
184pub struct ComposeIdentity {
185    /// The project name: `${run.name}`, which the runner may set (`<runner>-<run id>`).
186    pub project: Template,
187    /// The Compose files, in order.
188    pub files: NonEmpty<PathTemplate>,
189    /// The runner's override file of host ports pinned at the first start (in the run's state
190    /// folder), named in every Compose argv. Always written, `services: {}` when nothing is
191    /// pinned.
192    #[serde(default, skip_serializing_if = "Option::is_none")]
193    pub pinned_ports_file: Option<PathTemplate>,
194}
195
196/// Whether a provider's commands run elevated.
197#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
198#[serde(rename_all = "lowercase")]
199pub enum Elevation {
200    /// Never.
201    #[default]
202    None,
203    /// On Windows, management commands run elevated (one prompt per operation) when the runner
204    /// may not manage Hyper-V itself. Status reads never are.
205    Hyperv,
206}
207
208/// A provider the Vagrantfile supports.
209#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
210#[serde(deny_unknown_fields)]
211pub struct VagrantProvider {
212    /// The provider.
213    pub id: Provider,
214    /// The hosts it exists on.
215    pub hosts: BTreeSet<HostOs>,
216    /// Its Vagrant plugin, when it needs one.
217    #[serde(default, skip_serializing_if = "Option::is_none")]
218    pub plugin: Option<ToolName>,
219    /// Whether its commands need elevation.
220    #[serde(default)]
221    pub elevation: Elevation,
222    /// The VMs run on a server (ESXi): its connection slots apply.
223    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
224    pub remote: bool,
225    /// Why this environment can't run on this provider, in Isoloom's words, or null.
226    #[serde(default)]
227    pub refusal: Option<String>,
228}
229
230/// The Vagrant providers of a run, and where Vagrant keeps its record of the run's machines.
231#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
232#[serde(deny_unknown_fields)]
233pub struct VagrantIdentity {
234    /// The providers the Vagrantfile supports.
235    pub providers: Vec<VagrantProvider>,
236    /// The folder Vagrant keeps its machines' state in (`VAGRANT_DOTFILE_PATH`, which the runner
237    /// sets for every Vagrant command of the run, its actions' included), in the run's state
238    /// folder (`${run.state_dir}/vagrant`): a replaced project folder never loses track of its
239    /// VMs. Absent: Vagrant's own `.vagrant` next to the Vagrantfile.
240    #[serde(default, skip_serializing_if = "Option::is_none")]
241    pub dotfile: Option<PathTemplate>,
242}
243
244/// Who keeps a Terraform state.
245#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
246#[serde(rename_all = "lowercase")]
247pub enum StateOwner {
248    /// The runner: local state in the run's state folder, outside the project's files (a replaced
249    /// project folder must not orphan what it created).
250    Runner,
251}
252
253/// The Terraform module of a run.
254#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
255#[serde(deny_unknown_fields)]
256pub struct TerraformIdentity {
257    /// The module's folder.
258    pub module: RelPath,
259    /// Who keeps the state.
260    pub state: StateOwner,
261    /// Variable files Isoloom generated, in order (`terraform.tfvars.json`).
262    #[serde(default, skip_serializing_if = "Vec::is_empty")]
263    pub var_files: Vec<RelPath>,
264    /// The errors of the module's providers that say a resource of the state is already gone,
265    /// where its delete fails instead of counting it done. A `down` whose destroy failed with one
266    /// of them drops that resource from the state and destroys again.
267    #[serde(default, skip_serializing_if = "Vec::is_empty")]
268    pub gone: Vec<GoneError>,
269}
270
271/// A provider's error saying that a resource instance of the state is already gone (deleted
272/// outside Terraform, or lost with the server's reset), printed by a delete that fails on it
273/// rather than counting it done: bpg/proxmox's IP set, `no such IPSet 'iso1n0'`.
274///
275/// When a destroy fails and one of its lines holds the text filled with an instance's attributes
276/// in the state, that instance is gone: the runner drops it from the state (`terraform state
277/// rm`), says which, and destroys again. An instance whose text no line holds
278/// stays, so the text must name the instance by its own attributes and in the provider's words,
279/// and only for a delete that is sure the instance is nowhere.
280#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
281#[serde(deny_unknown_fields)]
282pub struct GoneError {
283    /// The resource type (`proxmox_virtual_environment_firewall_ipset`).
284    pub resource: SnakeName,
285    /// What its delete prints for an instance that is gone (`no such IPSet '{id}'`).
286    pub error: GoneText,
287}
288
289/// How the runner finds what a run created, without reading the generated files: one key, the
290/// driver's.
291#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
292#[serde(rename_all = "lowercase", deny_unknown_fields)]
293pub enum Identity {
294    /// The Compose project.
295    Compose(ComposeIdentity),
296    /// The Vagrant providers.
297    Vagrant(VagrantIdentity),
298    /// The Terraform module and state.
299    Terraform(TerraformIdentity),
300}
301
302impl Identity {
303    /// The driver this identity is for.
304    #[must_use]
305    pub fn driver(&self) -> Driver {
306        match self {
307            Identity::Compose(_) => Driver::Compose,
308            Identity::Vagrant(_) => Driver::Vagrant,
309            Identity::Terraform(_) => Driver::Terraform,
310        }
311    }
312}