Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrap errors #35

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 57 additions & 12 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub use givc_common::query::{Event, QueryResult};
use givc_common::types::*;

use crate::endpoint::{EndpointConfig, TlsConfig};
use crate::error::StatusWrapExt;

type Client = pb::admin_service_client::AdminServiceClient<Channel>;

Expand Down Expand Up @@ -94,7 +95,12 @@ impl AdminClient {
vm_name,
args,
};
let _response = self.connect_to().await?.start_application(request).await?;
let _response = self
.connect_to()
.await?
.start_application(request)
.await
.rewrap_err()?;
Ok(())
}

Expand All @@ -104,7 +110,12 @@ impl AdminClient {
vm_name: None,
args: Vec::new(),
};
let _response = self.connect_to().await?.stop_application(request).await?;
let _response = self
.connect_to()
.await?
.stop_application(request)
.await
.rewrap_err()?;
Ok(())
}

Expand All @@ -114,7 +125,12 @@ impl AdminClient {
vm_name: None,
args: Vec::new(),
};
let _response = self.connect_to().await?.pause_application(request).await?;
let _response = self
.connect_to()
.await?
.pause_application(request)
.await
.rewrap_err()?;
Ok(())
}

Expand All @@ -124,31 +140,56 @@ impl AdminClient {
vm_name: None,
args: Vec::new(),
};
let _response = self.connect_to().await?.resume_application(request).await?;
let _response = self
.connect_to()
.await?
.resume_application(request)
.await
.rewrap_err()?;
Ok(())
}

pub async fn reboot(&self) -> anyhow::Result<()> {
let request = pb::admin::Empty {};
let _response = self.connect_to().await?.reboot(request).await?;
let _response = self
.connect_to()
.await?
.reboot(request)
.await
.rewrap_err()?;
Ok(())
}

pub async fn poweroff(&self) -> anyhow::Result<()> {
let request = pb::admin::Empty {};
let _response = self.connect_to().await?.poweroff(request).await?;
let _response = self
.connect_to()
.await?
.poweroff(request)
.await
.rewrap_err()?;
Ok(())
}

pub async fn suspend(&self) -> anyhow::Result<()> {
let request = pb::admin::Empty {};
let _response = self.connect_to().await?.suspend(request).await?;
let _response = self
.connect_to()
.await?
.suspend(request)
.await
.rewrap_err()?;
Ok(())
}

pub async fn wakeup(&self) -> anyhow::Result<()> {
let request = pb::admin::Empty {};
let _response = self.connect_to().await?.wakeup(request).await?;
let _response = self
.connect_to()
.await?
.wakeup(request)
.await
.rewrap_err()?;
Ok(())
}

