spider-agent-cli 0.8.1

The spider-agent command line tool, built to be called by an agent: fetch, crawl, search and extract as JSON or NDJSON, with local routing, status-aware escalation and a budget.
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Keeping the binary current.
//!
//! Three moments, in the order a release reaches a machine:
//!
//! 1. A run notices the last check is more than [`CHECK_INTERVAL`] old and
//!    starts a detached `spider-agent update` in the background, then gets on
//!    with its own command. The command's output, timing and exit code do not
//!    depend on that child in any way; nothing waits for it and its standard
//!    streams are closed.
//! 2. That child asks where the newest release is, downloads `SHA256SUMS.txt`
//!    and its minisign signature, refuses both unless a pinned release key
//!    signed the sums for this version, downloads the archive for this
//!    platform, refuses it unless it matches, takes the binary out, checks it answers
//!    `--version` with the tagged version, and stages it beside the running
//!    binary. It records the staged file's SHA-256 in `~/.spider/update.json`.
//! 3. The next run finds the staged file, hashes it, and only if the digest
//!    matches the recorded one renames it over the binary. It then starts the
//!    new binary in its own place with the same arguments, so that run is
//!    already on the new version.
//!
//! `spider-agent update` does the same work in the foreground and installs at
//! once. [`OPT_OUT_ENV`] or `--no-update` turns every part of this off,
//! including step 3 for an update staged earlier. A non-empty `CI` skips
//! steps 1 and 3 and leaves `update` working.
//!
//! Every problem here becomes a line on stderr or nothing at all. None of them
//! changes what the caller's command does.

mod archive;
mod install;
mod release;
mod signature;
mod state;

use minisign_verify::PublicKey;

use std::fmt;
use std::time::Duration;

use url::Url;

use crate::cli::{Command, Global};
use crate::exit::{Code, Failure, Run};
use crate::progress::Log;

use self::install::{Applied, Target};
use self::release::{Release, Releases};
use self::state::{Lock, Notice, Staged, State};

/// Set to anything but an empty value to turn self update off entirely.
pub const OPT_OUT_ENV: &str = "SPIDER_AGENT_NO_UPDATE";

/// Marks the detached child that runs a background check. Not a setting.
const BACKGROUND_ENV: &str = "SPIDER_AGENT_UPDATE_BACKGROUND";

/// How often a background check may run.
pub const CHECK_INTERVAL: Duration = Duration::from_secs(24 * 60 * 60);

/// Where releases are published.
pub const RELEASES: &str = "https://github.com/spider-rs/spider-cloud-agent/releases";

/// Where to look for releases, whether plain http to loopback is allowed, and
/// which keys may sign a release.
pub struct Settings {
    base: Url,
    allow_loopback_http: bool,
    keys: Vec<PublicKey>,
}

/// The settings this build runs under, or `None` when it does not update
/// itself.
///
/// A release build always reads [`RELEASES`] over https, and the code that
/// reads anything else is not compiled into it: the `debug_assertions` cfg is
/// off under the release profile this workspace ships, so neither the
/// environment read below nor the loopback exception exists there.
///
/// A release build also trusts only the keys pinned in `release-keys.pub`.
///
/// A debug build updates itself only when `SPIDER_AGENT_UPDATE_BASE` names a
/// release base, which is how the tests point it at a loopback stub. Without
/// it a debug build does nothing, so `cargo run` and `cargo test` never touch
/// the network or replace a binary under `target/`. A debug build given
/// `SPIDER_AGENT_UPDATE_KEY` trusts that one key in place of the pinned ones,
/// so the tests can sign releases with a throwaway key.
fn settings() -> Option<Settings> {
    #[cfg(debug_assertions)]
    {
        let base = std::env::var("SPIDER_AGENT_UPDATE_BASE").ok()?;
        let keys = match std::env::var("SPIDER_AGENT_UPDATE_KEY") {
            Ok(key) => signature::parse_keys(&key).ok()?,
            Err(_) => signature::pinned().ok()?,
        };
        Url::parse(&base).ok().map(|base| Settings {
            base,
            allow_loopback_http: true,
            keys,
        })
    }
    #[cfg(not(debug_assertions))]
    {
        let keys = signature::pinned().ok()?;
        Url::parse(RELEASES).ok().map(|base| Settings {
            base,
            allow_loopback_http: false,
            keys,
        })
    }
}

