smbcloud-cli 0.7.0

smbCloud command line interface.
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
use {
    crate::{
        cli::CommandResult,
        client,
        deploy::known_hosts,
        ui::{fail_message, fail_symbol, succeed_symbol},
    },
    anyhow::{anyhow, Result},
    chrono::Utc,
    smbcloud_auth::me::me,
    smbcloud_model::project::{DeploymentPayload, DeploymentStatus},
    smbcloud_network::environment::Environment,
    smbcloud_networking_project::{
        crud_project_deployment_create::create_deployment, crud_project_deployment_update::update,
    },
    smbcloud_utils::config::Config,
    spinners::{Spinner, Spinners},
    std::{
        fs,
        io::Write,
        path::{Path, PathBuf},
        process::{Command, Output, Stdio},
    },
    tempfile::NamedTempFile,
    toml::Value,
};

const DEFAULT_RUST_TARGET: &str = "x86_64-unknown-linux-gnu";

/// Deploys a Rust service by cross-compiling a Linux binary locally, uploading
/// only the executable, and restarting it on the remote host.
///
/// Required config fields:
///   - `kind = "rust"`
///   - `path`        — remote app directory on the server
///
/// Optional config fields:
///   - `source`      — local crate directory (defaults to current directory)
///   - `binary_name` — binary filename to upload; falls back to Cargo package name
///   - `rust_target` — local cross-compilation target triple; defaults to `x86_64-unknown-linux-gnu`
///   - `process_manager` — `"nohup"` (default) or `"systemd"`; systemd restarts
///     `<binary_name>.service` with `systemctl --user` (see `resolve_supervisor`)
///   - `port`, `health_path` — when both are set, the deploy polls
///     `http://127.0.0.1:<port><health_path>` after the restart
///
/// The binary is uploaded as `<binary>.new`, the live one is kept as
/// `<binary>.previous`, and a failed check restores it.
pub async fn process_deploy_rust(env: Environment, config: Config) -> Result<CommandResult> {
    let deploy_start = std::time::Instant::now();

    let source = config.project.source.as_deref().unwrap_or(".");
    let remote_path = config.project.path.as_deref().ok_or_else(|| {
        anyhow!(fail_message(
            "path not set in .smb/config.toml (e.g. path = \"apps/rest-api/my-rust-app\")"
        ))
    })?;

    let source_dir = Path::new(source);
    if !source_dir.exists() {
        return Err(anyhow!(fail_message(&format!(
            "Source path '{}' does not exist. Check the 'source' field in .smb/config.toml.",
            source
        ))));
    }

    let supervisor = resolve_supervisor(config.project.process_manager.as_deref())?;
    let health_url =
        resolve_health_url(config.project.port, config.project.health_path.as_deref())?;
    let binary_name = resolve_binary_name(&config, source_dir)?;
    let rust_target = config
        .project
        .rust_target
        .as_deref()
        .unwrap_or(DEFAULT_RUST_TARGET);

    // Header
    println!();
    println!("  {}", console::style(&config.name).white().bold());
    println!();

    let binary_path = build_local_binary(source, rust_target, &binary_name)?;

    let access_token = crate::token::get_smb_token::get_smb_token(env)?;
    let user = me(env, client(), &access_token).await?;
    let runner = config.project.runner;
    let rsync_host = runner.rsync_host();

    let deploy_ref = git2::Repository::discover(source)
        .ok()
        .and_then(|repo| {
            let head = repo.head().ok()?;
            let commit = head.peel_to_commit().ok()?;
            Some(commit.id().to_string())
        })
        .unwrap_or_else(|| Utc::now().format("%Y%m%dT%H%M%SZ").to_string());
    let created_deployment = create_deployment(
        env,
        client(),
        &access_token,
        config.project.id,
        DeploymentPayload {
            commit_hash: deploy_ref.clone(),
            status: DeploymentStatus::Started,
            frontend_app_id: config.project.frontend_app_id.clone(),
        },
    )
    .await
    .ok();

    let home = dirs::home_dir().ok_or_else(|| anyhow!("Could not determine home directory"))?;
    let identity_file = home.join(".ssh").join(format!("id_{}@smbcloud", user.id));
    let identity_file_str = identity_file.to_string_lossy().into_owned();

    let mut known_hosts_file = NamedTempFile::new()
        .map_err(|error| anyhow!("Failed to create temp known_hosts file: {}", error))?;
    writeln!(known_hosts_file, "{}", known_hosts::for_host(&rsync_host))
        .map_err(|error| anyhow!("Failed to write known_hosts: {}", error))?;

    let mut prepare_spinner = Spinner::new(
        Spinners::SimpleDotsScrolling,
        format!(
            "  {} {}",
            console::style("◼").cyan(),
            console::style("Preparing server…").dim()
        ),
    );

    let prepare_script = build_remote_prepare_script(remote_path);
    let prepare_output = run_remote_script(
        &identity_file_str,
        &known_hosts_file,
        &rsync_host,
        &prepare_script,
    )
    .map_err(|error| {
        anyhow!(fail_message(&format!(
            "Failed to prepare remote directory: {}",
            error
        )))
    })?;

    if !prepare_output.status.success() {
        prepare_spinner.stop_and_persist(
            &fail_symbol(),
            format!(
                "  {} {}",
                console::style("✘").red(),
                fail_message("Server prepare failed")
            ),
        );
        print_output_details(&prepare_output);
        drop(known_hosts_file);
        mark_failed(
            &deploy_ref,
            &created_deployment,
            &config,
            env,
            &access_token,
        )
        .await;
        return Err(anyhow!(fail_message(&format!(
            "Failed to prepare remote directory '{}'",
            remote_path
        ))));
    }

    let ssh_command = build_ssh_command(&identity_file_str, &known_hosts_file);
    let remote_with_slash = if remote_path.ends_with('/') {
        remote_path.to_owned()
    } else {
        format!("{}/", remote_path)
    };
    let destination = format!(
        "git@{}:{}{}.new",
        rsync_host, remote_with_slash, binary_name
    );
    let binary_path_str = binary_path.to_string_lossy().into_owned();

    let binary_size = fs::metadata(&binary_path).map(|m| m.len()).unwrap_or(0);
    let upload_size = if binary_size > 1_000_000 {
        format!("{} MB", binary_size / 1_000_000)
    } else {
        format!("{} KB", binary_size / 1_000)
    };

    prepare_spinner.stop_and_persist(
        &format!("  {}", console::style("\u{25fc}").cyan()),
        format!(
            "{}    {} \u{2192} {} ({})",
            console::style("Upload").white().bold(),
            console::style(&binary_name).dim(),
            console::style(format!("{}:{}", rsync_host, remote_with_slash)).dim(),
            console::style(&upload_size).dim(),
        ),
    );

    let mut upload_spinner = Spinner::new(
        Spinners::Hamburger,
        format!(
            "  {} {}",
            console::style("\u{25fc}").cyan(),
            console::style("Uploading\u{2026}").dim()
        ),
    );

    let upload_output = Command::new("rsync")
        .args(["-az", "-e", &ssh_command, &binary_path_str, &destination])
        .output()
        .map_err(|error| anyhow!(fail_message(&format!("Failed to launch rsync: {}", error))))?;

    if !upload_output.status.success() {
        upload_spinner.stop_and_persist(
            &fail_symbol(),
            format!(
                "  {} {}",
                console::style("✘").red(),
                fail_message("Upload failed")
            ),
        );
        print_output_details(&upload_output);
        drop(known_hosts_file);
        mark_failed(
            &deploy_ref,
            &created_deployment,
            &config,
            env,
            &access_token,
        )
        .await;
        return Err(anyhow!(fail_message(&format!(
            "Failed to upload '{}' to '{}'",
            binary_name, remote_path
        ))));
    }

    upload_spinner.stop_and_persist(
        &format!("  {}", console::style("\u{25fc}").cyan()),
        format!(
            "{}    Starting {}\u{2026}",
            console::style("Launch").white().bold(),
            console::style(&binary_name).dim(),
        ),
    );

    let deploy_script =
        build_remote_start_script(remote_path, &binary_name, supervisor, health_url.as_deref());
    let ssh_output = run_remote_script(
        &identity_file_str,
        &known_hosts_file,
        &rsync_host,
        &deploy_script,
    )
    .map_err(|error| anyhow!(fail_message(&format!("Failed to spawn SSH: {}", error))))?;

    drop(known_hosts_file);

    if !ssh_output.status.success() {
        println!(
            "  {} {}",
            console::style("\u{2718}").red(),
            fail_message("Launch failed")
        );
        print_output_details(&ssh_output);
        if let Some(line) = String::from_utf8_lossy(&ssh_output.stdout)
            .lines()
            .find(|line| line.starts_with("Rolled back"))
        {
            eprintln!("{}", line);
        }
        mark_failed(
            &deploy_ref,
            &created_deployment,
            &config,
            env,
            &access_token,
        )
        .await;
        return Err(anyhow!(fail_message(&format!(
            "SSH deploy script exited with status {}",
            ssh_output.status
        ))));
    }

    let stdout_text = String::from_utf8_lossy(&ssh_output.stdout);
    let pid_line = stdout_text
        .lines()
        .find(|line| line.starts_with("Started"))
        .unwrap_or("Started");

    println!(
        "  {} {}    {}",
        console::style("\u{25fc}").cyan(),
        console::style("Launch").white().bold(),
        console::style(pid_line).dim(),
    );

    if let Some(ref deployment) = created_deployment {
        let _ = update(
            env,
            client(),
            access_token,
            config.project.id,
            deployment.id,
            DeploymentPayload {
                commit_hash: deploy_ref,
                status: DeploymentStatus::Done,
                frontend_app_id: config.project.frontend_app_id.clone(),
            },
        )
        .await;
    }

    let elapsed = deploy_start.elapsed().as_secs();
    let duration = if elapsed >= 60 {
        format!("{}m {}s", elapsed / 60, elapsed % 60)
    } else {
        format!("{}s", elapsed)
    };

    println!();

    Ok(CommandResult {
        spinner: Spinner::new(Spinners::Hamburger, String::new()),
        symbol: succeed_symbol(),
        msg: format!(
            "Deployed {} in {}",
            console::style(&config.name).white().bold(),
            console::style(&duration).cyan(),
        ),
    })
}

