Skip to main content

systemprompt_cli/commands/cloud/deploy/
mod.rs

1//! `cloud deploy` command: prompts, preflight, and progress rendering.
2//!
3//! Resolves the active cloud profile and tenant, runs the `cloud doctor`
4//! preflight (deploys hard-block on a failing report), then hands the typed
5//! request to [`DeployOrchestrator`] in the [`pipeline`] module, which owns
6//! the image build/push, secret provisioning, and the deploy call. Rendering
7//! flows back through [`CliDeployProgress`].
8//!
9//! Deploys are stateless container rebuilds: runtime files created inside the
10//! previous container are not preserved. Use `systemprompt cloud backup`
11//! first to keep a copy.
12//!
13//! Copyright (c) systemprompt.io — Business Source License 1.1.
14//! See <https://systemprompt.io> for licensing details.
15
16pub mod pipeline;
17pub mod progress;
18mod select;
19
20pub(in crate::commands::cloud) use progress::CliDeployProgress;
21pub(in crate::commands::cloud) use select::resolve_profile;
22
23use anyhow::{Context, Result, anyhow, bail};
24use systemprompt_cloud::{CloudPath, ProfilePath, TenantStore, get_cloud_paths};
25use systemprompt_identifiers::TenantId;
26use systemprompt_logging::CliService;
27
28use pipeline::{DeployOptions, DeployOrchestrator, DeployRequest};
29
30use super::tenant::get_credentials;
31use crate::cli_settings::CliConfig;
32use crate::interactive::Prompter;
33use crate::shared::project::ProjectRoot;
34
35pub(super) struct DeployArgs {
36    pub skip_push: bool,
37    pub profile_name: Option<String>,
38    pub check: bool,
39}
40
41pub(super) async fn execute(
42    args: DeployArgs,
43    prompter: &dyn Prompter,
44    config: &CliConfig,
45) -> Result<()> {
46    CliService::section("systemprompt.io Cloud Deploy");
47
48    let (profile, profile_path) = resolve_profile(prompter, args.profile_name.as_deref(), config)?;
49
50    if profile.target != systemprompt_models::ProfileType::Cloud {
51        bail!(
52            "Cannot deploy a local profile. Create a cloud profile with: systemprompt cloud \
53             profile create <name>"
54        );
55    }
56
57    let profile_dir = profile_path
58        .parent()
59        .ok_or_else(|| anyhow!("Invalid profile path"))?;
60    let report = super::doctor::run(&profile, profile_dir).await;
61    report.render();
62    if report.has_blocking() {
63        bail!("Deploy preflight failed — fix the items above before deploying.");
64    }
65    if args.check {
66        CliService::success("Deploy preflight passed (--check; nothing deployed)");
67        return Ok(());
68    }
69
70    let target = resolve_deploy_target(&profile)?;
71    let project = ProjectRoot::discover().map_err(|e| anyhow!("{}", e))?;
72
73    let request = DeployRequest {
74        tenant_id: target.tenant_id,
75        tenant_name: target.tenant_name,
76        profile_name: profile.name.clone(),
77        project_root: project.as_path().to_path_buf(),
78        credentials: target.creds,
79        secrets_path: ProfilePath::Secrets.resolve(profile_dir),
80        signing_key_path: super::doctor::resolve_signing_key_path(&profile, profile_dir),
81        options: DeployOptions {
82            skip_push: args.skip_push,
83        },
84    };
85
86    let progress = CliDeployProgress::new();
87    DeployOrchestrator::new()
88        .deploy(&request, &progress)
89        .await?;
90
91    Ok(())
92}
93
94#[derive(Debug)]
95pub struct DeployTarget {
96    pub tenant_id: TenantId,
97    pub tenant_name: String,
98    pub hostname: Option<String>,
99    pub creds: systemprompt_cloud::CloudCredentials,
100}
101
102pub fn resolve_deploy_target(profile: &systemprompt_models::Profile) -> Result<DeployTarget> {
103    let cloud_config = profile
104        .cloud
105        .as_ref()
106        .ok_or_else(|| anyhow!("No cloud configuration in profile"))?;
107
108    let tenant_id = cloud_config
109        .tenant_id
110        .as_ref()
111        .map(TenantId::new)
112        .ok_or_else(|| anyhow!("No tenant configured. Run 'systemprompt cloud config'"))?;
113
114    let creds = get_credentials()?;
115    if creds.is_token_expired() {
116        bail!("Token expired. Run 'systemprompt cloud login' to refresh.");
117    }
118
119    let cloud_paths = get_cloud_paths();
120    let tenants_path = cloud_paths.resolve(CloudPath::Tenants);
121    let tenant_store = TenantStore::load_from_path(&tenants_path)
122        .context("Tenants not synced. Run 'systemprompt cloud login'")?;
123
124    let tenant = tenant_store.find_tenant(&tenant_id).ok_or_else(|| {
125        anyhow!(
126            "Tenant {} not found. Run 'systemprompt cloud login'",
127            tenant_id
128        )
129    })?;
130
131    Ok(DeployTarget {
132        tenant_id,
133        tenant_name: tenant.name.clone(),
134        hostname: tenant.hostname.clone(),
135        creds,
136    })
137}