blue-lang-cli 0.0.74

The blue command line: run, fmt, ast, erase, check.
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
//! `blue image build`, `blue workload render` and `blue diff`: the doors to
//! the Bluefile's `image` and `workload` words (`blue_lang_pkg::deploy`).
//!
//! Each one hands the work to the tool that owns it. An image is built by
//! nix, through the project engine's `packages.image-<name>` (substrate's
//! hardened builders); this door only says which attribute and reads back
//! what was built. A workload is rendered from `Bluefile.lock`, the same bytes
//! nix gates fresh. A cluster is read with `kubectl get` and nothing else.
//!
//! **There is no `blue apply`, by design.** Applying is GitOps: the rendered
//! file is committed and a reconciler (a Flux Kustomization) converges the
//! cluster on it. `blue diff` shows what that commit would change, under an
//! explicit `--context`, because the operator's kubeconfig holds production
//! contexts and a default one is a guess.

use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode, Stdio};

use blue_lang_pkg::deploy::diff::{diff as compare, Change};
use blue_lang_pkg::deploy::render::{render_text, workloads, RenderError};
use blue_lang_pkg::lock::{Lock, ManifestRecord, LOCK_FILE};

/// `blue image …`.
#[derive(clap::Subcommand)]
pub enum ImageCmd {
    /// Build an `image(...)` through nix and report the tarball, its size and
    /// its layers. With no NAME, every image the Bluefile declares.
    ///
    /// Builds `<dir>#packages.<system>.image-<name>`, the project engine's
    /// lowering of the word. Images are Linux; off Linux the default system is
    /// x86_64-linux, built by a remote builder. Arguments after `--` go to
    /// `nix build` (`-- --override-input blue ../blue`).
    Build {
        name: Option<String>,
        /// The project directory: its Bluefile.lock and its flake.
        #[arg(long, default_value = ".")]
        dir: PathBuf,
        /// The Linux system to build for.
        #[arg(long)]
        system: Option<String>,
        #[arg(last = true)]
        nix_args: Vec<String>,
    },
}

/// `blue workload …`.
#[derive(clap::Subcommand)]
pub enum WorkloadCmd {
    /// Print a workload's rendered objects, or with `--write` rewrite every
    /// workload's committed `output` file. Reads Bluefile.lock, as nix does.
    Render {
        name: Option<String>,
        #[arg(long, default_value = LOCK_FILE)]
        lock: PathBuf,
        #[arg(long)]
        write: bool,
    },
}

/// `blue diff`.
#[derive(clap::Args)]
pub struct DiffArgs {
    /// One workload; every workload when omitted.
    name: Option<String>,
    /// The kubeconfig context to read. Required: there is no default.
    #[arg(long)]
    context: Option<String>,
    #[arg(long, default_value = LOCK_FILE)]
    lock: PathBuf,
    /// The kubectl to run.
    #[arg(long, default_value = "kubectl")]
    kubectl: PathBuf,
}

