Some cleanup.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user