/// The platform part of the asset name, for the platforms releases are built
/// for. Anything else has no asset and is not updated.
pub const fn triple() -> Option<&'static str> {
    if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
        Some("aarch64-apple-darwin")
    } else if cfg!(all(target_os = "macos", target_arch = "x86_64")) {
        Some("x86_64-apple-darwin")
    } else if cfg!(all(
        target_os = "linux",
        target_env = "gnu",
        target_arch = "aarch64"
    )) {
        Some("aarch64-unknown-linux-gnu")
    } else if cfg!(all(
        target_os = "linux",
        target_env = "gnu",
        target_arch = "x86_64"
    )) {
        Some("x86_64-unknown-linux-gnu")
    } else {
        None
    }
}

/// A release version: three numbers and nothing after them. A tag with a
/// pre-release suffix is not a version this tool moves to on its own.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version(u64, u64, u64);

impl Version {
    pub fn parse(text: &str) -> Option<Version> {
        let mut parts = text.split('.');
        let mut number = || -> Option<u64> {
            let part = parts.next()?;
            if part.is_empty() || !part.bytes().all(|byte| byte.is_ascii_digit()) {
                return None;
            }
            part.parse().ok()
        };
        let version = Version(number()?, number()?, number()?);
        parts.next().is_none().then_some(version)
    }

    /// The version of this binary.
    pub fn current() -> Option<Version> {
        Version::parse(env!("CARGO_PKG_VERSION"))
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.0, self.1, self.2)
    }
}

/// Why an update did not happen.
#[derive(Debug)]
pub enum Problem {
    /// The release host could not be reached, or answered with a failure.
    Unreachable(String),
    /// The host's limit for callers without an account was hit.
    RateLimited,
    /// Downloaded bytes did not match `SHA256SUMS.txt`, or the sums file has
    /// no valid signature from a release key.
    Mismatch(String),
    /// The release is not usable: no checksum line, an archive that does not
    /// open, or a binary that does not run here.
    Broken(String),
    /// Something on this machine failed.
    Local(String),
}

impl Problem {
    fn message(&self) -> String {
        match self {
            Problem::Unreachable(text) | Problem::Broken(text) | Problem::Local(text) => {
                text.clone()
            }
            Problem::RateLimited => {
                "the release host is limiting requests from this address. Try again later."
                    .to_string()
            }
            Problem::Mismatch(text) => format!("{text}. Nothing was installed."),
        }
    }

    fn code(&self) -> Code {
        match self {
            Problem::Unreachable(_) | Problem::RateLimited => Code::Transport,
            Problem::Mismatch(_) | Problem::Broken(_) | Problem::Local(_) => Code::Failed,
        }
    }
}

fn opted_out(global: &Global) -> bool {
    global.no_update || std::env::var_os("SPIDER_AGENT_NO_UPDATE").is_some_and(|v| !v.is_empty())
}

/// A CI job neither starts a check nor installs a staged update, so a
/// pipeline never swaps its own binary partway through. `update` still works.
fn in_ci() -> bool {
    std::env::var_os("CI").is_some_and(|v| !v.is_empty())
}

fn in_background() -> bool {
    std::env::var_os("SPIDER_AGENT_UPDATE_BACKGROUND").is_some_and(|v| !v.is_empty())
}

/// The line that tells someone how to update by hand.
fn manual(releases_page: &str) -> String {
    let asset = triple().map_or_else(
        || "the archive for this platform".to_string(),
        |triple| format!("spider-agent-<version>-{triple}.tar.gz"),
    );
    format!(
        "To update by hand, download {asset} and SHA256SUMS.txt from {releases_page}, check the archive with shasum -a 256 -c, and replace the binary."
    )
}

/// Everything that happens before the caller's command runs: a deferred
/// notice, an update staged earlier, and a background check if one is due.
///
/// Returns normally in every case but one: when a staged update was installed
/// and the new binary started in this process's place, this never returns.
pub fn before_command(global: &Global, command: Option<&Command>, log: Log) {
    if opted_out(global) || matches!(command, Some(Command::Update)) || in_background() || in_ci() {
        return;
    }
    if settings().is_none() || triple().is_none() {
        return;
    }
    let Ok(target) = Target::current() else {
        return;
    };
    let mut state = state::read();
    if state.notice.is_some() {
        print_notice(log);
    }
    if std::fs::symlink_metadata(target.staged_path()).is_ok() || state.staged.is_some() {
        apply_staged(&target, log);
        state = state::read();
    }
    if state::due(state.last_check, state::now(), CHECK_INTERVAL) {
        start_background_check(&target);
    }
}

/// Print and clear the deferred notice. Under the lock, so two runs starting
/// together print it once between them.
fn print_notice(log: Log) {
    let Some(_lock) = Lock::try_take() else {
        return;
    };
    let mut state = state::read();
    let Some(notice) = state.notice.take() else {
        return;
    };
    if notice.warning {
        log.failed(format!("update: {}", notice.text));
    } else {
        log.say(format!("update: {}", notice.text));
    }
    let _ = state::write(&state);
}

/// Install a staged update if it is for this binary, newer than it, and still
/// the bytes that were verified; then run the caller's command on it.
fn apply_staged(target: &Target, log: Log) {
    let Some(lock) = Lock::try_take() else {
        return;
    };
    let mut state = state::read();
    let record = state
        .staged
        .clone()
        .filter(|staged| staged.target == target.path);
    let staged_path = target.staged_path();
    let Some(record) = record else {
        // A file at the staged path that no check recorded is not trusted,
        // whoever put it there.
        if std::fs::symlink_metadata(&staged_path).is_ok() {
            let _ = std::fs::remove_file(&staged_path);
            log.note("update: removed a staged file no update check recorded");
        }
        return;
    };
    state.staged = None;
    if std::fs::symlink_metadata(&staged_path).is_err() {
        // Recorded, but gone: installed by another run, or cleaned up.
        let _ = state::write(&state);
        return;
    }
    let newer = Version::parse(&record.version)
        .zip(Version::current())
        .is_some_and(|(staged, current)| staged > current);
    let digest = archive::parse_hex(&record.sha256);
    let outcome = match (newer, digest) {
        (true, Some(digest)) => install::apply(target, &digest),
        _ => {
            let _ = std::fs::remove_file(&staged_path);
            Applied::Refused(String::new())
        }
    };
    let _ = state::write(&state);
    drop(lock);
    match outcome {
        Applied::Installed => {
            log.say(format!(
                "update: spider-agent {} replaced {}",
                record.version,
                env!("CARGO_PKG_VERSION")
            ));
            #[cfg(unix)]
            {
                let error = install::restart(target);
                log.failed(format!(
                    "update: could not start the new version ({error}). This run continues on {}.",
                    env!("CARGO_PKG_VERSION")
                ));
            }
        }
        Applied::Refused(reason) if !reason.is_empty() => {
            log.failed(format!(
                "update: {reason}, so it was deleted and nothing was installed."
            ));
        }
        Applied::Refused(_) => {}
        Applied::Failed(reason) => log.failed(format!("update: {reason}")),
    }
}

/// Stamp the check time and start the child. The stamp goes first and under
/// the lock, so runs that start together start one child between them, and a
/// child that fails still waits out the interval before the next one.
fn start_background_check(target: &Target) {
    let Some(_lock) = Lock::try_take() else {
        return;
    };
    let mut state = state::read();
    let now = state::now();
    if !state::due(state.last_check, now, CHECK_INTERVAL) {
        return;
    }
    state.last_check = now;
    state.outcome = None;
    // No state file means no throttle, and a check on every run. Rather than
    // that, a machine that cannot keep the file does not check.
    if state::write(&state).is_err() {
        return;
    }
    let _ = install::spawn_background_check(target);
}

/// `spider-agent update`.
pub async fn command(global: &Global, log: Log) -> Run<Code> {
    if in_background() {
        background().await;
        return Ok(Code::Ok);
    }
    if opted_out(global) {
        return Err(Failure::usage(format!(
            "self update is turned off by {OPT_OUT_ENV} or --no-update"
        )));
    }
    let Some(settings) = settings() else {
        return Err(Failure::usage(
            "a debug build does not update itself. Build with --release, or install a release.",
        ));
    };
    let Some(triple) = triple() else {
        return Err(Failure::new(
            Code::Failed,
            format!(
                "no release is built for this platform. {}",
                manual(RELEASES)
            ),
        ));
    };
    let Some(current) = Version::current() else {
        return Err(Failure::new(
            Code::Failed,
            "this binary's version cannot be read",
        ));
    };
    let failure =
        |problem: Problem| Failure::new(problem.code(), format!("update: {}", problem.message()));
    let target = Target::current().map_err(failure)?;
    let Some(_lock) = Lock::try_take() else {
        return Err(Failure::new(
            Code::Failed,
            "update: another spider-agent is updating right now. Try again in a minute.",
        ));
    };
    let releases = Releases::new(&settings).map_err(failure)?;
    let latest = releases.latest().await.map_err(failure)?;
    let mut state = state::read();
    state.last_check = state::now();
    if latest.version <= current {
        let _ = state::write(&state);
        log.say(format!(
            "update: spider-agent {current} is the newest release"
        ));
        return Ok(Code::Ok);
    }
    if let Some(reason) = install::not_ours(&target, &manual(&releases.page(&latest.tag))) {
        let _ = state::write(&state);
        return Err(Failure::new(
            Code::Output,
            format!("update: spider-agent {} is out. {reason}", latest.version),
        ));
    }
    let bytes = release::fetch_verified(&releases, &settings.keys, &latest, triple)
        .await
        .map_err(failure)?;
    let version = latest.version.to_string();
    install::install(&target, &bytes, &version)
        .await
        .map_err(failure)?;
    // Whatever was staged is older than or equal to what is now installed.
    if state
        .staged
        .as_ref()
        .is_some_and(|staged| staged.target == target.path)
    {
        state.staged = None;
        let _ = std::fs::remove_file(target.staged_path());
    }
    let _ = state::write(&state);
    log.say(format!(
        "update: installed spider-agent {version} over {current} at {}",
        target.path.display()
    ));
    Ok(Code::Ok)
}

