Skip to content

Commit

Permalink
feaat: show node network IPs when home-network is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Jan 17, 2025
1 parent 375e095 commit 210edf6
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 20 deletions.
1 change: 1 addition & 0 deletions migrations/20250116191347_node_ips.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE nodes ADD ips TEXT
8 changes: 8 additions & 0 deletions src/db_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct CachedNodeMetadata {
records: String,
connected_peers: String,
kbuckets_peers: String,
ips: String,
}

impl CachedNodeMetadata {
Expand Down Expand Up @@ -101,6 +102,9 @@ impl CachedNodeMetadata {
if let Ok(v) = self.kbuckets_peers.parse::<usize>() {
info.kbuckets_peers = Some(v);
}
if !self.ips.is_empty() {
info.ips = Some(self.ips.clone());
}
}
}

Expand Down Expand Up @@ -279,6 +283,10 @@ impl DbClient {
updates.push("kbuckets_peers=?");
params.push(kbuckets_peers.to_string());
}
if let Some(ips) = &info.ips {
updates.push("ips=?");
params.push(ips.clone());
}

if updates.is_empty() {
return; // no updates to make
Expand Down
35 changes: 25 additions & 10 deletions src/docker_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ impl DockerClient {
pub async fn start_container(
&self,
id: &ContainerId,
) -> Result<(Option<String>, Option<String>), DockerClientError> {
get_ips: bool,
) -> Result<(Option<String>, Option<String>, Option<String>), DockerClientError> {
let url = format!("{DOCKER_CONTAINERS_API}/{id}/start");
logging::log!("[START] Sending Docker request to START a container: {url} ...");
self.send_request(ReqMethod::post_empty_body(), &url, &[])
Expand All @@ -281,7 +282,7 @@ impl DockerClient {
.await?;

// let's try to retrieve new version
self.get_node_version_and_peer_id(id).await
self.get_node_version_and_peer_id(id, get_ips).await
}

// Request the Docker server to STOP a container matching the given id
Expand Down Expand Up @@ -416,7 +417,8 @@ impl DockerClient {
pub async fn upgrade_node_in_container(
&self,
id: &ContainerId,
) -> Result<Option<String>, DockerClientError> {
get_ips: bool,
) -> Result<(Option<String>, Option<String>), DockerClientError> {
logging::log!("[UPGRADE] Sending Docker request to UPGRADE node within a container...");

let cmd = "./antup node -n -p /app".to_string();
Expand All @@ -442,22 +444,23 @@ impl DockerClient {
}

// let's try to retrieve new version, forget it if there is any error
let (new_version, _) = self
.get_node_version_and_peer_id(id)
let (new_version, _, ips) = self
.get_node_version_and_peer_id(id, get_ips)
.await
.unwrap_or_default();

// restart container to run with new node version
self.restart_container(id).await?;

Ok(new_version)
Ok((new_version, ips))
}

// Retrieve version of the node binary and its peer id
pub async fn get_node_version_and_peer_id(
&self,
id: &ContainerId,
) -> Result<(Option<String>, Option<String>), DockerClientError> {
get_ips: bool,
) -> Result<(Option<String>, Option<String>, Option<String>), DockerClientError> {
let cmd = "/app/antnode --version | grep -oE 'Autonomi Node v[0-9]+\\.[0-9]+\\.[0-9]+.*$'"
.to_string();
let (_, resp_str) = self
Expand All @@ -481,14 +484,26 @@ impl DockerClient {
};
logging::log!("Node peer id in container {id}: {peer_id:?}");

Ok((version, peer_id))
let ips = if get_ips {
let cmd = "hostname -I | sed 's/^[ \t]*//;s/[ \t]*$//;s/ /, /g'".to_string();
let (_, ips) = self
.exec_in_container(id, cmd, "get node network IPs")
.await?;
logging::log!("Node IPs in container {id}: {ips}");
Some(ips)
} else {
None
};

Ok((version, peer_id, ips))
}

// Clears the node's PeerId within the containver and restarts it
pub async fn regenerate_peer_id_in_container(
&self,
id: &ContainerId,
) -> Result<(Option<String>, Option<String>), DockerClientError> {
get_ips: bool,
) -> Result<(Option<String>, Option<String>, Option<String>), DockerClientError> {
logging::log!("[RECYCLE] Recycling container by clearing node's peer-id ...");

// we write an empty file at '/app/node_data/secret-key-recycle' so the container removes
Expand All @@ -512,7 +527,7 @@ impl DockerClient {

logging::log!("Finished recycling node container: {id}");

self.get_node_version_and_peer_id(id).await
self.get_node_version_and_peer_id(id, get_ips).await
}

// Restart the container wich has given id
Expand Down
1 change: 1 addition & 0 deletions src/node_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub struct NodeInstanceInfo {
pub kbuckets_peers: Option<usize>,
pub shunned_count: Option<usize>,
pub net_size: Option<usize>,
pub ips: Option<String>,
}

impl NodeInstanceInfo {
Expand Down
26 changes: 24 additions & 2 deletions src/nodes_list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,30 @@ fn NodeInstanceView(
</div>
</p>
<p>
<span class="node-info-item">"Home-network: "</span>
{move || info.read().home_network}
<Show
when=move || !info.read().home_network
fallback=|| {
view! {
<span class="node-info-item">"Home-network: "</span>
"On"
}
}
>
<div class="flex flex-row">
<div class="basis-1/2">
<span class="node-info-item">"Home-network: "</span>
"Off"
</div>
<div class="basis-1/12">
<span class="node-info-item">"IPs: "</span>
</div>
<div class="basis-2/5 overflow-hidden relative">
<div class="absolute whitespace-nowrap animate-slide">
{move || info.get().ips.unwrap_or_default()}
</div>
</div>
</div>
</Show>
</p>
<p>
<span class="node-info-item">"Created: "</span>
Expand Down
30 changes: 22 additions & 8 deletions src/server_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,18 @@ async fn helper_start_node_instance(
.update_node_status(&container_id, NodeStatus::Restarting)
.await;

let (version, peer_id) = context.docker_client.start_container(&container_id).await?;
let (version, peer_id, ips) = context
.docker_client
.start_container(&container_id, true)
.await?;
context
.db_client
.update_node_metadata_fields(
&container_id,
&[
("bin_version", &version.unwrap_or_default()),
("peer_id", &peer_id.unwrap_or_default()),
("ips", &ips.unwrap_or_default()),
],
)
.await;
Expand Down Expand Up @@ -233,7 +237,11 @@ async fn helper_stop_node_instance(
.db_client
.update_node_metadata_fields(
&container_id,
&[("connected_peers", "0"), ("kbuckets_peers", "0")],
&[
("connected_peers", "0"),
("kbuckets_peers", "0"),
("ips", ""),
],
)
.await;
context
Expand Down Expand Up @@ -271,7 +279,7 @@ pub(crate) async fn helper_upgrade_node_instance(
node_status_locked: &ImmutableNodeStatus,
db_client: &DbClient,
docker_client: &DockerClient,
) -> Result<Option<String>, DockerClientError> {
) -> Result<(Option<String>, Option<String>), DockerClientError> {
// TODO: use docker 'extract' api to simply copy the new node binary into the container.
node_status_locked
.insert(
Expand All @@ -283,9 +291,11 @@ pub(crate) async fn helper_upgrade_node_instance(
.update_node_status(container_id, NodeStatus::Upgrading)
.await;

let res = docker_client.upgrade_node_in_container(container_id).await;
let res = docker_client
.upgrade_node_in_container(container_id, true)
.await;

if let Ok(ref new_version) = res {
if let Ok((ref new_version, ref ips)) = res {
logging::log!(
"Node binary upgraded to v{} in container {container_id}.",
new_version.as_deref().unwrap_or("[unknown]")
Expand All @@ -295,7 +305,10 @@ pub(crate) async fn helper_upgrade_node_instance(
db_client
.update_node_metadata_fields(
container_id,
&[("bin_version", new_version.as_deref().unwrap_or_default())],
&[
("bin_version", new_version.as_deref().unwrap_or_default()),
("ips", ips.as_deref().unwrap_or_default()),
],
)
.await;
db_client
Expand Down Expand Up @@ -379,9 +392,9 @@ pub async fn recycle_node_instance(container_id: ContainerId) -> Result<(), Serv
.update_node_status(&container_id, NodeStatus::Recycling)
.await;

let (version, peer_id) = context
let (version, peer_id, ips) = context
.docker_client
.regenerate_peer_id_in_container(&container_id)
.regenerate_peer_id_in_container(&container_id, true)
.await?;

context
Expand All @@ -391,6 +404,7 @@ pub async fn recycle_node_instance(container_id: ContainerId) -> Result<(), Serv
&[
("bin_version", &version.unwrap_or_default()),
("peer_id", &peer_id.unwrap_or_default()),
("ips", &ips.unwrap_or_default()),
],
)
.await;
Expand Down
13 changes: 13 additions & 0 deletions style/tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@
.btn-manage-nodes-action {
@apply flex justify-center items-center w-[52px] h-[52px] text-gray-500 hover:text-gray-900 bg-white rounded-full border border-gray-200 dark:border-gray-600 shadow-sm dark:hover:text-white dark:text-gray-400 hover:bg-gray-50 dark:bg-gray-700 dark:hover:bg-gray-600 focus:ring-4 focus:ring-gray-300 focus:outline-none dark:focus:ring-gray-400;
}

@keyframes slide {
0% {
transform: translateX(25%); /* Start off-screen to the right */
}
100% {
transform: translateX(-85%); /* Slide out of view to the left */
}
}

.animate-slide {
animation: slide 25s linear infinite; /* Apply the sliding animation */
}

0 comments on commit 210edf6

Please sign in to comment.