shipslip 0.14.0

Deploy Laravel apps over SSH with a preview, a deploy lock, and a receipt.
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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
use std::collections::BTreeMap;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;

use tokio::io::AsyncWriteExt;
use tokio::sync::mpsc::Sender;

use super::*;

/// A temp folder holding a Laravel-like git checkout and a `bin` with a
/// fake `php`, removed on drop.
struct Fixture {
    root: PathBuf,
}

impl Fixture {
    fn new() -> Self {
        static NEXT: AtomicUsize = AtomicUsize::new(0);
        let root = std::env::temp_dir().join(format!(
            "shipslip-connection-check-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        ));
        let _ = fs::remove_dir_all(&root);
        fs::create_dir_all(root.join("bin")).unwrap();
        let fixture = Self {
            root: fs::canonicalize(root).unwrap(),
        };
        fixture.sh("mkdir app && cd app && git init -q -b main \
             && echo '<?php' > artisan && mkdir -p storage/logs \
             && echo '[2026-11-13 10:00:00] production.ERROR: x' > storage/logs/laravel.log \
             && git add artisan && git -c user.email=t@t -c user.name=t commit -qm init");
        fixture.php("echo 'Laravel Framework 11.9.2'");
        fixture
    }

    fn app(&self) -> String {
        self.root.join("app").to_string_lossy().into_owned()
    }

    fn sh(&self, script: &str) {
        let status = Command::new("bash")
            .arg("-c")
            .arg(script)
            .current_dir(&self.root)
            .status()
            .unwrap();
        assert!(status.success(), "{script}");
    }

    /// The fake `php`: refuses anything but `artisan --version --no-ansi`.
    fn php(&self, body: &str) {
        let path = self.root.join("bin/php");
        fs::write(
            &path,
            format!(
                "#!/bin/bash\n[ \"$*\" = 'artisan --version --no-ansi' ] || {{ echo \"php $*\" >&2; exit 99; }}\n{body}\n"
            ),
        )
        .unwrap();
        fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap();
    }

    fn request(&self) -> Request {
        Request {
            alias: "web".into(),
            path: self.app(),
            branch: "main".into(),
            log_path: "storage/logs/laravel.log".into(),
            log_daily: false,
        }
    }

    fn ssh(&self) -> FakeSsh {
        FakeSsh {
            resolve: Ok(resolved()),
            connect: Ok(()),
            path: format!(
                "{}:{}",
                self.root.join("bin").display(),
                std::env::var("PATH").unwrap()
            ),
            lose_on: None,
            prefix: String::new(),
        }
    }
}

impl Drop for Fixture {
    fn drop(&mut self) {
        let _ = Command::new("chmod")
            .args(["-R", "u+rwx"])
            .arg(&self.root)
            .status();
        let _ = fs::remove_dir_all(&self.root);
    }
}

fn resolved() -> Resolved {
    Resolved {
        host_name: "203.0.113.7".into(),
        user: "forge".into(),
        port: 22,
    }
}

/// Resolves and connects as told; the connection runs each script in a
/// local `bash` with `path` as `PATH`, or drops on the `lose_on`th script.
struct FakeSsh {
    resolve: Result<Resolved, String>,
    connect: Result<(), TransportError>,
    path: String,
    lose_on: Option<usize>,
    /// Shell run before each probe script, standing in for a login profile.
    prefix: String,
}

impl Ssh for FakeSsh {
    type Connection = LocalBash;

    fn resolve(&self, alias: &str) -> Result<Resolved, ResolveError> {
        assert_eq!(alias, "web");
        self.resolve.clone().map_err(ResolveError::Failed)
    }

    async fn connect(&self, alias: &str) -> Result<LocalBash, TransportError> {
        assert_eq!(alias, "web");
        self.connect.clone()?;
        Ok(LocalBash {
            path: self.path.clone(),
            lose_on: self.lose_on,
            prefix: self.prefix.clone(),
            scripts: Mutex::default(),
        })
    }
}

struct LocalBash {
    path: String,
    lose_on: Option<usize>,
    prefix: String,
    scripts: Mutex<Vec<String>>,
}

impl Transport for LocalBash {
    async fn run(
        &self,
        script: &str,
        output: Sender<String>,
        _line_limit: usize,
    ) -> Result<i32, TransportError> {
        let count = {
            let mut scripts = self.scripts.lock().unwrap();
            scripts.push(script.to_string());
            scripts.len()
        };
        if self.lose_on == Some(count) {
            return Err(TransportError::ConnectionLost("broken pipe".into()));
        }
        let mut child = tokio::process::Command::new("/bin/bash")
            .arg("-s")
            .env("PATH", &self.path)
            .current_dir("/")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .unwrap();
        let mut stdin = child.stdin.take().unwrap();
        stdin
            .write_all(format!("{}\n{script}", self.prefix).as_bytes())
            .await
            .unwrap();
        drop(stdin);
        let out = child.wait_with_output().await.unwrap();
        for line in String::from_utf8_lossy(&out.stdout).lines() {
            output.send(line.to_string()).await.unwrap();
        }
        Ok(out.status.code().unwrap())
    }

    async fn reconnect(&self) -> Result<(), TransportError> {
        panic!("the connection check never reconnects")
    }
}

async fn check(ssh: &FakeSsh, request: &Request) -> Vec<(Probe, Outcome)> {
    let mut events = Vec::new();
    let results = run(ssh, request, |event| events.push(event)).await;
    let mut expected = Vec::new();
    for (probe, outcome) in &results {
        if *outcome != Outcome::NotRun {
            expected.push(CheckEvent::Started(*probe));
        }
        expected.push(CheckEvent::Finished(*probe, outcome.clone()));
    }
    assert_eq!(
        events, expected,
        "each probe starts, then finishes, in order"
    );
    assert_eq!(
        results.iter().map(|(probe, _)| *probe).collect::<Vec<_>>(),
        Probe::ALL
    );
    results
}

fn outcomes(results: &[(Probe, Outcome)]) -> Vec<Outcome> {
    results.iter().map(|(_, outcome)| outcome.clone()).collect()
}

fn not_run(n: usize) -> Vec<Outcome> {
    vec![Outcome::NotRun; n]
}

#[tokio::test]
async fn every_probe_passes_on_a_reachable_checkout() {
    let fx = Fixture::new();
    let results = check(&fx.ssh(), &fx.request()).await;
    assert_eq!(
        outcomes(&results),
        [
            Outcome::Pass(Passed::Alias(resolved())),
            Outcome::Pass(Passed::Connected),
            Outcome::Pass(Passed::Checkout),
            Outcome::Pass(Passed::Branch),
            Outcome::Pass(Passed::Artisan("Laravel Framework 11.9.2".into())),
            Outcome::Pass(Passed::Log("storage/logs/laravel.log".into())),
        ]
    );
}

#[tokio::test]
async fn an_alias_that_does_not_resolve_runs_nothing_else() {
    let fx = Fixture::new();
    let ssh = FakeSsh {
        resolve: Err("Bad configuration option".into()),
        ..fx.ssh()
    };
    let results = check(&ssh, &fx.request()).await;
    let mut expected = vec![Outcome::Fail(Failed::Alias(
        "ssh -G failed: Bad configuration option".into(),
    ))];
    expected.extend(not_run(5));
    assert_eq!(outcomes(&results), expected);
}

#[tokio::test]
async fn a_refused_connection_is_classified_and_the_rest_is_not_run() {
    let fx = Fixture::new();
    for (message, failure) in [
        (
            "No ED25519 host key is known for server and you have requested strict checking.\n\
             Host key verification failed.",
            ConnectFailure::HostKeyUnknown,
        ),
        ("Host key verification failed.", ConnectFailure::HostKeyUnverified),
        (
            "@@@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @@@\nHost key verification failed.",
            ConnectFailure::HostKeyChanged,
        ),
        ("Permission denied (publickey).", ConnectFailure::KeyNotLoaded),
        ("Connection refused", ConnectFailure::Other),
    ] {
        let ssh = FakeSsh {
            connect: Err(TransportError::Connect(message.into())),
            ..fx.ssh()
        };
        let results = check(&ssh, &fx.request()).await;
        let mut expected = vec![
            Outcome::Pass(Passed::Alias(resolved())),
            Outcome::Fail(Failed::Connect {
                failure,
                message: message.into(),
            }),
        ];
        expected.extend(not_run(4));
        assert_eq!(outcomes(&results), expected, "{message}");
    }
}

#[tokio::test]
async fn a_connection_lost_mid_check_fails_that_probe_and_never_reconnects() {
    let fx = Fixture::new();
    let ssh = FakeSsh {
        lose_on: Some(2),
        ..fx.ssh()
    };
    let results = check(&ssh, &fx.request()).await;
    assert_eq!(
        outcomes(&results)[2..],
        [
            Outcome::Pass(Passed::Checkout),
            Outcome::Fail(Failed::Lost("broken pipe".into())),
            Outcome::NotRun,
            Outcome::NotRun,
        ]
    );
}

#[tokio::test]
async fn a_missing_remote_path_runs_no_probe_inside_it() {
    let fx = Fixture::new();
    let request = Request {
        path: format!("{}/gone", fx.app()),
        ..fx.request()
    };
    let results = check(&fx.ssh(), &request).await;
    let mut expected = vec![Outcome::Fail(Failed::NoFolder)];
    expected.extend(not_run(3));
    assert_eq!(outcomes(&results)[2..], expected);
}

#[tokio::test]
async fn a_folder_outside_git_skips_the_branch_but_checks_artisan_and_the_log() {
    let fx = Fixture::new();
    fx.sh("rm -rf app/.git");
    let results = check(&fx.ssh(), &fx.request()).await;
    let outcomes = outcomes(&results);
    assert!(
        matches!(&outcomes[2], Outcome::Fail(Failed::NotCheckout(messages)) if messages.iter().any(|m| m.contains("not a git repository"))),
        "{:?}",
        outcomes[2]
    );
    assert_eq!(
        outcomes[3..],
        [
            Outcome::NotRun,
            Outcome::Pass(Passed::Artisan("Laravel Framework 11.9.2".into())),
            Outcome::Pass(Passed::Log("storage/logs/laravel.log".into())),
        ]
    );
}

#[tokio::test]
async fn another_branch_or_a_detached_head_fails_the_branch_probe() {
    let fx = Fixture::new();
    fx.sh("git -C app checkout -q -b feature");
    let results = check(&fx.ssh(), &fx.request()).await;
    assert_eq!(
        results[3].1,
        Outcome::Fail(Failed::OtherBranch("feature".into()))
    );
    assert_eq!(
        results[4].1,
        Outcome::Pass(Passed::Artisan("Laravel Framework 11.9.2".into()))
    );

    fx.sh("git -C app checkout -q --detach");
    let results = check(&fx.ssh(), &fx.request()).await;
    assert_eq!(results[3].1, Outcome::Fail(Failed::Detached));
}

#[tokio::test]
async fn artisan_fails_without_the_file_without_php_or_on_an_error() {
    let fx = Fixture::new();
    fx.php("echo 'PHP Fatal error: vendor/autoload.php missing'; exit 255");
    let results = check(&fx.ssh(), &fx.request()).await;
    assert_eq!(
        results[4].1,
        Outcome::Fail(Failed::Artisan {
            code: 255,
            output: vec!["PHP Fatal error: vendor/autoload.php missing".into()],
        })
    );

    // A PATH with the tools the probes use, but no php.
    let tools = fx.root.join("tools");
    fs::create_dir(&tools).unwrap();
    for tool in ["git", "head", "sed"] {
        let found = Command::new("bash")
            .args(["-c", &format!("command -v {tool}")])
            .output()
            .unwrap();
        let found = String::from_utf8(found.stdout).unwrap();
        std::os::unix::fs::symlink(found.trim(), tools.join(tool)).unwrap();
    }
    let ssh = FakeSsh {
        path: tools.to_string_lossy().into_owned(),
        ..fx.ssh()
    };
    let results = check(&ssh, &fx.request()).await;
    assert_eq!(results[4].1, Outcome::Fail(Failed::NoPhp));
    assert_eq!(results[3].1, Outcome::Pass(Passed::Branch));

    fx.sh("rm app/artisan");
    let results = check(&fx.ssh(), &fx.request()).await;
    assert_eq!(results[4].1, Outcome::Fail(Failed::NoArtisan));
}

#[tokio::test]
async fn the_log_probe_names_a_missing_or_unreadable_file() {
    let fx = Fixture::new();
    let request = Request {
        log_path: "storage/logs/other.log".into(),
        ..fx.request()
    };
    let results = check(&fx.ssh(), &request).await;
    assert_eq!(
        results[5].1,
        Outcome::Fail(Failed::NoLog("storage/logs/other.log".into()))
    );

    // Root reads any file, so the mode alone proves nothing there.
    if unsafe { libc::geteuid() } != 0 {
        fx.sh("chmod 000 app/storage/logs/laravel.log");
        let results = check(&fx.ssh(), &fx.request()).await;
        assert_eq!(
            results[5].1,
            Outcome::Fail(Failed::LogUnreadable("storage/logs/laravel.log".into()))
        );
    }
}

#[tokio::test]
async fn a_daily_log_reads_the_newest_dated_file() {
    let fx = Fixture::new();
    let request = Request {
        log_path: "storage/logs/laravel".into(),
        log_daily: true,
        ..fx.request()
    };
    let results = check(&fx.ssh(), &request).await;
    assert_eq!(
        results[5].1,
        Outcome::Fail(Failed::NoLog("storage/logs/laravel-YYYY-MM-DD.log".into()))
    );

    fx.sh(
        "cd app/storage/logs && touch laravel-2026-11-12.log laravel-2026-11-13.log laravel-x.log",
    );
    let results = check(&fx.ssh(), &request).await;
    assert_eq!(
        results[5].1,
        Outcome::Pass(Passed::Log("storage/logs/laravel-2026-11-13.log".into()))
    );
}

#[tokio::test]
async fn quoting_keeps_a_hostile_path_and_log_as_data() {
    let fx = Fixture::new();
    let request = Request {
        path: format!("{}/x'; touch pwned; '", fx.app()),
        log_path: "$(touch pwned)".into(),
        ..fx.request()
    };
    check(&fx.ssh(), &request).await;
    let request = Request {
        log_path: "$(touch pwned)'\"".into(),
        ..fx.request()
    };
    check(&fx.ssh(), &request).await;
    assert!(!fx.root.join("pwned").exists());
    assert!(!fx.root.join("app/pwned").exists());
    assert!(!Path::new("/pwned").exists());
}

/// Every file's bytes, mode and modification time under `root`.
fn snapshot(root: &Path) -> BTreeMap<PathBuf, (Vec<u8>, u32, std::time::SystemTime)> {
    let mut files = BTreeMap::new();
    let mut dirs = vec![root.to_path_buf()];
    while let Some(dir) = dirs.pop() {
        for entry in fs::read_dir(&dir).unwrap() {
            let path = entry.unwrap().path();
            let meta = fs::symlink_metadata(&path).unwrap();
            if meta.is_dir() {
                dirs.push(path.clone());
            }
            let bytes = if meta.is_file() {
                fs::read(&path).unwrap()
            } else {
                Vec::new()
            };
            files.insert(
                path,
                (bytes, meta.permissions().mode(), meta.modified().unwrap()),
            );
        }
    }
    files
}

/// Covers Shipslip's commands only: the fake `php` doesn't boot Laravel,
/// which may refresh its own cache files (ADR 0005).
#[tokio::test]
async fn no_probe_writes_anything_on_the_server() {
    let fx = Fixture::new();
    fx.sh("cd app/storage/logs && touch laravel-2026-11-13.log");
    let before = snapshot(&fx.root);
    let daily = Request {
        log_path: "storage/logs/laravel".into(),
        log_daily: true,
        ..fx.request()
    };
    for request in [fx.request(), daily] {
        let results = check(&fx.ssh(), &request).await;
        assert!(
            results
                .iter()
                .all(|(_, outcome)| matches!(outcome, Outcome::Pass(_))),
            "{results:?}"
        );
    }
    assert_eq!(snapshot(&fx.root), before);

    for probe in [Probe::Checkout, Probe::Branch, Probe::Artisan, Probe::Log] {
        let text = script(probe, &fx.request());
        for writer in [
            "git fetch",
            "git pull",
            "git checkout",
            "git reset",
            "git status",
            "git gc",
            "rm ",
            "mv ",
            "cp ",
            "touch ",
            "mkdir",
            "tee ",
            "chmod",
            "sudo",
            "artisan down",
            "artisan up",
            "artisan migrate",
            "artisan optimize",
            "artisan cache",
        ] {
            assert!(!text.contains(writer), "{probe:?} runs `{writer}`");
        }
        assert!(
            !text.lines().any(|line| line.contains('>')
                && !line.contains(">/dev/null")
                && !line.contains("2>&1")
                && !line.contains(">&2")),
            "{probe:?} redirects into a file:\n{text}"
        );
    }
}

#[tokio::test]
async fn a_probe_that_exits_non_zero_is_unexpected_with_its_last_lines() {
    let fx = Fixture::new();
    let ssh = FakeSsh {
        prefix: "echo 'profile: broken'; exit 3".into(),
        ..fx.ssh()
    };
    let results = check(&ssh, &fx.request()).await;
    let unexpected = Outcome::Fail(Failed::Unexpected {
        code: 3,
        output: vec!["profile: broken".into()],
    });
    assert_eq!(
        outcomes(&results)[2..],
        [
            unexpected.clone(),
            Outcome::NotRun,
            unexpected.clone(),
            unexpected
        ]
    );
}

#[tokio::test]
async fn a_probe_that_prints_none_of_its_answers_is_unexpected() {
    let fx = Fixture::new();
    let ssh = FakeSsh {
        prefix: "exec >/dev/null".into(),
        ..fx.ssh()
    };
    let results = check(&ssh, &fx.request()).await;
    let unexpected = Outcome::Fail(Failed::Unexpected {
        code: 0,
        output: Vec::new(),
    });
    assert_eq!(
        outcomes(&results)[2..],
        [
            Outcome::Fail(Failed::NotCheckout(Vec::new())),
            Outcome::NotRun,
            unexpected.clone(),
            unexpected.clone()
        ]
    );

    // Only the branch probe goes quiet.
    let ssh = FakeSsh {
        prefix: r#"echo() { case "$1" in @branch*|@detached) ;; *) builtin echo "$@" ;; esac; }"#
            .into(),
        ..fx.ssh()
    };
    let results = check(&ssh, &fx.request()).await;
    assert_eq!(results[2].1, Outcome::Pass(Passed::Checkout));
    assert_eq!(results[3].1, unexpected);
}