#[derive(Debug, thiserror::Error)]
pub enum DeployError {
    #[error(
        "`blue diff` reads a cluster, and names it: pass --context. The kubeconfig \
         may hold production contexts, so there is no default. (blue never applies; \
         a GitOps reconciler applies the committed files.)"
    )]
    NoContext,
    #[error("{path}: {source}")]
    Read {
        path: String,
        #[source]
        source: std::io::Error,
    },
    #[error("{path}: {source}")]
    Write {
        path: String,
        #[source]
        source: std::io::Error,
    },
    #[error("{path} is not readable JSON: {source}")]
    Json {
        path: String,
        #[source]
        source: serde_json::Error,
    },
    #[error("{0}")]
    Render(#[from] RenderError),
    #[error("the Bluefile declares no {what}")]
    None { what: &'static str },
    #[error("no {what} `{name}`; the Bluefile declares {}", .known.join(", "))]
    Unknown {
        what: &'static str,
        name: String,
        known: Vec<String>,
    },
    #[error("the Bluefile declares several workloads ({}); name one, or pass --write", .known.join(", "))]
    PickOne { known: Vec<String> },
    #[error("could not start `{program}`: {source}")]
    Spawn {
        program: String,
        #[source]
        source: std::io::Error,
    },
    #[error("nix build failed ({status})")]
    Nix { status: std::process::ExitStatus },
    #[error("nix build printed {got} path(s) for 2 attributes")]
    NixOutput { got: usize },
    #[error("{path} has no image entry with `Layers`")]
    Layers { path: String },
    #[error("{target}: kubectl get failed ({status}): {stderr}")]
    Kubectl {
        target: String,
        status: std::process::ExitStatus,
        stderr: String,
    },
    #[error("{path}: an item of kind `{kind}` is not one blue renders")]
    Kind { path: String, kind: String },
}

fn read(path: &Path) -> Result<String, DeployError> {
    std::fs::read_to_string(path).map_err(|source| DeployError::Read {
        path: path.display().to_string(),
        source,
    })
}

fn json<T: serde::de::DeserializeOwned>(path: &Path, text: &str) -> Result<T, DeployError> {
    serde_json::from_str(text).map_err(|source| DeployError::Json {
        path: path.display().to_string(),
        source,
    })
}

fn manifest_at(lock: &Path) -> Result<ManifestRecord, DeployError> {
    let lock_value: Lock = json(lock, &read(lock)?)?;
    Ok(lock_value.manifest)
}

/// The names a command acts on: the one named, or all of them.
fn select<'a>(
    what: &'static str,
    name: Option<&'a str>,
    known: Vec<&'a str>,
) -> Result<Vec<&'a str>, DeployError> {
    if known.is_empty() {
        return Err(DeployError::None { what });
    }
    match name {
        None => Ok(known),
        Some(n) if known.contains(&n) => Ok(vec![n]),
        Some(n) => Err(DeployError::Unknown {
            what,
            name: n.to_string(),
            known: known.into_iter().map(String::from).collect(),
        }),
    }
}

/// The Linux system an image is built for when `--system` is not given: the
/// host's own on Linux, else x86_64-linux.
fn default_system() -> &'static str {
    match (std::env::consts::OS, std::env::consts::ARCH) {
        ("linux", "aarch64") => "aarch64-linux",
        _ => "x86_64-linux",
    }
}

/// A byte count as people read it.
struct Size(u64);

impl std::fmt::Display for Size {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mib = self.0 as f64 / (1024.0 * 1024.0);
        write!(f, "{mib:.1} MiB ({} bytes)", self.0)
    }
}

pub fn image(cmd: ImageCmd) -> Result<ExitCode, DeployError> {
    let ImageCmd::Build {
        name,
        dir,
        system,
        nix_args,
    } = cmd;
    let lock = dir.join(LOCK_FILE);
    let manifest = manifest_at(&lock)?;
    let known = manifest.project.images.keys().map(String::as_str).collect();
    let system: &str = system.as_deref().unwrap_or(default_system());
    let flake = std::fs::canonicalize(&dir).map_err(|source| DeployError::Read {
        path: dir.display().to_string(),
        source,
    })?;
    let flake = flake.display().to_string();
    for image in select("image", name.as_deref(), known)? {
        let attr = ["packages.", system, ".image-", image].concat();
        let tarball_attr = [flake.as_str(), "#", &attr].concat();
        let manifest_attr = [tarball_attr.as_str(), "-manifest"].concat();
        let out = Command::new("nix")
            .args(["build", "--no-link", "--print-out-paths"])
            .arg(&tarball_attr)
            .arg(&manifest_attr)
            .args(&nix_args)
            .stderr(Stdio::inherit())
            .output()
            .map_err(|source| DeployError::Spawn {
                program: "nix".into(),
                source,
            })?;
        if !out.status.success() {
            return Err(DeployError::Nix { status: out.status });
        }
        let stdout = String::from_utf8_lossy(&out.stdout);
        let paths: Vec<&str> = stdout.lines().filter(|l| !l.is_empty()).collect();
        let [tarball, docker_manifest] = paths.as_slice() else {
            return Err(DeployError::NixOutput { got: paths.len() });
        };
        let tarball = Path::new(tarball);
        let size = std::fs::metadata(tarball)
            .map_err(|source| DeployError::Read {
                path: tarball.display().to_string(),
                source,
            })?
            .len();
        let docker_manifest = Path::new(docker_manifest);
        let entries: Vec<serde_json::Value> = json(docker_manifest, &read(docker_manifest)?)?;
        let layers = entries
            .first()
            .and_then(|e| e.get("Layers"))
            .and_then(serde_json::Value::as_array)
            .ok_or_else(|| DeployError::Layers {
                path: docker_manifest.display().to_string(),
            })?
            .len();
        let spec = &manifest.project.images[image];
        println!("image {image} ({system})");
        println!("  tarball  {}", tarball.display());
        println!("  size     {}", Size(size));
        println!("  layers   {layers}");
        println!("  ref      {}:{}", spec.repository, manifest.version);
    }
    Ok(ExitCode::SUCCESS)
}

