Skip to main content

systemprompt_cli/commands/cloud/profile/
templates.rs

1use anyhow::{Context, Result};
2use regex::Regex;
3use std::path::Path;
4use systemprompt_cloud::constants::container;
5use systemprompt_logging::CliService;
6use systemprompt_models::{CliPaths, Profile};
7
8use crate::commands::cloud::init::templates::ai_config;
9
10use crate::cloud::dockerfile::DockerfileBuilder;
11
12pub use crate::shared::profile::{generate_display_name, generate_jwt_secret};
13
14pub fn save_profile(profile: &Profile, profile_path: &Path) -> Result<()> {
15    let header = format!(
16        "# systemprompt.io Profile: {}\n# Generated by 'systemprompt cloud profile create'",
17        profile.display_name
18    );
19
20    crate::shared::profile::save_profile_yaml(profile, profile_path, Some(&header))
21}
22
23pub fn save_dockerfile(path: &Path, profile_name: &str, project_root: &Path) -> Result<()> {
24    let content = DockerfileBuilder::new(project_root)
25        .with_profile(profile_name)
26        .build();
27
28    std::fs::write(path, &content)
29        .with_context(|| format!("Failed to write {}", path.display()))?;
30
31    Ok(())
32}
33
34pub fn save_entrypoint(path: &Path) -> Result<()> {
35    let content = format!(
36        r#"#!/bin/sh
37set -e
38
39echo "Running database migrations..."
40{bin}/systemprompt {db_migrate_cmd}
41
42echo "Starting services..."
43exec {bin}/systemprompt {services_serve_cmd} --foreground
44"#,
45        bin = container::BIN,
46        db_migrate_cmd = CliPaths::db_migrate_cmd(),
47        services_serve_cmd = CliPaths::services_serve_cmd(),
48    );
49
50    if let Some(parent) = path.parent() {
51        std::fs::create_dir_all(parent)
52            .with_context(|| format!("Failed to create directory {}", parent.display()))?;
53    }
54
55    std::fs::write(path, &content)
56        .with_context(|| format!("Failed to write {}", path.display()))?;
57
58    #[cfg(unix)]
59    {
60        use std::os::unix::fs::PermissionsExt;
61        let permissions = std::fs::Permissions::from_mode(0o755);
62        std::fs::set_permissions(path, permissions)
63            .with_context(|| format!("Failed to set permissions on {}", path.display()))?;
64    }
65
66    Ok(())
67}
68
69pub fn save_dockerignore(path: &Path) -> Result<()> {
70    let content = r".git
71.gitignore
72.gitmodules
73target/debug
74target/release/.fingerprint
75target/release/build
76target/release/deps
77target/release/examples
78target/release/incremental
79target/release/.cargo-lock
80.cargo
81.systemprompt/credentials.json
82.systemprompt/tenants.json
83.systemprompt/**/secrets.json
84.systemprompt/docker
85.systemprompt/storage
86.env*
87backup
88docs
89instructions
90*.md
91web/node_modules
92.vscode
93.idea
94logs
95*.log
96plan
97";
98
99    if let Some(parent) = path.parent() {
100        std::fs::create_dir_all(parent)
101            .with_context(|| format!("Failed to create directory {}", parent.display()))?;
102    }
103
104    std::fs::write(path, content).with_context(|| format!("Failed to write {}", path.display()))?;
105
106    Ok(())
107}
108
109#[derive(Debug)]
110pub struct DatabaseUrls<'a> {
111    pub external: &'a str,
112    pub internal: Option<&'a str>,
113}
114
115pub fn save_secrets(
116    db_urls: &DatabaseUrls<'_>,
117    api_keys: &super::api_keys::ApiKeys,
118    sync_token: Option<&str>,
119    secrets_path: &Path,
120    is_cloud_tenant: bool,
121) -> Result<()> {
122    use serde_json::json;
123    use systemprompt_models::Profile;
124
125    if Profile::is_masked_database_url(db_urls.external) {
126        CliService::warning(
127            "Database URL appears to be masked. Credentials may not work correctly.",
128        );
129        CliService::warning(
130            "Run 'systemprompt cloud tenant refresh-credentials' to fetch real credentials.",
131        );
132    }
133
134    if let Some(internal) = db_urls.internal {
135        if Profile::is_masked_database_url(internal) {
136            CliService::warning(
137                "Internal database URL appears to be masked. Credentials may not work correctly.",
138            );
139        }
140    }
141
142    if sync_token.is_none() && is_cloud_tenant {
143        CliService::warning("Sync token not available. Cloud sync will not work.");
144        CliService::warning("Run 'systemprompt cloud tenant rotate-sync-token' to generate one.");
145    }
146
147    if let Some(parent) = secrets_path.parent() {
148        std::fs::create_dir_all(parent)
149            .with_context(|| format!("Failed to create directory {}", parent.display()))?;
150    }
151
152    let mut secrets = json!({
153        "jwt_secret": generate_jwt_secret(),
154        "database_url": db_urls.external,
155        "gemini": api_keys.gemini,
156        "anthropic": api_keys.anthropic,
157        "openai": api_keys.openai
158    });
159
160    if let Some(internal) = db_urls.internal {
161        secrets["internal_database_url"] = json!(internal);
162    }
163
164    if let Some(token) = sync_token {
165        secrets["sync_token"] = json!(token);
166    }
167
168    let content = serde_json::to_string_pretty(&secrets).context("Failed to serialize secrets")?;
169
170    std::fs::write(secrets_path, content)
171        .with_context(|| format!("Failed to write {}", secrets_path.display()))?;
172
173    #[cfg(unix)]
174    {
175        use std::os::unix::fs::PermissionsExt;
176        let permissions = std::fs::Permissions::from_mode(0o600);
177        std::fs::set_permissions(secrets_path, permissions)
178            .with_context(|| format!("Failed to set permissions on {}", secrets_path.display()))?;
179    }
180
181    Ok(())
182}
183
184
185pub fn get_services_path() -> Result<String> {
186    if let Ok(path) = std::env::var("SYSTEMPROMPT_SERVICES_PATH") {
187        return Ok(path);
188    }
189
190    let cwd = std::env::current_dir().context("Failed to get current directory")?;
191    let services_path = cwd.join("services");
192
193    Ok(services_path.to_string_lossy().to_string())
194}
195
196pub async fn validate_connection(db_url: &str) -> bool {
197    use tokio::time::{timeout, Duration};
198
199    let result = timeout(Duration::from_secs(5), async {
200        sqlx::postgres::PgPoolOptions::new()
201            .max_connections(1)
202            .connect(db_url)
203            .await
204    })
205    .await;
206
207    matches!(result, Ok(Ok(_)))
208}
209
210pub async fn run_migrations_cmd(profile_path: &Path) -> Result<()> {
211    use std::process::Command;
212
213    CliService::info("Running database migrations...");
214
215    let current_exe = std::env::current_exe().context("Failed to get executable path")?;
216    let profile_path_str = profile_path.to_string_lossy();
217
218    let output = Command::new(&current_exe)
219        .args(CliPaths::db_migrate_args())
220        .env("SYSTEMPROMPT_PROFILE", profile_path_str.as_ref())
221        .output()
222        .context("Failed to run migrations")?;
223
224    if output.status.success() {
225        CliService::success("Migrations completed");
226        return Ok(());
227    }
228
229    let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
230    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
231
232    let error_output = if !stderr.is_empty() {
233        stderr
234    } else if !stdout.is_empty() {
235        stdout
236    } else {
237        "Unknown error (no output)".to_string()
238    };
239
240    anyhow::bail!("Migration failed: {}", error_output)
241}
242
243pub fn update_ai_config_default_provider(provider: &str) -> Result<()> {
244    let services_path = get_services_path()?;
245    let ai_dir = Path::new(&services_path).join("ai");
246    let ai_config_path = ai_dir.join("config.yaml");
247
248    if !ai_config_path.exists() {
249        CliService::warning("AI config not found. Creating services/ai/config.yaml");
250        CliService::info("Run 'systemprompt cloud init' for full project setup");
251
252        std::fs::create_dir_all(&ai_dir)
253            .with_context(|| format!("Failed to create directory {}", ai_dir.display()))?;
254        std::fs::write(&ai_config_path, ai_config(provider))
255            .with_context(|| format!("Failed to write {}", ai_config_path.display()))?;
256        CliService::success(&format!("Created: {}", ai_config_path.display()));
257        return Ok(());
258    }
259
260    let content = std::fs::read_to_string(&ai_config_path)
261        .with_context(|| format!("Failed to read {}", ai_config_path.display()))?;
262    let re = Regex::new(r#"default_provider:\s*"?\w+"?"#).context("Failed to compile regex")?;
263    let updated = re.replace(&content, format!(r#"default_provider: "{}""#, provider));
264
265    std::fs::write(&ai_config_path, updated.as_ref())
266        .with_context(|| format!("Failed to write {}", ai_config_path.display()))?;
267    CliService::success(&format!(
268        "Updated default_provider to '{}' in AI config",
269        provider
270    ));
271    Ok(())
272}