tape: cleanup/simplify media_change code

This commit is contained in:
Dietmar Maurer
2020-12-30 17:16:57 +01:00
parent ff58c51919
commit 284eb5daff
4 changed files with 74 additions and 82 deletions

View File

@ -2,56 +2,30 @@ use anyhow::Error;
use proxmox::tools::email::sendmail;
use super::MediaChange;
/// Send email to a person to request a manual media change
pub struct ChangeMediaEmail {
drive: String,
to: String,
}
impl ChangeMediaEmail {
pub fn new(drive: &str, to: &str) -> Self {
Self {
drive: String::from(drive),
to: String::from(to),
}
}
}
impl MediaChange for ChangeMediaEmail {
fn load_media(&mut self, changer_id: &str) -> Result<(), Error> {
let subject = format!("Load Media '{}' request for drive '{}'", changer_id, self.drive);
let mut text = String::new();
text.push_str("Please insert the requested media into the backup drive.\n\n");
text.push_str(&format!("Drive: {}\n", self.drive));
text.push_str(&format!("Media: {}\n", changer_id));
sendmail(
&[&self.to],
&subject,
Some(&text),
None,
None,
None,
)?;
Ok(())
}
fn unload_media(&mut self) -> Result<(), Error> {
/* ignore ? */
Ok(())
}
fn list_media_changer_ids(&self) -> Result<Vec<String>, Error> {
Ok(Vec::new())
}
pub fn send_load_media_email(
drive: &str,
changer_id: &str,
to: &str,
) -> Result<(), Error> {
let subject = format!("Load Media '{}' request for drive '{}'", changer_id, drive);
let mut text = String::new();
text.push_str("Please insert the requested media into the backup drive.\n\n");
text.push_str(&format!("Drive: {}\n", drive));
text.push_str(&format!("Media: {}\n", changer_id));
sendmail(
&[to],
&subject,
Some(&text),
None,
None,
None,
)?;
Ok(())
}

View File

@ -33,7 +33,7 @@ use crate::{
},
changer::{
MediaChange,
ChangeMediaEmail,
send_load_media_email,
},
},
};
@ -146,41 +146,32 @@ pub trait TapeDriver {
fn eject_media(&mut self) -> Result<(), Error>;
}
/// Get the media changer (name + MediaChange) associated with a tape drie.
/// Get the media changer (MediaChange + name) associated with a tape drive.
///
/// If allow_email is set, returns an ChangeMediaEmail instance for
/// standalone tape drives (changer name set to "").
/// Returns Ok(None) if the drive has no associated changer device.
pub fn media_changer(
config: &SectionConfigData,
drive: &str,
allow_email: bool,
) -> Result<(Box<dyn MediaChange>, String), Error> {
) -> Result<Option<(Box<dyn MediaChange>, String)>, Error> {
match config.sections.get(drive) {
Some((section_type_name, config)) => {
match section_type_name.as_ref() {
"virtual" => {
let tape = VirtualTapeDrive::deserialize(config)?;
Ok((Box::new(tape), drive.to_string()))
Ok(Some((Box::new(tape), drive.to_string())))
}
"linux" => {
let tape = LinuxTapeDrive::deserialize(config)?;
match tape.changer {
Some(ref changer_name) => {
let changer_name = changer_name.to_string();
Ok((Box::new(tape), changer_name))
Ok(Some((Box::new(tape), changer_name)))
}
None => {
if !allow_email {
bail!("drive '{}' has no changer device", drive);
}
let to = "root@localhost"; // fixme
let changer = ChangeMediaEmail::new(drive, to);
Ok((Box::new(changer), String::new()))
},
None => Ok(None),
}
}
_ => bail!("drive type '{}' not implemented!"),
_ => bail!("unknown drive type '{}' - internal error"),
}
}
None => {
@ -189,6 +180,26 @@ pub fn media_changer(
}
}
/// Get the media changer (MediaChange + name) associated with a tape drive.
///
/// This fail if the drive has no associated changer device.
pub fn required_media_changer(
config: &SectionConfigData,
drive: &str,
) -> Result<(Box<dyn MediaChange>, String), Error> {
match media_changer(config, drive) {
Ok(Some(result)) => {
return Ok(result);
}
Ok(None) => {
bail!("drive '{}' has no associated changer device", drive);
},
Err(err) => {
return Err(err);
}
}
}
pub fn open_drive(
config: &SectionConfigData,
drive: &str,
@ -209,7 +220,7 @@ pub fn open_drive(
.map_err(|err| format_err!("open drive '{}' ({}) failed - {}", drive, tape.path, err))?;
Ok(Box::new(handle))
}
_ => bail!("drive type '{}' not implemented!"),
_ => bail!("unknown drive type '{}' - internal error"),
}
}
None => {
@ -235,7 +246,11 @@ pub fn request_and_load_media(
let check_label = |handle: &mut dyn TapeDriver, uuid: &proxmox::tools::Uuid| {
if let Ok(Some(media_id)) = handle.read_label() {
println!("found media label {} ({})", media_id.label.changer_id, media_id.label.uuid.to_string());
worker.log(format!(
"found media label {} ({})",
media_id.label.changer_id,
media_id.label.uuid.to_string(),
));
if media_id.label.uuid == *uuid {
return Ok(media_id);
}
@ -278,9 +293,8 @@ pub fn request_and_load_media(
worker.log(format!("Please insert media '{}' into drive '{}'", changer_id, drive));
let to = "root@localhost"; // fixme
let mut changer = ChangeMediaEmail::new(drive, to);
changer.load_media(&changer_id)?; // semd email
send_load_media_email(drive, &changer_id, to)?;
let mut last_media_uuid = None;