codezero 0.0.5

A CLI setup wizard for running CodeZero locally
use std::{collections::HashMap, fs, path::PathBuf};

use crate::{
    action::{
        ACTIONS_DIR, ActionCatalog, ActionEntry, SERVICE_CONFIGURATION_PATH, ServiceConfiguration,
        action_fragment_path, aquila_action_env_prefix, parse_install_spec,
    },
    bundle::generate_token,
    env_file,
    runner::{Runner, default_runner, refresh_aquila_config},
    ui,
};

const ENV_PATH: &str = ".codezero/.env";

pub fn install(index_path: Option<PathBuf>, spec: String) -> anyhow::Result<()> {
    if !fs::exists(ENV_PATH)? {
        anyhow::bail!("No CodeZero setup found. Run `codezero setup` first.");
    }

    let (name, tag_override) = parse_install_spec(&spec);
    let catalog = ActionCatalog::load(index_path.as_deref())?;
    let entry = catalog.find(name).ok_or_else(|| {
        anyhow::anyhow!(
            "Unknown action '{name}'. Available: {}",
            catalog.names().join(", ")
        )
    })?;

    let env = env_file::read_all(ENV_PATH)?;
    check_dependencies(entry, &catalog, &env)?;

    let tag = match tag_override {
        Some(tag) => tag.to_string(),
        None => env
            .get("ACTION_IMAGE_TAG")
            .filter(|tag| !tag.is_empty())
            .cloned()
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "No ACTION_IMAGE_TAG set in {ENV_PATH}. Run `codezero configure` to set one, or specify a version explicitly (e.g. `{name}@1.2.3`)."
                )
            })?,
    };
    let token = generate_token(64);

    // `.codezero/service.configuration.json` is local bookkeeping only -
    // aquila's actual accepted-token list is generated by config-generator
    // purely from `.env` (it has no access to this file), so the token only
    // takes effect once it's an AQUILA_ACTION_<KEY>_IDENTIFIER/TOKEN pair
    // there.
    let mut config = ServiceConfiguration::load_or_default(SERVICE_CONFIGURATION_PATH)?;
    let updated = config.upsert_action(&entry.identifier, &token);
    config.save(SERVICE_CONFIGURATION_PATH)?;

    let prefix = aquila_action_env_prefix(&entry.identifier);
    let identifier_key = format!("{prefix}_IDENTIFIER");
    let token_key = format!("{prefix}_TOKEN");
    env_file::set_values(
        ENV_PATH,
        &[
            (identifier_key.as_str(), entry.identifier.as_str()),
            (token_key.as_str(), token.as_str()),
        ],
    )?;

    write_action_compose_fragment(entry, &tag, &token)?;

    ui::success_line(&format!(
        "{} {} ({tag})",
        if updated { "Updated" } else { "Installed" },
        entry.identifier
    ));
    if !entry.description.is_empty() {
        ui::muted_line(&entry.description);
    }

    let runner = default_runner();
    match runner
        .start_service(&entry.identifier)
        .and_then(|()| refresh_aquila_config(&runner))
    {
        Ok(()) => ui::success_line("Restarted Aquila to apply the new configuration."),
        Err(error) => ui::warn_line(&format!(
            "Couldn't apply the change automatically ({error}). Run `codezero start` to apply."
        )),
    }

    Ok(())
}

/// Checks that every action this one depends on is actually usable first.
/// `rest-action`/`cron-action` ship inside the base stack itself, gated
/// behind the `runtime` compose profile, rather than being something
/// `install` creates a compose fragment for - so they're checked against
/// `COMPOSE_PROFILES` instead of the installed-actions directory.
fn check_dependencies(
    entry: &ActionEntry,
    catalog: &ActionCatalog,
    env: &HashMap<String, String>,
) -> anyhow::Result<()> {
    let active_profiles: Vec<&str> = env
        .get("COMPOSE_PROFILES")
        .map(|value| value.split(',').collect())
        .unwrap_or_default();

    for dependency in &entry.dependencies {
        anyhow::ensure!(
            catalog.find(dependency).is_some(),
            "{} depends on '{dependency}', which isn't a known action.",
            entry.identifier
        );

        let satisfied = match dependency.as_str() {
            "rest-action" | "cron-action" => active_profiles.contains(&"runtime"),
            other => action_fragment_path(other).exists(),
        };

        anyhow::ensure!(
            satisfied,
            "{} depends on '{dependency}', which isn't installed/enabled. Run `codezero plugin install {dependency}` \
             (or, if it's built into the base stack, `codezero configure` and enable the 'runtime' profile).",
            entry.identifier
        );
    }

    Ok(())
}

