2020-04-17 12:11:25 +00:00
|
|
|
//use anyhow::{bail, format_err, Error};
|
2019-06-27 07:01:41 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-05-18 07:57:35 +00:00
|
|
|
use serde_json::{json, Value};
|
2019-06-27 07:01:41 +00:00
|
|
|
|
2019-11-21 13:36:28 +00:00
|
|
|
use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
|
|
|
|
|
2019-06-27 07:01:41 +00:00
|
|
|
use crate::server::WorkerTask;
|
|
|
|
use crate::backup::*;
|
|
|
|
use crate::server::formatter::*;
|
|
|
|
|
|
|
|
//use proxmox::tools;
|
|
|
|
|
|
|
|
/// `RpcEnvironmet` implementation for backup reader service
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ReaderEnvironment {
|
|
|
|
env_type: RpcEnvironmentType,
|
2020-05-18 07:57:35 +00:00
|
|
|
result_attributes: Value,
|
2019-06-27 07:01:41 +00:00
|
|
|
user: String,
|
|
|
|
pub debug: bool,
|
|
|
|
pub formatter: &'static OutputFormatter,
|
|
|
|
pub worker: Arc<WorkerTask>,
|
|
|
|
pub datastore: Arc<DataStore>,
|
|
|
|
pub backup_dir: BackupDir,
|
|
|
|
// state: Arc<Mutex<SharedBackupState>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReaderEnvironment {
|
|
|
|
pub fn new(
|
|
|
|
env_type: RpcEnvironmentType,
|
|
|
|
user: String,
|
|
|
|
worker: Arc<WorkerTask>,
|
|
|
|
datastore: Arc<DataStore>,
|
|
|
|
backup_dir: BackupDir,
|
|
|
|
) -> Self {
|
|
|
|
|
|
|
|
|
|
|
|
Self {
|
2020-05-18 07:57:35 +00:00
|
|
|
result_attributes: json!({}),
|
2019-06-27 07:01:41 +00:00
|
|
|
env_type,
|
|
|
|
user,
|
|
|
|
worker,
|
|
|
|
datastore,
|
|
|
|
debug: false,
|
|
|
|
formatter: &JSON_FORMATTER,
|
|
|
|
backup_dir,
|
|
|
|
//state: Arc::new(Mutex::new(state)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log<S: AsRef<str>>(&self, msg: S) {
|
|
|
|
self.worker.log(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug<S: AsRef<str>>(&self, msg: S) {
|
|
|
|
if self.debug { self.worker.log(msg); }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RpcEnvironment for ReaderEnvironment {
|
|
|
|
|
2020-05-18 07:57:35 +00:00
|
|
|
fn result_attrib_mut(&mut self) -> &mut Value {
|
|
|
|
&mut self.result_attributes
|
2019-06-27 07:01:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 07:57:35 +00:00
|
|
|
fn result_attrib(&self) -> &Value {
|
|
|
|
&self.result_attributes
|
2019-06-27 07:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn env_type(&self) -> RpcEnvironmentType {
|
|
|
|
self.env_type
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_user(&mut self, _user: Option<String>) {
|
|
|
|
panic!("unable to change user");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_user(&self) -> Option<String> {
|
|
|
|
Some(self.user.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<ReaderEnvironment> for dyn RpcEnvironment {
|
|
|
|
fn as_ref(&self) -> &ReaderEnvironment {
|
|
|
|
self.as_any().downcast_ref::<ReaderEnvironment>().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<ReaderEnvironment> for Box<dyn RpcEnvironment> {
|
|
|
|
fn as_ref(&self) -> &ReaderEnvironment {
|
|
|
|
self.as_any().downcast_ref::<ReaderEnvironment>().unwrap()
|
|
|
|
}
|
|
|
|
}
|