Skip to content

Commit

Permalink
Add node shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
dayeon5470 committed Feb 19, 2024
1 parent 57b0e98 commit b909e89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ipnet = { version = "2", features = ["serde"] }
jsonwebtoken = "9"
lazy_static = "1"
num-traits = "0.2"
oinq = { git = "https://github.com/petabi/oinq.git", tag = "0.9.1" }
oinq = { git = "https://github.com/petabi/oinq.git", tag = "0.9.3" }
reqwest = { version = "0.11", default-features = false, features = [
"rustls-tls-native-roots",
] }
Expand Down
32 changes: 32 additions & 0 deletions src/graphql/node/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,36 @@ impl NodeControlMutation {
)
}
}

#[graphql(guard = "RoleGuard::new(Role::SystemAdministrator)
.or(RoleGuard::new(Role::SecurityAdministrator))")]
async fn node_shutdown(&self, ctx: &Context<'_>, hostname: String) -> Result<String> {
let agents = ctx.data::<BoxedAgentManager>()?;
let review_hostname = roxy::hostname();

if !review_hostname.is_empty() && review_hostname == hostname {
roxy::power_off().map_or_else(|e| Err(e.to_string().into()), |_| Ok(hostname))
} else {
let apps = agents.online_apps_by_host_id().await?;
let Some(apps) = apps.get(&hostname) else {
return Err("unable to gather info of online agents".into());
};
let Some((key, _)) = apps.first() else {
return Err("unable to access first of online agents".into());
};

let code: u32 = RequestCode::Shutdown.into();
let msg = bincode::serialize(&code)?;
let response = agents.send_and_recv(key, &msg).await?;
let Ok(response) =
bincode::DefaultOptions::new().deserialize::<Result<(), &str>>(&response)
else {
return Ok(hostname);
};
response.map_or_else(
|e| Err(format!("unable to shutdown the system: {e}").into()),
|()| Ok(hostname),
)
}
}
}

0 comments on commit b909e89

Please sign in to comment.