proxmox-backup/src/bin/proxmox-tape.rs

183 lines
4.0 KiB
Rust
Raw Normal View History

2020-12-09 16:35:31 +00:00
use anyhow::{format_err, Error};
use serde_json::Value;
use proxmox::{
api::{
2020-12-09 16:35:31 +00:00
api,
cli::*,
2020-12-09 16:35:31 +00:00
ApiHandler,
RpcEnvironment,
2020-12-09 16:35:31 +00:00
section_config::SectionConfigData,
},
};
use proxmox_backup::{
api2::{
self,
types::{
DRIVE_ID_SCHEMA,
},
},
config::{
self,
drive::complete_drive_name,
},
};
mod proxmox_tape;
use proxmox_tape::*;
2020-12-09 16:35:31 +00:00
fn lookup_drive_name(
param: &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; }
drive_names.push(name);
}
if drive_names.len() == 1 {
Some(drive_names[0].to_owned())
} else {
None
}
})
.ok_or_else(|| format_err!("unable to get (default) drive name"))?;
Ok(drive)
}
#[api(
input: {
properties: {
drive: {
schema: DRIVE_ID_SCHEMA,
optional: true,
},
fast: {
description: "Use fast erase.",
type: bool,
optional: true,
default: true,
},
},
},
)]
/// Erase media
fn erase_media(
mut param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into();
let info = &api2::tape::drive::API_METHOD_ERASE_MEDIA;
match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
Ok(())
}
2020-12-09 16:43:38 +00:00
#[api(
input: {
properties: {
drive: {
schema: DRIVE_ID_SCHEMA,
optional: true,
},
},
},
)]
/// Rewind tape
fn rewind(
mut param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
2020-12-09 16:50:48 +00:00
2020-12-09 16:43:38 +00:00
let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into();
let info = &api2::tape::drive::API_METHOD_REWIND;
match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
Ok(())
}
2020-12-09 16:50:48 +00:00
#[api(
input: {
properties: {
drive: {
schema: DRIVE_ID_SCHEMA,
optional: true,
},
},
},
)]
/// Eject/Unload drive media
fn eject_media(
mut param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into();
let info = &api2::tape::drive::API_METHOD_EJECT_MEDIA;
match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
Ok(())
}
fn main() {
let cmd_def = CliCommandMap::new()
2020-12-09 16:43:38 +00:00
.insert(
"rewind",
CliCommand::new(&API_METHOD_REWIND)
.completion_cb("drive", complete_drive_name)
)
2020-12-09 16:35:31 +00:00
.insert(
"erase",
CliCommand::new(&API_METHOD_ERASE_MEDIA)
.completion_cb("drive", complete_drive_name)
)
2020-12-09 16:50:48 +00:00
.insert(
"eject",
CliCommand::new(&API_METHOD_EJECT_MEDIA)
.completion_cb("drive", complete_drive_name)
)
.insert("changer", changer_commands())
.insert("drive", drive_commands())
;
let mut rpcenv = CliEnvironment::new();
rpcenv.set_auth_id(Some(String::from("root@pam")));
proxmox_backup::tools::runtime::main(run_async_cli_command(cmd_def, rpcenv));
}