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 23, 2024
1 parent 57b0e98 commit ca24144
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- Add `apply_target_id` field to `Node` struct for reverting node status.
- Add `apply_in_progress` field to `Node` struct for reverting node status.
- Add `node_shutdown` Graphql API.

### Fixed

Expand Down
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
58 changes: 57 additions & 1 deletion src/graphql/node/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl NodeControlMutation {
let agents = ctx.data::<BoxedAgentManager>()?;
let review_hostname = roxy::hostname();
if !review_hostname.is_empty() && review_hostname == hostname {
roxy::reboot().map_or_else(|e| Err(e.to_string().into()), |_| Ok(hostname))
Err("reboot the system".into())
} else {
let apps = agents.online_apps_by_host_id().await?;
let Some(apps) = apps.get(&hostname) else {
Expand All @@ -40,4 +40,60 @@ 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 {
Err("shutdown the system".into())
} 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),
)
}
}
}

#[cfg(test)]
mod tests {
use crate::graphql::TestSchema;
#[tokio::test]
async fn test_node_reboot() {
let schema = TestSchema::new().await;

// node_shutdown
let res = schema
.execute(
r#"mutation {
nodeReboot(hostname:"localhost")
}"#,
)
.await;

for error in res.errors {
eprintln!("GraphQL error: {:?}", error);
}

assert_eq!(res.data.to_string(), r#"{nodeReboot: "localhost"}"#);
}
}

0 comments on commit ca24144

Please sign in to comment.