Skip to main content

greentic_operator/
runner_exec.rs

1use std::path::{Path, PathBuf};
2
3use greentic_runner_desktop::{RunOptions, RunResult, TenantContext};
4use serde_json::Value as JsonValue;
5
6use crate::domains::Domain;
7use crate::state_layout;
8
9pub struct RunOutput {
10    pub result: RunResult,
11    pub run_dir: PathBuf,
12}
13
14pub struct RunRequest {
15    pub root: PathBuf,
16    pub domain: Domain,
17    pub pack_path: PathBuf,
18    pub pack_label: String,
19    pub flow_id: String,
20    pub tenant: String,
21    pub team: Option<String>,
22    pub input: JsonValue,
23    pub dist_offline: bool,
24}
25
26pub fn run_provider_pack_flow(request: RunRequest) -> anyhow::Result<RunOutput> {
27    let run_dir = state_layout::run_dir(
28        &request.root,
29        request.domain,
30        &request.pack_label,
31        &request.flow_id,
32    )?;
33    std::fs::create_dir_all(&run_dir)?;
34    let input_path = run_dir.join("input.json");
35    let input_json = serde_json::to_string_pretty(&request.input)?;
36    std::fs::write(&input_path, input_json)?;
37
38    let opts = RunOptions {
39        entry_flow: Some(request.flow_id.clone()),
40        input: request.input,
41        ctx: TenantContext {
42            tenant_id: Some(request.tenant),
43            team_id: request.team,
44            user_id: Some("operator".to_string()),
45            session_id: None,
46        },
47        dist_offline: request.dist_offline,
48        artifacts_dir: Some(run_dir.clone()),
49        ..RunOptions::default()
50    };
51
52    let result = greentic_runner_desktop::run_pack_with_options(&request.pack_path, opts)?;
53    write_run_artifacts(&run_dir, &result)?;
54
55    Ok(RunOutput { result, run_dir })
56}
57
58fn write_run_artifacts(run_dir: &Path, result: &RunResult) -> anyhow::Result<()> {
59    let run_json = run_dir.join("run.json");
60    let summary_path = run_dir.join("summary.txt");
61    let artifacts_path = run_dir.join("artifacts_dir");
62
63    let json = serde_json::to_string_pretty(result)?;
64    std::fs::write(run_json, json)?;
65
66    let summary = format!(
67        "status: {:?}\npack_id: {}\nflow_id: {}\nerror: {}\n",
68        result.status,
69        result.pack_id,
70        result.flow_id,
71        result.error.clone().unwrap_or_else(|| "none".to_string())
72    );
73    std::fs::write(summary_path, summary)?;
74    std::fs::write(artifacts_path, result.artifacts_dir.display().to_string())?;
75
76    Ok(())
77}