olai-stack-runtime 0.0.3

Run trestle environments on Docker Compose: a ComposeEngine over the Docker CLI, status, health and events
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! [`NeonCli`]: the [`NeonControl`] that runs the Neon CLI.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::Stdio;

use async_trait::async_trait;
use serde::Deserialize;
use serde::de::DeserializeOwned;
use tokio::process::Command;

use super::{
    Branch, Connection, CreateBranch, Function, NeonControl, NeonError, Storage, StorageCredential,
};
use crate::secrets::SecretValue;

/// The environment variable naming the Neon CLI to run, ahead of every other place it is
/// looked for.
pub const NEON_CLI_VAR: &str = "NEON_CLI";

/// Where the Neon CLI is, in order: `explicit`, `$NEON_CLI`, `neon` on `PATH`, the Homebrew
/// prefixes and `~/.npm-global/bin`, a login shell's `command -v neon`, then the same for
/// `neonctl`. An app started from the Finder has launchd's `PATH`, which holds none of them,
/// hence the fixed places and the login shell.
pub fn locate_neon(explicit: Option<&Path>) -> Option<PathBuf> {
    if let Some(path) = explicit {
        return path.is_file().then(|| path.to_owned());
    }
    if let Some(path) = std::env::var_os(NEON_CLI_VAR).map(PathBuf::from)
        && path.is_file()
    {
        return Some(path);
    }
    ["neon", "neonctl"].into_iter().find_map(find_program)
}

fn find_program(name: &str) -> Option<PathBuf> {
    let on_path = std::env::var_os("PATH")
        .into_iter()
        .flat_map(|p| std::env::split_paths(&p).collect::<Vec<_>>())
        .map(|dir| dir.join(name));
    let home = std::env::var_os("HOME").map(PathBuf::from);
    let fixed = ["/opt/homebrew/bin", "/usr/local/bin"]
        .into_iter()
        .map(PathBuf::from)
        .chain(home.map(|h| h.join(".npm-global/bin")))
        .map(|dir| dir.join(name));
    on_path
        .chain(fixed)
        .find(|p| p.is_file())
        .or_else(|| login_shell_lookup(name))
}

/// `command -v <name>` in the user's login shell, which reads their profile's `PATH`.
fn login_shell_lookup(name: &str) -> Option<PathBuf> {
    let shell = std::env::var_os("SHELL")?;
    let out = std::process::Command::new(shell)
        .args(["-lc", &format!("command -v {name}")])
        .stdin(Stdio::null())
        .stderr(Stdio::null())
        .output()
        .ok()?;
    let path = PathBuf::from(String::from_utf8(out.stdout).ok()?.trim());
    (out.status.success() && path.is_file()).then_some(path)
}

/// The Neon CLI (`neon`, or `neonctl`), run with `--output json`. Signed in the way the CLI is
/// (`~/.config/neon`, shared with the terminal) or with `NEON_API_KEY`.
#[derive(Clone, Debug)]
pub struct NeonCli {
    program: PathBuf,
}

impl NeonCli {
    /// The CLI [`locate_neon`] finds; [`NeonError::NotInstalled`] when there is none.
    pub fn new() -> Result<Self, NeonError> {
        locate_neon(None)
            .map(Self::with_program)
            .ok_or(NeonError::NotInstalled)
    }

    /// The CLI at `program`.
    pub fn with_program(program: impl Into<PathBuf>) -> Self {
        NeonCli {
            program: program.into(),
        }
    }

    /// The program it runs.
    pub fn program(&self) -> &Path {
        &self.program
    }

    /// Run `neon <args> --output json` and return its stdout.
    async fn run(&self, args: &[&str]) -> Result<String, NeonError> {
        let command = args.join(" ");
        let out = Command::new(&self.program)
            .args(args)
            .args(["--output", "json"])
            .stdin(Stdio::null())
            .kill_on_drop(true)
            .output()
            .await
            .map_err(|source| match source.kind() {
                std::io::ErrorKind::NotFound => NeonError::NotInstalled,
                _ => NeonError::Io {
                    command: command.clone(),
                    source,
                },
            })?;
        let stderr = String::from_utf8_lossy(&out.stderr).trim().to_owned();
        if !out.status.success() {
            return Err(if is_auth_error(&stderr) {
                NeonError::NotAuthenticated(stderr)
            } else {
                NeonError::Failed { command, stderr }
            });
        }
        String::from_utf8(out.stdout).map_err(|e| NeonError::Parse {
            command,
            reason: e.to_string(),
        })
    }

    /// Run `args` and parse the first JSON value it prints.
    async fn json<T: DeserializeOwned>(&self, args: &[&str]) -> Result<T, NeonError> {
        let stdout = self.run(args).await?;
        first_json(&stdout).map_err(|reason| NeonError::Parse {
            command: args.join(" "),
            reason,
        })
    }
}

