Skip to main content

systemprompt_cli/commands/cloud/doctor/
mod.rs

1//! `cloud doctor`: pre-deploy preflight for runtime prerequisites.
2//!
3//! Validates the things that otherwise only surface as a post-deploy 500 — a
4//! valid profile (incl. `governance.authz`), a provisionable signing key,
5//! `secrets.json` with the required keys and provider credentials, a
6//! `trusted_proxies` set that covers the Fly peer range — and probes
7//! database/hook reachability. The preflight runs automatically before
8//! `cloud deploy` builds an image, and is exposed standalone (`cloud doctor`)
9//! so an operator can check a profile without deploying.
10//!
11//! Copyright (c) systemprompt.io — Business Source License 1.1.
12//! See <https://systemprompt.io> for licensing details.
13
14mod checks;
15pub mod distributed;
16
17pub(in crate::commands::cloud) use checks::resolve_signing_key_path;
18pub use checks::{
19    check_extension_configs, check_profile_valid, check_provider_secrets, check_proxy_topology,
20    check_required_secrets, check_signing_key,
21};
22
23use std::collections::HashMap;
24use std::path::{Path, PathBuf};
25
26use anyhow::{Result, anyhow, bail};
27use systemprompt_cloud::ProfilePath;
28use systemprompt_loader::ConfigLoader;
29use systemprompt_logging::CliService;
30use systemprompt_models::Profile;
31
32use super::deploy::resolve_profile;
33use crate::cli_settings::CliConfig;
34use crate::interactive::Prompter;
35use systemprompt_cloud::secrets_env::load_secrets_json;
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub enum CheckStatus {
39    Pass,
40    Warn,
41    Fail,
42}
43
44#[derive(Debug)]
45pub struct CheckResult {
46    pub name: &'static str,
47    pub status: CheckStatus,
48    pub detail: String,
49}
50
51pub(in crate::commands::cloud) struct DoctorReport {
52    checks: Vec<CheckResult>,
53}
54
55impl DoctorReport {
56    pub(in crate::commands::cloud) fn has_blocking(&self) -> bool {
57        self.checks.iter().any(|c| c.status == CheckStatus::Fail)
58    }
59
60    pub(in crate::commands::cloud) fn render(&self) {
61        CliService::section("Deploy preflight");
62        for check in &self.checks {
63            let line = format!("{}: {}", check.name, check.detail);
64            match check.status {
65                CheckStatus::Pass => CliService::success(&line),
66                CheckStatus::Warn => CliService::warning(&line),
67                CheckStatus::Fail => CliService::error(&line),
68            }
69        }
70    }
71}
72
73pub(in crate::commands::cloud) async fn run(
74    profile: &Profile,
75    profile_dir: &Path,
76    distributed: bool,
77) -> DoctorReport {
78    let mut checks = vec![check_profile_valid(profile)];
79
80    let secrets_path = ProfilePath::Secrets.resolve(profile_dir);
81    let secrets = load_secrets_json(&secrets_path).unwrap_or_else(|_| {
82        checks.push(CheckResult::fail(
83            "secrets-file",
84            format!(
85                "secrets.json not found or unreadable at {}",
86                secrets_path.display()
87            ),
88        ));
89        HashMap::new()
90    });
91
92    checks.push(check_required_secrets(&secrets));
93    checks.push(check_signing_key(profile, profile_dir, &secrets));
94    // Why: the catalog lives in the services tree the profile points at, which
95    // for a cloud profile exists on the host rather than here — so an
96    // unreadable tree is a warning that names the path, not a failed check.
97    let services_root = PathBuf::from(profile.paths.config());
98    match ConfigLoader::load_from_path(&services_root) {
99        Ok(services) => checks.push(check_provider_secrets(&services.providers, &secrets)),
100        Err(err) => checks.push(CheckResult::warn(
101            "providers",
102            format!(
103                "services config at {} could not be loaded, so provider credentials were not \
104                 checked: {err}",
105                services_root.display()
106            ),
107        )),
108    }
109    checks.push(check_extension_configs(profile));
110    checks.push(check_proxy_topology(profile));
111    checks.push(checks::check_governance_hook_url(profile));
112    checks.push(checks::check_database_reachable(&secrets).await);
113    if distributed {
114        checks.extend(distributed::run(profile, &secrets).await);
115    }
116
117    DoctorReport { checks }
118}
119
120pub(in crate::commands::cloud) async fn execute(
121    profile_name: Option<String>,
122    distributed: bool,
123    prompter: &dyn Prompter,
124    config: &CliConfig,
125) -> Result<()> {
126    let (profile, profile_path) = resolve_profile(prompter, profile_name.as_deref(), config)?;
127    let profile_dir = profile_path
128        .parent()
129        .ok_or_else(|| anyhow!("Invalid profile path"))?;
130
131    let report = run(&profile, profile_dir, distributed).await;
132    report.render();
133
134    if report.has_blocking() {
135        bail!("Deploy preflight failed — fix the items above before deploying.");
136    }
137    CliService::success("Deploy preflight passed");
138    Ok(())
139}