Expand All @@ -165,7 +206,8 @@ impl AdminClient {
self.connect_to()
.await?
.query_list(pb::admin::Empty {})
.await?
.await
.rewrap_err()?
.into_inner()
.list
.into_iter()
Expand All @@ -177,15 +219,17 @@ impl AdminClient {
self.connect_to()
.await?
.set_locale(pb::admin::LocaleRequest { locale })
.await?;
.await
.rewrap_err()?;
Ok(())
}

pub async fn set_timezone(&self, timezone: String) -> anyhow::Result<()> {
self.connect_to()
.await?
.set_timezone(pb::admin::TimezoneRequest { timezone })
.await?;
.await
.rewrap_err()?;
Ok(())
}

Expand All @@ -199,7 +243,8 @@ impl AdminClient {
.connect_to()
.await?
.watch(pb::admin::Empty {})
.await?
.await
.rewrap_err()?
.into_inner();

let list = match watch.try_next().await? {
Expand Down
26 changes: 26 additions & 0 deletions client/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anyhow::Error;
use tonic::Status;
use tonic_types::StatusExt;

fn rewrap_error(status: Status) -> Error {
let mut err = Error::msg(status.message().to_owned());
let details = status.get_error_details();
if let Some(debug_info) = details.debug_info() {
err = err.context(format!("Detail: {}", debug_info.detail));
err = debug_info
.stack_entries
.iter()
.fold(err, |err, each| err.context(format!("Stack: {each}")))
};
err
}

pub trait StatusWrapExt<T> {
fn rewrap_err(self) -> Result<T, Error>;
}

impl<T> StatusWrapExt<T> for Result<T, Status> {
fn rewrap_err(self) -> Result<T, Error> {
self.map_err(rewrap_error)
}
}
1 change: 1 addition & 0 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod client;
pub mod endpoint;
pub mod error;
pub use crate::client::{AdminClient, QueryResult};
1 change: 0 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
};
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
pre-commit-hooks-nix = {
url = "github:cachix/pre-commit-hooks.nix";
Expand Down
20 changes: 14 additions & 6 deletions src/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ impl AdminServiceImpl {
let vmservice = format_service_name(vmname, None);

/* Return error if the vm is not registered */
let endpoint = self.agent_endpoint(&vmservice)?;
let endpoint = self
.agent_endpoint(&vmservice)
.with_context(|| format!("{vmservice} not registered"))?;
let client = SystemDClient::new(endpoint);

/* Check status of the unit */
Expand All @@ -181,7 +183,7 @@ impl AdminServiceImpl {
Err(anyhow!("Service {unit} is not loaded!"))
}
Err(e) => {
eprintln!("Error retrieving unit status: {e}");
error!("Error retrieving unit status: {e}");
Err(e)
}
}
Expand All @@ -194,7 +196,7 @@ impl AdminServiceImpl {
let status = client
.get_remote_status(name.to_string())
.await
.with_context(|| format!("cannot retrieve vm status for {name}"))?;
.with_context(|| format!("cannot retrieve vm status for {name}, host agent failed"))?;

if status.load_state != "loaded" {
bail!("vm {name} not loaded")
Expand Down Expand Up @@ -224,6 +226,7 @@ impl AdminServiceImpl {
match (entry.r#type.vm, entry.r#type.service) {
(VmType::AppVM, ServiceType::App) => {
if entry.status.is_exitted() {
debug!("Deregister exitted {}", entry.name);
self.registry.deregister(&entry.name)?;
}
Ok(())
Expand All @@ -235,7 +238,10 @@ impl AdminServiceImpl {
.with_context(|| format!("handing error, by restart VM {}", entry.name))?;
Ok(()) // FIXME: should use `?` from line above, why it didn't work?
}
(x, y) => bail!("Don't known how to handle_error for VM type: {x:?}:{y:?}"),
(x, y) => {
error!("Don't known how to handle_error for VM type: {x:?}:{y:?}");
Ok(())
}
}
}

Expand Down Expand Up @@ -283,6 +289,7 @@ impl AdminServiceImpl {
if let Err(err) = self.monitor_routine().await {
error!("Error during watch: {err}");
}
info!("{:#?}", self.registry)
}
}

Expand All @@ -307,6 +314,7 @@ impl AdminServiceImpl {
match self.registry.by_name(&systemd_agent) {
std::result::Result::Ok(e) => e,
Err(_) => {
info!("Starting up VM {vm_name}");
self.start_vm(&vm_name)
.await
.with_context(|| format!("Starting vm for {name}"))?;
Expand Down Expand Up @@ -481,15 +489,15 @@ impl pb::admin_service_server::AdminService for AdminService {
escalate(request, |_| async {
self.inner
.send_system_command(String::from("suspend.target"))
.await;
.await?;
Ok(Empty {})
})
.await
}

async fn wakeup(
&self,
request: tonic::Request<Empty>,
_request: tonic::Request<Empty>,
) -> std::result::Result<tonic::Response<Empty>, tonic::Status> {
println!("Not supported");
Err(Status::unimplemented("Not supported"))
Expand Down
50 changes: 43 additions & 7 deletions src/systemd_api/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::pb;
use givc_client::endpoint::EndpointConfig;
use givc_client::error::StatusWrapExt;
use tonic::transport::Channel;

type Client = pb::systemd::unit_control_service_client::UnitControlServiceClient<Channel>;
Expand All @@ -24,7 +25,12 @@ impl SystemDClient {
unit: String,
) -> anyhow::Result<crate::types::UnitStatus> {
let request = pb::systemd::UnitRequest { unit_name: unit };
let response = self.connect().await?.get_unit_status(request).await?;
let response = self
.connect()
.await?
.get_unit_status(request)
.await
.rewrap_err()?;
let status = response
.into_inner()
.unit_status
Expand All @@ -42,35 +48,60 @@ impl SystemDClient {

pub async fn start_remote(&self, unit: String) -> anyhow::Result<String> {
let request = pb::systemd::UnitRequest { unit_name: unit };
let resp = self.connect().await?.start_unit(request).await?;
let resp = self
.connect()
.await?
.start_unit(request)
.await
.rewrap_err()?;
let status = resp.into_inner();
Ok(status.cmd_status)
}

pub async fn stop_remote(&self, unit: String) -> anyhow::Result<String> {
let request = pb::systemd::UnitRequest { unit_name: unit };
let resp = self.connect().await?.stop_unit(request).await?;
let resp = self
.connect()
.await?
.stop_unit(request)
.await
.rewrap_err()?;
let status = resp.into_inner();
Ok(status.cmd_status)
}

pub async fn kill_remote(&self, unit: String) -> anyhow::Result<String> {
let request = pb::systemd::UnitRequest { unit_name: unit };
let resp = self.connect().await?.kill_unit(request).await?;
let resp = self
.connect()
.await?
.kill_unit(request)
.await
.rewrap_err()?;
let status = resp.into_inner();
Ok(status.cmd_status)
}

pub async fn pause_remote(&self, unit: String) -> anyhow::Result<String> {
let request = pb::systemd::UnitRequest { unit_name: unit };
let resp = self.connect().await?.freeze_unit(request).await?;
let resp = self
.connect()
.await?
.freeze_unit(request)
.await
.rewrap_err()?;
let status = resp.into_inner();
Ok(status.cmd_status)
}

pub async fn resume_remote(&self, unit: String) -> anyhow::Result<String> {
let request = pb::systemd::UnitRequest { unit_name: unit };
let resp = self.connect().await?.unfreeze_unit(request).await?;
let resp = self
.connect()
.await?
.unfreeze_unit(request)
.await
.rewrap_err()?;
let status = resp.into_inner();
Ok(status.cmd_status)
}
Expand All @@ -84,7 +115,12 @@ impl SystemDClient {
unit_name: unit,
args,
};
let resp = self.connect().await?.start_application(request).await?;
let resp = self
.connect()
.await?
.start_application(request)
.await
.rewrap_err()?;
let status = resp.into_inner();
Ok(status.cmd_status)
}
Expand Down
Loading
Loading