/// Whether `stderr` says the CLI isn't signed in.
fn is_auth_error(stderr: &str) -> bool {
    let s = stderr.to_ascii_lowercase();
    [
        "unauthorized",
        "not authenticated",
        "authentication",
        "401",
        "neon auth",
    ]
    .iter()
    .any(|needle| s.contains(needle))
}

/// The first JSON value in `text`, which several verbs surround with lines of text (a
/// `WARNING:` after `credentials create`, progress lines around `functions deploy`).
pub(super) fn first_json<T: DeserializeOwned>(text: &str) -> Result<T, String> {
    let mut last = String::from("no JSON in the output");
    for (at, _) in text
        .match_indices(['{', '['])
        .filter(|(at, _)| *at == 0 || text[..*at].ends_with('\n'))
    {
        let mut values = serde_json::Deserializer::from_str(&text[at..]).into_iter::<T>();
        match values.next() {
            Some(Ok(value)) => return Ok(value),
            Some(Err(e)) => last = e.to_string(),
            None => {}
        }
    }
    Err(last)
}

/// `GET /projects/<p>/branches`: the branches, and their annotations keyed by branch id.
#[derive(Deserialize)]
pub(super) struct ApiBranches {
    pub branches: Vec<Branch>,
    #[serde(default)]
    pub annotations: BTreeMap<String, ApiAnnotation>,
}

#[derive(Deserialize)]
pub(super) struct ApiAnnotation {
    #[serde(default)]
    pub value: BTreeMap<String, String>,
}

impl ApiBranches {
    /// The branches with their annotations filled in.
    pub fn into_branches(mut self) -> Vec<Branch> {
        for branch in &mut self.branches {
            if let Some(a) = self.annotations.remove(&branch.id) {
                branch.annotations = a.value;
            }
        }
        self.branches
    }
}

/// `connection-string --extended`.
#[derive(Deserialize)]
pub(super) struct Extended {
    pub connection_string: String,
    pub host: String,
    pub role: String,
    pub password: String,
    pub database: String,
    #[serde(default)]
    pub options: String,
}

impl From<Extended> for Connection {
    fn from(e: Extended) -> Self {
        Connection {
            url: SecretValue::new(e.connection_string),
            host: e.host,
            role: e.role,
            password: SecretValue::new(e.password),
            database: e.database,
            options: e.options,
        }
    }
}

/// `credentials create`.
#[derive(Deserialize)]
pub(super) struct CreatedCredential {
    pub token_id: String,
    pub s3_secret_access_key: String,
}

#[derive(Deserialize)]
struct Named {
    name: String,
}

/// A database of `databases list`.
#[derive(Deserialize)]
struct Database {
    name: String,
    owner_name: String,
}

/// The database Neon creates with a project. `connection-string` and `databases create` only
/// pick a default while the branch has one database and one role; a branch that hosts more
/// (another service's database, a read-only role) needs both named.
const DEFAULT_DATABASE: &str = "neondb";

impl NeonCli {
    async fn database_list(&self, project: &str, branch: &str) -> Result<Vec<Database>, NeonError> {
        self.json(&[
            "databases",
            "list",
            "--project-id",
            project,
            "--branch",
            branch,
        ])
        .await
    }

    /// The branch's main database: `neondb` when it has one, else the first.
    async fn main_database(&self, project: &str, branch: &str) -> Result<Database, NeonError> {
        let mut dbs = self.database_list(project, branch).await?;
        if dbs.is_empty() {
            return Err(NeonError::Failed {
                command: "databases list".into(),
                stderr: format!("branch {branch} has no database"),
            });
        }
        let at = dbs
            .iter()
            .position(|d| d.name == DEFAULT_DATABASE)
            .unwrap_or(0);
        Ok(dbs.swap_remove(at))
    }
}

#[async_trait]
impl NeonControl for NeonCli {
    async fn auth_status(&self) -> Result<String, NeonError> {
        #[derive(Deserialize)]
        struct Me {
            #[serde(default)]
            login: Option<String>,
            #[serde(default)]
            email: Option<String>,
        }
        let me: Me = self.json(&["me"]).await?;
        Ok(me.login.or(me.email).unwrap_or_default())
    }

    async fn branches(&self, project: &str) -> Result<Vec<Branch>, NeonError> {
        let path = format!("/projects/{project}/branches");
        let api: ApiBranches = self.json(&["api", &path]).await?;
        Ok(api.into_branches())
    }

    async fn create_branch(&self, project: &str, spec: &CreateBranch) -> Result<Branch, NeonError> {
        #[derive(Deserialize)]
        struct Created {
            branch: Branch,
        }
        let annotations = serde_json::to_string(&spec.annotations).unwrap_or_default();
        let mut args = vec![
            "branches",
            "create",
            "--project-id",
            project,
            "--name",
            &spec.name,
            "--no-secrets",
        ];
        if let Some(parent) = &spec.parent {
            args.extend(["--parent", parent]);
        }
        if let Some(at) = &spec.expires_at {
            args.extend(["--expires-at", at]);
        }
        if !spec.annotations.is_empty() {
            args.extend(["--annotation", &annotations]);
        }
        let mut created: Created = self.json(&args).await?;
        // The CLI stores annotations but doesn't print them back.
        created.branch.annotations = spec.annotations.clone();
        Ok(created.branch)
    }

