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::{
config,
config::{
self,
drive::check_drive_exists,
},
api2::types::{
UPID_SCHEMA,
DRIVE_ID_SCHEMA,
@ -46,7 +49,7 @@ use crate::{
open_drive,
media_changer,
update_changer_online_status,
file_formats::{
file_formats::{
DriveLabel,
MediaSetLabel,
},
@ -440,16 +443,6 @@ pub fn read_label(drive: String) -> Result<MediaLabelInfoFlat, Error> {
drive: {
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: {
@ -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.
///
/// This method queries the changer to get a list of media labels. It
/// 'read-labels' is set, it then loads any unknown media into the
/// drive, reads the label, and store the result to the media
/// database.
/// This method queries the changer to get a list of media labels.
///
/// Note: This updates the media online status.
pub fn inventory(
drive: String,
read_labels: Option<bool>,
read_all_labels: Option<bool>,
) -> Result<Vec<LabelUuidMap>, Error> {
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()?;
@ -489,8 +479,6 @@ pub fn inventory(
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() {
if changer_id.starts_with("CLN") {
// skip cleaning unit
@ -499,52 +487,120 @@ pub fn inventory(
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) {
list.push(LabelUuidMap { changer_id, uuid: Some(media_id.label.uuid.to_string()) });
continue;
}
}
if !do_read {
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()) });
} else {
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)
}
#[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(
input: {
@ -684,6 +740,7 @@ pub const SUBDIRS: SubdirMap = &sorted!([
"inventory",
&Router::new()
.get(&API_METHOD_INVENTORY)
.put(&API_METHOD_UPDATE_INVENTORY)
),
(
"label-media",

View File

@ -1,5 +1,5 @@
use anyhow::{format_err, Error};
use serde_json::Value;
use serde_json::{json, Value};
use proxmox::{
api::{
@ -323,18 +323,39 @@ fn read_label(
},
},
)]
/// List or update media labels (Changer Inventory)
fn inventory(
mut param: Value,
/// List (and update) media labels (Changer Inventory)
async fn inventory(
read_labels: Option<bool>,
read_all_labels: Option<bool>,
param: Value,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into();
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 param = json!({ "drive": &drive });
let mut data = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(),

View File

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