2020-12-14 06:55:57 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
2020-12-14 07:58:40 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2020-12-29 10:58:26 +00:00
|
|
|
use serde::{Serialize, Deserialize};
|
2020-12-14 06:55:57 +00:00
|
|
|
|
2020-12-14 07:58:40 +00:00
|
|
|
use proxmox::{
|
|
|
|
api::{api, Router, SubdirMap},
|
|
|
|
list_subdirs_api_method,
|
2021-01-20 16:53:06 +00:00
|
|
|
tools::Uuid,
|
2020-12-14 07:58:40 +00:00
|
|
|
};
|
2020-12-14 06:55:57 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
config::{
|
|
|
|
self,
|
|
|
|
},
|
|
|
|
api2::types::{
|
2020-12-29 10:58:26 +00:00
|
|
|
BACKUP_ID_SCHEMA,
|
|
|
|
BACKUP_TYPE_SCHEMA,
|
2020-12-14 06:55:57 +00:00
|
|
|
MEDIA_POOL_NAME_SCHEMA,
|
2020-12-14 07:58:40 +00:00
|
|
|
MEDIA_LABEL_SCHEMA,
|
2021-01-20 16:53:06 +00:00
|
|
|
MEDIA_UUID_SCHEMA,
|
|
|
|
MEDIA_SET_UUID_SCHEMA,
|
2020-12-14 06:55:57 +00:00
|
|
|
MediaPoolConfig,
|
|
|
|
MediaListEntry,
|
|
|
|
MediaStatus,
|
2020-12-29 10:58:26 +00:00
|
|
|
MediaContentEntry,
|
|
|
|
},
|
|
|
|
backup::{
|
|
|
|
BackupDir,
|
2020-12-14 06:55:57 +00:00
|
|
|
},
|
|
|
|
tape::{
|
|
|
|
TAPE_STATUS_DIR,
|
|
|
|
Inventory,
|
|
|
|
MediaPool,
|
2020-12-29 10:58:26 +00:00
|
|
|
MediaCatalog,
|
2021-01-21 16:12:01 +00:00
|
|
|
changer::update_online_status,
|
2020-12-14 06:55:57 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
pool: {
|
|
|
|
schema: MEDIA_POOL_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "List of registered backup media.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: MediaListEntry,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// List pool media
|
|
|
|
pub async fn list_media(pool: Option<String>) -> Result<Vec<MediaListEntry>, Error> {
|
|
|
|
|
|
|
|
let (config, _digest) = config::media_pool::config()?;
|
|
|
|
|
|
|
|
let status_path = Path::new(TAPE_STATUS_DIR);
|
|
|
|
|
2021-01-09 09:24:48 +00:00
|
|
|
let catalogs = tokio::task::spawn_blocking(move || {
|
|
|
|
// update online media status
|
2020-12-14 06:55:57 +00:00
|
|
|
if let Err(err) = update_online_status(status_path) {
|
|
|
|
eprintln!("{}", err);
|
|
|
|
eprintln!("update online media status failed - using old state");
|
|
|
|
}
|
2021-01-09 09:24:48 +00:00
|
|
|
// test what catalog files we have
|
|
|
|
MediaCatalog::media_with_catalogs(status_path)
|
|
|
|
}).await??;
|
2020-12-14 06:55:57 +00:00
|
|
|
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
for (_section_type, data) in config.sections.values() {
|
|
|
|
let pool_name = match data["name"].as_str() {
|
|
|
|
None => continue,
|
|
|
|
Some(name) => name,
|
|
|
|
};
|
|
|
|
if let Some(ref name) = pool {
|
|
|
|
if name != pool_name {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let config: MediaPoolConfig = config.lookup("pool", pool_name)?;
|
|
|
|
|
2021-01-01 09:03:59 +00:00
|
|
|
let use_offline_media = true; // does not matter here
|
|
|
|
let pool = MediaPool::with_config(status_path, &config, use_offline_media)?;
|
2020-12-14 06:55:57 +00:00
|
|
|
|
|
|
|
let current_time = proxmox::tools::time::epoch_i64();
|
|
|
|
|
|
|
|
for media in pool.list_media() {
|
|
|
|
let expired = pool.media_is_expired(&media, current_time);
|
|
|
|
|
2021-01-12 11:00:39 +00:00
|
|
|
let media_set_uuid = media.media_set_label()
|
2021-01-20 16:53:06 +00:00
|
|
|
.map(|set| set.uuid.clone());
|
2020-12-14 06:55:57 +00:00
|
|
|
|
2021-01-12 11:00:39 +00:00
|
|
|
let seq_nr = media.media_set_label()
|
2020-12-14 06:55:57 +00:00
|
|
|
.map(|set| set.seq_nr);
|
|
|
|
|
2021-01-12 11:00:39 +00:00
|
|
|
let media_set_name = media.media_set_label()
|
2020-12-14 06:55:57 +00:00
|
|
|
.map(|set| {
|
|
|
|
pool.generate_media_set_name(&set.uuid, config.template.clone())
|
|
|
|
.unwrap_or_else(|_| set.uuid.to_string())
|
|
|
|
});
|
|
|
|
|
2021-01-09 09:24:48 +00:00
|
|
|
let catalog_ok = if media.media_set_label().is_none() {
|
|
|
|
// Media is empty, we need no catalog
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
catalogs.contains(media.uuid())
|
|
|
|
};
|
|
|
|
|
2020-12-14 06:55:57 +00:00
|
|
|
list.push(MediaListEntry {
|
2021-01-20 16:53:06 +00:00
|
|
|
uuid: media.uuid().clone(),
|
2021-01-13 12:26:59 +00:00
|
|
|
label_text: media.label_text().to_string(),
|
2021-01-12 11:00:39 +00:00
|
|
|
ctime: media.ctime(),
|
2020-12-14 06:55:57 +00:00
|
|
|
pool: Some(pool_name.to_string()),
|
2020-12-16 09:45:58 +00:00
|
|
|
location: media.location().clone(),
|
2020-12-14 06:55:57 +00:00
|
|
|
status: *media.status(),
|
2021-01-09 09:24:48 +00:00
|
|
|
catalog: catalog_ok,
|
2020-12-14 06:55:57 +00:00
|
|
|
expired,
|
2021-01-12 11:00:39 +00:00
|
|
|
media_set_ctime: media.media_set_label().map(|set| set.ctime),
|
2020-12-14 06:55:57 +00:00
|
|
|
media_set_uuid,
|
|
|
|
media_set_name,
|
|
|
|
seq_nr,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pool.is_none() {
|
|
|
|
|
|
|
|
let inventory = Inventory::load(status_path)?;
|
|
|
|
|
|
|
|
for media_id in inventory.list_unassigned_media() {
|
|
|
|
|
2021-01-01 15:15:13 +00:00
|
|
|
let (mut status, location) = inventory.status_and_location(&media_id.label.uuid);
|
2020-12-14 06:55:57 +00:00
|
|
|
|
|
|
|
if status == MediaStatus::Unknown {
|
|
|
|
status = MediaStatus::Writable;
|
|
|
|
}
|
|
|
|
|
|
|
|
list.push(MediaListEntry {
|
2021-01-20 16:53:06 +00:00
|
|
|
uuid: media_id.label.uuid.clone(),
|
2021-01-12 11:00:39 +00:00
|
|
|
ctime: media_id.label.ctime,
|
2021-01-13 12:26:59 +00:00
|
|
|
label_text: media_id.label.label_text.to_string(),
|
2020-12-14 06:55:57 +00:00
|
|
|
location,
|
|
|
|
status,
|
2021-01-09 09:24:48 +00:00
|
|
|
catalog: true, // empty, so we do not need a catalog
|
2020-12-14 06:55:57 +00:00
|
|
|
expired: false,
|
|
|
|
media_set_uuid: None,
|
|
|
|
media_set_name: None,
|
2021-01-12 11:00:39 +00:00
|
|
|
media_set_ctime: None,
|
2020-12-14 06:55:57 +00:00
|
|
|
seq_nr: None,
|
|
|
|
pool: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
|
2020-12-14 07:58:40 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
2021-01-13 12:26:59 +00:00
|
|
|
"label-text": {
|
2020-12-14 07:58:40 +00:00
|
|
|
schema: MEDIA_LABEL_SCHEMA,
|
|
|
|
},
|
|
|
|
force: {
|
|
|
|
description: "Force removal (even if media is used in a media set).",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Destroy media (completely remove from database)
|
2021-01-13 12:26:59 +00:00
|
|
|
pub fn destroy_media(label_text: String, force: Option<bool>,) -> Result<(), Error> {
|
2020-12-14 07:58:40 +00:00
|
|
|
|
|
|
|
let force = force.unwrap_or(false);
|
|
|
|
|
|
|
|
let status_path = Path::new(TAPE_STATUS_DIR);
|
|
|
|
let mut inventory = Inventory::load(status_path)?;
|
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
let media_id = inventory.find_media_by_label_text(&label_text)
|
|
|
|
.ok_or_else(|| format_err!("no such media '{}'", label_text))?;
|
2020-12-14 07:58:40 +00:00
|
|
|
|
|
|
|
if !force {
|
|
|
|
if let Some(ref set) = media_id.media_set_label {
|
|
|
|
let is_empty = set.uuid.as_ref() == [0u8;16];
|
|
|
|
if !is_empty {
|
2021-01-13 12:26:59 +00:00
|
|
|
bail!("media '{}' contains data (please use 'force' flag to remove.", label_text);
|
2020-12-14 07:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let uuid = media_id.label.uuid.clone();
|
|
|
|
|
|
|
|
inventory.remove_media(&uuid)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:58:26 +00:00
|
|
|
#[api(
|
|
|
|
properties: {
|
|
|
|
pool: {
|
|
|
|
schema: MEDIA_POOL_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-13 12:26:59 +00:00
|
|
|
"label-text": {
|
2020-12-29 10:58:26 +00:00
|
|
|
schema: MEDIA_LABEL_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"media": {
|
2021-01-20 16:53:06 +00:00
|
|
|
schema: MEDIA_UUID_SCHEMA,
|
2020-12-29 10:58:26 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"media-set": {
|
2021-01-20 16:53:06 +00:00
|
|
|
schema: MEDIA_SET_UUID_SCHEMA,
|
2020-12-29 10:58:26 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"backup-type": {
|
|
|
|
schema: BACKUP_TYPE_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"backup-id": {
|
|
|
|
schema: BACKUP_ID_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
#[derive(Serialize,Deserialize)]
|
|
|
|
#[serde(rename_all="kebab-case")]
|
|
|
|
/// Content list filter parameters
|
|
|
|
pub struct MediaContentListFilter {
|
|
|
|
pub pool: Option<String>,
|
2021-01-13 12:26:59 +00:00
|
|
|
pub label_text: Option<String>,
|
2021-01-20 16:53:06 +00:00
|
|
|
pub media: Option<Uuid>,
|
|
|
|
pub media_set: Option<Uuid>,
|
2020-12-29 10:58:26 +00:00
|
|
|
pub backup_type: Option<String>,
|
|
|
|
pub backup_id: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"filter": {
|
|
|
|
type: MediaContentListFilter,
|
|
|
|
flatten: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "Media content list.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: MediaContentEntry,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// List media content
|
|
|
|
pub fn list_content(
|
|
|
|
filter: MediaContentListFilter,
|
|
|
|
) -> Result<Vec<MediaContentEntry>, Error> {
|
|
|
|
|
|
|
|
let (config, _digest) = config::media_pool::config()?;
|
|
|
|
|
|
|
|
let status_path = Path::new(TAPE_STATUS_DIR);
|
|
|
|
let inventory = Inventory::load(status_path)?;
|
|
|
|
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
for media_id in inventory.list_used_media() {
|
|
|
|
let set = media_id.media_set_label.as_ref().unwrap();
|
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
if let Some(ref label_text) = filter.label_text {
|
|
|
|
if &media_id.label.label_text != label_text { continue; }
|
2020-12-29 10:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref pool) = filter.pool {
|
|
|
|
if &set.pool != pool { continue; }
|
|
|
|
}
|
|
|
|
|
2021-01-20 16:53:06 +00:00
|
|
|
if let Some(ref media_uuid) = filter.media {
|
2020-12-29 10:58:26 +00:00
|
|
|
if &media_id.label.uuid != media_uuid { continue; }
|
|
|
|
}
|
|
|
|
|
2021-01-20 16:53:06 +00:00
|
|
|
if let Some(ref media_set_uuid) = filter.media_set {
|
2020-12-29 10:58:26 +00:00
|
|
|
if &set.uuid != media_set_uuid { continue; }
|
|
|
|
}
|
|
|
|
|
|
|
|
let config: MediaPoolConfig = config.lookup("pool", &set.pool)?;
|
|
|
|
|
|
|
|
let media_set_name = inventory
|
|
|
|
.generate_media_set_name(&set.uuid, config.template.clone())
|
|
|
|
.unwrap_or_else(|_| set.uuid.to_string());
|
|
|
|
|
|
|
|
let catalog = MediaCatalog::open(status_path, &media_id.label.uuid, false, false)?;
|
|
|
|
|
|
|
|
for snapshot in catalog.snapshot_index().keys() {
|
|
|
|
let backup_dir: BackupDir = snapshot.parse()?;
|
|
|
|
|
|
|
|
if let Some(ref backup_type) = filter.backup_type {
|
|
|
|
if backup_dir.group().backup_type() != backup_type { continue; }
|
|
|
|
}
|
|
|
|
if let Some(ref backup_id) = filter.backup_id {
|
|
|
|
if backup_dir.group().backup_id() != backup_id { continue; }
|
|
|
|
}
|
|
|
|
|
|
|
|
list.push(MediaContentEntry {
|
2021-01-20 16:53:06 +00:00
|
|
|
uuid: media_id.label.uuid.clone(),
|
2021-01-13 12:26:59 +00:00
|
|
|
label_text: media_id.label.label_text.to_string(),
|
2020-12-29 10:58:26 +00:00
|
|
|
pool: set.pool.clone(),
|
|
|
|
media_set_name: media_set_name.clone(),
|
2021-01-20 16:53:06 +00:00
|
|
|
media_set_uuid: set.uuid.clone(),
|
2020-12-29 10:58:26 +00:00
|
|
|
seq_nr: set.seq_nr,
|
|
|
|
snapshot: snapshot.to_owned(),
|
|
|
|
backup_time: backup_dir.backup_time(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
|
2020-12-14 06:55:57 +00:00
|
|
|
const SUBDIRS: SubdirMap = &[
|
2020-12-29 11:09:51 +00:00
|
|
|
(
|
|
|
|
"content",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_LIST_CONTENT)
|
|
|
|
),
|
2020-12-14 07:58:40 +00:00
|
|
|
(
|
|
|
|
"destroy",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_DESTROY_MEDIA)
|
|
|
|
),
|
2020-12-14 06:55:57 +00:00
|
|
|
(
|
|
|
|
"list",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_LIST_MEDIA)
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(&list_subdirs_api_method!(SUBDIRS))
|
|
|
|
.subdirs(SUBDIRS);
|