isothrum_machine/
cloud.rs1use std::time::Duration;
15
16use isothrum_runner::ProcessSpec;
17
18use crate::context::Context;
19use crate::failure::{self, Failure, Refusal};
20
21pub mod aws;
22pub mod azure;
23pub mod gcp;
24pub mod oci;
25pub mod token;
26
27pub use token::{TokenError, digitalocean, linode};
28
29pub use isothrum_runner::Cloud;
30
31pub const CLI_TIMEOUT: Duration = Duration::from_secs(60);
34
35#[must_use]
38pub fn cli(cloud: Cloud) -> Option<&'static str> {
39 match cloud {
40 Cloud::Aws => Some(aws::CLI),
41 Cloud::Azure => Some(azure::CLI),
42 Cloud::Gcp => Some(gcp::CLI),
43 Cloud::Oci => Some(oci::CLI),
44 _ => None,
45 }
46}
47
48async fn run(cx: &Context, limit: Duration, spec: &ProcessSpec, read: fn(&str) -> Refusal) -> Result<String, Failure> {
50 failure::answer(cx, limit, spec, read).await
51}
52
53async fn ask(cx: &Context, program: &str, args: &[&str], read: fn(&str) -> Refusal) -> Result<String, Failure> {
55 run(cx, CLI_TIMEOUT, &ProcessSpec::new(program, args.iter().copied()), read).await
56}
57
58fn unreadable(program: &str, why: impl std::fmt::Display) -> Failure {
60 Failure::Unreadable {
61 program: program.to_owned(),
62 why: why.to_string(),
63 }
64}
65
66fn json_list(program: &str, out: &str) -> Result<Vec<serde_json::Value>, Failure> {
69 serde_json::from_str(out).map_err(|e| unreadable(program, format!("its answer isn't a JSON list ({e})")))
70}
71
72fn line(out: &str) -> String {
74 out.trim().to_owned()
75}
76
77fn says(said: &str, words: &[&str]) -> bool {
79 let lower = said.to_ascii_lowercase();
80 words.iter().any(|w| lower.contains(&w.to_ascii_lowercase()))
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
85#[non_exhaustive]
86pub enum SignIn {
87 Aws {
90 profile: Option<String>,
92 },
93 Azure,
95 Gcp,
98}
99
100impl SignIn {
101 #[must_use]
103 pub fn command(&self) -> ProcessSpec {
104 match self {
105 SignIn::Aws { profile } => {
106 let mut args = vec!["login".to_owned()];
107 if let Some(p) = profile {
108 args.extend(["--profile".to_owned(), p.clone()]);
109 }
110 ProcessSpec::new(aws::CLI, args)
111 }
112 SignIn::Azure => ProcessSpec::new(azure::CLI, ["login"]),
113 SignIn::Gcp => ProcessSpec::new(gcp::CLI, ["auth", "login", "--update-adc"]),
114 }
115 }
116}
117
118pub async fn sign_in(cx: &Context, which: &SignIn, on_line: &mut (dyn FnMut(&str) + Send)) -> Result<(), Failure> {
127 let spec = which.command();
128 on_line(&format!("$ {}", spec.line()));
129 let mut last = String::new();
130 let mut each = |_: isothrum_runner::Stream, l: &str| {
131 if !l.trim().is_empty() {
132 l.trim().clone_into(&mut last);
133 }
134 on_line(l);
135 };
136 let program = spec.program.clone();
137 match cx.runner.run(&spec, &mut each).await {
138 Ok(exit) if exit.success() => Ok(()),
139 Ok(exit) => Err(Failure::Refused {
140 refusal: Refusal::Other,
141 said: if last.is_empty() { exit.to_string() } else { last },
142 program,
143 }),
144 Err(isothrum_runner::ProcessError::Missing { .. }) => Err(Failure::Missing { program }),
145 Err(other) => Err(Failure::NotStarted {
146 program,
147 reason: other.to_string(),
148 }),
149 }
150}
151
152#[cfg(test)]
153mod tests;