Something
This commit is contained in:
@@ -6,9 +6,12 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/moby/moby/api/types/swarm"
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -19,7 +22,9 @@ func prepare(w http.ResponseWriter, r *http.Request){
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO: add token auth
|
||||
//if !bearerAuth(w, r){
|
||||
// return;
|
||||
//}
|
||||
|
||||
rawBody, err := io.ReadAll(r.Body);
|
||||
if err != nil {
|
||||
@@ -27,8 +32,7 @@ func prepare(w http.ResponseWriter, r *http.Request){
|
||||
}
|
||||
|
||||
var bodyDecoded struct{
|
||||
volume string
|
||||
node string
|
||||
ServiceId string `json:"serviceId"`
|
||||
};
|
||||
|
||||
err = json.Unmarshal(rawBody, &bodyDecoded);
|
||||
@@ -36,34 +40,71 @@ func prepare(w http.ResponseWriter, r *http.Request){
|
||||
panic("Failed to unmarshal json."+ err.Error());
|
||||
}
|
||||
|
||||
scaleDown(bodyDecoded.ServiceId);
|
||||
|
||||
inspectResoults, err := ApiClient.ServiceInspect(context.Background(), bodyDecoded.ServiceId, client.ServiceInspectOptions{});
|
||||
if err != nil{
|
||||
panic("Failed to inspect service."+ err.Error());
|
||||
}
|
||||
|
||||
labels := inspectResoults.Service.Spec.Labels;
|
||||
time.Sleep(10);
|
||||
|
||||
maxConcurrent := uint64(1);
|
||||
totalCompletions := uint64(1);
|
||||
targetVolume := bodyDecoded.volume;
|
||||
targetNode := bodyDecoded.node;
|
||||
targetVolumes := strings.Split(labels["blazena.volumes"], ",");
|
||||
targetNode := labels["blazena.node"];
|
||||
helperCommand := `apk add openssh rsync && \
|
||||
ssh-keygen -t ed25519 -f /host_key && \
|
||||
mkdir -p /root/.ssh/ && \
|
||||
echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIByYbl8vu946LPycSO5pBohq3vMvvl+wX7snu1Bqpd7p test" > /root/.ssh/authorized_keys && \
|
||||
/usr/sbin/sshd -h /host_key -p 22 -D`;
|
||||
|
||||
for _, targetVolume := range targetVolumes{
|
||||
|
||||
ApiClient.ServiceCreate(context.Background(), client.ServiceCreateOptions{
|
||||
Spec: swarm.ServiceSpec{
|
||||
Mode: swarm.ServiceMode{
|
||||
ReplicatedJob: &swarm.ReplicatedJob{
|
||||
MaxConcurrent: &maxConcurrent,
|
||||
TotalCompletions: &totalCompletions,
|
||||
_, err := ApiClient.ServiceCreate(context.Background(), client.ServiceCreateOptions{
|
||||
Spec: swarm.ServiceSpec{
|
||||
Annotations: swarm.Annotations{
|
||||
Labels: map[string]string{"blazena.helper": "true"},
|
||||
},
|
||||
},
|
||||
TaskTemplate: swarm.TaskSpec{
|
||||
ContainerSpec: &swarm.ContainerSpec{
|
||||
Image: "docker.io/library/alpine:latest",
|
||||
Mounts: []mount.Mount{
|
||||
mount.Mount{
|
||||
Source: targetVolume,
|
||||
Target: "/volume",
|
||||
Type: "bind",
|
||||
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",
|
||||
},
|
||||
},
|
||||
},
|
||||
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},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
if err != nil {
|
||||
panic("Failed to create helper service."+ err.Error());
|
||||
}
|
||||
|
||||
fmt.Fprint(w, bodyDecoded.ServiceId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user