manta-cli 2.0.0-beta.63

Another CLI for ALPS
//! Implements the `manta backup vcluster` command.
//!
//! Drives `POST /api/v1/migrate/backup` to dump a cluster's BOS / CFS
//! / HSM / IMS state to a destination folder on the server. Runs an
//! optional pre-hook before the POST and a post-hook after. The hook
//! perms are validated up-front so a misconfigured hook never blocks
//! mid-backup. Inverse of [`super::super::restore::vcluster`].

use anyhow::{Context, Error};

use crate::common::app_context::AppContext;
use crate::http_client::{MantaClient, OpenApiResultExt};
use crate::openapi_client::types::MigrateBackupRequest;
use crate::output::action_result;

pub struct ExecParams<'a> {
  pub bos: Option<&'a str>,
  pub destination: Option<&'a str>,
  pub prehook: Option<&'a str>,
  pub posthook: Option<&'a str>,
  pub output: Option<&'a str>,
}

/// Back up cluster configuration to a local bundle.
///
/// # Errors
///
/// Returns an error when `--bos` or `--destination` is missing, when a
/// hook script fails its perms check, when a hook execution exits
/// non-zero, when the HTTP client cannot be built, or when the
/// `migrate_backup` call fails.
pub async fn exec(
  ctx: &AppContext<'_>,
  token: &str,
  p: ExecParams<'_>,
) -> Result<(), Error> {
  let bos = p.bos;
  let destination = p.destination;
  let prehook = p.prehook;
  let posthook = p.posthook;
  let output_opt = p.output;
  let bos_value = bos.context("BOS template is required")?;
  let destination_value =
    destination.context("Destination folder is required")?;

  action_result::print(
    &format!(
      "Migrate backup\n BOS Template: {}\n Destination folder: {}\n Pre-hook: {}\n Post-hook: {}",
      bos_value,
      destination_value,
      prehook.unwrap_or("none"),
      posthook.unwrap_or("none"),
    ),
    output_opt,
  )?;

  crate::common::hooks::validate_hook(prehook, "pre")?;
  crate::common::hooks::validate_hook(posthook, "post")?;

  crate::common::hooks::run_hook_if_present(prehook, "pre")?;

  let client = MantaClient::from_app_ctx(ctx, Some(token))?;
  client
    .openapi
    .migrate_backup(
      client.site_name(),
      &MigrateBackupRequest {
        bos: bos.map(str::to_string),
        destination: destination.map(str::to_string),
      },
    )
    .await
    .into_anyhow()?;
  tracing::debug!("Migrate backup completed successfully.");

  crate::common::hooks::run_hook_if_present(posthook, "post")?;

  action_result::print("Backup completed", output_opt)?;

  Ok(())
}