From 2dda7dbb38a39f3aab05ce2c60fc3baef2e8e8c2 Mon Sep 17 00:00:00 2001 From: rony5394 <143897221+rony5394@users.noreply.github.com> Date: Thu, 5 Mar 2026 14:25:04 +0100 Subject: [PATCH] Added shutdown. --- docker/docker.go | 9 ++++++++- docker/prepare.go | 2 ++ host/host.go | 27 ++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index 06e886b..56b2dad 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -30,7 +30,7 @@ type aService struct{ } func Run(){ - ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM); + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM); var err error; ApiClient, err = client.NewClientWithOpts(client.FromEnv); @@ -56,6 +56,13 @@ func Run(){ http.HandleFunc("/scale/down", scaleDown); http.HandleFunc("/prepare", prepare); http.HandleFunc("/cleanup", cleanup); + // I'll make it better someday. + http.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) { + if !bearerAuth(w, r){return} + fmt.Fprint(w, "Shutdown!"); + time.Sleep(1*time.Second); + stop(); + }); ApiClient.NetworkCreate(context.Background(), "blazenaPohar", network.CreateOptions{ Attachable: true, diff --git a/docker/prepare.go b/docker/prepare.go index bf0d70e..0754613 100644 --- a/docker/prepare.go +++ b/docker/prepare.go @@ -72,6 +72,7 @@ func prepare(w http.ResponseWriter, r *http.Request){ Source: bodyDecoded.VolumeId, Target: "/volume", Type: "volume", + ReadOnly: true, }, }, StopGracePeriod: &stopGracePeriod, @@ -90,6 +91,7 @@ func prepare(w http.ResponseWriter, r *http.Request){ } time.Sleep(7*time.Second); + fmt.Fprint(w, bodyDecoded.ServiceId); } diff --git a/host/host.go b/host/host.go index 63809f2..de9ab5e 100644 --- a/host/host.go +++ b/host/host.go @@ -51,10 +51,10 @@ func Run(Config cfg.Config) { if !prepareService(Config, service, volume) {continue} fmt.Println("Done!"); + // Skiping Host Key Check is temporary. command := `apk add --no-cache rsync openssh-client && \ - ping -c 10 BlazenaHelper`; - //rsync -avz --delete -e "ssh -i /ssh-key -p 2222 -o StrictHostKeyChecking=no" \ - //root@`+Config.Nodes[service.Node].Ip+`:/volume/ /tmp` + rsync -avz --delete -e "ssh -i /ssh-key -p 2222 -o StrictHostKeyChecking=no" \ + root@tasks.BlazenaHelper:/volume/ /tmp/` + volume; exec, err := DockerClient.ContainerExecCreate(context.Background(), "BlazenaStorage", container.ExecOptions{ @@ -86,6 +86,8 @@ func Run(Config cfg.Config) { DockerClient.ContainerRemove(context.Background(), "BlazenaStorage", container.RemoveOptions{ Force: true, }); + + if !shutdown(Config){panic("Failed to shutdown docker api!");} } func getServices(Config cfg.Config)[]aService{ @@ -278,3 +280,22 @@ func createStorageContainer(Config cfg.Config, DockerClient *client.Client){ } + +func shutdown(Config cfg.Config)bool{ + rq, err := http.NewRequest("POST", Config.DockerManagerBaseUrl + "/shutdown", nil); + + if err != nil{ + panic("Failed to create http request"+ err.Error()); + } + + rq.Header.Set("Authorization", "Bearer "+ token); + rq.Close = true; + _, err = http.DefaultClient.Do(rq); + + // if err != nil{ + // panic("Failed to send http request"+ err.Error()); + // } + + return true; + +}