fn write_action_compose_fragment(
    entry: &ActionEntry,
    tag: &str,
    token: &str,
) -> anyhow::Result<()> {
    fs::create_dir_all(ACTIONS_DIR)?;

    let docker = &entry.deployment.docker;
    // No scheme prefix here: hercules' Action SDK passes this straight into a
    // gRPC channel target (bare "host:port", like the SDK's own docs example
    // 'aquila.example.com:8081'), not a URL. An "http://" prefix produces a
    // malformed resolver target ("dns:http://aquila:8081") that only
    // resolves by accident.
    let compose = format!(
        "name: codezero\nservices:\n  {identifier}:\n    image: {image}:{tag}\n    depends_on:\n      aquila:\n        condition: service_started\n    environment:\n      {aquila_url_var}: aquila:8081\n      {auth_token_var}: {token}\n    restart: unless-stopped\n    profiles:\n      - runtime\n",
        identifier = entry.identifier,
        image = docker.image,
        aquila_url_var = docker.aquila_url_var,
        auth_token_var = docker.auth_token_var,
    );

    fs::write(action_fragment_path(&entry.identifier), compose)?;

    Ok(())
}

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

    fn fixture_entry(dependencies: Vec<&str>) -> ActionEntry {
        serde_json::from_value(serde_json::json!({
            "name": "gls",
            "identifier": "gls-action",
            "deployment": { "docker": { "image": "ghcr.io/code0-tech/centaurus/ci-builds/gls-action" } },
            "dependencies": dependencies
        }))
        .unwrap()
    }

    fn fixture_catalog() -> ActionCatalog {
        serde_json::from_value(serde_json::json!({
            "actions": [
                {
                    "name": "rest-action",
                    "identifier": "rest-action",
                    "deployment": { "docker": { "image": "ghcr.io/code0-tech/centaurus/ci-builds/rest-action" } }
                },
                {
                    "name": "cron-action",
                    "identifier": "cron-action",
                    "deployment": { "docker": { "image": "ghcr.io/code0-tech/centaurus/ci-builds/cron-action" } }
                }
            ]
        }))
        .unwrap()
    }

    #[test]
    fn allows_install_when_there_are_no_dependencies() {
        let entry = fixture_entry(vec![]);
        let catalog = fixture_catalog();
        let env = HashMap::new();

        assert!(check_dependencies(&entry, &catalog, &env).is_ok());
    }

    #[test]
    fn allows_install_when_the_runtime_profile_is_active() {
        let entry = fixture_entry(vec!["rest-action"]);
        let catalog = fixture_catalog();
        let mut env = HashMap::new();
        env.insert("COMPOSE_PROFILES".to_string(), "ide,runtime".to_string());

        assert!(check_dependencies(&entry, &catalog, &env).is_ok());
    }

    #[test]
    fn blocks_install_when_the_runtime_profile_is_missing() {
        let entry = fixture_entry(vec!["rest-action"]);
        let catalog = fixture_catalog();
        let mut env = HashMap::new();
        env.insert("COMPOSE_PROFILES".to_string(), "ide".to_string());

        let error = check_dependencies(&entry, &catalog, &env).unwrap_err();
        assert!(error.to_string().contains("rest-action"));
    }

    #[test]
    fn blocks_install_for_a_dependency_that_is_not_a_known_action() {
        let entry = fixture_entry(vec!["something-nonexistent"]);
        let catalog = fixture_catalog();
        let mut env = HashMap::new();
        env.insert("COMPOSE_PROFILES".to_string(), "ide,runtime".to_string());

        assert!(check_dependencies(&entry, &catalog, &env).is_err());
    }
}