tape: split inventory api

inventory: sync, list labels with uuids,
update_inventory: WorkerTask, updates database
This commit is contained in:
Dietmar Maurer 2020-12-11 10:42:29 +01:00
parent 6dbad5b4b5
commit e92c75815b
3 changed files with 148 additions and 69 deletions

View File

@ -19,7 +19,10 @@ use proxmox::{
}; };
use crate::{ use crate::{
config, config::{
self,
drive::check_drive_exists,
},
api2::types::{ api2::types::{
UPID_SCHEMA, UPID_SCHEMA,
DRIVE_ID_SCHEMA, DRIVE_ID_SCHEMA,
@ -46,7 +49,7 @@ use crate::{
open_drive, open_drive,
media_changer, media_changer,
update_changer_online_status, update_changer_online_status,
file_formats::{ file_formats::{
DriveLabel, DriveLabel,
MediaSetLabel, MediaSetLabel,
}, },
@ -440,16 +443,6 @@ pub fn read_label(drive: String) -> Result<MediaLabelInfoFlat, Error> {
drive: { drive: {
schema: DRIVE_ID_SCHEMA, schema: DRIVE_ID_SCHEMA,
}, },
"read-labels": {
description: "Load unknown tapes and try read labels",
type: bool,
optional: true,
},
"read-all-labels": {
description: "Load all tapes and try read labels (even if already inventoried)",
type: bool,
optional: true,
},
}, },
}, },
returns: { returns: {
@ -460,23 +453,20 @@ pub fn read_label(drive: String) -> Result<MediaLabelInfoFlat, Error> {
}, },
}, },
)] )]
/// List (and update) media labels (Changer Inventory) /// List known media labels (Changer Inventory)
/// ///
/// Note: Only useful for drives with associated changer device. /// Note: Only useful for drives with associated changer device.
/// ///
/// This method queries the changer to get a list of media labels. It /// This method queries the changer to get a list of media labels.
/// 'read-labels' is set, it then loads any unknown media into the ///
/// drive, reads the label, and store the result to the media /// Note: This updates the media online status.
/// database.
pub fn inventory( pub fn inventory(
drive: String, drive: String,
read_labels: Option<bool>,
read_all_labels: Option<bool>,
) -> Result<Vec<LabelUuidMap>, Error> { ) -> Result<Vec<LabelUuidMap>, Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
let (mut changer, changer_name) = media_changer(&config, &drive, false)?; let (changer, changer_name) = media_changer(&config, &drive, false)?;
let changer_id_list = changer.list_media_changer_ids()?; let changer_id_list = changer.list_media_changer_ids()?;
@ -489,8 +479,6 @@ pub fn inventory(
let mut list = Vec::new(); let mut list = Vec::new();
let do_read = read_labels.unwrap_or(false) || read_all_labels.unwrap_or(false);
for changer_id in changer_id_list.iter() { for changer_id in changer_id_list.iter() {
if changer_id.starts_with("CLN") { if changer_id.starts_with("CLN") {
// skip cleaning unit // skip cleaning unit
@ -499,52 +487,120 @@ pub fn inventory(
let changer_id = changer_id.to_string(); let changer_id = changer_id.to_string();
if !read_all_labels.unwrap_or(false) { if let Some(media_id) = inventory.find_media_by_changer_id(&changer_id) {
if let Some(media_id) = inventory.find_media_by_changer_id(&changer_id) { list.push(LabelUuidMap { changer_id, uuid: Some(media_id.label.uuid.to_string()) });
list.push(LabelUuidMap { changer_id, uuid: Some(media_id.label.uuid.to_string()) }); } else {
continue;
}
}
if !do_read {
list.push(LabelUuidMap { changer_id, uuid: None }); list.push(LabelUuidMap { changer_id, uuid: None });
continue;
}
if let Err(err) = changer.load_media(&changer_id) {
eprintln!("unable to load media '{}' - {}", changer_id, err);
list.push(LabelUuidMap { changer_id, uuid: None });
continue;
}
let mut drive = open_drive(&config, &drive)?;
match drive.read_label() {
Err(err) => {
eprintln!("unable to read label form media '{}' - {}", changer_id, err);
list.push(LabelUuidMap { changer_id, uuid: None });
}
Ok(None) => {
// no label on media (empty)
list.push(LabelUuidMap { changer_id, uuid: None });
}
Ok(Some(info)) => {
if changer_id != info.label.changer_id {
eprintln!("label changer ID missmatch ({} != {})", changer_id, info.label.changer_id);
list.push(LabelUuidMap { changer_id, uuid: None });
continue;
}
let uuid = info.label.uuid.to_string();
inventory.store(info.into())?;
list.push(LabelUuidMap { changer_id, uuid: Some(uuid) });
}
} }
} }
Ok(list) Ok(list)
} }
#[api(
input: {
properties: {
drive: {
schema: DRIVE_ID_SCHEMA,
},
"read-all-labels": {
description: "Load all tapes and try read labels (even if already inventoried)",
type: bool,
optional: true,
},
},
},
returns: {
schema: UPID_SCHEMA,
},
)]
/// Update inventory
///
/// Note: Only useful for drives with associated changer device.
///
/// This method queries the changer to get a list of media labels. It
/// then loads any unknown media into the drive, reads the label, and
/// store the result to the media database.
///
/// Note: This updates the media online status.
pub fn update_inventory(
drive: String,
read_all_labels: Option<bool>,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
let (config, _digest) = config::drive::config()?;
check_drive_exists(&config, &drive)?; // early check before starting worker
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let upid_str = WorkerTask::new_thread(
"inventory-update",
Some(drive.clone()),
auth_id,
true,
move |worker| {
let (mut changer, changer_name) = media_changer(&config, &drive, false)?;
let changer_id_list = changer.list_media_changer_ids()?;
if changer_id_list.is_empty() {
worker.log(format!("changer device does not list any media labels"));
}
let state_path = Path::new(TAPE_STATUS_DIR);
let mut inventory = Inventory::load(state_path)?;
let mut state_db = MediaStateDatabase::load(state_path)?;
update_changer_online_status(&config, &mut inventory, &mut state_db, &changer_name, &changer_id_list)?;
for changer_id in changer_id_list.iter() {
if changer_id.starts_with("CLN") {
worker.log(format!("skip cleaning unit '{}'", changer_id));
continue;
}
let changer_id = changer_id.to_string();
if !read_all_labels.unwrap_or(false) {
if let Some(_) = inventory.find_media_by_changer_id(&changer_id) {
worker.log(format!("media '{}' already inventoried", changer_id));
continue;
}
}
if let Err(err) = changer.load_media(&changer_id) {
worker.warn(format!("unable to load media '{}' - {}", changer_id, err));
continue;
}
let mut drive = open_drive(&config, &drive)?;
match drive.read_label() {
Err(err) => {
worker.warn(format!("unable to read label form media '{}' - {}", changer_id, err));
}
Ok(None) => {
worker.log(format!("media '{}' is empty", changer_id));
}
Ok(Some(info)) => {
if changer_id != info.label.changer_id {
worker.warn(format!("label changer ID missmatch ({} != {})", changer_id, info.label.changer_id));
continue;
}
worker.log(format!("inventorize media '{}' with uuid '{}'", changer_id, info.label.uuid));
inventory.store(info.into())?;
}
}
}
Ok(())
}
)?;
Ok(upid_str.into())
}
#[api( #[api(
input: { input: {
@ -684,6 +740,7 @@ pub const SUBDIRS: SubdirMap = &sorted!([
"inventory", "inventory",
&Router::new() &Router::new()
.get(&API_METHOD_INVENTORY) .get(&API_METHOD_INVENTORY)
.put(&API_METHOD_UPDATE_INVENTORY)
), ),
( (
"label-media", "label-media",

View File

@ -1,5 +1,5 @@
use anyhow::{format_err, Error}; use anyhow::{format_err, Error};
use serde_json::Value; use serde_json::{json, Value};
use proxmox::{ use proxmox::{
api::{ api::{
@ -323,18 +323,39 @@ fn read_label(
}, },
}, },
)] )]
/// List or update media labels (Changer Inventory) /// List (and update) media labels (Changer Inventory)
fn inventory( async fn inventory(
mut param: Value, read_labels: Option<bool>,
read_all_labels: Option<bool>,
param: Value,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> { ) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into();
let output_format = get_output_format(&param); let output_format = get_output_format(&param);
let (config, _digest) = config::drive::config()?;
let drive = lookup_drive_name(&param, &config)?;
let do_read = read_labels.unwrap_or(false) || read_all_labels.unwrap_or(false);
if do_read {
let mut param = json!({
"drive": &drive,
});
if let Some(true) = read_all_labels {
param["read-all-labels"] = true.into();
}
let info = &api2::tape::drive::API_METHOD_UPDATE_INVENTORY;
let result = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),
};
wait_for_local_worker(result.as_str().unwrap()).await?;
}
let info = &api2::tape::drive::API_METHOD_INVENTORY; let info = &api2::tape::drive::API_METHOD_INVENTORY;
let param = json!({ "drive": &drive });
let mut data = match info.handler { let mut data = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(), _ => unreachable!(),

View File

@ -278,6 +278,7 @@ Ext.define('PBS.Utils', {
dircreate: [gettext('Directory Storage'), gettext('Create')], dircreate: [gettext('Directory Storage'), gettext('Create')],
dirremove: [gettext('Directory'), gettext('Remove')], dirremove: [gettext('Directory'), gettext('Remove')],
garbage_collection: ['Datastore', gettext('Garbage collect')], garbage_collection: ['Datastore', gettext('Garbage collect')],
"inventory-update": [gettext('Drive'), gettext('Inventory update')],
"label-media": [gettext('Drive'), gettext('Label media')], "label-media": [gettext('Drive'), gettext('Label media')],
logrotate: [null, gettext('Log Rotation')], logrotate: [null, gettext('Log Rotation')],
prune: (type, id) => PBS.Utils.render_datastore_worker_id(id, gettext('Prune')), prune: (type, id) => PBS.Utils.render_datastore_worker_id(id, gettext('Prune')),