Something.

This commit is contained in:
rony5394
2026-02-23 15:39:57 +01:00
parent 136bb4c5b8
commit 9751a602cc
6 changed files with 67 additions and 11 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
ssh-key ssh-key
ssh-key.pub ssh-key.pub
ssk-key.tar

View File

@@ -13,6 +13,7 @@ import (
"net/http" "net/http"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client" "github.com/docker/docker/client"
) )
@@ -53,6 +54,16 @@ func Run(){
http.HandleFunc("/services", listServices); http.HandleFunc("/services", listServices);
http.HandleFunc("/prepare", prepare); http.HandleFunc("/prepare", prepare);
http.HandleFunc("/cleanup", cleanup); http.HandleFunc("/cleanup", cleanup);
ApiClient.NetworkCreate(context.Background(), "blazenaPohar", network.CreateOptions{
Attachable: true,
// Internal: true,
Driver: "overlay",
Labels: map[string]string{
"blazena.pohar": "true",
},
});
go func(){ go func(){
err = server.ListenAndServe(); err = server.ListenAndServe();
if err == http.ErrServerClosed { if err == http.ErrServerClosed {
@@ -69,6 +80,7 @@ func Run(){
<-ctx.Done(); <-ctx.Done();
fmt.Println("Stopping http server."); fmt.Println("Stopping http server.");
server.Close(); server.Close();
ApiClient.NetworkRemove(context.Background(), "blazenaPohar");
fmt.Println("Exiting!"); fmt.Println("Exiting!");
} }

View File

@@ -51,9 +51,7 @@ func prepare(w http.ResponseWriter, r *http.Request){
maxConcurrent := uint64(1); maxConcurrent := uint64(1);
totalCompletions := uint64(1); totalCompletions := uint64(1);
targetNode := labels["blazena.node"]; targetNode := labels["blazena.node"];
helperCommand := `apk add openssh rsync && \ helperCommand := `ssh-keygen -t ed25519 -f /host_key && \
ssh-keygen -t ed25519 -f /host_key && \
mkdir -p /root/.ssh/ && \
echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIByYbl8vu946LPycSO5pBohq3vMvvl+wX7snu1Bqpd7p test" > /root/.ssh/authorized_keys && \ echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIByYbl8vu946LPycSO5pBohq3vMvvl+wX7snu1Bqpd7p test" > /root/.ssh/authorized_keys && \
/usr/sbin/sshd -h /host_key -p 22 -D`; /usr/sbin/sshd -h /host_key -p 22 -D`;
@@ -70,7 +68,7 @@ func prepare(w http.ResponseWriter, r *http.Request){
}, },
TaskTemplate: swarm.TaskSpec{ TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{ ContainerSpec: &swarm.ContainerSpec{
Image: "docker.io/library/alpine:latest", Image: "registry.lan.ronycloud.dev/blazena/helper:latest",
Command: []string{"sh", "-c", helperCommand}, Command: []string{"sh", "-c", helperCommand},
Mounts: []mount.Mount{ Mounts: []mount.Mount{
mount.Mount{ mount.Mount{

View File

@@ -18,6 +18,7 @@ func scaleDown(serviceId string){
newScale := uint64(0); newScale := uint64(0);
updatedSpec.Mode.Replicated.Replicas = &newScale; updatedSpec.Mode.Replicated.Replicas = &newScale;
updatedSpec.Labels["blazena.scaledDown"] = "true";
scale.Store(serviceId, *originalScale); scale.Store(serviceId, *originalScale);
@@ -46,6 +47,7 @@ func scaleUp(serviceId string){
updatedSpec := inspectresoult.Spec; updatedSpec := inspectresoult.Spec;
updatedSpec.Mode.Replicated.Replicas = &originalScaleChecked; updatedSpec.Mode.Replicated.Replicas = &originalScaleChecked;
delete(updatedSpec.Labels, "blazena.scaledDown");
ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{}); ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
} }

4
helper.Dockerfile Normal file
View File

@@ -0,0 +1,4 @@
FROM alpine:3
RUN mkdir -p /root/.ssh/
RUN apk add openssh rsync --no-cache

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os"
"time" "time"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
@@ -46,16 +47,37 @@ func Run(Config cfg.Config) {
fmt.Println("Preparing: " + service.ServiceId + " volume: " + volume); fmt.Println("Preparing: " + service.ServiceId + " volume: " + volume);
if !prepareService(Config, service, volume) {continue} if !prepareService(Config, service, volume) {continue}
fmt.Println("Done!"); fmt.Println("Done!");
time.Sleep(5*time.Second);
fmt.Println("Cleaning Up: " + service.ServiceId + " volume: " + volume); command := `apk add --no-cache rsync openssh-client && \
echo "It Works Under Water!"`;
//rsync -avz --delete -e "ssh -i /ssh-key -p 2222 -o StrictHostKeyChecking=no" \
//root@`+Config.Nodes[service.Node].Ip+`:/volume/ /tmp`
time.Sleep(15*time.Second);
exec, err := DockerClient.ContainerExecCreate(context.Background(), "BlazenaStorage", container.ExecOptions{
Cmd: []string{"sh", "-c", command},
AttachStdout: true,
AttachStderr: true,
Tty: false,
});
if err != nil {
panic("Failed to create rsync exec!"+err.Error());
}
resp, err := DockerClient.ContainerExecAttach(context.Background(), exec.ID, container.ExecStartOptions{});
defer resp.Close();
io.Copy(os.Stdout, resp.Reader)
fmt.Println("Cleaning Up: " + service.ServiceId);
cleanupService(Config, service); cleanupService(Config, service);
fmt.Println("Done!"); fmt.Println("Done!");
} }
} }
time.Sleep(15*time.Second);
DockerClient.ContainerRemove(context.Background(), "BlazenaStorage", container.RemoveOptions{ DockerClient.ContainerRemove(context.Background(), "BlazenaStorage", container.RemoveOptions{
Force: true, Force: true,
}); });
@@ -169,7 +191,7 @@ func createStorageContainer(Config cfg.Config, DockerClient *client.Client){
Labels: map[string]string{ Labels: map[string]string{
"blazena.storage": "true", "blazena.storage": "true",
}, },
Cmd: strslice.StrSlice{"sleep", "infinity"}, Cmd: strslice.StrSlice{"sleep", "1h"},
}, &container.HostConfig{ }, &container.HostConfig{
Mounts: []mount.Mount{ Mounts: []mount.Mount{
mount.Mount{ mount.Mount{
@@ -180,7 +202,7 @@ func createStorageContainer(Config cfg.Config, DockerClient *client.Client){
}, },
}, },
//AutoRemove: true, //AutoRemove: true,
NetworkMode: "blazena", NetworkMode: "blazenaPohar",
}, &network.NetworkingConfig{ }, &network.NetworkingConfig{
}, &v1.Platform{}, "BlazenaStorage"); }, &v1.Platform{}, "BlazenaStorage");
@@ -194,4 +216,21 @@ func createStorageContainer(Config cfg.Config, DockerClient *client.Client){
panic("Failed to start BlazenaStorage container!"+err.Error()); panic("Failed to start BlazenaStorage container!"+err.Error());
} }
reader, err := os.Open("./ssh-key.tar");
if err != nil {
panic("Failed To open ssh key file for reading!"+err.Error());
}
defer reader.Close();
err = DockerClient.CopyToContainer(context.Background(), "BlazenaStorage", "/", reader, container.CopyToContainerOptions{
AllowOverwriteDirWithFile: true,
});
if err != nil {
panic("Failed to copy ssh key to container!"+err.Error());
}
} }