fn resolve_binary_name(config: &Config, source_dir: &Path) -> Result<String> {
    if let Some(binary_name) = config.project.binary_name.as_deref() {
        let binary_name = binary_name.trim();
        if binary_name.is_empty() {
            return Err(anyhow!(fail_message(
                "binary_name in .smb/config.toml cannot be empty."
            )));
        }
        return Ok(binary_name.to_owned());
    }

    let cargo_toml_path = source_dir.join("Cargo.toml");
    if !cargo_toml_path.exists() {
        return Err(anyhow!(fail_message(&format!(
            "Cargo.toml not found at '{}'. Set 'source' to the crate directory or add 'binary_name' to .smb/config.toml.",
            cargo_toml_path.display()
        ))));
    }

    let cargo_toml = fs::read_to_string(&cargo_toml_path).map_err(|error| {
        anyhow!(fail_message(&format!(
            "Failed to read '{}': {}",
            cargo_toml_path.display(),
            error
        )))
    })?;

    let manifest: Value = toml::from_str(&cargo_toml).map_err(|error| {
        anyhow!(fail_message(&format!(
            "Failed to parse '{}': {}",
            cargo_toml_path.display(),
            error
        )))
    })?;

    let package_name = manifest
        .get("package")
        .and_then(Value::as_table)
        .and_then(|package| package.get("name"))
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|name| !name.is_empty())
        .ok_or_else(|| {
            anyhow!(fail_message(
                "Could not determine Rust binary name from Cargo.toml. Add 'binary_name' to .smb/config.toml."
            ))
        })?;

    Ok(package_name.to_owned())
}

