Skip to content

Commit

Permalink
Make some codes more clear using cmd_lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Dec 19, 2024
1 parent e485698 commit d8e121b
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 280 deletions.
94 changes: 94 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ time = "0.3.37"
uuid = { version = "1.11.0", features = ["v4"] }
which = "7.0.0"
smallvec = "1.13.2"
cmd_lib = "1.9.5"

[patch.crates-io]
tokio-vsock = { git = "https://github.com/kvinwang/tokio-vsock", branch = "shared-self-accept" }
1 change: 1 addition & 0 deletions tappd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ default-net.workspace = true
rocket-vsock-listener.workspace = true
sd-notify.workspace = true
reqwest.workspace = true
cmd_lib.workspace = true
16 changes: 8 additions & 8 deletions tappd/src/guest_api_service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{path::Path, process::Command};
use std::{fmt::Debug, path::Path};

use anyhow::{Context, Result};
use bollard::{container::ListContainersOptions, Docker};
use cmd_lib::run_cmd as cmd;
use fs_err as fs;
use guest_api::{
guest_api_server::{GuestApiRpc, GuestApiServer},
Expand All @@ -12,6 +13,7 @@ use host_api::Notification;
use ra_rpc::{CallContext, RpcCall};
use serde::Deserialize;
use tappd_rpc::worker_server::WorkerRpc as _;
use tracing::error;

use crate::{rpc_service::ExternalRpcHandler, AppState};

Expand Down Expand Up @@ -57,9 +59,9 @@ impl GuestApiRpc for GuestApiHandler {
async fn shutdown(self) -> Result<()> {
tokio::spawn(async move {
notify_host("shutdown.progress", "stopping app").await.ok();
run_command("systemctl stop app-compose").ok();
perr(cmd!(systemctl stop app-compose));
notify_host("shutdown.progress", "powering off").await.ok();
run_command("systemctl poweroff").ok();
perr(cmd!(systemctl poweroff));
});
Ok(())
}
Expand Down Expand Up @@ -217,10 +219,8 @@ pub async fn notify_host(event: &str, payload: &str) -> Result<()> {
Ok(())
}

fn run_command(command: &str) -> Result<()> {
let output = Command::new("sh").arg("-c").arg(command).output()?;
if !output.status.success() {
return Err(anyhow::anyhow!("Command failed: {}", output.status));
fn perr<T, E: Debug>(result: Result<T, E>) {
if let Err(e) = &result {
error!("{e:?}");
}
Ok(())
}
1 change: 1 addition & 0 deletions tdxctl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ra-tls.workspace = true
tproxy-rpc.workspace = true
tdx-attest.workspace = true
host-api = { workspace = true, features = ["client"] }
cmd_lib.workspace = true

[dev-dependencies]
rand.workspace = true
Loading

0 comments on commit d8e121b

Please sign in to comment.