pub fn workload(cmd: WorkloadCmd) -> Result<ExitCode, DeployError> {
    let WorkloadCmd::Render { name, lock, write } = cmd;
    let manifest = manifest_at(&lock)?;
    let names = select("workload", name.as_deref(), workloads(&manifest))?;
    if write {
        let root = lock.parent().unwrap_or(Path::new("."));
        for w in names {
            let path = root.join(&manifest.project.workloads[w].output);
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent).map_err(|source| DeployError::Write {
                    path: parent.display().to_string(),
                    source,
                })?;
            }
            std::fs::write(&path, render_text(&manifest, w)?).map_err(|source| {
                DeployError::Write {
                    path: path.display().to_string(),
                    source,
                }
            })?;
            eprintln!("blue: wrote {}", path.display());
        }
        return Ok(ExitCode::SUCCESS);
    }
    let [one] = names.as_slice() else {
        return Err(DeployError::PickOne {
            known: names.into_iter().map(String::from).collect(),
        });
    };
    print!("{}", render_text(&manifest, one)?);
    Ok(ExitCode::SUCCESS)
}

/// The one cluster call `blue diff` makes. `get`, by construction: this is
/// the only place an argument list for kubectl is built.
fn kubectl_get<'a>(
    context: &'a str,
    resource: &'a str,
    name: &'a str,
    namespace: &'a str,
) -> [&'a str; 11] {
    [
        "--context",
        context,
        "get",
        resource,
        name,
        "--namespace",
        namespace,
        "--output",
        "json",
        "--ignore-not-found",
        "--show-managed-fields=false",
    ]
}

/// The resource `kubectl get` names a rendered kind by.
fn resource_of(kind: &str) -> Option<&'static str> {
    match kind {
        "Deployment" => Some("deployment.apps"),
        "Service" => Some("service"),
        _ => None,
    }
}

/// What one rendered object looks like against the cluster.
enum Verdict {
    Same,
    Absent,
    Differs(Vec<Change>),
}

/// `blue diff`: exit 0 when the cluster holds what the committed files
/// state, 1 when it does not, 2 when the comparison could not be made.
pub fn diff(args: DiffArgs) -> ExitCode {
    match run_diff(args) {
        Ok(true) => ExitCode::SUCCESS,
        Ok(false) => ExitCode::from(1),
        Err(e) => {
            eprintln!("blue: {e}");
            ExitCode::from(2)
        }
    }
}

fn run_diff(args: DiffArgs) -> Result<bool, DeployError> {
    let context = match args.context.as_deref() {
        Some(c) if !c.trim().is_empty() => c,
        _ => return Err(DeployError::NoContext),
    };
    let manifest = manifest_at(&args.lock)?;
    let root = args.lock.parent().unwrap_or(Path::new("."));
    let mut same = true;
    for w in select("workload", args.name.as_deref(), workloads(&manifest))? {
        let path = root.join(&manifest.project.workloads[w].output);
        let committed = read(&path)?;
        if committed != render_text(&manifest, w)? {
            eprintln!(
                "blue: {} is stale against Bluefile.lock; comparing the committed file (`blue workload render --write` regenerates it)",
                path.display()
            );
        }
        let list: serde_json::Value = json(&path, &committed)?;
        let items = list
            .get("items")
            .and_then(serde_json::Value::as_array)
            .cloned()
            .unwrap_or_default();
        for item in &items {
            let text = |k: &[&str]| {
                k.iter()
                    .try_fold(item, |v, key| v.get(*key))
                    .and_then(serde_json::Value::as_str)
                    .unwrap_or_default()
                    .to_string()
            };
            let kind = text(&["kind"]);
            let Some(resource) = resource_of(&kind) else {
                return Err(DeployError::Kind {
                    path: path.display().to_string(),
                    kind,
                });
            };
            let name = text(&["metadata", "name"]);
            let namespace = text(&["metadata", "namespace"]);
            let target = [resource, " ", &namespace, "/", &name].concat();
            let out = Command::new(&args.kubectl)
                .args(kubectl_get(context, resource, &name, &namespace))
                .stdin(Stdio::null())
                .output()
                .map_err(|source| DeployError::Spawn {
                    program: args.kubectl.display().to_string(),
                    source,
                })?;
            if !out.status.success() {
                return Err(DeployError::Kubectl {
                    target,
                    status: out.status,
                    stderr: String::from_utf8_lossy(&out.stderr).trim().to_string(),
                });
            }
            let verdict = verdict(item, &out.stdout).map_err(|source| DeployError::Json {
                path: target.clone(),
                source,
            })?;
            match verdict {
                Verdict::Same => println!("= {target}"),
                Verdict::Absent => {
                    same = false;
                    println!("+ {target}: not in the cluster; applying the commit creates it");
                }
                Verdict::Differs(changes) => {
                    same = false;
                    println!("~ {target}");
                    for c in changes {
                        println!("    {c}");
                    }
                }
            }
        }
    }
    Ok(same)
}

