isothrum_runner/options.rs
1//! What the embedder passes to an operation: its params, secrets and choices.
2
3use std::collections::BTreeMap;
4
5use crate::secrets::Secrets;
6
7/// What `up` does when the run already exists.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9#[non_exhaustive]
10pub enum Occupied {
11 /// Fails with `already_running` when anything of the run is up or parked.
12 Refuse,
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<String>,
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. Recorded at `up`, so later operations go the same way.
38 pub ssh_jump: Option<String>,
39 /// How long the run may live, for a manifest with an `expiry` (its default otherwise).
40 pub lifetime: Option<std::time::Duration>,
41}
42
43impl RunOptions {
44 /// No param, no secret, nothing recorded about the place; a parked run is resumed.
45 #[must_use]
46 pub fn new() -> RunOptions {
47 RunOptions::default()
48 }
49
50 /// Sets param `name` to `value`.
51 #[must_use]
52 pub fn with_param(mut self, name: impl Into<String>, value: impl Into<String>) -> RunOptions {
53 self.params.insert(name.into(), value.into());
54 self
55 }
56
57 /// Sets every param of `params`.
58 #[must_use]
59 pub fn with_params(mut self, params: impl IntoIterator<Item = (String, String)>) -> RunOptions {
60 self.params.extend(params);
61 self
62 }
63
64 /// Gives the secret and input slots `secrets`.
65 #[must_use]
66 pub fn with_secrets(mut self, secrets: Secrets) -> RunOptions {
67 self.secrets = secrets;
68 self
69 }
70
71 /// Records the place the run is on as `label`.
72 #[must_use]
73 pub fn with_host_label(mut self, label: impl Into<String>) -> RunOptions {
74 self.host_label = Some(label.into());
75 self
76 }
77
78 /// Runs with Vagrant provider `provider`.
79 #[must_use]
80 pub fn with_provider(mut self, provider: impl Into<String>) -> RunOptions {
81 self.provider = Some(provider.into());
82 self
83 }
84
85 /// Goes through `login` (`user@host`) for every SSH of the run.
86 #[must_use]
87 pub fn with_ssh_jump(mut self, login: impl Into<String>) -> RunOptions {
88 self.ssh_jump = Some(login.into());
89 self
90 }
91
92 /// Lets the run live `lifetime` (a manifest with an `expiry`).
93 #[must_use]
94 pub fn with_lifetime(mut self, lifetime: std::time::Duration) -> RunOptions {
95 self.lifetime = Some(lifetime);
96 self
97 }
98
99 /// What `up` does when the run exists.
100 #[must_use]
101 pub fn with_occupied(mut self, occupied: Occupied) -> RunOptions {
102 self.occupied = occupied;
103 self
104 }
105}