From 6861962b56662d404a213d3358e8af4ad8f16a5a Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 3 Oct 2024 17:18:18 +0100 Subject: [PATCH 1/9] feat: restart container function --- src/builder.rs | 7 ++++++- src/cmd.rs | 1 + src/dock.rs | 40 +++++++++++++++++++++++++++++++++++++--- src/handler.rs | 5 +++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index f480aa9b..5492a618 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -260,7 +260,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( diff --git a/src/cmd.rs b/src/cmd.rs index 962f5eb8..5c1c1b86 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -127,6 +127,7 @@ pub enum SwarmCmd { ListContainers, StartContainer(String), StopContainer(String), + RestartContainer(String), UpdateNode(UpdateNode), GetStatistics(Option), AddBoltwallAdminPubkey(AddAdminRequest), diff --git a/src/dock.rs b/src/dock.rs index ffc6a54f..992cba43 100644 --- a/src/dock.rs +++ b/src/dock.rs @@ -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; @@ -207,6 +207,40 @@ 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); + } + + start_container(&docker, &new_id).await?; + + 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)), + }; + + Ok(()) +} + pub async fn upload_to_container( docker: &Docker, img_name: &str, diff --git a/src/handler.rs b/src/handler.rs index 9f8462e2..0041638e 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -104,6 +104,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 From 7be9afd81c32f3a6fc30f75b6bc09b0ee7a2a4d5 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 3 Oct 2024 17:21:24 +0100 Subject: [PATCH 2/9] fix: when updating node run pre_setup script --- src/builder.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/builder.rs b/src/builder.rs index 5492a618..cde3923e 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -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?; From 0a33b98135ba45448a03136ecc3102f5b3c7486d Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 3 Oct 2024 17:57:56 +0100 Subject: [PATCH 3/9] feat: create rstart button --- app/src/nodes/RestartNode.svelte | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/src/nodes/RestartNode.svelte diff --git a/app/src/nodes/RestartNode.svelte b/app/src/nodes/RestartNode.svelte new file mode 100644 index 00000000..b91b5659 --- /dev/null +++ b/app/src/nodes/RestartNode.svelte @@ -0,0 +1,25 @@ + + + + + From 676534c1cea622e26e8844e4d29d924e6134e6d1 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 3 Oct 2024 18:38:01 +0100 Subject: [PATCH 4/9] feat: restart service frontend --- app/src/Dashboard.svelte | 3 +++ app/src/api/cmd.ts | 3 ++- app/src/api/swarm.ts | 4 ++++ app/src/nodes/RestartNode.svelte | 33 +++++++++++++++++++++++--------- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/app/src/Dashboard.svelte b/app/src/Dashboard.svelte index f4293581..7f4cfa90 100644 --- a/app/src/Dashboard.svelte +++ b/app/src/Dashboard.svelte @@ -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[]) { @@ -143,6 +144,8 @@ /> + + {/if} diff --git a/app/src/api/cmd.ts b/app/src/api/cmd.ts index db1dd0ae..03a6fe6b 100644 --- a/app/src/api/cmd.ts +++ b/app/src/api/cmd.ts @@ -86,7 +86,8 @@ export type Cmd = | "StartChildSwarmContainers" | "UpdateChildSwarmContainers" | "CreateNewEc2Instance" - | "GetAwsInstanceTypes"; + | "GetAwsInstanceTypes" + | "RestartContainer"; interface CmdData { cmd: Cmd; diff --git a/app/src/api/swarm.ts b/app/src/api/swarm.ts index a85819b2..d6866f01 100644 --- a/app/src/api/swarm.ts +++ b/app/src/api/swarm.ts @@ -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); } diff --git a/app/src/nodes/RestartNode.svelte b/app/src/nodes/RestartNode.svelte index b91b5659..89f96515 100644 --- a/app/src/nodes/RestartNode.svelte +++ b/app/src/nodes/RestartNode.svelte @@ -1,25 +1,40 @@ -