I am not proud of it.
Why did I choose to do this. I could get some more sleep but I choose to write this. I just want some snapshot before I start breaking stuff.
This commit is contained in:
69
docker/prepare.go
Normal file
69
docker/prepare.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/moby/moby/api/types/swarm"
|
||||
"github.com/moby/moby/api/types/mount"
|
||||
"github.com/moby/moby/client"
|
||||
)
|
||||
|
||||
func prepare(w http.ResponseWriter, r *http.Request){
|
||||
if r.Method != http.MethodPost{
|
||||
w.WriteHeader(http.StatusMethodNotAllowed);
|
||||
fmt.Fprint(w, "Method Not Allowed");
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO: add token auth
|
||||
|
||||
rawBody, err := io.ReadAll(r.Body);
|
||||
if err != nil {
|
||||
panic("Failed to read body!");
|
||||
}
|
||||
|
||||
var bodyDecoded struct{
|
||||
volume string
|
||||
node string
|
||||
};
|
||||
|
||||
err = json.Unmarshal(rawBody, &bodyDecoded);
|
||||
if err != nil {
|
||||
panic("Failed to unmarshal json."+ err.Error());
|
||||
}
|
||||
|
||||
maxConcurrent := uint64(1);
|
||||
totalCompletions := uint64(1);
|
||||
targetVolume := bodyDecoded.volume;
|
||||
targetNode := bodyDecoded.node;
|
||||
|
||||
ApiClient.ServiceCreate(context.Background(), client.ServiceCreateOptions{
|
||||
Spec: swarm.ServiceSpec{
|
||||
Mode: swarm.ServiceMode{
|
||||
ReplicatedJob: &swarm.ReplicatedJob{
|
||||
MaxConcurrent: &maxConcurrent,
|
||||
TotalCompletions: &totalCompletions,
|
||||
},
|
||||
},
|
||||
TaskTemplate: swarm.TaskSpec{
|
||||
ContainerSpec: &swarm.ContainerSpec{
|
||||
Image: "docker.io/library/alpine:latest",
|
||||
Mounts: []mount.Mount{
|
||||
mount.Mount{
|
||||
Source: targetVolume,
|
||||
Target: "/volume",
|
||||
Type: "bind",
|
||||
},
|
||||
},
|
||||
},
|
||||
Placement: &swarm.Placement{
|
||||
Constraints: []string{"node.hostname=="+targetNode},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user