/// The detached child. Nobody reads its output, so what it has to say goes
/// into the state file for the next run.
async fn background() {
    let Some(_lock) = Lock::try_take() else {
        return;
    };
    let mut state = state::read();
    let outcome = check_and_stage(&mut state).await;
    state.outcome = Some(outcome);
    let _ = state::write(&state);
}

async fn check_and_stage(state: &mut State) -> String {
    let (Some(settings), Some(triple), Some(current)) = (settings(), triple(), Version::current())
    else {
        return "skipped: this build does not update itself".to_string();
    };
    let target = match Target::current() {
        Ok(target) => target,
        Err(problem) => return format!("skipped: {}", problem.message()),
    };
    let releases = match Releases::new(&settings) {
        Ok(releases) => releases,
        Err(problem) => return format!("skipped: {}", problem.message()),
    };
    // A rate limit, an outage or a missing release is not worth a line on
    // anyone's terminal. The next check is a day away either way.
    let latest: Release = match releases.latest().await {
        Ok(latest) => latest,
        Err(problem) => return format!("skipped: {}", problem.message()),
    };
    if latest.version <= current {
        return "up to date".to_string();
    }
    let version = latest.version.to_string();
    let already = state
        .staged
        .as_ref()
        .is_some_and(|staged| staged.target == target.path && staged.version == version)
        && target.staged_path().is_file();
    if already {
        return "staged".to_string();
    }
    if let Some(reason) = install::not_ours(&target, &manual(&releases.page(&latest.tag))) {
        if state.told.as_deref() != Some(version.as_str()) {
            state.notice = Some(Notice {
                text: format!("spider-agent {version} is out. {reason}"),
                warning: false,
            });
            state.told = Some(version);
        }
        return "skipped: not ours to replace".to_string();
    }
    let bytes = match release::fetch_verified(&releases, &settings.keys, &latest, triple).await {
        Ok(bytes) => bytes,
        Err(problem @ (Problem::Mismatch(_) | Problem::Broken(_))) => {
            state.notice = Some(Notice {
                text: problem.message(),
                warning: true,
            });
            return format!("refused: {}", problem.message());
        }
        Err(problem) => return format!("skipped: {}", problem.message()),
    };
    match install::stage(&target, &bytes, &version).await {
        Ok(digest) => {
            state.staged = Some(Staged {
                version,
                sha256: archive::hex(&digest),
                target: target.path,
            });
            "staged".to_string()
        }
        Err(problem @ Problem::Broken(_)) => {
            state.notice = Some(Notice {
                text: format!("{}. Nothing was installed.", problem.message()),
                warning: true,
            });
            format!("refused: {}", problem.message())
        }
        Err(problem) => format!("skipped: {}", problem.message()),
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use super::*;

    #[test]
    fn versions_compare_as_numbers_and_refuse_anything_else() {
        let v = |text| Version::parse(text).unwrap();
        assert!(v("0.10.0") > v("0.9.9"));
        assert!(v("1.0.0") > v("0.99.99"));
        assert_eq!(v("0.4.0"), v("0.4.0"));
        for bad in [
            "0.4",
            "0.4.0.a",
            "0.4.0-rc.1",
            "v0.4.0",
            "",
            "0..1",
            "+1.2.3",
        ] {
            assert!(Version::parse(bad).is_none(), "{bad}");
        }
        assert!(Version::current().is_some());
    }
}