Created
March 26, 2020 00:31
-
-
Save dhilst/37de54f6c600e6a2ad165d5639b8b24c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused_imports)] | |
use super::config::Machine; | |
use super::email; | |
use futures::future::join_all; | |
use log; | |
use std::io; | |
use std::process::{Command, ExitStatus}; | |
use tokio::task::spawn_blocking; | |
fn run_cmd(cmd: &'static str, args: Vec<&str>) -> io::Result<ExitStatus> { | |
Command::new(cmd).args(args).status() | |
} | |
fn shutdown_arg(node: &Machine, mode: &str) -> io::Result<ExitStatus> { | |
run_cmd( | |
"ipmitool", | |
vec![ | |
"-I", | |
"lanplus", | |
"-H", | |
&node.bmc_ip, | |
"-U", | |
&node.bmc_user, | |
"-P", | |
&node.bmc_password, | |
"chassis", | |
"power", | |
mode, | |
], | |
) | |
} | |
async fn shutdown_gracefully(node: &'static Machine) -> io::Result<ExitStatus> { | |
spawn_blocking(move || shutdown_arg(&node, "soft")).await? | |
} | |
async fn shutdown_force(node: &'static Machine) { | |
if let Err(err) = spawn_blocking(move || shutdown_arg(&node, "off")).await { | |
log::error!("Failed to shutdown {}", node.name); | |
} | |
} | |
async fn shutdown(node: &'static Machine) { | |
if let Err(err) = shutdown_gracefully(&node).await { | |
log::error!("Error while powering off {}, {}", node.name, err); | |
log::error!("Forcing shutdown of {}", node.name); | |
shutdown_force(&node).await; | |
} | |
} | |
pub async fn shutdown_machines(nodes: Vec<&'static Machine>) { | |
for node in nodes { | |
shutdown(node).await | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment