Some cleanup.

This commit is contained in:
rony5394
2026-05-25 15:39:14 +02:00
parent ef37f17378
commit 0f316e8149
7 changed files with 64 additions and 36 deletions

View File

@@ -2,10 +2,10 @@ package docker
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"time"
"github.com/docker/docker/api/types/swarm"
@@ -20,44 +20,40 @@ func cleanup(w http.ResponseWriter, r *http.Request){
if !bearerAuth(w, r) {return;}
rawBody, err := io.ReadAll(r.Body);
if err != nil {
panic("Failed to read body!");
}
var bodyDecoded struct{
ServiceId string `json:"serviceId"`
};
err = json.Unmarshal(rawBody, &bodyDecoded);
if err != nil {
panic("Failed to unmarshal json."+ err.Error());
}
listResoult, err := ApiClient.ServiceList(context.Background(), swarm.ServiceListOptions{});
if err != nil {
panic("Failed to list services."+ err.Error());
slog.Error("Failed to list services", slog.Any("propagatedError", err));
os.Exit(1);
}
var helperServiceId string;
var helperServices int;
for _, service := range listResoult{
if service.Spec.Labels["blazena.helper"] != "true" {
continue;
}
helperServiceId = service.ID;
break;
helperServices ++;
}
if helperServiceId == ""{
panic("Helper service not found!");
slog.Warn("Helper service wasn't found");
http.Error(w, "Internal Server Error", http.StatusInternalServerError);
return;
}
if helperServices > 1{
slog.Error("There are more than 1 helper service.");
os.Exit(1);
}
err = ApiClient.ServiceRemove(context.Background(), helperServiceId);
if err != nil {
panic("Failed to remove helper service."+ err.Error());
}
//TODO: add proper wait system
time.Sleep(7*time.Second);
fmt.Fprint(w, bodyDecoded.ServiceId);
fmt.Fprint(w, helperServiceId);
}