2021-01-07 14:12:19 +00:00
|
|
|
use anyhow::{bail, Error};
|
2020-12-09 11:58:43 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use proxmox::{
|
|
|
|
api::{
|
|
|
|
api,
|
|
|
|
cli::*,
|
|
|
|
RpcEnvironment,
|
|
|
|
ApiHandler,
|
2021-01-07 14:12:19 +00:00
|
|
|
section_config::SectionConfigData,
|
2020-12-09 11:58:43 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
use proxmox_backup::{
|
|
|
|
api2::{
|
|
|
|
self,
|
|
|
|
types::{
|
2020-12-13 08:22:08 +00:00
|
|
|
CHANGER_NAME_SCHEMA,
|
2020-12-09 11:58:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
tape::{
|
2021-01-21 16:25:32 +00:00
|
|
|
complete_changer_path,
|
2021-01-21 16:12:01 +00:00
|
|
|
drive::{
|
|
|
|
media_changer,
|
|
|
|
},
|
2020-12-09 11:58:43 +00:00
|
|
|
},
|
|
|
|
config::{
|
2021-01-07 14:12:19 +00:00
|
|
|
self,
|
2020-12-09 11:58:43 +00:00
|
|
|
drive::{
|
|
|
|
complete_drive_name,
|
|
|
|
complete_changer_name,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-07 14:12:19 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(drive) = crate::lookup_drive_name(&Value::Null, config) {
|
|
|
|
if let Ok(Some((_, name))) = media_changer(config, &drive) {
|
|
|
|
return Ok(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!("unable to get (default) changer name");
|
|
|
|
}
|
|
|
|
|
2020-12-09 11:58:43 +00:00
|
|
|
pub fn changer_commands() -> CommandLineInterface {
|
|
|
|
|
|
|
|
let cmd_def = CliCommandMap::new()
|
2020-12-10 06:58:45 +00:00
|
|
|
.insert("scan", CliCommand::new(&API_METHOD_SCAN_FOR_CHANGERS))
|
2020-12-09 11:58:43 +00:00
|
|
|
.insert("list", CliCommand::new(&API_METHOD_LIST_CHANGERS))
|
|
|
|
.insert("config",
|
|
|
|
CliCommand::new(&API_METHOD_GET_CONFIG)
|
|
|
|
.arg_param(&["name"])
|
|
|
|
.completion_cb("name", complete_changer_name)
|
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"remove",
|
2020-12-10 07:35:11 +00:00
|
|
|
CliCommand::new(&api2::config::changer::API_METHOD_DELETE_CHANGER)
|
2020-12-09 11:58:43 +00:00
|
|
|
.arg_param(&["name"])
|
|
|
|
.completion_cb("name", complete_changer_name)
|
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"create",
|
2020-12-10 07:35:11 +00:00
|
|
|
CliCommand::new(&api2::config::changer::API_METHOD_CREATE_CHANGER)
|
2020-12-09 11:58:43 +00:00
|
|
|
.arg_param(&["name"])
|
|
|
|
.completion_cb("name", complete_drive_name)
|
|
|
|
.completion_cb("path", complete_changer_path)
|
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"update",
|
2020-12-10 07:35:11 +00:00
|
|
|
CliCommand::new(&api2::config::changer::API_METHOD_UPDATE_CHANGER)
|
2020-12-09 11:58:43 +00:00
|
|
|
.arg_param(&["name"])
|
|
|
|
.completion_cb("name", complete_changer_name)
|
|
|
|
.completion_cb("path", complete_changer_path)
|
|
|
|
)
|
|
|
|
.insert("status",
|
|
|
|
CliCommand::new(&API_METHOD_GET_STATUS)
|
|
|
|
.arg_param(&["name"])
|
|
|
|
.completion_cb("name", complete_changer_name)
|
|
|
|
)
|
|
|
|
.insert("transfer",
|
2021-01-07 16:09:47 +00:00
|
|
|
CliCommand::new(&API_METHOD_TRANSFER)
|
2020-12-09 11:58:43 +00:00
|
|
|
.arg_param(&["name"])
|
|
|
|
.completion_cb("name", complete_changer_name)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
|
|
|
cmd_def.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// List changers
|
|
|
|
fn list_changers(
|
|
|
|
param: Value,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
2021-01-25 15:30:57 +00:00
|
|
|
let info = &api2::tape::changer::API_METHOD_LIST_CHANGERS;
|
2020-12-09 11:58:43 +00:00
|
|
|
let mut data = match info.handler {
|
|
|
|
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let options = default_table_format_options()
|
|
|
|
.column(ColumnConfig::new("name"))
|
|
|
|
.column(ColumnConfig::new("path"))
|
|
|
|
.column(ColumnConfig::new("vendor"))
|
|
|
|
.column(ColumnConfig::new("model"))
|
|
|
|
.column(ColumnConfig::new("serial"))
|
|
|
|
;
|
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
|
2020-12-09 11:58:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Scan for SCSI tape changers
|
|
|
|
fn scan_for_changers(
|
|
|
|
param: Value,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
let info = &api2::tape::changer::API_METHOD_SCAN_CHANGERS;
|
|
|
|
let mut data = match info.handler {
|
|
|
|
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let options = default_table_format_options()
|
|
|
|
.column(ColumnConfig::new("path"))
|
|
|
|
.column(ColumnConfig::new("vendor"))
|
|
|
|
.column(ColumnConfig::new("model"))
|
|
|
|
.column(ColumnConfig::new("serial"))
|
|
|
|
;
|
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
|
2020-12-09 11:58:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
name: {
|
2020-12-13 08:22:08 +00:00
|
|
|
schema: CHANGER_NAME_SCHEMA,
|
2020-12-09 11:58:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Get tape changer configuration
|
|
|
|
fn get_config(
|
|
|
|
param: Value,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
let info = &api2::config::changer::API_METHOD_GET_CONFIG;
|
|
|
|
let mut data = match info.handler {
|
|
|
|
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let options = default_table_format_options()
|
|
|
|
.column(ColumnConfig::new("name"))
|
|
|
|
.column(ColumnConfig::new("path"))
|
2021-01-06 10:06:50 +00:00
|
|
|
.column(ColumnConfig::new("export-slots"))
|
2020-12-09 11:58:43 +00:00
|
|
|
;
|
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
|
2020-12-09 11:58:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-07 14:12:19 +00:00
|
|
|
name: {
|
2020-12-13 08:22:08 +00:00
|
|
|
schema: CHANGER_NAME_SCHEMA,
|
2021-01-07 14:12:19 +00:00
|
|
|
optional: true,
|
2020-12-09 11:58:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Get tape changer status
|
2020-12-14 06:14:24 +00:00
|
|
|
async fn get_status(
|
2021-01-07 14:12:19 +00:00
|
|
|
mut param: Value,
|
2020-12-09 11:58:43 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2021-01-07 14:12:19 +00:00
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
param["name"] = lookup_changer_name(¶m, &config)?.into();
|
|
|
|
|
2020-12-09 11:58:43 +00:00
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
let info = &api2::tape::changer::API_METHOD_GET_STATUS;
|
|
|
|
let mut data = match info.handler {
|
2020-12-14 06:14:24 +00:00
|
|
|
ApiHandler::Async(handler) => (handler)(param, info, rpcenv).await?,
|
2020-12-09 11:58:43 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
let render_label_text = |value: &Value, _record: &Value| -> Result<String, Error> {
|
2021-01-04 11:06:05 +00:00
|
|
|
if value.is_null() {
|
|
|
|
return Ok(String::new());
|
|
|
|
}
|
|
|
|
let text = value.as_str().unwrap().to_string();
|
|
|
|
if text.is_empty() {
|
2021-01-19 09:50:42 +00:00
|
|
|
Ok(String::from("--FULL--"))
|
2021-01-04 11:06:05 +00:00
|
|
|
} else {
|
|
|
|
Ok(text)
|
|
|
|
}
|
|
|
|
};
|
2021-01-07 14:12:19 +00:00
|
|
|
|
2020-12-09 11:58:43 +00:00
|
|
|
let options = default_table_format_options()
|
2020-12-28 12:32:56 +00:00
|
|
|
.sortby("entry-kind", false)
|
|
|
|
.sortby("entry-id", false)
|
2020-12-09 11:58:43 +00:00
|
|
|
.column(ColumnConfig::new("entry-kind"))
|
|
|
|
.column(ColumnConfig::new("entry-id"))
|
2021-01-13 12:26:59 +00:00
|
|
|
.column(ColumnConfig::new("label-text").renderer(render_label_text))
|
2020-12-09 11:58:43 +00:00
|
|
|
.column(ColumnConfig::new("loaded-slot"))
|
|
|
|
;
|
|
|
|
|
2020-12-18 11:26:07 +00:00
|
|
|
format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
|
2020-12-09 11:58:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-01-07 16:09:47 +00:00
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
schema: CHANGER_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
from: {
|
|
|
|
description: "Source slot number",
|
|
|
|
type: u64,
|
|
|
|
minimum: 1,
|
|
|
|
},
|
|
|
|
to: {
|
|
|
|
description: "Destination slot number",
|
|
|
|
type: u64,
|
|
|
|
minimum: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Transfers media from one slot to another
|
|
|
|
pub async fn transfer(
|
|
|
|
mut param: Value,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
param["name"] = lookup_changer_name(¶m, &config)?.into();
|
|
|
|
|
|
|
|
let info = &api2::tape::changer::API_METHOD_TRANSFER;
|
|
|
|
match info.handler {
|
|
|
|
ApiHandler::Async(handler) => (handler)(param, info, rpcenv).await?,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|