Skip to content

Commit

Permalink
Merge pull request #413 from stakwork/swarm-version
Browse files Browse the repository at this point in the history
chore: display swarm version
  • Loading branch information
Evanfeenstra authored Nov 26, 2024
2 parents 970a32f + b4e157f commit fc9a3df
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 37 deletions.
22 changes: 16 additions & 6 deletions app/src/Dashboard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
selectedNode,
nodes_exited,
current_swarm_user,
swarmVersion,
} from "./store";
import {
Loading,
Expand Down Expand Up @@ -139,10 +140,14 @@
<div class="head_section">
<div class="lefty logo-wrap">
<img class="logo" alt="Sphinx icon" src="favicon.jpeg" />
<span
class="stack-title"
style={`color:${$stack.ready ? "white" : "#999"}`}>Sphinx Stack</span
>
<div>
<span
class="stack-title"
style={`color:${$stack.ready ? "white" : "#999"}`}
>Sphinx Stack
</span>
<p class="swarm_version">{$swarmVersion}</p>
</div>
{#if !$stack.ready}
<InlineLoading />
{/if}
Expand Down Expand Up @@ -220,6 +225,7 @@
.logo-wrap {
display: flex;
align-items: center;
gap: 2.5rem;
}
.head_section {
Expand All @@ -230,7 +236,6 @@
.logo-wrap .logo {
width: 70px;
padding: 12px;
margin-left: 2.5rem;
}
.body {
display: flex;
Expand All @@ -239,10 +244,10 @@
.lefty {
height: 100%;
border-right: 1px solid #101317;
padding: 1rem;
}
.stack-title {
color: white;
margin-left: 0.5rem;
font-size: 1.2rem;
width: 18rem;
}
Expand Down Expand Up @@ -270,4 +275,9 @@
font-weight: 500;
cursor: pointer;
}
.swarm_version {
font-size: 0.9rem;
font-family: "Barlow";
}
</style>
3 changes: 3 additions & 0 deletions app/src/helpers/swarm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Writable } from "svelte/store";
import { get_all_image_actual_version, get_image_tags } from "../api/swarm";
import type { Stack, Node } from "../nodes";
import { swarmVersion } from "../store";

export async function getImageVersion(
stack: Writable<Stack>,
Expand All @@ -15,6 +16,8 @@ export async function getImageVersion(
version_object[image_version.name] = image_version.version;
}

swarmVersion.set(version_object["swarm"]);

stack.update((stack) => {
for (let i = 0; i < stack.nodes.length; i++) {
const new_version = version_object[stack.nodes[i].name];
Expand Down
2 changes: 2 additions & 0 deletions app/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const users = writable<User[]>(initialUsers);

export const current_swarm_user = writable<SwarmUser>();

export const swarmVersion = writable<string>("");

export const tribes = writable<Tribe>({
page: 1,
total: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/super/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub enum SwarmCmd {
ChangeChildSwarmPassword(ChangeUserPasswordBySuperAdminRequest),
GetLightningBotsDetails,
ChangeLightningBotLabel(ChangeLightningBotLabel),
CreateInvoiceForLightningBot(CreateInvoiceLightningBotReq)
CreateInvoiceForLightningBot(CreateInvoiceLightningBotReq),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
58 changes: 37 additions & 21 deletions src/bin/super/lightning_bots.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
cmd::{
ChangeLightningBotLabel, LightningBotAccountRes, LightningBotBalanceRes, SuperSwarmResponse, CreateInvoiceLightningBotReq,LightningBotCreateInvoiceReq
ChangeLightningBotLabel, CreateInvoiceLightningBotReq, LightningBotAccountRes,
LightningBotBalanceRes, LightningBotCreateInvoiceReq, SuperSwarmResponse,
},
state::{LightningBotsDetails, Super},
};
Expand Down Expand Up @@ -199,15 +200,21 @@ pub async fn change_lightning_bot_label(
}
}

pub async fn create_invoice_lightning_bot(state: &Super,info: CreateInvoiceLightningBotReq)-> SuperSwarmResponse {
pub async fn create_invoice_lightning_bot(
state: &Super,
info: CreateInvoiceLightningBotReq,
) -> SuperSwarmResponse {
// find bot
let bot_option = state.lightning_bots.iter().find(|lightning_bot | lightning_bot.url == info.id);
let bot_option = state
.lightning_bots
.iter()
.find(|lightning_bot| lightning_bot.url == info.id);
if bot_option.is_none() {
return SuperSwarmResponse {
success: false,
message: "bot does not exist".to_string(),
data: None
}
data: None,
};
}

let bot = bot_option.unwrap();
Expand All @@ -216,52 +223,61 @@ pub async fn create_invoice_lightning_bot(state: &Super,info: CreateInvoiceLight
let invoice_res = match create_invoice_request(&bot.url, &bot.token, info.amt_msat).await {
Ok(res) => res,
Err(err) => {
log::error!("Error making request to create invoice: {}", err.to_string());
log::error!(
"Error making request to create invoice: {}",
err.to_string()
);
return SuperSwarmResponse {
success: false,
message: err.to_string(),
data: None
}
data: None,
};
}
};

if invoice_res.status() != StatusCode::OK {
log::error!("Did not get status created OK for creating invoice: {}", invoice_res.status());
log::error!(
"Did not get status created OK for creating invoice: {}",
invoice_res.status()
);
return SuperSwarmResponse {
success: false,
message:format!("Got unexpected status response {}", invoice_res.status()),
data: None
}
message: format!("Got unexpected status response {}", invoice_res.status()),
data: None,
};
}

let invoice_details:Value = match invoice_res.json().await {
let invoice_details: Value = match invoice_res.json().await {
Ok(value) => value,
Err(err) => {
log::error!("Error converting response to value: {}", err.to_string());
return SuperSwarmResponse {
success: false,
message: format!("Error converting response to value: {}", err.to_string()),
data: None
}
data: None,
};
}
};

// return response to frontend
SuperSwarmResponse {
success: true,
message: "invoice created".to_string(),
data: Some(invoice_details)
data: Some(invoice_details),
}
}

async fn create_invoice_request(url: &str, token: &str, amt_msat: u64) -> Result<Response, Error> {
let client = make_reqwest_client();

let body = LightningBotCreateInvoiceReq {
amt_msat: amt_msat
};
let body = LightningBotCreateInvoiceReq { amt_msat: amt_msat };

let res = client.post(format!("https://{}/invoice", url)).header("x-admin-token", token).json(&body).send().await?;
let res = client
.post(format!("https://{}/invoice", url))
.header("x-admin-token", token)
.json(&body)
.send()
.await?;

Ok (res)
Ok(res)
}
7 changes: 4 additions & 3 deletions src/bin/super/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ mod util;

use cmd::{AddSwarmResponse, SuperSwarmResponse};
use cmd::{Cmd, SwarmCmd};
use lightning_bots::{change_lightning_bot_label, get_lightning_bots_details, create_invoice_lightning_bot};
use lightning_bots::{
change_lightning_bot_label, create_invoice_lightning_bot, get_lightning_bots_details,
};
use sphinx_swarm::utils::getenv;
use state::RemoteStack;
use state::Super;
Expand Down Expand Up @@ -413,8 +415,7 @@ pub async fn super_handle(
Some(serde_json::to_string(&res)?)
}
SwarmCmd::CreateInvoiceForLightningBot(info) => {
let res: SuperSwarmResponse =
create_invoice_lightning_bot(&state, info).await;
let res: SuperSwarmResponse = create_invoice_lightning_bot(&state, info).await;
Some(serde_json::to_string(&res)?)
}
},
Expand Down
36 changes: 30 additions & 6 deletions src/dock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,13 @@ pub struct ImageVersion {
pub version: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ParsedNode {
pub name: String,
pub org: String,
pub host: String,
}

// get image digest
pub async fn get_image_digest(image_name: &str) -> Result<GetImageDigestResponse> {
let docker = Docker::connect_with_local_defaults()?;
Expand Down Expand Up @@ -595,12 +602,31 @@ pub async fn get_image_digest(image_name: &str) -> Result<GetImageDigestResponse
pub async fn get_image_actual_version(nodes: &Vec<Node>) -> Result<GetImageActualVersionResponse> {
let docker = Docker::connect_with_local_defaults()?;

let mut images_version: Vec<ImageVersion> = Vec::new();
let mut parsed_node: Vec<ParsedNode> = vec![ParsedNode {
name: "swarm".to_string(),
host: "sphinx-swarm".to_string(),
org: "".to_string(),
}];

for node in nodes.iter() {
let node_name = node.name();
let host = domain(&node_name);
let image_id = get_image_id(&docker, &host).await;

let node_image = find_img(&node_name, nodes)?;
let org = node_image.repo().org;

parsed_node.push(ParsedNode {
name: node_name,
org: org,
host: host,
})
}

let mut images_version: Vec<ImageVersion> = Vec::new();

for node in parsed_node.iter() {
let node_name = node.name.clone();
let image_id = get_image_id(&docker, &node.host).await;
if image_id.is_empty() {
images_version.push(ImageVersion {
name: node_name,
Expand All @@ -624,10 +650,8 @@ pub async fn get_image_actual_version(nodes: &Vec<Node>) -> Result<GetImageActua
let mut image_name = digest_parts[0].to_string();
let checksome = digest_parts[1];

let node_image = find_img(&node_name, nodes)?;

if node_image.repo().org == "library" {
image_name = format!("{}/{}", node_image.repo().org, image_name);
if node.org == "library" {
image_name = format!("{}/{}", node.org, image_name);
}

let version = get_image_version_from_digest(&image_name, checksome).await;
Expand Down

0 comments on commit fc9a3df

Please sign in to comment.