Added shutdown.

This commit is contained in:
rony5394
2026-03-05 14:25:04 +01:00
parent 44f533bd43
commit 2dda7dbb38
3 changed files with 34 additions and 4 deletions

View File

@@ -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,

View File

@@ -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);
}

View File

@@ -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;
}