2022-10-19 20:32:15 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-10-18 18:03:19 +00:00
|
|
|
|
2022-10-26 22:13:42 +00:00
|
|
|
use crate::action::{Action, ActionDescription, ActionState};
|
2022-10-18 18:03:19 +00:00
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
|
|
|
|
pub struct EncryptVolume {
|
2022-10-19 20:32:15 +00:00
|
|
|
disk: PathBuf,
|
|
|
|
password: String,
|
2022-10-18 18:03:19 +00:00
|
|
|
action_state: ActionState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EncryptVolume {
|
|
|
|
#[tracing::instrument(skip_all)]
|
2022-10-19 20:32:15 +00:00
|
|
|
pub async fn plan(
|
|
|
|
disk: impl AsRef<Path>,
|
|
|
|
password: String,
|
2022-10-26 21:14:53 +00:00
|
|
|
) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
|
2022-10-18 18:03:19 +00:00
|
|
|
Ok(Self {
|
2022-10-19 20:32:15 +00:00
|
|
|
disk: disk.as_ref().to_path_buf(),
|
|
|
|
password,
|
2022-10-18 18:03:19 +00:00
|
|
|
action_state: ActionState::Uncompleted,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2022-10-28 19:44:07 +00:00
|
|
|
#[typetag::serde(name = "encrypt_volume")]
|
2022-10-26 22:13:42 +00:00
|
|
|
impl Action for EncryptVolume {
|
2022-10-18 18:03:19 +00:00
|
|
|
fn describe_execute(&self) -> Vec<ActionDescription> {
|
|
|
|
if self.action_state == ActionState::Completed {
|
|
|
|
vec![]
|
|
|
|
} else {
|
|
|
|
vec![ActionDescription::new(
|
2022-10-19 20:32:15 +00:00
|
|
|
format!("Encrypt volume `{}`", self.disk.display()),
|
|
|
|
vec![],
|
2022-10-18 18:03:19 +00:00
|
|
|
)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all, fields(
|
2022-10-19 20:32:15 +00:00
|
|
|
disk = %self.disk.display(),
|
2022-10-18 18:03:19 +00:00
|
|
|
))]
|
2022-10-26 21:14:53 +00:00
|
|
|
async fn execute(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
2022-10-19 20:32:15 +00:00
|
|
|
let Self {
|
2022-10-25 18:57:09 +00:00
|
|
|
disk: _,
|
|
|
|
password: _,
|
2022-10-19 20:32:15 +00:00
|
|
|
action_state,
|
|
|
|
} = self;
|
2022-10-18 18:03:19 +00:00
|
|
|
if *action_state == ActionState::Completed {
|
2022-10-19 20:32:15 +00:00
|
|
|
tracing::trace!("Already completed: Encrypting volume");
|
2022-10-18 18:03:19 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2022-10-19 20:32:15 +00:00
|
|
|
tracing::debug!("Encrypting volume");
|
2022-10-18 18:03:19 +00:00
|
|
|
|
2022-10-19 20:32:15 +00:00
|
|
|
todo!();
|
2022-10-18 18:03:19 +00:00
|
|
|
|
2022-10-19 20:32:15 +00:00
|
|
|
tracing::trace!("Encrypted volume");
|
2022-10-18 18:03:19 +00:00
|
|
|
*action_state = ActionState::Completed;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn describe_revert(&self) -> Vec<ActionDescription> {
|
|
|
|
if self.action_state == ActionState::Uncompleted {
|
|
|
|
vec![]
|
|
|
|
} else {
|
2022-10-19 20:32:15 +00:00
|
|
|
vec![]
|
2022-10-18 18:03:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all, fields(
|
2022-10-19 20:32:15 +00:00
|
|
|
disk = %self.disk.display(),
|
2022-10-18 18:03:19 +00:00
|
|
|
))]
|
2022-10-26 21:14:53 +00:00
|
|
|
async fn revert(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
2022-10-19 20:32:15 +00:00
|
|
|
let Self {
|
2022-10-25 18:57:09 +00:00
|
|
|
disk: _,
|
|
|
|
password: _,
|
2022-10-19 20:32:15 +00:00
|
|
|
action_state,
|
|
|
|
} = self;
|
2022-10-18 18:03:19 +00:00
|
|
|
if *action_state == ActionState::Uncompleted {
|
2022-10-19 20:32:15 +00:00
|
|
|
tracing::trace!("Already reverted: Unencrypted volume (noop)");
|
2022-10-18 18:03:19 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2022-10-19 20:32:15 +00:00
|
|
|
tracing::debug!("Unencrypted volume (noop)");
|
2022-10-18 18:03:19 +00:00
|
|
|
|
2022-10-19 20:32:15 +00:00
|
|
|
tracing::trace!("Unencrypted volume (noop)");
|
2022-10-18 18:03:19 +00:00
|
|
|
*action_state = ActionState::Completed;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:13:42 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2022-10-18 18:03:19 +00:00
|
|
|
pub enum EncryptVolumeError {
|
|
|
|
#[error("Failed to execute command")]
|
2022-10-26 22:13:42 +00:00
|
|
|
Command(#[source] std::io::Error),
|
2022-10-18 18:03:19 +00:00
|
|
|
}
|