Skip to content

Commit

Permalink
Merge pull request #324 from stakwork/restart-services
Browse files Browse the repository at this point in the history
Restart services
  • Loading branch information
Evanfeenstra authored Oct 4, 2024
2 parents 72dea27 + 63cc1d5 commit 3afc6b0
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 39 deletions.
3 changes: 3 additions & 0 deletions app/src/Dashboard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ChangePassword from "./auth/ChangePassword.svelte";
import type { Container } from "./api/swarm";
import { getImageVersion } from "./helpers/swarm";
import RestartNode from "./nodes/RestartNode.svelte";
let selectedName = "";
async function getNodeVersion(nodes: Node[]) {
Expand Down Expand Up @@ -143,6 +144,8 @@
/>

<NodeUpdate />

<RestartNode />
{/if}
</section>
</div>
Expand Down
4 changes: 3 additions & 1 deletion app/src/api/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export type Cmd =
| "StartChildSwarmContainers"
| "UpdateChildSwarmContainers"
| "CreateNewEc2Instance"
| "GetAwsInstanceTypes";
| "GetAwsInstanceTypes"
| "RestartContainer"
| "RestartChildSwarmContainers";

interface CmdData {
cmd: Cmd;
Expand Down
14 changes: 14 additions & 0 deletions app/src/api/swarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export async function update_node(id: string) {
return await swarmCmd("UpdateNode", { id, version: "latest" });
}

export async function restart_node(id: string) {
return await swarmCmd("RestartContainer", id);
}

export async function get_container_stat(name?: string) {
return await swarmCmd("GetStatistics", name);
}
Expand Down Expand Up @@ -270,6 +274,16 @@ export async function stop_child_swarm_containers({
return await swarmCmd("StopChildSwarmContainers", { nodes, host });
}

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

export async function start_child_swarm_containers({
nodes,
host,
Expand Down
40 changes: 40 additions & 0 deletions app/src/nodes/RestartNode.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<script lang="ts">
import { Button, InlineLoading } from "carbon-components-svelte";
import { selectedNode, stack } from "../store";
import { Restart } from "carbon-icons-svelte";
import * as api from "../api";
import { getImageVersion } from "../helpers/swarm";
let restarting = false;
async function restartContainer() {
let name = $selectedNode.name;
if (!name) return;
console.log("restart!", name);
restarting = true;
await api.swarm.restart_node(name);
await getImageVersion(name, stack, selectedNode);
restarting = false;
}
</script>

<aside class="node-action-wrap">
{#if restarting}
<InlineLoading description={`Restarting ${$selectedNode.name}...`} />
{:else}
<Button
kind="primary"
disabled={restarting}
class="btn-restart"
on:click={restartContainer}
iconDescription={`Restart ${$selectedNode.name}`}
icon={Restart}
size="field"
/>
{/if}
</aside>

<style>
.node-action-wrap {
margin-left: 1rem;
}
</style>
1 change: 1 addition & 0 deletions src/bin/super/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub enum SwarmCmd {
StopChildSwarmContainers(AccessNodesInfo),
StartChildSwarmContainers(AccessNodesInfo),
UpdateChildSwarmContainers(AccessNodesInfo),
RestartChildSwarmContainers(AccessNodesInfo),
CreateNewEc2Instance(CreateEc2InstanceInfo),
GetAwsInstanceTypes,
}
Expand Down
6 changes: 6 additions & 0 deletions src/bin/super/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ pub async fn super_handle(

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

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

Expand Down
14 changes: 4 additions & 10 deletions src/bin/super/superapp/src/Remotes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
stop_child_swarm_containers,
update_child_swarm_containers,
get_aws_instance_types,
restart_child_swarm_containers,
} from "../../../../../app/src/api/swarm";
let open_create_edit = false;
Expand Down Expand Up @@ -315,26 +316,19 @@
const host = selectedRowIds[i];
try {
const services = await getServices(host, false);
const services = await getServices(host, true);
if (services.length === 0) {
errors.push(`${host}: Does not have valid service`);
break;
}
const stop_result = await stop_child_swarm_containers({
const restart_response = await restart_child_swarm_containers({
nodes: services,
host,
});
handle_api_res(host, stop_result);
const start_result = await start_child_swarm_containers({
nodes: services,
host,
});
handle_api_res(host, start_result);
handle_api_res(host, restart_response);
} catch (error) {
console.log("Error: ", error);
errors.push(`${host}: Unexpected Error occured`);
Expand Down
37 changes: 15 additions & 22 deletions src/bin/super/superapp/src/ViewNodes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import {
get_child_swarm_config,
get_child_swarm_containers,
restart_child_swarm_containers,
start_child_swarm_containers,
stop_child_swarm_containers,
update_child_swarm_containers,
Expand Down Expand Up @@ -145,7 +146,7 @@
async function restartAllContainer() {
nodes_to_be_modified = [];
for (let i = 0; i < sortedNodes.length; i++) {
nodes_to_be_modified.push(`${sortedNodes[i].id}.sphinx`);
nodes_to_be_modified.push(`${sortedNodes[i].id}`);
}
await restart_all_node_handler(nodes_to_be_modified);
Expand Down Expand Up @@ -208,7 +209,7 @@
let formatted_node_ids = [];
for (let i = 0; i < selectedRowIds.length; i++) {
formatted_node_ids.push(`${selectedRowIds[i]}.sphinx`);
formatted_node_ids.push(`${selectedRowIds[i]}`);
}
await restart_all_node_handler(formatted_node_ids);
Expand All @@ -218,12 +219,7 @@
async function restart_all_node_handler(nodes: string[]) {
loading = true;
const stop_result = await stop_child_swarm_containers({
nodes,
host: $selectedNode,
});
const start_result = await start_child_swarm_containers({
const restart_result = await restart_child_swarm_containers({
nodes,
host: $selectedNode,
});
Expand All @@ -234,28 +230,20 @@
message = "Restarted All node successfully";
if (start_result === false || stop_result === false) {
if (restart_result === false) {
errorMessage = true;
message = start_result.message || stop_result.message;
message = restart_result.message;
}
if (
start_result.success &&
start_result.data &&
start_result.data.stack_error
restart_result.success &&
restart_result.data &&
restart_result.data.stack_error
) {
message = `Start Containers: ${start_result.data.stack_error}`;
message = `Start Containers: ${restart_result.data.stack_error}`;
errorMessage = true;
}
if (
stop_result.success &&
stop_result.data &&
stop_result.data.stack_error
) {
message = `Stop Containers: ${stop_result.data.stack_error}`;
errorMessage = true;
}
show_notification = true;
}
Expand Down Expand Up @@ -296,6 +284,7 @@
// { key: "version", value: "Version" },
{ key: "update", value: "Update" },
{ key: "stop", value: "Stop/Start" },
{ key: "restart", value: "Restart" },
]}
selectable
bind:selectedRowIds
Expand Down Expand Up @@ -354,6 +343,10 @@
<Button on:click={() => updateContainers([`${row.id}`])}
>Update</Button
>
{:else if cell.key === "restart"}
<Button on:click={() => restart_all_node_handler([`${row.id}`])}
>Restart</Button
>
{:else}
{cell.value}
{/if}
Expand Down
2 changes: 2 additions & 0 deletions src/bin/super/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ pub async fn access_child_swarm_containers(
}));
} else if cmd_text == "StartContainer" {
cmd = Cmd::Swarm(SwarmCmd::StartContainer(node.clone()))
} else if cmd_text == "RestartContainer" {
cmd = Cmd::Swarm(SwarmCmd::RestartContainer(node.clone()))
} else {
cmd = Cmd::Swarm(SwarmCmd::StopContainer(node.clone()))
}
Expand Down
11 changes: 10 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ pub async fn update_node(
let id = create_container(&docker, theconfig).await?;
log::info!("=> created {}", &hostname);

if let Err(e) = theimg.pre_startup(docker).await {
log::warn!("pre_startup failed {} {:?}", &id, e);
}

//remove client
start_container(&docker, &id).await?;

Expand All @@ -260,7 +264,12 @@ pub async fn update_node(
Ok(())
}

async fn make_client(proj: &str, docker: &Docker, theimg: &Image, state: &mut State) -> Result<()> {
pub async fn make_client(
proj: &str,
docker: &Docker,
theimg: &Image,
state: &mut State,
) -> Result<()> {
// create and connect client
theimg
.connect_client(
Expand Down
1 change: 1 addition & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub enum SwarmCmd {
ListContainers,
StartContainer(String),
StopContainer(String),
RestartContainer(String),
UpdateNode(UpdateNode),
GetStatistics(Option<String>),
AddBoltwallAdminPubkey(AddAdminRequest),
Expand Down
44 changes: 41 additions & 3 deletions src/dock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use std::error::Error;
use tokio::io::AsyncReadExt;

use crate::backup::bucket_name;
use crate::builder::find_img;
use crate::config::STATE;
use crate::images::DockerHubImage;
use crate::builder::{find_img, make_client};
use crate::config::{State, STATE};
use crate::images::{DockerConfig, DockerHubImage};
use crate::mount_backedup_volume::download_from_s3;
use crate::utils::{domain, getenv, sleep_ms};
use tokio::fs::File;
Expand Down Expand Up @@ -207,6 +207,44 @@ pub async fn remove_container(docker: &Docker, id: &str) -> Result<()> {
Ok(())
}

pub async fn restart_node_container(
docker: &Docker,
node_name: &str,
state: &mut State,
proj: &str,
) -> Result<()> {
let img = find_img(node_name, &state.stack.nodes)?;
let hostname = domain(&node_name);

let theconfig = img.make_config(&state.stack.nodes, docker).await?;

img.remove_client(&mut state.clients);

stop_and_remove(docker, &hostname).await?;

let new_id = create_container(&docker, theconfig).await?;
log::info!("=> created {}", &hostname);

if let Err(e) = img.pre_startup(docker).await {
log::warn!("pre_startup failed {} {:?}", &new_id, e);
}

match start_container(&docker, &new_id).await {
Ok(()) => {
log::info!("Container started successfully");
img.post_startup(proj, docker).await?;

let _ = match make_client(proj, docker, &img, state).await {
Ok(_) => Ok(()),
Err(e) => Err(anyhow!("FAILED TO MAKE CLIENT {:?}", e)),
};
}
Err(err) => log::error!("Error starting container: {}", err.to_string()),
}

Ok(())
}

pub async fn upload_to_container(
docker: &Docker,
img_name: &str,
Expand Down
6 changes: 6 additions & 0 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn access(cmd: &Cmd, state: &State, user_id: &Option<u32>) -> bool {
SwarmCmd::UpdateSwarm => true,
SwarmCmd::ListContainers => true,
SwarmCmd::UpdateNode(_) => true,
SwarmCmd::RestartContainer(_) => true,
_ => false,
},
_ => false,
Expand Down Expand Up @@ -104,6 +105,11 @@ pub async fn handle(
let res = stop_container(docker, &id).await?;
Some(serde_json::to_string(&res)?)
}
SwarmCmd::RestartContainer(id) => {
log::info!("RestartContainer -> {}", id);
let res = restart_node_container(docker, &id, &mut state, proj).await?;
Some(serde_json::to_string(&res)?)
}
SwarmCmd::AddNode(node) => {
log::info!("AddNode -> {:?}", node);
// add a node via docker
Expand Down
10 changes: 8 additions & 2 deletions src/images/neo4j.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,14 @@ fn neo4j(node: &Neo4jImage) -> Config<String> {
format!("{}", dbms_allow_upgrade),
format!("{}", dbms_default_database),
format!("NEO4J_dbms_security_auth__minimum__password__length=4"),
format!("{}", "NEO4J_dbms_security_procedures_unrestricted=apoc.*,algo.*,gds.*"),
format!("{}", "NEO4J_dbms_security_procedures_whitelist=apoc.*,gds.*,algo.*"),
format!(
"{}",
"NEO4J_dbms_security_procedures_unrestricted=apoc.*,algo.*,gds.*"
),
format!(
"{}",
"NEO4J_dbms_security_procedures_whitelist=apoc.*,gds.*,algo.*"
),
format!("NEO4J_PLUGINS=[\"graph-data-science\"]"),
]),
..Default::default()
Expand Down

0 comments on commit 3afc6b0

Please sign in to comment.