Switched to normal docker api.

This commit is contained in:
rony5394
2026-02-20 22:40:24 +01:00
parent 11f01ab459
commit 48c4e9a5c2
7 changed files with 105 additions and 80 deletions

View File

@@ -6,8 +6,9 @@ import (
"fmt"
"io"
"net/http"
"time"
"github.com/moby/moby/client"
"github.com/docker/docker/api/types/swarm"
)
func cleanup(w http.ResponseWriter, r *http.Request){
@@ -33,16 +34,14 @@ func cleanup(w http.ResponseWriter, r *http.Request){
panic("Failed to unmarshal json."+ err.Error());
}
scaleUp(bodyDecoded.ServiceId);
listResoult, err := ApiClient.ServiceList(context.Background(), client.ServiceListOptions{});
listResoult, err := ApiClient.ServiceList(context.Background(), swarm.ServiceListOptions{});
if err != nil {
panic("Failed to list services."+ err.Error());
}
var helperServiceId string;
for _, service := range listResoult.Items {
for _, service := range listResoult{
if service.Spec.Labels["blazena.helper"] != "true" {
continue;
}
@@ -54,9 +53,14 @@ func cleanup(w http.ResponseWriter, r *http.Request){
panic("Helper service not found!");
}
_, err = ApiClient.ServiceRemove(context.Background(), helperServiceId, client.ServiceRemoveOptions{});
err = ApiClient.ServiceRemove(context.Background(), helperServiceId);
if err != nil {
panic("Failed to remove helper service."+ err.Error());
}
//TODO: Add proper wait system
time.Sleep(10*time.Second);
scaleUp(bodyDecoded.ServiceId);
fmt.Fprint(w, bodyDecoded.ServiceId);
}

View File

@@ -13,7 +13,8 @@ import (
"net/http"
"github.com/moby/moby/client"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
)
// Add mutex.
@@ -31,17 +32,17 @@ func Run(){
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM);
var err error;
ApiClient, err = client.New(client.FromEnv);
ApiClient, err = client.NewClientWithOpts(client.FromEnv);
if(err != nil){
panic("Docker client was not able to init from env!" + err.Error());
}
info, err := ApiClient.Info(context.Background(), client.InfoOptions{});
info, err := ApiClient.Info(context.Background())
if(err != nil){
panic("Error getting info!" + err.Error());
}
if(!info.Info.Swarm.ControlAvailable){
if(!info.Swarm.ControlAvailable){
panic("Node is not a swarm manager.");
}
@@ -92,14 +93,14 @@ func listServices(w http.ResponseWriter, r *http.Request){
if !bearerAuth(w,r) {return};
list, err := ApiClient.ServiceList(context.Background(), client.ServiceListOptions{});
list, err := ApiClient.ServiceList(context.Background(), swarm.ServiceListOptions{});
if(err != nil){
panic("Unable to list services!" + err.Error());
}
var services []aService;
for _, service:= range list.Items{
for _, service:= range list{
var settings map[string]string = service.Spec.Labels;

View File

@@ -9,10 +9,8 @@ import (
"strings"
"time"
"github.com/moby/moby/api/types/mount"
"github.com/moby/moby/api/types/network"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/client"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/swarm"
)
func prepare(w http.ResponseWriter, r *http.Request){
@@ -22,9 +20,7 @@ func prepare(w http.ResponseWriter, r *http.Request){
return;
}
//if !bearerAuth(w, r){
// return;
//}
if !bearerAuth(w, r){return;}
rawBody, err := io.ReadAll(r.Body);
if err != nil {
@@ -41,13 +37,15 @@ func prepare(w http.ResponseWriter, r *http.Request){
}
scaleDown(bodyDecoded.ServiceId);
//TODO: Add proper wait system
time.Sleep(10*time.Second);
inspectResoults, err := ApiClient.ServiceInspect(context.Background(), bodyDecoded.ServiceId, client.ServiceInspectOptions{});
inspectResoults, _, err := ApiClient.ServiceInspectWithRaw(context.Background(), bodyDecoded.ServiceId, swarm.ServiceInspectOptions{});
if err != nil{
panic("Failed to inspect service."+ err.Error());
}
labels := inspectResoults.Service.Spec.Labels;
labels := inspectResoults.Spec.Labels;
time.Sleep(10);
maxConcurrent := uint64(1);
@@ -62,44 +60,42 @@ func prepare(w http.ResponseWriter, r *http.Request){
for _, targetVolume := range targetVolumes{
_, err := ApiClient.ServiceCreate(context.Background(), client.ServiceCreateOptions{
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{
Labels: map[string]string{"blazena.helper": "true"},
_, err := ApiClient.ServiceCreate(context.Background(), swarm.ServiceSpec{
Annotations: swarm.Annotations{
Labels: map[string]string{"blazena.helper": "true"},
},
Mode: swarm.ServiceMode{
ReplicatedJob: &swarm.ReplicatedJob{
MaxConcurrent: &maxConcurrent,
TotalCompletions: &totalCompletions,
},
Mode: swarm.ServiceMode{
ReplicatedJob: &swarm.ReplicatedJob{
MaxConcurrent: &maxConcurrent,
TotalCompletions: &totalCompletions,
},
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Image: "docker.io/library/alpine:latest",
Command: []string{"sh", "-c", helperCommand},
Mounts: []mount.Mount{
mount.Mount{
Source: targetVolume,
Target: "/volume",
Type: "volume",
},
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Image: "docker.io/library/alpine:latest",
Command: []string{"sh", "-c", helperCommand},
Mounts: []mount.Mount{
mount.Mount{
Source: targetVolume,
Target: "/volume",
Type: "volume",
},
},
Placement: &swarm.Placement{
Constraints: []string{"node.hostname=="+targetNode},
},
},
EndpointSpec: &swarm.EndpointSpec{
Ports: []swarm.PortConfig{
swarm.PortConfig{
Protocol: network.TCP,
TargetPort: uint32(22),
PublishedPort: uint32(2222),
},
Placement: &swarm.Placement{
Constraints: []string{"node.hostname=="+targetNode},
},
},
EndpointSpec: &swarm.EndpointSpec{
Ports: []swarm.PortConfig{
swarm.PortConfig{
Protocol: swarm.PortConfigProtocolTCP,
TargetPort: uint32(22),
PublishedPort: uint32(2222),
},
},
},
});
}, swarm.ServiceCreateOptions{});
if err != nil {
panic("Failed to create helper service."+ err.Error());

View File

@@ -3,36 +3,32 @@ package docker
import (
"context"
"github.com/moby/moby/client"
"github.com/docker/docker/api/types/swarm"
);
func scaleDown(serviceId string){
inspectresoult, err := ApiClient.ServiceInspect(context.Background(), serviceId, client.ServiceInspectOptions{});
inspectresoult, _, err := ApiClient.ServiceInspectWithRaw(context.Background(), serviceId, swarm.ServiceInspectOptions{});
if err != nil{
panic("Error inspecting service!"+ err.Error());
}
originalScale := inspectresoult.Service.Spec.Mode.Replicated.Replicas;
updatedSpec := inspectresoult.Service.Spec;
originalScale := inspectresoult.Spec.Mode.Replicated.Replicas;
updatedSpec := inspectresoult.Spec;
newScale := uint64(0);
updatedSpec.Mode.Replicated.Replicas = &newScale;
scale.Store(serviceId, *originalScale);
_, err = ApiClient.ServiceUpdate(context.Background(), serviceId, client.ServiceUpdateOptions{
Spec: updatedSpec,
Version: inspectresoult.Service.Version,
});
_, err = ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
if(err != nil){
panic("Failed to update service."+ err.Error());
}
}
func scaleUp(serviceId string){
inspectresoult, err := ApiClient.ServiceInspect(context.Background(), serviceId, client.ServiceInspectOptions{});
inspectresoult, _, err := ApiClient.ServiceInspectWithRaw(context.Background(), serviceId, swarm.ServiceInspectOptions{});
if err != nil{
panic("Error inspecting service!"+ err.Error());
@@ -47,15 +43,9 @@ func scaleUp(serviceId string){
if(!ok){
panic("Its very not okay!")
}
updatedSpec := inspectresoult.Service.Spec;
updatedSpec := inspectresoult.Spec;
updatedSpec.Mode.Replicated.Replicas = &originalScaleChecked;
ApiClient.ServiceUpdate(context.Background(), serviceId, client.ServiceUpdateOptions{
Spec: updatedSpec,
Version: inspectresoult.Service.Version,
});
ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
}