cli: tape: rust format

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-04-06 16:27:32 +02:00
parent 5525ec246f
commit 938a1f137c
7 changed files with 172 additions and 321 deletions

View File

@ -4,48 +4,34 @@ use anyhow::{bail, format_err, Error};
use serde_json::{json, Value};
use proxmox_io::ReadExt;
use proxmox_router::RpcEnvironment;
use proxmox_router::cli::*;
use proxmox_router::RpcEnvironment;
use proxmox_schema::api;
use proxmox_section_config::SectionConfigData;
use proxmox_time::strftime_local;
use pbs_client::view_task_result;
use pbs_tools::format::{
render_epoch,
render_bytes_human_readable,
};
use pbs_tools::format::{render_bytes_human_readable, render_epoch};
use pbs_config::datastore::complete_datastore_name;
use pbs_config::drive::complete_drive_name;
use pbs_config::media_pool::complete_pool_name;
use pbs_config::datastore::complete_datastore_name;
use pbs_api_types::{
Userid, Authid, DATASTORE_SCHEMA, DATASTORE_MAP_LIST_SCHEMA,
DRIVE_NAME_SCHEMA, MEDIA_LABEL_SCHEMA, MEDIA_POOL_NAME_SCHEMA,
TAPE_RESTORE_SNAPSHOT_SCHEMA, GROUP_FILTER_LIST_SCHEMA, GroupListItem,
HumanByte
};
use pbs_tape::{
PROXMOX_BACKUP_CONTENT_HEADER_MAGIC_1_0, BlockReadError, MediaContentHeader,
Authid, GroupListItem, HumanByte, Userid, DATASTORE_MAP_LIST_SCHEMA, DATASTORE_SCHEMA,
DRIVE_NAME_SCHEMA, GROUP_FILTER_LIST_SCHEMA, MEDIA_LABEL_SCHEMA, MEDIA_POOL_NAME_SCHEMA,
TAPE_RESTORE_SNAPSHOT_SCHEMA,
};
use pbs_tape::{BlockReadError, MediaContentHeader, PROXMOX_BACKUP_CONTENT_HEADER_MAGIC_1_0};
use proxmox_backup::{
api2,
tape::{
drive::{
open_drive,
lock_tape_device,
set_tape_device_state,
},
complete_media_label_text,
complete_media_set_uuid,
complete_media_set_snapshots,
file_formats::{
proxmox_tape_magic_to_text,
},
},
client_helpers::connect_to_localhost,
tape::{
complete_media_label_text, complete_media_set_snapshots, complete_media_set_uuid,
drive::{lock_tape_device, open_drive, set_tape_device_state},
file_formats::proxmox_tape_magic_to_text,
},
};
mod proxmox_tape;
@ -58,14 +44,15 @@ async fn get_backup_groups(store: &str) -> Result<Vec<GroupListItem>, Error> {
.await?;
match api_res.get("data") {
Some(data) => Ok(serde_json::from_value::<Vec<GroupListItem>>(data.to_owned())?),
Some(data) => Ok(serde_json::from_value::<Vec<GroupListItem>>(
data.to_owned(),
)?),
None => bail!("could not get group list"),
}
}
// shell completion helper
pub fn complete_datastore_group_filter(_arg: &str, param: &HashMap<String, String>) -> Vec<String> {
let mut list = Vec::new();
list.push("regex:".to_string());
@ -76,31 +63,30 @@ pub fn complete_datastore_group_filter(_arg: &str, param: &HashMap<String, Strin
if let Some(store) = param.get("store") {
let groups = proxmox_async::runtime::block_on(async { get_backup_groups(store).await });
if let Ok(groups) = groups {
list.extend(groups.iter().map(|group| format!("group:{}/{}", group.backup_type, group.backup_id)));
list.extend(
groups
.iter()
.map(|group| format!("group:{}/{}", group.backup_type, group.backup_id)),
);
}
}
list
}
pub fn extract_drive_name(
param: &mut Value,
config: &SectionConfigData,
) -> Result<String, Error> {
pub fn extract_drive_name(param: &mut Value, config: &SectionConfigData) -> Result<String, Error> {
let drive = param["drive"]
.as_str()
.map(String::from)
.or_else(|| std::env::var("PROXMOX_TAPE_DRIVE").ok())
.or_else(|| {
let mut drive_names = Vec::new();
for (name, (section_type, _)) in config.sections.iter() {
if !(section_type == "linux" || section_type == "virtual") { continue; }
if section_type == "linux" || section_type == "virtual" {
drive_names.push(name);
}
}
if drive_names.len() == 1 {
Some(drive_names[0].to_owned())
@ -139,7 +125,6 @@ pub fn extract_drive_name(
)]
/// Format media
async fn format_media(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -172,7 +157,6 @@ async fn format_media(mut param: Value) -> Result<(), Error> {
)]
/// Rewind tape
async fn rewind(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -205,7 +189,6 @@ async fn rewind(mut param: Value) -> Result<(), Error> {
)]
/// Eject/Unload drive media
async fn eject_media(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -241,7 +224,6 @@ async fn eject_media(mut param: Value) -> Result<(), Error> {
)]
/// Load media with specified label
async fn load_media(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -273,7 +255,6 @@ async fn load_media(mut param: Value) -> Result<(), Error> {
)]
/// Export media with specified label
async fn export_media(mut param: Value) -> Result<(), Error> {
let (config, _digest) = pbs_config::drive::config()?;
let drive = extract_drive_name(&mut param, &config)?;
@ -303,7 +284,6 @@ async fn export_media(mut param: Value) -> Result<(), Error> {
)]
/// Load media from the specified slot
async fn load_media_from_slot(mut param: Value) -> Result<(), Error> {
let (config, _digest) = pbs_config::drive::config()?;
let drive = extract_drive_name(&mut param, &config)?;
@ -338,7 +318,6 @@ async fn load_media_from_slot(mut param: Value) -> Result<(), Error> {
)]
/// Unload media via changer
async fn unload_media(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -378,7 +357,6 @@ async fn unload_media(mut param: Value) -> Result<(), Error> {
)]
/// Label media
async fn label_media(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -416,7 +394,6 @@ async fn label_media(mut param: Value) -> Result<(), Error> {
)]
/// Read media label
async fn read_label(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -438,8 +415,7 @@ async fn read_label(mut param: Value) -> Result<(), Error> {
.column(ColumnConfig::new("pool"))
.column(ColumnConfig::new("media-set-uuid"))
.column(ColumnConfig::new("media-set-ctime").renderer(render_epoch))
.column(ColumnConfig::new("encryption-key-fingerprint"))
;
.column(ColumnConfig::new("encryption-key-fingerprint"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -476,7 +452,6 @@ async fn inventory(
read_all_labels: Option<bool>,
mut param: Value,
) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -489,7 +464,6 @@ async fn inventory(
let path = format!("api2/json/tape/drive/{}/inventory", drive);
if do_read {
let mut param = json!({});
if let Some(true) = read_all_labels {
param["read-all-labels"] = true.into();
@ -506,8 +480,7 @@ async fn inventory(
let options = default_table_format_options()
.column(ColumnConfig::new("label-text"))
.column(ColumnConfig::new("uuid"))
;
.column(ColumnConfig::new("uuid"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -534,7 +507,6 @@ async fn inventory(
)]
/// Label media with barcodes from changer device
async fn barcode_label_media(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -563,7 +535,6 @@ async fn barcode_label_media(mut param: Value) -> Result<(), Error> {
)]
/// Move to end of media (MTEOM, used to debug)
fn move_to_eom(mut param: Value) -> Result<(), Error> {
let (config, _digest) = pbs_config::drive::config()?;
let drive = extract_drive_name(&mut param, &config)?;
@ -593,7 +564,6 @@ fn move_to_eom(mut param: Value) -> Result<(), Error> {
/// Note: This reads unless the driver returns an IO Error, so this
/// method is expected to fails when we reach EOT.
fn debug_scan(mut param: Value) -> Result<(), Error> {
let (config, _digest) = pbs_config::drive::config()?;
let drive = extract_drive_name(&mut param, &config)?;
@ -628,8 +598,12 @@ fn debug_scan(mut param: Value) -> Result<(), Error> {
match header {
Ok(header) => {
if header.magic != PROXMOX_BACKUP_CONTENT_HEADER_MAGIC_1_0 {
println!("got MediaContentHeader with wrong magic: {:?}", header.magic);
} else if let Some(name) = proxmox_tape_magic_to_text(&header.content_magic) {
println!(
"got MediaContentHeader with wrong magic: {:?}",
header.magic
);
} else if let Some(name) = proxmox_tape_magic_to_text(&header.content_magic)
{
println!("got content header: {}", name);
println!(" uuid: {}", header.content_uuid());
println!(" ctime: {}", strftime_local("%c", header.ctime)?);
@ -673,7 +647,6 @@ fn debug_scan(mut param: Value) -> Result<(), Error> {
)]
/// Read Cartridge Memory (Medium auxiliary memory attributes)
async fn cartridge_memory(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -691,8 +664,7 @@ async fn cartridge_memory(mut param: Value) -> Result<(), Error> {
let options = default_table_format_options()
.column(ColumnConfig::new("id"))
.column(ColumnConfig::new("name"))
.column(ColumnConfig::new("value"))
;
.column(ColumnConfig::new("value"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
Ok(())
@ -714,7 +686,6 @@ async fn cartridge_memory(mut param: Value) -> Result<(), Error> {
)]
/// Read Volume Statistics (SCSI log page 17h)
async fn volume_statistics(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -752,7 +723,6 @@ async fn volume_statistics(mut param: Value) -> Result<(), Error> {
)]
/// Get drive/media status
async fn status(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -769,7 +739,7 @@ async fn status(mut param: Value) -> Result<(), Error> {
let render_percentage = |value: &Value, _record: &Value| {
match value.as_f64() {
Some(wearout) => Ok(format!("{:.2}%", wearout*100.0)),
Some(wearout) => Ok(format!("{:.2}%", wearout * 100.0)),
None => Ok(String::from("ERROR")), // should never happen
}
};
@ -788,8 +758,7 @@ async fn status(mut param: Value) -> Result<(), Error> {
.column(ColumnConfig::new("bytes-read").renderer(render_bytes_human_readable))
.column(ColumnConfig::new("medium-passes"))
.column(ColumnConfig::new("medium-wearout").renderer(render_percentage))
.column(ColumnConfig::new("volume-mounts"))
;
.column(ColumnConfig::new("volume-mounts"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -812,7 +781,6 @@ async fn status(mut param: Value) -> Result<(), Error> {
)]
/// Clean drive
async fn clean_drive(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -887,7 +855,6 @@ async fn clean_drive(mut param: Value) -> Result<(), Error> {
)]
/// Backup datastore to tape media pool
async fn backup(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -942,7 +909,6 @@ async fn backup(mut param: Value) -> Result<(), Error> {
)]
/// Restore data from media-set
async fn restore(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -989,7 +955,6 @@ async fn restore(mut param: Value) -> Result<(), Error> {
)]
/// Scan media and record content
async fn catalog_media(mut param: Value) -> Result<(), Error> {
let output_format = extract_output_format(&mut param);
let (config, _digest) = pbs_config::drive::config()?;
@ -1007,7 +972,6 @@ async fn catalog_media(mut param: Value) -> Result<(), Error> {
}
fn main() {
let cmd_def = CliCommandMap::new()
.insert(
"backup",
@ -1016,7 +980,7 @@ fn main() {
.completion_cb("drive", complete_drive_name)
.completion_cb("store", complete_datastore_name)
.completion_cb("pool", complete_pool_name)
.completion_cb("groups", complete_datastore_group_filter)
.completion_cb("groups", complete_datastore_group_filter),
)
.insert(
"restore",
@ -1024,80 +988,69 @@ fn main() {
.arg_param(&["media-set", "store", "snapshots"])
.completion_cb("store", complete_datastore_name)
.completion_cb("media-set", complete_media_set_uuid)
.completion_cb("snapshots", complete_media_set_snapshots)
.completion_cb("snapshots", complete_media_set_snapshots),
)
.insert(
"barcode-label",
CliCommand::new(&API_METHOD_BARCODE_LABEL_MEDIA)
.completion_cb("drive", complete_drive_name)
.completion_cb("pool", complete_pool_name)
.completion_cb("pool", complete_pool_name),
)
.insert(
"rewind",
CliCommand::new(&API_METHOD_REWIND)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_REWIND).completion_cb("drive", complete_drive_name),
)
.insert(
"scan",
CliCommand::new(&API_METHOD_DEBUG_SCAN)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_DEBUG_SCAN).completion_cb("drive", complete_drive_name),
)
.insert(
"status",
CliCommand::new(&API_METHOD_STATUS)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_STATUS).completion_cb("drive", complete_drive_name),
)
.insert(
"eod",
CliCommand::new(&API_METHOD_MOVE_TO_EOM)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_MOVE_TO_EOM).completion_cb("drive", complete_drive_name),
)
.insert(
"format",
CliCommand::new(&API_METHOD_FORMAT_MEDIA)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_FORMAT_MEDIA).completion_cb("drive", complete_drive_name),
)
.insert(
"eject",
CliCommand::new(&API_METHOD_EJECT_MEDIA)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_EJECT_MEDIA).completion_cb("drive", complete_drive_name),
)
.insert(
"inventory",
CliCommand::new(&API_METHOD_INVENTORY)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_INVENTORY).completion_cb("drive", complete_drive_name),
)
.insert(
"read-label",
CliCommand::new(&API_METHOD_READ_LABEL)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_READ_LABEL).completion_cb("drive", complete_drive_name),
)
.insert(
"catalog",
CliCommand::new(&API_METHOD_CATALOG_MEDIA)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_CATALOG_MEDIA).completion_cb("drive", complete_drive_name),
)
.insert(
"cartridge-memory",
CliCommand::new(&API_METHOD_CARTRIDGE_MEMORY)
.completion_cb("drive", complete_drive_name)
.completion_cb("drive", complete_drive_name),
)
.insert(
"volume-statistics",
CliCommand::new(&API_METHOD_VOLUME_STATISTICS)
.completion_cb("drive", complete_drive_name)
.completion_cb("drive", complete_drive_name),
)
.insert(
"clean",
CliCommand::new(&API_METHOD_CLEAN_DRIVE)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_CLEAN_DRIVE).completion_cb("drive", complete_drive_name),
)
.insert(
"label",
CliCommand::new(&API_METHOD_LABEL_MEDIA)
.completion_cb("drive", complete_drive_name)
.completion_cb("pool", complete_pool_name)
.completion_cb("pool", complete_pool_name),
)
.insert("changer", changer_commands())
.insert("drive", drive_commands())
@ -1110,27 +1063,25 @@ fn main() {
CliCommand::new(&API_METHOD_LOAD_MEDIA)
.arg_param(&["label-text"])
.completion_cb("drive", complete_drive_name)
.completion_cb("label-text", complete_media_label_text)
.completion_cb("label-text", complete_media_label_text),
)
.insert(
"load-media-from-slot",
CliCommand::new(&API_METHOD_LOAD_MEDIA_FROM_SLOT)
.arg_param(&["source-slot"])
.completion_cb("drive", complete_drive_name)
.completion_cb("drive", complete_drive_name),
)
.insert(
"unload",
CliCommand::new(&API_METHOD_UNLOAD_MEDIA)
.completion_cb("drive", complete_drive_name)
CliCommand::new(&API_METHOD_UNLOAD_MEDIA).completion_cb("drive", complete_drive_name),
)
.insert(
"export-media",
CliCommand::new(&API_METHOD_EXPORT_MEDIA)
.arg_param(&["label-text"])
.completion_cb("drive", complete_drive_name)
.completion_cb("label-text", complete_media_label_text)
)
;
.completion_cb("label-text", complete_media_label_text),
);
let mut rpcenv = CliEnvironment::new();
rpcenv.set_auth_id(Some(String::from("root@pam")));

View File

@ -22,7 +22,6 @@ use proxmox_backup::client_helpers::connect_to_localhost;
)]
/// Tape backup job list.
fn list_tape_backup_jobs(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
let output_format = get_output_format(&param);
//let info = &api2::config::tape_backup_job::API_METHOD_LIST_TAPE_BACKUP_JOBS;
@ -62,7 +61,6 @@ fn list_tape_backup_jobs(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Resul
)]
/// Show tape backup job configuration
fn show_tape_backup_job(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> {
let output_format = get_output_format(&param);
let info = &api2::config::tape_backup_job::API_METHOD_READ_TAPE_BACKUP_JOB;
@ -88,14 +86,15 @@ fn show_tape_backup_job(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result
)]
/// Run THape Backup Job
async fn run_tape_backup_job(mut param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let id = param["id"].take().as_str().unwrap().to_string();
let client = connect_to_localhost()?;
let result = client.post(&format!("api2/json/tape/backup/{}", id), Some(param)).await?;
let result = client
.post(&format!("api2/json/tape/backup/{}", id), Some(param))
.await?;
view_task_result(&client, result, &output_format).await?;
@ -103,41 +102,45 @@ async fn run_tape_backup_job(mut param: Value) -> Result<(), Error> {
}
pub fn backup_job_commands() -> CommandLineInterface {
let cmd_def = CliCommandMap::new()
.insert("list", CliCommand::new(&API_METHOD_LIST_TAPE_BACKUP_JOBS))
.insert("show",
.insert(
"show",
CliCommand::new(&API_METHOD_SHOW_TAPE_BACKUP_JOB)
.arg_param(&["id"])
.completion_cb("id", pbs_config::tape_job::complete_tape_job_id)
.completion_cb("id", pbs_config::tape_job::complete_tape_job_id),
)
.insert("run",
.insert(
"run",
CliCommand::new(&API_METHOD_RUN_TAPE_BACKUP_JOB)
.arg_param(&["id"])
.completion_cb("id", pbs_config::tape_job::complete_tape_job_id)
.completion_cb("id", pbs_config::tape_job::complete_tape_job_id),
)
.insert("create",
.insert(
"create",
CliCommand::new(&api2::config::tape_backup_job::API_METHOD_CREATE_TAPE_BACKUP_JOB)
.arg_param(&["id"])
.completion_cb("id", pbs_config::tape_job::complete_tape_job_id)
.completion_cb("schedule", pbs_config::datastore::complete_calendar_event)
.completion_cb("store", pbs_config::datastore::complete_datastore_name)
.completion_cb("pool", pbs_config::media_pool::complete_pool_name)
.completion_cb("drive", crate::complete_drive_name)
.completion_cb("drive", crate::complete_drive_name),
)
.insert("update",
.insert(
"update",
CliCommand::new(&api2::config::tape_backup_job::API_METHOD_UPDATE_TAPE_BACKUP_JOB)
.arg_param(&["id"])
.completion_cb("id", pbs_config::tape_job::complete_tape_job_id)
.completion_cb("schedule", pbs_config::datastore::complete_calendar_event)
.completion_cb("store", pbs_config::datastore::complete_datastore_name)
.completion_cb("pool", pbs_config::media_pool::complete_pool_name)
.completion_cb("drive", crate::complete_drive_name)
.completion_cb("drive", crate::complete_drive_name),
)
.insert("remove",
.insert(
"remove",
CliCommand::new(&api2::config::tape_backup_job::API_METHOD_DELETE_TAPE_BACKUP_JOB)
.arg_param(&["id"])
.completion_cb("id", pbs_config::tape_job::complete_tape_job_id)
.completion_cb("id", pbs_config::tape_job::complete_tape_job_id),
);
cmd_def.into()

View File

@ -5,22 +5,15 @@ use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
use proxmox_schema::api;
use proxmox_section_config::SectionConfigData;
use pbs_config::drive::{
complete_drive_name,
complete_changer_name,
};
use pbs_config::drive::{complete_changer_name, complete_drive_name};
use pbs_api_types::CHANGER_NAME_SCHEMA;
use pbs_tape::linux_list_drives::{complete_changer_path};
use pbs_tape::linux_list_drives::complete_changer_path;
use proxmox_backup::{api2, tape::drive::media_changer};
pub fn lookup_changer_name(
param: &Value,
config: &SectionConfigData,
) -> Result<String, Error> {
pub fn lookup_changer_name(param: &Value, config: &SectionConfigData) -> Result<String, Error> {
if let Some(name) = param["name"].as_str() {
return Ok(String::from(name));
}
@ -37,46 +30,47 @@ pub fn lookup_changer_name(
}
pub fn changer_commands() -> CommandLineInterface {
let cmd_def = CliCommandMap::new()
.insert("scan", CliCommand::new(&API_METHOD_SCAN_FOR_CHANGERS))
.insert("list", CliCommand::new(&API_METHOD_LIST_CHANGERS))
.insert("config",
.insert(
"config",
CliCommand::new(&API_METHOD_GET_CONFIG)
.arg_param(&["name"])
.completion_cb("name", complete_changer_name)
.completion_cb("name", complete_changer_name),
)
.insert(
"remove",
CliCommand::new(&api2::config::changer::API_METHOD_DELETE_CHANGER)
.arg_param(&["name"])
.completion_cb("name", complete_changer_name)
.completion_cb("name", complete_changer_name),
)
.insert(
"create",
CliCommand::new(&api2::config::changer::API_METHOD_CREATE_CHANGER)
.arg_param(&["name"])
.completion_cb("name", complete_drive_name)
.completion_cb("path", complete_changer_path)
.completion_cb("path", complete_changer_path),
)
.insert(
"update",
CliCommand::new(&api2::config::changer::API_METHOD_UPDATE_CHANGER)
.arg_param(&["name"])
.completion_cb("name", complete_changer_name)
.completion_cb("path", complete_changer_path)
.completion_cb("path", complete_changer_path),
)
.insert("status",
.insert(
"status",
CliCommand::new(&API_METHOD_GET_STATUS)
.arg_param(&["name"])
.completion_cb("name", complete_changer_name)
.completion_cb("name", complete_changer_name),
)
.insert("transfer",
.insert(
"transfer",
CliCommand::new(&API_METHOD_TRANSFER)
.arg_param(&["name"])
.completion_cb("name", complete_changer_name)
)
;
.completion_cb("name", complete_changer_name),
);
cmd_def.into()
}
@ -92,11 +86,7 @@ pub fn changer_commands() -> CommandLineInterface {
},
)]
/// List changers
fn list_changers(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn list_changers(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::tape::changer::API_METHOD_LIST_CHANGERS;
let mut data = match info.handler {
@ -109,8 +99,7 @@ fn list_changers(
.column(ColumnConfig::new("path"))
.column(ColumnConfig::new("vendor"))
.column(ColumnConfig::new("model"))
.column(ColumnConfig::new("serial"))
;
.column(ColumnConfig::new("serial"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -128,11 +117,7 @@ fn list_changers(
},
)]
/// Scan for SCSI tape changers
fn scan_for_changers(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn scan_for_changers(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::tape::API_METHOD_SCAN_CHANGERS;
let mut data = match info.handler {
@ -144,8 +129,7 @@ fn scan_for_changers(
.column(ColumnConfig::new("path"))
.column(ColumnConfig::new("vendor"))
.column(ColumnConfig::new("model"))
.column(ColumnConfig::new("serial"))
;
.column(ColumnConfig::new("serial"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -166,11 +150,7 @@ fn scan_for_changers(
},
)]
/// Get tape changer configuration
fn get_config(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn get_config(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::config::changer::API_METHOD_GET_CONFIG;
let mut data = match info.handler {
@ -181,8 +161,7 @@ fn get_config(
let options = default_table_format_options()
.column(ColumnConfig::new("name"))
.column(ColumnConfig::new("path"))
.column(ColumnConfig::new("export-slots"))
;
.column(ColumnConfig::new("export-slots"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -210,11 +189,7 @@ fn get_config(
},
)]
/// Get tape changer status
async fn get_status(
mut param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
async fn get_status(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let (config, _digest) = pbs_config::drive::config()?;
param["name"] = lookup_changer_name(&param, &config)?.into();
@ -244,8 +219,7 @@ async fn get_status(
.column(ColumnConfig::new("entry-kind"))
.column(ColumnConfig::new("entry-id"))
.column(ColumnConfig::new("label-text").renderer(render_label_text))
.column(ColumnConfig::new("loaded-slot"))
;
.column(ColumnConfig::new("loaded-slot"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -273,11 +247,7 @@ async fn get_status(
},
)]
/// Transfers media from one slot to another
pub async fn transfer(
mut param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
pub async fn transfer(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let (config, _digest) = pbs_config::drive::config()?;
param["name"] = lookup_changer_name(&param, &config)?.into();

View File

@ -6,31 +6,27 @@ use proxmox_schema::api;
use pbs_api_types::DRIVE_NAME_SCHEMA;
use pbs_config::drive::{
complete_drive_name,
complete_changer_name,
complete_lto_drive_name,
};
use pbs_config::drive::{complete_changer_name, complete_drive_name, complete_lto_drive_name};
use pbs_tape::linux_list_drives::{complete_drive_path};
use pbs_tape::linux_list_drives::complete_drive_path;
use proxmox_backup::api2;
pub fn drive_commands() -> CommandLineInterface {
let cmd_def = CliCommandMap::new()
.insert("scan", CliCommand::new(&API_METHOD_SCAN_FOR_DRIVES))
.insert("list", CliCommand::new(&API_METHOD_LIST_DRIVES))
.insert("config",
.insert(
"config",
CliCommand::new(&API_METHOD_GET_CONFIG)
.arg_param(&["name"])
.completion_cb("name", complete_lto_drive_name)
.completion_cb("name", complete_lto_drive_name),
)
.insert(
"remove",
CliCommand::new(&api2::config::drive::API_METHOD_DELETE_DRIVE)
.arg_param(&["name"])
.completion_cb("name", complete_lto_drive_name)
.completion_cb("name", complete_lto_drive_name),
)
.insert(
"create",
@ -38,7 +34,7 @@ pub fn drive_commands() -> CommandLineInterface {
.arg_param(&["name"])
.completion_cb("name", complete_drive_name)
.completion_cb("path", complete_drive_path)
.completion_cb("changer", complete_changer_name)
.completion_cb("changer", complete_changer_name),
)
.insert(
"update",
@ -46,9 +42,8 @@ pub fn drive_commands() -> CommandLineInterface {
.arg_param(&["name"])
.completion_cb("name", complete_lto_drive_name)
.completion_cb("path", complete_drive_path)
.completion_cb("changer", complete_changer_name)
)
;
.completion_cb("changer", complete_changer_name),
);
cmd_def.into()
}
@ -64,11 +59,7 @@ pub fn drive_commands() -> CommandLineInterface {
},
)]
/// List drives
fn list_drives(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn list_drives(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::tape::drive::API_METHOD_LIST_DRIVES;
let mut data = match info.handler {
@ -82,8 +73,7 @@ fn list_drives(
.column(ColumnConfig::new("changer"))
.column(ColumnConfig::new("vendor"))
.column(ColumnConfig::new("model"))
.column(ColumnConfig::new("serial"))
;
.column(ColumnConfig::new("serial"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -101,11 +91,7 @@ fn list_drives(
}
)]
/// Scan for drives
fn scan_for_drives(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn scan_for_drives(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::tape::API_METHOD_SCAN_DRIVES;
let mut data = match info.handler {
@ -117,15 +103,13 @@ fn scan_for_drives(
.column(ColumnConfig::new("path"))
.column(ColumnConfig::new("vendor"))
.column(ColumnConfig::new("model"))
.column(ColumnConfig::new("serial"))
;
.column(ColumnConfig::new("serial"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
Ok(())
}
#[api(
input: {
properties: {
@ -140,11 +124,7 @@ fn scan_for_drives(
},
)]
/// Get pool configuration
fn get_config(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn get_config(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::config::drive::API_METHOD_GET_CONFIG;
let mut data = match info.handler {
@ -156,8 +136,7 @@ fn get_config(
.column(ColumnConfig::new("name"))
.column(ColumnConfig::new("path"))
.column(ColumnConfig::new("changer"))
.column(ColumnConfig::new("changer-drivenum"))
;
.column(ColumnConfig::new("changer-drivenum"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);

View File

@ -6,53 +6,45 @@ use proxmox_schema::{api, param_bail};
use proxmox_sys::linux::tty;
use pbs_api_types::{
Fingerprint, Kdf, DRIVE_NAME_SCHEMA, TAPE_ENCRYPTION_KEY_FINGERPRINT_SCHEMA,
PASSWORD_HINT_SCHEMA,
Fingerprint, Kdf, DRIVE_NAME_SCHEMA, PASSWORD_HINT_SCHEMA,
TAPE_ENCRYPTION_KEY_FINGERPRINT_SCHEMA,
};
use pbs_datastore::paperkey::{PaperkeyFormat, generate_paper_key};
use pbs_config::tape_encryption_keys::{load_key_configs,complete_key_fingerprint};
use pbs_config::key_config::KeyConfig;
use pbs_config::tape_encryption_keys::{complete_key_fingerprint, load_key_configs};
use pbs_datastore::paperkey::{generate_paper_key, PaperkeyFormat};
use proxmox_backup::api2;
pub fn encryption_key_commands() -> CommandLineInterface {
let cmd_def = CliCommandMap::new()
.insert("list", CliCommand::new(&API_METHOD_LIST_KEYS))
.insert(
"create",
CliCommand::new(&API_METHOD_CREATE_KEY)
)
.insert("create", CliCommand::new(&API_METHOD_CREATE_KEY))
.insert(
"change-passphrase",
CliCommand::new(&API_METHOD_CHANGE_PASSPHRASE)
.arg_param(&["fingerprint"])
.completion_cb("fingerprint", complete_key_fingerprint)
.completion_cb("fingerprint", complete_key_fingerprint),
)
.insert(
"show",
CliCommand::new(&API_METHOD_SHOW_KEY)
.arg_param(&["fingerprint"])
.completion_cb("fingerprint", complete_key_fingerprint)
.completion_cb("fingerprint", complete_key_fingerprint),
)
.insert(
"paperkey",
CliCommand::new(&API_METHOD_PAPER_KEY)
.arg_param(&["fingerprint"])
.completion_cb("fingerprint", complete_key_fingerprint)
)
.insert(
"restore",
CliCommand::new(&API_METHOD_RESTORE_KEY)
.completion_cb("fingerprint", complete_key_fingerprint),
)
.insert("restore", CliCommand::new(&API_METHOD_RESTORE_KEY))
.insert(
"remove",
CliCommand::new(&api2::config::tape_encryption_keys::API_METHOD_DELETE_KEY)
.arg_param(&["fingerprint"])
.completion_cb("fingerprint", complete_key_fingerprint)
)
;
.completion_cb("fingerprint", complete_key_fingerprint),
);
cmd_def.into()
}
@ -82,7 +74,6 @@ fn paper_key(
subject: Option<String>,
output_format: Option<PaperkeyFormat>,
) -> Result<(), Error> {
let (config_map, _digest) = load_key_configs()?;
let key_config = match config_map.get(&fingerprint) {
@ -109,11 +100,7 @@ fn paper_key(
},
)]
/// Print the encryption key's metadata.
fn show_key(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn show_key(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::config::tape_encryption_keys::API_METHOD_READ_KEY;
@ -162,7 +149,6 @@ fn change_passphrase(
mut param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
if !tty::stdin_isatty() {
bail!("unable to change passphrase - no tty");
}
@ -288,11 +274,7 @@ async fn restore_key(
},
)]
/// Create key (read password from stdin)
fn create_key(
mut param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn create_key(mut param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
if !tty::stdin_isatty() {
bail!("no password input mechanism available");
}
@ -312,7 +294,6 @@ fn create_key(
Ok(())
}
#[api(
input: {
properties: {
@ -324,11 +305,7 @@ fn create_key(
},
)]
/// List keys
fn list_keys(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn list_keys(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::config::tape_encryption_keys::API_METHOD_LIST_KEYS;
let mut data = match info.handler {
@ -338,8 +315,7 @@ fn list_keys(
let options = default_table_format_options()
.column(ColumnConfig::new("fingerprint"))
.column(ColumnConfig::new("hint"))
;
.column(ColumnConfig::new("hint"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);

View File

@ -1,39 +1,34 @@
use anyhow::{Error};
use anyhow::Error;
use serde_json::Value;
use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
use proxmox_schema::api;
use pbs_api_types::{
MEDIA_POOL_NAME_SCHEMA, CHANGER_NAME_SCHEMA, MediaStatus, MediaListEntry,
MediaContentListFilter,
MediaContentListFilter, MediaListEntry, MediaStatus, CHANGER_NAME_SCHEMA,
MEDIA_POOL_NAME_SCHEMA,
};
use pbs_config::drive::complete_changer_name;
use pbs_config::media_pool::complete_pool_name;
use proxmox_backup::{
api2,
tape::{
complete_media_label_text,
complete_media_uuid,
complete_media_set_uuid,
},
tape::{complete_media_label_text, complete_media_set_uuid, complete_media_uuid},
};
pub fn media_commands() -> CommandLineInterface {
let cmd_def = CliCommandMap::new()
.insert(
"list",
CliCommand::new(&API_METHOD_LIST_MEDIA)
.completion_cb("pool", complete_pool_name)
.completion_cb("update-status-changer", complete_changer_name)
.completion_cb("update-status-changer", complete_changer_name),
)
.insert(
"destroy",
CliCommand::new(&api2::tape::media::API_METHOD_DESTROY_MEDIA)
.arg_param(&["label-text"])
.completion_cb("label-text", complete_media_label_text)
.completion_cb("label-text", complete_media_label_text),
)
.insert(
"content",
@ -41,9 +36,8 @@ pub fn media_commands() -> CommandLineInterface {
.completion_cb("pool", complete_pool_name)
.completion_cb("label-text", complete_media_label_text)
.completion_cb("media", complete_media_uuid)
.completion_cb("media-set", complete_media_set_uuid)
)
;
.completion_cb("media-set", complete_media_set_uuid),
);
cmd_def.into()
}
@ -74,11 +68,7 @@ pub fn media_commands() -> CommandLineInterface {
},
)]
/// List pool media
async fn list_media(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
async fn list_media(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::tape::media::API_METHOD_LIST_MEDIA;
let mut data = match info.handler {
@ -89,17 +79,17 @@ async fn list_media(
fn render_status(_value: &Value, record: &Value) -> Result<String, Error> {
let record: MediaListEntry = serde_json::from_value(record.clone())?;
Ok(match record.status {
MediaStatus::Damaged | MediaStatus::Retired => {
serde_json::to_value(&record.status)?
.as_str().unwrap()
.to_string()
}
MediaStatus::Damaged | MediaStatus::Retired => serde_json::to_value(&record.status)?
.as_str()
.unwrap()
.to_string(),
_ => {
if record.expired {
String::from("expired")
} else {
serde_json::to_value(&record.status)?
.as_str().unwrap()
.as_str()
.unwrap()
.to_string()
}
}
@ -127,8 +117,7 @@ async fn list_media(
.column(ColumnConfig::new("location"))
.column(ColumnConfig::new("catalog").renderer(catalog_status))
.column(ColumnConfig::new("uuid"))
.column(ColumnConfig::new("media-set-uuid"))
;
.column(ColumnConfig::new("media-set-uuid"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -150,11 +139,7 @@ async fn list_media(
},
)]
/// List media content
fn list_content(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn list_content(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::tape::media::API_METHOD_LIST_CONTENT;
let mut data = match info.handler {
@ -174,11 +159,9 @@ fn list_content(
.column(ColumnConfig::new("seq-nr"))
.column(ColumnConfig::new("store"))
.column(ColumnConfig::new("snapshot"))
.column(ColumnConfig::new("media-set-uuid"))
;
.column(ColumnConfig::new("media-set-uuid"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
Ok(())
}

View File

@ -1,4 +1,4 @@
use anyhow::{Error};
use anyhow::Error;
use serde_json::Value;
use proxmox_router::{cli::*, ApiHandler, RpcEnvironment};
@ -11,35 +11,34 @@ use pbs_config::tape_encryption_keys::complete_key_fingerprint;
use proxmox_backup::api2;
pub fn pool_commands() -> CommandLineInterface {
let cmd_def = CliCommandMap::new()
.insert("list", CliCommand::new(&API_METHOD_LIST_POOLS))
.insert("config",
.insert(
"config",
CliCommand::new(&API_METHOD_GET_CONFIG)
.arg_param(&["name"])
.completion_cb("name", complete_pool_name)
.completion_cb("name", complete_pool_name),
)
.insert(
"remove",
CliCommand::new(&api2::config::media_pool::API_METHOD_DELETE_POOL)
.arg_param(&["name"])
.completion_cb("name", complete_pool_name)
.completion_cb("name", complete_pool_name),
)
.insert(
"create",
CliCommand::new(&api2::config::media_pool::API_METHOD_CREATE_POOL)
.arg_param(&["name"])
.completion_cb("name", complete_pool_name)
.completion_cb("encrypt", complete_key_fingerprint)
.completion_cb("encrypt", complete_key_fingerprint),
)
.insert(
"update",
CliCommand::new(&api2::config::media_pool::API_METHOD_UPDATE_POOL)
.arg_param(&["name"])
.completion_cb("name", complete_pool_name)
.completion_cb("encrypt", complete_key_fingerprint)
)
;
.completion_cb("encrypt", complete_key_fingerprint),
);
cmd_def.into()
}
@ -55,11 +54,7 @@ pub fn pool_commands() -> CommandLineInterface {
},
)]
/// List media pool
fn list_pools(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn list_pools(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::config::media_pool::API_METHOD_LIST_POOLS;
let mut data = match info.handler {
@ -80,8 +75,7 @@ fn list_pools(
.column(ColumnConfig::new("allocation"))
.column(ColumnConfig::new("retention"))
.column(ColumnConfig::new("template"))
.column(ColumnConfig::new("encrypt").renderer(render_encryption))
;
.column(ColumnConfig::new("encrypt").renderer(render_encryption));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
@ -102,11 +96,7 @@ fn list_pools(
},
)]
/// Get media pool configuration
fn get_config(
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
fn get_config(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<(), Error> {
let output_format = get_output_format(&param);
let info = &api2::config::media_pool::API_METHOD_GET_CONFIG;
let mut data = match info.handler {
@ -119,8 +109,7 @@ fn get_config(
.column(ColumnConfig::new("allocation"))
.column(ColumnConfig::new("retention"))
.column(ColumnConfig::new("template"))
.column(ColumnConfig::new("encrypt"))
;
.column(ColumnConfig::new("encrypt"));
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);