    async fn delete_branch(&self, project: &str, id: &str) -> Result<(), NeonError> {
        self.run(&["branches", "delete", id, "--project-id", project])
            .await
            .map(drop)
    }

    async fn reset_branch(&self, project: &str, id: &str) -> Result<(), NeonError> {
        self.run(&["branches", "reset", id, "--parent", "--project-id", project])
            .await
            .map(drop)
    }

    async fn connection(
        &self,
        project: &str,
        branch: &str,
        database: Option<&str>,
        pooled: bool,
    ) -> Result<Connection, NeonError> {
        let mut args = vec![
            "connection-string",
            branch,
            "--project-id",
            project,
            "--extended",
        ];
        if pooled {
            args.push("--pooled");
        }
        let main;
        let db = match database {
            Some(db) => db,
            None => {
                main = self.main_database(project, branch).await?;
                main.name.as_str()
            }
        };
        args.extend(["--database-name", db]);
        let extended: Extended = self.json(&args).await?;
        Ok(extended.into())
    }

    async fn databases(&self, project: &str, branch: &str) -> Result<Vec<String>, NeonError> {
        let dbs = self.database_list(project, branch).await?;
        Ok(dbs.into_iter().map(|d| d.name).collect())
    }

    async fn create_database(
        &self,
        project: &str,
        branch: &str,
        name: &str,
    ) -> Result<(), NeonError> {
        // Owned like the main database, by the role `connection-string` connects as.
        let owner = self.main_database(project, branch).await?.owner_name;
        self.run(&[
            "databases",
            "create",
            "--name",
            name,
            "--owner-name",
            &owner,
            "--project-id",
            project,
            "--branch",
            branch,
        ])
        .await
        .map(drop)
    }

    async fn functions(&self, project: &str, branch: &str) -> Result<Vec<Function>, NeonError> {
        self.json(&[
            "functions",
            "list",
            "--project-id",
            project,
            "--branch",
            branch,
        ])
        .await
    }

    async fn storage(&self, project: &str, branch: &str) -> Result<Storage, NeonError> {
        let path = format!("/projects/{project}/branches/{branch}/storage");
        self.json(&["api", &path]).await
    }

    async fn buckets(&self, project: &str, branch: &str) -> Result<Vec<String>, NeonError> {
        let buckets: Vec<Named> = self
            .json(&[
                "buckets",
                "list",
                "--project-id",
                project,
                "--branch",
                branch,
            ])
            .await?;
        Ok(buckets.into_iter().map(|b| b.name).collect())
    }

    async fn create_bucket(
        &self,
        project: &str,
        branch: &str,
        name: &str,
    ) -> Result<(), NeonError> {
        // Prints a sentence, not JSON.
        self.run(&[
            "buckets",
            "create",
            name,
            "--project-id",
            project,
            "--branch",
            branch,
        ])
        .await
        .map(drop)
    }

    async fn create_credential(
        &self,
        project: &str,
        branch: &str,
        name: &str,
    ) -> Result<StorageCredential, NeonError> {
        let created: CreatedCredential = self
            .json(&[
                "credentials",
                "create",
                "--project-id",
                project,
                "--branch",
                branch,
                "--name",
                name,
                "--scope",
                "storage:read",
                "--scope",
                "storage:write",
            ])
            .await?;
        Ok(StorageCredential {
            token_id: created.token_id,
            secret_access_key: SecretValue::new(created.s3_secret_access_key),
        })
    }

    async fn revoke_credential(
        &self,
        project: &str,
        branch: &str,
        token_id: &str,
    ) -> Result<(), NeonError> {
        self.run(&[
            "credentials",
            "revoke",
            token_id,
            "--project-id",
            project,
            "--branch",
            branch,
        ])
        .await
        .map(drop)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_first_json_value_wins_around_text() {
        let v: serde_json::Value =
            first_json("Function deployment triggered.\n{\"id\": 1}\nDone.\n").unwrap();
        assert_eq!(v["id"], 1);
        let v: serde_json::Value = first_json("{\"a\": [1]}\nWARNING: store it\n").unwrap();
        assert_eq!(v["a"][0], 1);
        // A `{` inside a line of text isn't the start of a value.
        let v: serde_json::Value = first_json("note {curly}\n[2]").unwrap();
        assert_eq!(v[0], 2);
        assert!(first_json::<serde_json::Value>("Bucket created").is_err());
    }

    #[test]
    fn auth_errors_are_told_apart() {
        assert!(is_auth_error("ERROR: Unauthorized. Run `neon auth`"));
        assert!(!is_auth_error("ERROR: branch not found"));
    }
}