Skip to main content

isothrum_runner/
options.rs

1//! What the embedder passes to an operation: its params, secrets and choices.
2
3use std::collections::BTreeMap;
4
5use isoloom_manifest::Provider;
6
7use crate::secrets::Secrets;
8
9/// What `up` does when the run already exists.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11#[non_exhaustive]
12pub enum Occupied {
13    /// Resumes a parked run instead of starting it again (leftovers are still removed first).
14    #[default]
15    ResumeParked,
16    /// Removes whatever exists, then starts afresh.
17    Replace,
18}
19
20/// The options of one operation.
21#[derive(Debug, Clone, Default)]
22#[non_exhaustive]
23pub struct RunOptions {
24    /// A Vagrant provider among the manifest's (`${run.provider}`).
25    pub provider: Option<Provider>,
26    /// Non-secret values, by param name.
27    pub params: BTreeMap<String, String>,
28    /// Secret and input values, by slot name. Never stored: pass them again to an operation that
29    /// creates something.
30    pub secrets: Secrets,
31    /// What the embedder calls the place the run is on (a server or account id): recorded, so a
32    /// later operation asked for elsewhere is refused (`running_elsewhere`).
33    pub host_label: Option<String>,
34    /// What `up` does when the run exists.
35    pub occupied: Occupied,
36    /// A login (`user@host`) every SSH of the run goes through first: a Proxmox node whose run
37    /// bridge this host can't route to. Read by an `up` that starts the run, and recorded: every
38    /// later operation (a resume through `up` too) goes the way the run started, whatever its
39    /// options say.
40    pub ssh_jump: Option<String>,
41    /// How long the run may live, for a manifest with an `expiry` (its default otherwise).
42    pub lifetime: Option<std::time::Duration>,
43}
44
45impl RunOptions {
46    /// No param, no secret, nothing recorded about the place; a parked run is resumed.
47    #[must_use]
48    pub fn new() -> RunOptions {
49        RunOptions::default()
50    }
51
52    /// Sets param `name` to `value`.
53    #[must_use]
54    pub fn with_param(mut self, name: impl Into<String>, value: impl Into<String>) -> RunOptions {
55        self.params.insert(name.into(), value.into());
56        self
57    }
58
59    /// Sets every param of `params`.
60    #[must_use]
61    pub fn with_params(mut self, params: impl IntoIterator<Item = (String, String)>) -> RunOptions {
62        self.params.extend(params);
63        self
64    }
65
66    /// Gives the secret and input slots `secrets`.
67    #[must_use]
68    pub fn with_secrets(mut self, secrets: Secrets) -> RunOptions {
69        self.secrets = secrets;
70        self
71    }
72
73    /// Records the place the run is on as `label`.
74    #[must_use]
75    pub fn with_host_label(mut self, label: impl Into<String>) -> RunOptions {
76        self.host_label = Some(label.into());
77        self
78    }
79
80    /// Runs with Vagrant provider `provider`.
81    #[must_use]
82    pub fn with_provider(mut self, provider: Provider) -> RunOptions {
83        self.provider = Some(provider);
84        self
85    }
86
87    /// Goes through `login` (`user@host`) for every SSH of the run, once an `up` starting it
88    /// records it ([`RunOptions::ssh_jump`]).
89    #[must_use]
90    pub fn with_ssh_jump(mut self, login: impl Into<String>) -> RunOptions {
91        self.ssh_jump = Some(login.into());
92        self
93    }
94
95    /// Lets the run live `lifetime` (a manifest with an `expiry`).
96    #[must_use]
97    pub fn with_lifetime(mut self, lifetime: std::time::Duration) -> RunOptions {
98        self.lifetime = Some(lifetime);
99        self
100    }
101
102    /// What `up` does when the run exists.
103    #[must_use]
104    pub fn with_occupied(mut self, occupied: Occupied) -> RunOptions {
105        self.occupied = occupied;
106        self
107    }
108}