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,8 +60,7 @@ func prepare(w http.ResponseWriter, r *http.Request){
for _, targetVolume := range targetVolumes{
_, err := ApiClient.ServiceCreate(context.Background(), client.ServiceCreateOptions{
Spec: swarm.ServiceSpec{
_, err := ApiClient.ServiceCreate(context.Background(), swarm.ServiceSpec{
Annotations: swarm.Annotations{
Labels: map[string]string{"blazena.helper": "true"},
},
@@ -92,14 +89,13 @@ func prepare(w http.ResponseWriter, r *http.Request){
EndpointSpec: &swarm.EndpointSpec{
Ports: []swarm.PortConfig{
swarm.PortConfig{
Protocol: network.TCP,
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{});
}

19
go.mod
View File

@@ -4,23 +4,24 @@ go 1.25.5
require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/errdefs/pkg v0.3.0 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v28.5.2+incompatible // indirect
github.com/docker/go-connections v0.6.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/moby/api v1.52.0 // indirect
github.com/moby/moby/client v0.2.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
golang.org/x/sys v0.33.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.65.0 // indirect
go.opentelemetry.io/otel v1.40.0 // indirect
go.opentelemetry.io/otel/metric v1.40.0 // indirect
go.opentelemetry.io/otel/trace v1.40.0 // indirect
golang.org/x/sys v0.40.0 // indirect
)

22
go.sum
View File

@@ -1,11 +1,15 @@
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE=
github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/docker v28.5.2+incompatible h1:DBX0Y0zAjZbSrm1uzOkdr1onVghKaftjlSWt4AFexzM=
github.com/docker/docker v28.5.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94=
github.com/docker/go-connections v0.6.0/go.mod h1:AahvXYshr6JgfUJGdDCs2b5EZG/vmaMAntpSFH5BFKE=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
@@ -15,10 +19,14 @@ github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSw
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/moby v28.5.2+incompatible h1:hIn6qcenb3JY1E3STwqEbBvJ8bha+u1LpqjX4CBvNCk=
github.com/moby/moby v28.5.2+incompatible/go.mod h1:fDXVQ6+S340veQPv35CzDahGBmHsiclFwfEygB/TWMc=
github.com/moby/moby/api v1.52.0 h1:00BtlJY4MXkkt84WhUZPRqt5TvPbgig2FZvTbe3igYg=
github.com/moby/moby/api v1.52.0/go.mod h1:8mb+ReTlisw4pS6BRzCMts5M49W5M7bKt1cJy/YbAqc=
github.com/moby/moby/client v0.2.1 h1:1Grh1552mvv6i+sYOdY+xKKVTvzJegcVMhuXocyDz/k=
@@ -27,15 +35,29 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 h1:sbiXRNDSWJOTobXh5HyQKjq6wUC5tNybqjIqDpAY4CU=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0/go.mod h1:69uWxva0WgAA/4bu2Yy70SLDBwZXuQ6PbBpbsa5iZrQ=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.65.0 h1:7iP2uCb7sGddAr30RRS6xjKy7AZ2JtTOPA3oolgVSw8=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.65.0/go.mod h1:c7hN3ddxs/z6q9xwvfLPk+UHlWRQyaeR1LdgfL/66l0=
go.opentelemetry.io/otel v1.35.0 h1:xKWKPxrxB6OtMCbmMY021CqC45J+3Onta9MqjhnusiQ=
go.opentelemetry.io/otel v1.35.0/go.mod h1:UEqy8Zp11hpkUrL73gSlELM0DupHoiq72dR+Zqel/+Y=
go.opentelemetry.io/otel v1.40.0 h1:oA5YeOcpRTXq6NN7frwmwFR0Cn3RhTVZvXsP4duvCms=
go.opentelemetry.io/otel v1.40.0/go.mod h1:IMb+uXZUKkMXdPddhwAHm6UfOwJyh4ct1ybIlV14J0g=
go.opentelemetry.io/otel/metric v1.35.0 h1:0znxYu2SNyuMSQT4Y9WDWej0VpcsxkuklLa4/siN90M=
go.opentelemetry.io/otel/metric v1.35.0/go.mod h1:nKVFgxBZ2fReX6IlyW28MgZojkoAkJGaE8CpgeAU3oE=
go.opentelemetry.io/otel/metric v1.40.0 h1:rcZe317KPftE2rstWIBitCdVp89A2HqjkxR3c11+p9g=
go.opentelemetry.io/otel/metric v1.40.0/go.mod h1:ib/crwQH7N3r5kfiBZQbwrTge743UDc7DTFVZrrXnqc=
go.opentelemetry.io/otel/trace v1.35.0 h1:dPpEfJu1sDIqruz7BHFG3c7528f6ddfSWfFDVt/xgMs=
go.opentelemetry.io/otel/trace v1.35.0/go.mod h1:WUk7DtFp1Aw2MkvqGdwiXYDZZNvA/1J8o6xRXLrIkyc=
go.opentelemetry.io/otel/trace v1.40.0 h1:WA4etStDttCSYuhwvEa8OP8I5EWu24lkOzp+ZYblVjw=
go.opentelemetry.io/otel/trace v1.40.0/go.mod h1:zeAhriXecNGP/s2SEG3+Y8X9ujcJOTqQ5RgdEJcawiA=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=

View File

@@ -2,14 +2,15 @@ package host
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"github.com/docker/docker/client"
cfg "github.com/rony5394/blazena/config"
"github.com/moby/moby/client"
)
var token string = "12345";
@@ -22,6 +23,16 @@ type aService struct{
}
func Run(Config cfg.Config) {
DockerClient, err := client.NewClientWithOpts(client.FromEnv);
if err != nil {
panic("Failed to create DockerClient.");
}
_, err = DockerClient.Ping(context.Background())
if err != nil {
panic("Failed to ping DockerClient.");
}
services := getServices(Config);
for _, service := range services {