init, shutdown and restart

This commit is contained in:
Aiden McClelland
2021-09-03 18:00:38 -06:00
parent 169e6e6562
commit b0a64e3f6b
14 changed files with 585 additions and 98 deletions

View File

@@ -1,4 +1,52 @@
use std::sync::Arc;
use rpc_toolkit::command;
use crate::context::RpcContext;
use crate::disk::main::export_blocking;
use crate::util::display_none;
use crate::Error;
#[derive(Debug, Clone)]
pub struct Shutdown {
zfs_pool: Arc<String>,
restart: bool,
}
impl Shutdown {
/// BLOCKING
pub fn execute(&self) {
use std::process::Command;
if let Err(e) = export_blocking(&self.zfs_pool) {
log::error!("Error Exporting ZFS Pool: {}", e);
}
if self.restart {
Command::new("reboot").spawn().unwrap().wait().unwrap();
} else {
Command::new("shutdown")
.arg("now")
.spawn()
.unwrap()
.wait()
.unwrap();
}
}
}
#[command(display(display_none))]
pub async fn shutdown(#[context] ctx: RpcContext) -> Result<(), Error> {
ctx.shutdown.send(Some(Shutdown {
zfs_pool: ctx.zfs_pool_name.clone(),
restart: false,
}));
Ok(())
}
#[command(display(display_none))]
pub async fn restart(#[context] ctx: RpcContext) -> Result<(), Error> {
ctx.shutdown.send(Some(Shutdown {
zfs_pool: ctx.zfs_pool_name.clone(),
restart: true,
}));
Ok(())
}