Skip to content

Commit

Permalink
feat(swarm): list all running instances (ports etc) (#1051)
Browse files Browse the repository at this point in the history
Description
---
Lists all instances in swarm UI

Motivation and Context
---
Allows the user to easily see all allocated ports and base dir for each
running instance. This allows the user to work around the signaling
server URL not being specified in the connect link and manually specify
the port in e.g. template-web. Since we may change the wallet daemon to
allow browser connections through the webtransport, websockets etc using
libp2p negating the need for the signaling server, I did not implement
changes to the URL.

How Has This Been Tested?
---
Manually

What process can a PR reviewer use to test or verify this change?
---
Run swarm

Breaking Changes
---

- [x] None
- [ ] Requires data directory to be deleted
- [ ] Other - Please specify
  • Loading branch information
sdbondi authored Jun 25, 2024
1 parent 3941ac4 commit 143006b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl AllocatedPorts {
self.ports[name]
}

pub fn into_ports(self) -> HashMap<&'static str, u16> {
self.ports
}

pub async fn get_or_next_port(&mut self, name: &'static str) -> u16 {
if let Some(port) = self.ports.get(name) {
return *port;
Expand Down
48 changes: 45 additions & 3 deletions applications/tari_swarm_daemon/src/webserver/rpc/instances.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::{collections::HashMap, path::PathBuf};

use axum_jrpc::error::{JsonRpcError, JsonRpcErrorReason};
use serde::{Deserialize, Serialize};

use crate::webserver::context::HandlerContext;
use crate::{config::InstanceType, process_manager::InstanceId, webserver::context::HandlerContext};

pub type StartInstanceRequest = String;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize)]
pub struct StartInstanceResponse {
pub success: bool,
}
Expand Down Expand Up @@ -38,7 +40,7 @@ pub async fn start(

pub type StopInstanceRequest = String;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize)]
pub struct StopInstanceResponse {
pub success: bool,
}
Expand All @@ -63,6 +65,46 @@ pub async fn stop(context: &HandlerContext, req: StopInstanceRequest) -> Result<
Ok(StopInstanceResponse { success: true })
}

#[derive(Debug, Clone, Deserialize)]
pub struct ListInstancesRequest {
pub by_type: Option<InstanceType>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ListInstancesResponse {
pub instances: Vec<InstanceInfo>,
}

#[derive(Debug, Clone, Serialize)]
pub struct InstanceInfo {
pub id: InstanceId,
pub name: String,
pub ports: HashMap<&'static str, u16>,
pub base_path: PathBuf,
pub instance_type: InstanceType,
pub is_running: bool,
}

impl From<crate::process_manager::InstanceInfo> for InstanceInfo {
fn from(value: crate::process_manager::InstanceInfo) -> Self {
Self {
id: value.id,
name: value.name,
ports: value.ports.into_ports(),
base_path: value.base_path,
instance_type: value.instance_type,
is_running: value.is_running,
}
}
}

pub async fn list(context: &HandlerContext, req: ListInstancesRequest) -> Result<ListInstancesResponse, anyhow::Error> {
let instances = context.process_manager().list_instances(req.by_type).await?;
Ok(ListInstancesResponse {
instances: instances.into_iter().map(Into::into).collect(),
})
}

#[derive(Debug, Clone, Deserialize)]
pub struct DeleteInstanceDataRequest {
pub name: String,
Expand Down
1 change: 1 addition & 0 deletions applications/tari_swarm_daemon/src/webserver/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ async fn json_rpc_handler(Extension(context): Extension<Arc<HandlerContext>>, va
"add_validator_node" => call_handler(context, value, rpc::validator_nodes::create).await,
"start" => call_handler(context, value, rpc::instances::start).await,
"stop" => call_handler(context, value, rpc::instances::stop).await,
"list_instances" => call_handler(context, value, rpc::instances::list).await,
"delete_data" => call_handler(context, value, rpc::instances::delete_data).await,
_ => Ok(value.method_not_found(&value.method)),
}
Expand Down
13 changes: 7 additions & 6 deletions applications/tari_swarm_daemon/webui/src/routes/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,13 @@ function ExtraInfoVN({name, url, setRow, addTxToPool, autoRefresh, state, horizo
return (<>
<hr/>
<h3>Pool transaction</h3>
<table style={{width: "100%"}}>
<tr>
<td>Tx Id</td>
<td>Ready</td>
<td>Decision</td>
<td>Stage</td>
<table style={{
width: "100%"}}>
<tr>
<td>Tx Id</td>
<td>Ready</td>
<td>Decision</td>
<td>Stage</td>
</tr>
{pool.map(({atom}, i) => (
<tr key={i}>
Expand Down

0 comments on commit 143006b

Please sign in to comment.