/// Compare one rendered object with what `kubectl get --ignore-not-found`
/// printed: nothing when the object is absent.
fn verdict(rendered: &serde_json::Value, live: &[u8]) -> Result<Verdict, serde_json::Error> {
    if live.iter().all(u8::is_ascii_whitespace) {
        return Ok(Verdict::Absent);
    }
    let live: serde_json::Value = serde_json::from_slice(live)?;
    let changes = compare(rendered, &live);
    Ok(if changes.is_empty() {
        Verdict::Same
    } else {
        Verdict::Differs(changes)
    })
}

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

    /// **The read-only guarantee, pinned.** The one argument list blue builds
    /// for kubectl is a `get` under the named context, and no mutating verb
    /// appears in it.
    #[test]
    fn the_cluster_call_is_a_get_under_the_named_context() {
        let args = kubectl_get("dev", "deployment.apps", "hello", "blue");
        assert_eq!(&args[..3], &["--context", "dev", "get"]);
        for verb in [
            "apply", "create", "replace", "patch", "delete", "edit", "scale", "annotate", "label",
            "rollout", "set",
        ] {
            assert!(!args.contains(&verb), "{verb}");
        }
    }

    #[test]
    fn every_rendered_kind_has_a_resource() {
        assert_eq!(resource_of("Deployment"), Some("deployment.apps"));
        assert_eq!(resource_of("Service"), Some("service"));
        assert_eq!(resource_of("Secret"), None);
    }

    /// Against a fixture of what a cluster returns: absent, the same with the
    /// server's defaults added, and changed.
    #[test]
    fn a_verdict_reads_absent_same_and_changed() {
        let rendered = json!({"kind": "Deployment", "spec": {"replicas": 2}});
        assert!(matches!(verdict(&rendered, b""), Ok(Verdict::Absent)));
        assert!(matches!(verdict(&rendered, b"\n"), Ok(Verdict::Absent)));
        let live = br#"{"kind": "Deployment", "spec": {"replicas": 2, "strategy": {}}, "status": {"readyReplicas": 2}}"#;
        assert!(matches!(verdict(&rendered, live), Ok(Verdict::Same)));
        let live = br#"{"kind": "Deployment", "spec": {"replicas": 1}}"#;
        match verdict(&rendered, live) {
            Ok(Verdict::Differs(changes)) => {
                assert_eq!(changes.len(), 1);
                assert_eq!(changes[0].to_string(), "~ spec.replicas: 1 -> 2");
            }
            _ => panic!("expected a difference"),
        }
    }

    #[test]
    fn select_names_one_or_all_and_refuses_the_unknown() {
        assert_eq!(select("w", None, vec!["a", "b"]).unwrap(), vec!["a", "b"]);
        assert_eq!(select("w", Some("b"), vec!["a", "b"]).unwrap(), vec!["b"]);
        assert!(matches!(
            select("w", Some("c"), vec!["a"]),
            Err(DeployError::Unknown { .. })
        ));
        assert!(matches!(
            select("w", None, vec![]),
            Err(DeployError::None { .. })
        ));
    }

    #[test]
    fn a_size_reads_in_mebibytes_and_bytes() {
        assert_eq!(
            Size(3 * 1024 * 1024 + 512 * 1024).to_string(),
            "3.5 MiB (3670016 bytes)"
        );
    }
}