fn build_local_binary(source: &str, rust_target: &str, binary_name: &str) -> Result<PathBuf> {
    let mut build_command;
    let build_tool;
    let is_native = native_linux_target() == Some(rust_target);

    if !is_native && command_exists("cargo-zigbuild") {
        build_tool = "cargo zigbuild";
        build_command = Command::new("cargo");
        build_command.args([
            "zigbuild",
            "--release",
            "--target",
            rust_target,
            "--bin",
            binary_name,
        ]);
    } else if !is_native && command_exists("cross") {
        build_tool = "cross";
        build_command = Command::new("cross");
        build_command.args([
            "build",
            "--release",
            "--target",
            rust_target,
            "--bin",
            binary_name,
        ]);
    } else if is_native {
        build_tool = "cargo";
        build_command = Command::new("cargo");
        build_command.args([
            "build",
            "--release",
            "--target",
            rust_target,
            "--bin",
            binary_name,
        ]);
    } else {
        return Err(anyhow!(fail_message(&format!(
            "Cross-compilation tooling is required to build target '{}'. Install `cargo-zigbuild` (recommended) or `cross`, or run deploy from a matching Linux host.",
            rust_target
        ))));
    }

    let status = build_command
        .current_dir(source)
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()
        .map_err(|error| {
            anyhow!(fail_message(&format!(
                "Failed to spawn '{} build' for target '{}': {}",
                build_tool, rust_target, error
            )))
        })?;

    if !status.success() {
        return Err(anyhow!(fail_message(&format!(
            "'{} build --release --target {} --bin {}' exited with status {}",
            build_tool, rust_target, binary_name, status
        ))));
    }

    let binary_path = Path::new(source)
        .join("target")
        .join(rust_target)
        .join("release")
        .join(binary_name);

    if !binary_path.exists() {
        return Err(anyhow!(fail_message(&format!(
            "Built binary not found at '{}'.",
            binary_path.display()
        ))));
    }

    let binary_size = fs::metadata(&binary_path).map(|m| m.len()).unwrap_or(0);
    let size_display = if binary_size > 1_000_000 {
        format!("{} MB", binary_size / 1_000_000)
    } else {
        format!("{} KB", binary_size / 1_000)
    };

    println!(
        "  {} {}    {} \u{2192} {} ({})",
        console::style("\u{25fc}").cyan(),
        console::style("Build").white().bold(),
        console::style(binary_name).dim(),
        console::style(rust_target).dim(),
        console::style(&size_display).dim(),
    );

    Ok(binary_path)
}

