Skip to content

Commit

Permalink
Merge pull request #283 from stakwork/update-swarm-from-super
Browse files Browse the repository at this point in the history
Update swarm from super
  • Loading branch information
Evanfeenstra authored Sep 6, 2024
2 parents 7da2d37 + 3cc55bc commit 1177203
Show file tree
Hide file tree
Showing 13 changed files with 621 additions and 43 deletions.
8 changes: 7 additions & 1 deletion app/src/api/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const mode = import.meta.env.MODE;

if (mode === "super") {
root = "https://app.superadmin.sphinx.chat/api";
// root = "http://localhost:8005/api";
}

type CmdType =
Expand Down Expand Up @@ -78,7 +79,12 @@ export type Cmd =
| "GetApiToken"
| "AddNewSwarm"
| "UpdateSwarm"
| "DeleteSwarm";
| "DeleteSwarm"
| "GetChildSwarmConfig"
| "GetChildSwarmContainers"
| "StopChildSwarmContainers"
| "StartChildSwarmContainers"
| "UpdateChildSwarmContainers";

interface CmdData {
cmd: Cmd;
Expand Down
38 changes: 38 additions & 0 deletions app/src/api/swarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,41 @@ export async function update_user({
}) {
return await swarmCmd("UpdateUser", { pubkey, name, role, id });
}

export async function get_child_swarm_config({ host }: { host: string }) {
return await swarmCmd("GetChildSwarmConfig", { host });
}

export async function get_child_swarm_containers({ host }: { host: string }) {
return await swarmCmd("GetChildSwarmContainers", { host });
}

export async function stop_child_swarm_containers({
nodes,
host,
}: {
nodes: string[];
host: string;
}) {
return await swarmCmd("StopChildSwarmContainers", { nodes, host });
}

export async function start_child_swarm_containers({
nodes,
host,
}: {
nodes: string[];
host: string;
}) {
return await swarmCmd("StartChildSwarmContainers", { nodes, host });
}

export async function update_child_swarm_containers({
nodes,
host,
}: {
nodes: string[];
host: string;
}) {
return await swarmCmd("UpdateChildSwarmContainers", { nodes, host });
}
29 changes: 29 additions & 0 deletions src/bin/super/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", content = "data")]
Expand Down Expand Up @@ -67,10 +68,38 @@ pub enum SwarmCmd {
UpdateSwarm(UpdateSwarmInfo),
DeleteSwarm(DeleteSwarmInfo),
SetChildSwarm(ChildSwarm),
GetChildSwarmConfig(ChildSwarmIdentifier),
GetChildSwarmContainers(ChildSwarmIdentifier),
StopChildSwarmContainers(AccessNodesInfo),
StartChildSwarmContainers(AccessNodesInfo),
UpdateChildSwarmContainers(AccessNodesInfo),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChildSwarmIdentifier {
pub host: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AddSwarmResponse {
pub success: bool,
pub message: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SuperSwarmResponse {
pub success: bool,
pub message: String,
pub data: Option<Value>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LoginResponse {
pub token: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AccessNodesInfo {
pub host: String,
pub nodes: Vec<String>,
}
102 changes: 83 additions & 19 deletions src/bin/super/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ mod routes;
mod state;
mod util;

use cmd::AddSwarmResponse;
use cmd::{AddSwarmResponse, SuperSwarmResponse};
use cmd::{Cmd, SwarmCmd};
use sphinx_swarm::utils::getenv;
use state::RemoteStack;
use state::Super;
use util::add_new_swarm_details;
use util::{
accessing_child_container_controller, add_new_swarm_details, get_child_swarm_config,
get_child_swarm_containers,
};

use crate::checker::swarm_checker;
use anyhow::{anyhow, Context, Result};
Expand Down Expand Up @@ -72,24 +75,22 @@ pub async fn put_config_file(project: &str, rs: &Super) {

fn access(cmd: &Cmd, state: &Super, user_id: &Option<u32>) -> bool {
// login needs no auth
if let Cmd::Swarm(c) = cmd {
if let SwarmCmd::Login(_) = c {
return true;
}

if let SwarmCmd::SetChildSwarm(info) = c {
//get x-super-token
let token = getenv("SUPER_TOKEN").unwrap_or("".to_string());
if token.is_empty() {
return false;
}
//verify token
if token != info.token {
return false;
match cmd {
Cmd::Swarm(c) => match c {
SwarmCmd::Login(_) => return true,
SwarmCmd::SetChildSwarm(info) => {
//get x-super-token
let token = getenv("SUPER_TOKEN").unwrap_or("".to_string());
if token.is_empty() {
return false;
}
if token != info.token {
return false;
}
return true;
}

return true;
}
_ => {}
},
}
// user id required if not SwarmCmd::Login
if user_id.is_none() {
Expand Down Expand Up @@ -228,6 +229,69 @@ pub async fn super_handle(

Some(serde_json::to_string(&hm)?)
}
SwarmCmd::GetChildSwarmConfig(info) => {
let res: SuperSwarmResponse;
//find node
match state.find_swarm_by_host(&info.host) {
Some(swarm) => match get_child_swarm_config(&swarm).await {
Ok(result) => res = result,
Err(err) => {
res = SuperSwarmResponse {
success: false,
message: err.to_string(),
data: None,
}
}
},
None => {
res = SuperSwarmResponse {
success: false,
message: "Swarm does not exist".to_string(),
data: None,
}
}
}
Some(serde_json::to_string(&res)?)
}
SwarmCmd::GetChildSwarmContainers(info) => {
let res: SuperSwarmResponse;
match state.find_swarm_by_host(&info.host) {
Some(swarm) => match get_child_swarm_containers(&swarm).await {
Ok(result) => res = result,
Err(err) => {
res = SuperSwarmResponse {
success: false,
message: err.to_string(),
data: None,
}
}
},
None => {
res = SuperSwarmResponse {
success: false,
message: "Swarm does not exist".to_string(),
data: None,
}
}
}
Some(serde_json::to_string(&res)?)
}
SwarmCmd::StopChildSwarmContainers(info) => {
let res = accessing_child_container_controller(&state, info, "StopContainer").await;

Some(serde_json::to_string(&res)?)
}
SwarmCmd::StartChildSwarmContainers(info) => {
let res =
accessing_child_container_controller(&state, info, "StartContainer").await;

Some(serde_json::to_string(&res)?)
}
SwarmCmd::UpdateChildSwarmContainers(info) => {
let res = accessing_child_container_controller(&state, info, "UpdateNode").await;

Some(serde_json::to_string(&res)?)
}
},
};

Expand Down
4 changes: 2 additions & 2 deletions src/bin/super/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub async fn launch_rocket(
}

#[get("/cmd?<tag>&<txt>")]
pub async fn cmd(
async fn cmd(
sender: &State<mpsc::Sender<CmdRequest>>,
tag: &str,
txt: &str,
Expand All @@ -60,7 +60,7 @@ pub async fn cmd(
}

#[rocket::post("/super/add_new_swarm", data = "<body>")]
pub async fn add_new_swarm(
async fn add_new_swarm(
sender: &State<mpsc::Sender<CmdRequest>>,
body: Json<SendSwarmDetailsBody>,
verify_super_token: VerifySuperToken,
Expand Down
9 changes: 8 additions & 1 deletion src/bin/super/superapp/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import User from "carbon-icons-svelte/lib/User.svelte";
import { OverflowMenu, OverflowMenuItem } from "carbon-components-svelte";
import ChangePassword from "./ChangePassword.svelte";
import ViewNodes from "./ViewNodes.svelte";
let page = "main";
async function backToMain() {
page = "main";
}
async function viewNode() {
page = "view_nodes";
}
function handleChangePassword() {
page = "change_password";
}
Expand Down Expand Up @@ -38,8 +43,10 @@
<div class="body">
{#if page === "change_password"}
<ChangePassword back={backToMain} />
{:else if page === "view_nodes"}
<ViewNodes back={backToMain} />
{:else}
<Remotes />
<Remotes {viewNode} />
{/if}
</div>
{/if}
Expand Down
14 changes: 14 additions & 0 deletions src/bin/super/superapp/src/Remotes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import { onMount } from "svelte";
import type { Remote } from "./types/types";
import { splitHost } from "./utils/index";
import { selectedNode } from "./store";
let open_create_edit = false;
let open_delete = false;
Expand All @@ -35,6 +36,8 @@
let selectedRowIds = [];
export let viewNode = () => {};
async function getConfig() {
const conf = await api.swarm.get_config();
if (conf && conf.stacks && conf.stacks.length) {
Expand Down Expand Up @@ -229,6 +232,12 @@
function handleOnClose() {
clear_swarm_data();
}
function handleViewNodes(id: string) {
console.log(id);
selectedNode.set(id);
viewNode();
}
</script>

<main>
Expand Down Expand Up @@ -256,6 +265,7 @@
{ key: "ec2", value: "Instance" },
{ key: "tribes", value: "Tribes" },
{ key: "health", value: "Health" },
{ key: "nodes", value: "Nodes" },
{ key: "edit", value: "Edit" },
{ key: "delete", value: "Delete" },
]}
Expand Down Expand Up @@ -285,6 +295,10 @@
<Button size={"small"} on:click={() => handleEditSwarm(row.id)}>
Edit
</Button>
{:else if cell.key === "nodes"}
<Button size={"small"} on:click={() => handleViewNodes(row.id)}>
View Nodes
</Button>
{:else if cell.key === "delete"}
<Button
kind="danger"
Expand Down
Loading

0 comments on commit 1177203

Please sign in to comment.