plugin_run.rs 7.29 KiB
use crate::command_line_interface::CliOptions;
use crate::error::Error;
use crate::error::Result;
use crate::internal_api;
use crate::plugin_auth_crypto::DatabaseKey;
use crate::schema::Schema;
use internal_api::new_random_item_id;
use log::info;
use rusqlite::Transaction;
use std::process::Command;
use warp::http::status::StatusCode;
/// Run a plugin, making sure that the correct ENV variables and settings are passed
/// to the containerization / deployment processes.
///
/// Internally passes to docker / kubernetes / scripts
/// depending on how Pod is configured by the user.
#[allow(clippy::too_many_arguments)]
pub fn run_plugin_container(
    tx: &Transaction,
    schema: &Schema,
    container_image: String,
    target_item_id: &str,
    triggered_by_item_id: &str,
    pod_owner: &str,
    database_key: &DatabaseKey,
    cli_options: &CliOptions,
) -> Result<()> {
    info!(
        "Trying to run plugin container for target_item_id {}",
        target_item_id
    let item = internal_api::get_item_tx(tx, schema, target_item_id)?;
    let item = item.into_iter().next().ok_or_else(|| Error {
        code: StatusCode::BAD_REQUEST,
        msg: format!(
            "Failed to find target item {} to run a plugin against",
            target_item_id
    })?;
    let item = serde_json::to_string(&item)?;
    let auth = database_key.create_plugin_auth()?;
    let auth = serde_json::to_string(&auth)?;
    let container_id = format!(
        "{}-{}-{}",
        pod_owner.chars().take(10).collect::<String>(),
        container_image.chars().take(15).collect::<String>(),
        new_random_item_id()
    if cli_options.use_kubernetes {
        run_kubernetes_container(
            &container_image,
            container_id,
            &target_item_id,
            pod_owner,
            &auth,
            triggered_by_item_id,
            cli_options,
    } else {
        run_docker_container(
            &container_image,
            container_id,
            &target_item_id,
            pod_owner,
            &auth,
            triggered_by_item_id,
            cli_options,