fn build_remote_prepare_script(remote_path: &str) -> String {
    format!(
        r#"set -e
APP_PATH={remote_path}

case "$APP_PATH" in
    /*) ;;
    *) APP_PATH="$HOME/$APP_PATH" ;;
esac

mkdir -p "$APP_PATH"
echo "Prepared $APP_PATH"
"#,
        remote_path = shell_single_quote(remote_path),
    )
}

/// How the service is stopped and started on the server.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Supervisor {
    /// Kill the old process, start the new one with `nohup`.
    Nohup,
    /// `systemctl --user restart <binary_name>.service`.
    Systemd,
}

/// Maps `process_manager` to a [`Supervisor`] (matched case-insensitively).
/// `None`, `""` and `"nohup"` keep the original behaviour. Any other value is a
/// hard error: a typo like `system` must not start a nohup process next to a
/// systemd-owned one that already holds the port.
fn resolve_supervisor(process_manager: Option<&str>) -> Result<Supervisor> {
    match process_manager.map(str::trim) {
        None | Some("") => Ok(Supervisor::Nohup),
        Some(m) if m.eq_ignore_ascii_case("nohup") => Ok(Supervisor::Nohup),
        Some(m) if m.eq_ignore_ascii_case("systemd") => Ok(Supervisor::Systemd),
        Some(other) => Err(anyhow!(fail_message(&format!(
            "Unknown process_manager '{other}' in .smb/config.toml. \
             Expected \"nohup\" (default) or \"systemd\"."
        )))),
    }
}

/// Builds the URL the remote script polls after the restart. `None` unless both
/// `port` and `health_path` are set. The path ends up in a shell script, so it
/// is limited to URL-path characters, and every `%` must start a valid
/// percent-escape.
fn resolve_health_url(port: Option<u16>, health_path: Option<&str>) -> Result<Option<String>> {
    let Some(path) = health_path.map(str::trim).filter(|path| !path.is_empty()) else {
        return Ok(None);
    };
    let bytes = path.as_bytes();
    let valid = path.starts_with('/')
        && path
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '/' | '-' | '_' | '.' | '~' | '%'))
        && bytes.iter().enumerate().all(|(i, &b)| {
            b != b'%'
                || bytes
                    .get(i + 1..i + 3)
                    .is_some_and(|hex| hex.iter().all(u8::is_ascii_hexdigit))
        });
    if !valid {
        return Err(anyhow!(fail_message(&format!(
            "Invalid health_path '{path}' in .smb/config.toml. \
             It must start with '/' and contain only letters, digits and / - _ . ~ %; \
             each % must be followed by two hex digits."
        ))));
    }
    Ok(port.map(|port| format!("http://127.0.0.1:{port}{path}")))
}

/// Remote script: swap the uploaded `<binary>.new` into place (keeping the live
/// one as `<binary>.previous`), restart, verify, and roll back on failure.
fn build_remote_start_script(
    remote_path: &str,
    binary_name: &str,
    supervisor: Supervisor,
    health_url: Option<&str>,
) -> String {
    let mut script = format!(
        r#"set -e
exec 2>&1
APP_PATH={remote_path}
PROCESS_NAME={binary_name}

case "$APP_PATH" in
    /*) ;;
    *) APP_PATH="$HOME/$APP_PATH" ;;
esac

if [ ! -d "$APP_PATH" ]; then
    echo "Error: $APP_PATH is not a directory."
    exit 1
fi

cd "$APP_PATH"

if [ ! -f "$PROCESS_NAME.new" ]; then
    echo "Error: $PROCESS_NAME.new does not exist in $APP_PATH."
    exit 1
fi

"#,
        remote_path = shell_single_quote(remote_path),
        binary_name = shell_single_quote(binary_name),
    );
    script.push_str(&restart_section(supervisor));
    script.push_str(&verify_section(supervisor, health_url));
    script.push_str(swap_section());
    script.push_str(
        r#"if restart_service && check_service; then
    started
    echo "Done."
    exit 0
fi

echo "Error: $PROCESS_NAME failed its check after the restart."
show_logs
if [ -f "$PROCESS_NAME.previous" ]; then
    echo "Rolling back to the previous binary..."
    cp -p "$PROCESS_NAME.previous" "$PROCESS_NAME.rollback"
    mv -f "$PROCESS_NAME.rollback" "$PROCESS_NAME"
    if restart_service && check_service; then
        echo "Rolled back to the previous $PROCESS_NAME, which is serving again."
    else
        echo "Rolled back to the previous $PROCESS_NAME, but it failed its check too."
    fi
else
    echo "No previous binary to roll back to."
fi
exit 1
"#,
    );
    script
}

/// Keeps the live binary as `.previous` and moves `.new` into place.
fn swap_section() -> &'static str {
    r#"if [ -f "$PROCESS_NAME" ]; then
    cp -p "$PROCESS_NAME" "$PROCESS_NAME.previous"
fi
chmod +x "$PROCESS_NAME.new"
mv -f "$PROCESS_NAME.new" "$PROCESS_NAME"

"#
}

/// Defines `restart_service`. The systemd variant also fails early, before the
/// swap, when the unit does not exist.
fn restart_section(supervisor: Supervisor) -> String {
    match supervisor {
        Supervisor::Nohup => r#"restart_service() {
    PID=$(pidof "$PROCESS_NAME" 2>/dev/null || true)
    if [ -n "$PID" ]; then
        echo "Stopping $PROCESS_NAME ($PID)..."
        kill "$PID" 2>/dev/null || true
        sleep 2
        if kill -0 "$PID" 2>/dev/null; then
            echo "Force-killing $PROCESS_NAME ($PID)..."
            kill -9 "$PID" 2>/dev/null || true
        fi
    fi
    echo "Starting $PROCESS_NAME..."
    nohup "./$PROCESS_NAME" >> "$APP_PATH/$PROCESS_NAME.log" 2>&1 &
}

"#
        .to_owned(),
        Supervisor::Systemd => r#"export XDG_RUNTIME_DIR="/run/user/$(id -u)"
UNIT="$PROCESS_NAME.service"
if ! systemctl --user cat "$UNIT" >/dev/null 2>&1; then
    echo "Error: systemd --user unit $UNIT not found on the server."
    echo "Create ~/.config/systemd/user/$UNIT and enable it (see the"
    echo "smbcloud-deploy-rust guide, 'Process manager: systemd --user'),"
    echo "or unset process_manager to use the default start. Then re-deploy."
    exit 1
fi

restart_service() {
    echo "Restarting $UNIT via systemd --user..."
    systemctl --user restart "$UNIT"
}

"#
        .to_owned(),
    }
}

/// Defines `check_service`, `started` and `show_logs`. The check polls the
/// health URL for up to 30 seconds when there is one, else looks for a live
/// process (`is-active` under systemd, `pidof` under nohup).
fn verify_section(supervisor: Supervisor, health_url: Option<&str>) -> String {
    let mut section = String::new();
    match health_url {
        Some(url) => {
            section.push_str(&format!("HEALTH_URL={}\n\n", shell_single_quote(url)));
            section.push_str(
                r#"check_service() {
    echo "Waiting for $HEALTH_URL..."
    i=0
    while [ "$i" -lt 30 ]; do
        if curl -fsS -o /dev/null --max-time 2 "$HEALTH_URL"; then
            return 0
        fi
        i=$((i + 1))
        sleep 1
    done
    return 1
}

"#,
            );
        }
        None => section.push_str(match supervisor {
            Supervisor::Systemd => {
                r#"check_service() {
    sleep 3
    systemctl --user is-active "$UNIT" >/dev/null 2>&1
}

"#
            }
            Supervisor::Nohup => {
                r#"check_service() {
    sleep 1
    [ -n "$(pidof "$PROCESS_NAME" 2>/dev/null || true)" ]
}

"#
            }
        }),
    }
    section.push_str(match supervisor {
        Supervisor::Systemd => {
            r#"started() {
    echo "Started $PROCESS_NAME via $UNIT"
}

show_logs() {
    journalctl --user -u "$UNIT" -n 20 --no-pager 2>&1 || true
}

"#
        }
        Supervisor::Nohup => {
            r#"started() {
    echo "Started $PROCESS_NAME as $(pidof "$PROCESS_NAME" 2>/dev/null || true)"
}

show_logs() {
    tail -n 20 "$APP_PATH/$PROCESS_NAME.log" 2>&1 || true
}

"#
        }
    });
    section
}

fn build_ssh_command(identity_file: &str, known_hosts_file: &NamedTempFile) -> String {
    format!(
        "ssh -i {} -o StrictHostKeyChecking=yes -o UserKnownHostsFile={} -o IdentitiesOnly=yes -o PasswordAuthentication=no -o BatchMode=yes",
        identity_file,
        known_hosts_file.path().display(),
    )
}

fn build_ssh_args<'a>(
    identity_file: &'a str,
    known_hosts_file: &'a NamedTempFile,
    rsync_host: &'a str,
) -> Vec<String> {
    vec![
        "-i".to_owned(),
        identity_file.to_owned(),
        "-o".to_owned(),
        "StrictHostKeyChecking=yes".to_owned(),
        "-o".to_owned(),
        format!("UserKnownHostsFile={}", known_hosts_file.path().display()),
        "-o".to_owned(),
        "IdentitiesOnly=yes".to_owned(),
        "-o".to_owned(),
        "PasswordAuthentication=no".to_owned(),
        "-o".to_owned(),
        "BatchMode=yes".to_owned(),
        format!("git@{}", rsync_host),
        "bash".to_owned(),
        "-s".to_owned(),
    ]
}

fn run_remote_script(
    identity_file: &str,
    known_hosts_file: &NamedTempFile,
    rsync_host: &str,
    script: &str,
) -> Result<Output> {
    let mut child = Command::new("ssh")
        .args(build_ssh_args(identity_file, known_hosts_file, rsync_host))
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .map_err(|error| anyhow!("Failed to spawn SSH: {}", error))?;

    if let Some(mut stdin) = child.stdin.take() {
        stdin
            .write_all(script.as_bytes())
            .map_err(|error| anyhow!("Failed to write deploy script to SSH stdin: {}", error))?;
    }

    child
        .wait_with_output()
        .map_err(|error| anyhow!("Failed to wait for SSH process: {}", error))
}

fn print_output_details(output: &Output) {
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let details = if !stderr.trim().is_empty() {
        stderr
    } else {
        stdout
    };

    if !details.trim().is_empty() {
        eprintln!("{}", details.trim());
    }
}

fn command_exists(command: &str) -> bool {
    Command::new(command).arg("--version").output().is_ok()
}

fn native_linux_target() -> Option<&'static str> {
    match (std::env::consts::OS, std::env::consts::ARCH) {
        ("linux", "x86_64") => Some("x86_64-unknown-linux-gnu"),
        ("linux", "aarch64") => Some("aarch64-unknown-linux-gnu"),
        _ => None,
    }
}

fn shell_single_quote(value: &str) -> String {
    format!("'{}'", value.replace('\'', "'\"'\"'"))
}

async fn mark_failed(
    deploy_ref: &str,
    created_deployment: &Option<smbcloud_model::project::Deployment>,
    config: &Config,
    env: Environment,
    access_token: &str,
) {
    if let Some(ref deployment) = created_deployment {
        let _ = update(
            env,
            client(),
            access_token.to_owned(),
            config.project.id,
            deployment.id,
            DeploymentPayload {
                commit_hash: deploy_ref.to_owned(),
                status: DeploymentStatus::Failed,
                frontend_app_id: config.project.frontend_app_id.clone(),
            },
        )
        .await;
    }
}

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

    fn script(supervisor: Supervisor, health_url: Option<&str>) -> String {
        build_remote_start_script("apps/my-app", "my-app", supervisor, health_url)
    }

    #[test]
    fn resolve_supervisor_defaults_to_nohup() {
        for pm in [
            None,
            Some(""),
            Some("nohup"),
            Some("NoHup"),
            Some(" nohup "),
        ] {
            assert_eq!(resolve_supervisor(pm).unwrap(), Supervisor::Nohup, "{pm:?}");
        }
    }

    #[test]
    fn resolve_supervisor_systemd_is_case_insensitive() {
        for pm in ["systemd", "SystemD", " systemd "] {
            assert_eq!(resolve_supervisor(Some(pm)).unwrap(), Supervisor::Systemd);
        }
    }

    #[test]
    fn resolve_supervisor_rejects_unknown_value() {
        for pm in ["system", "pm2", "supervisord"] {
            let msg = resolve_supervisor(Some(pm)).unwrap_err().to_string();
            assert!(msg.contains(pm), "names the offending value: {msg}");
            assert!(msg.contains("nohup") && msg.contains("systemd"), "{msg}");
        }
    }

    #[test]
    fn systemd_script_restarts_the_user_unit() {
        let s = script(Supervisor::Systemd, None);
        assert!(s.contains("systemctl --user restart \"$UNIT\""));
        assert!(s.contains("UNIT=\"$PROCESS_NAME.service\""));
        assert!(s.contains("XDG_RUNTIME_DIR=\"/run/user/$(id -u)\""));
        assert!(s.contains("systemctl --user is-active"));
        assert!(!s.contains("nohup"));
        assert!(!s.contains("kill"));
        assert!(!s.contains("sudo"));
    }

    #[test]
    fn nohup_script_keeps_the_pidof_flow() {
        let s = script(Supervisor::Nohup, None);
        assert!(s.contains("pidof"));
        assert!(s.contains("nohup \"./$PROCESS_NAME\""));
        assert!(!s.contains("systemctl"));
    }

    #[test]
    fn both_scripts_back_up_and_swap_before_restarting() {
        for supervisor in [Supervisor::Nohup, Supervisor::Systemd] {
            let s = script(supervisor, None);
            let backup = s
                .find("cp -p \"$PROCESS_NAME\" \"$PROCESS_NAME.previous\"")
                .unwrap();
            let swap = s
                .find("mv -f \"$PROCESS_NAME.new\" \"$PROCESS_NAME\"")
                .unwrap();
            let restart = s.find("if restart_service && check_service").unwrap();
            assert!(backup < swap && swap < restart, "{supervisor:?}");
            assert!(s.contains("Rolled back"), "{supervisor:?}");
        }
    }

    #[test]
    fn health_url_controls_the_curl_poll() {
        for supervisor in [Supervisor::Nohup, Supervisor::Systemd] {
            let with = script(supervisor, Some("http://127.0.0.1:8080/health"));
            assert!(with.contains("curl -fsS"));
            assert!(with.contains("HEALTH_URL='http://127.0.0.1:8080/health'"));
            assert!(!script(supervisor, None).contains("curl"));
        }
    }

    #[test]
    fn health_url_needs_both_port_and_path() {
        let url = resolve_health_url(Some(8080), Some("/health")).unwrap();
        assert_eq!(url.as_deref(), Some("http://127.0.0.1:8080/health"));
        assert_eq!(resolve_health_url(None, Some("/health")).unwrap(), None);
        assert_eq!(resolve_health_url(Some(8080), None).unwrap(), None);
        assert_eq!(resolve_health_url(Some(8080), Some("")).unwrap(), None);
    }

    #[test]
    fn health_path_validation_rejects_unsafe_values() {
        for path in [
            "health", "/a b", "/x;rm", "/$(id)", "/a'b", "/foo%ZZ", "/foo%", "/foo%2",
        ] {
            assert!(
                resolve_health_url(Some(8080), Some(path)).is_err(),
                "{path}"
            );
        }
    }

    #[test]
    fn health_path_accepts_valid_percent_escapes() {
        let url = resolve_health_url(Some(8080), Some("/a%20b")).unwrap();
        assert_eq!(url.as_deref(), Some("http://127.0.0.1:8080/a%20b"));
    }

    #[test]
    fn binary_name_with_single_quote_is_quoted() {
        let s = build_remote_start_script("apps/x", "a'b", Supervisor::Nohup, None);
        assert!(s.contains("PROCESS_NAME='a'\"'\"'b'"));
    }

    #[test]
    fn every_script_variant_is_valid_bash() {
        for supervisor in [Supervisor::Nohup, Supervisor::Systemd] {
            for health in [None, Some("http://127.0.0.1:8080/health")] {
                let mut child = Command::new("bash")
                    .args(["-n", "-s"])
                    .stdin(Stdio::piped())
                    .stdout(Stdio::piped())
                    .stderr(Stdio::piped())
                    .spawn()
                    .expect("bash is available");
                child
                    .stdin
                    .take()
                    .unwrap()
                    .write_all(script(supervisor, health).as_bytes())
                    .unwrap();
                let out = child.wait_with_output().unwrap();
                assert!(
                    out.status.success(),
                    "{supervisor:?} {health:?}: {}",
                    String::from_utf8_lossy(&out.stderr)
                );
            }
        }
    }
}