Compare commits

..

8 Commits

Author SHA1 Message Date
rony5394
0f316e8149 Some cleanup. 2026-05-25 15:39:14 +02:00
rony5394
ef37f17378 Added more proper waiting mechanism. 2026-05-24 13:13:27 +02:00
rony5394
778bab644f Switched originalScale storage 2026-05-19 14:20:22 +02:00
rony5394
7214e26168 Added prepull. 2026-04-27 15:16:24 +02:00
rony5394
32069f7e9d Added handeling for dependents. 2026-04-19 13:39:00 +02:00
rony5394
87d8388810 feat: some safety checks 2026-04-18 16:35:45 +02:00
rony5394
d0d5168fd2 Some logging added.
This change is untested!
2026-04-13 15:31:06 +02:00
rony5394
4a33358596 Saving to storage.
New buildfile.
Storing backups in storage.
2026-04-09 15:32:54 +02:00
18 changed files with 576 additions and 103 deletions

View File

@@ -1,5 +1,6 @@
FROM docker.io/library/golang:1.25.5-alpine AS builder
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.25.5-alpine AS builder
ARG TARGETARCH
WORKDIR /build
COPY go.mod go.sum ./
@@ -8,13 +9,13 @@ RUN go mod download
COPY . /build
RUN CGO_ENABLED=0 GOOS=linux go build -o /blazena
RUN CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go build -o /blazena
FROM docker.io/library/alpine:3.3
RUN apk add openssh rsync --no-cache
FROM docker.io/library/alpine:3.23
RUN apk add openssh rsync btrfs-progs --no-cache
COPY --from=builder /blazena /
COPY --from=builder --chmod=+x /blazena /
EXPOSE 1234
ENV MODE=invalid

View File

@@ -1,8 +1,13 @@
package config;
package config
import (
"os"
"encoding/base64"
"encoding/json"
"errors"
"os"
"time"
"github.com/docker/docker/api/types/registry"
);
type Config struct {
@@ -14,10 +19,15 @@ type Config struct {
LocalBasePath string
BlazenaImageUrl string
RegistryAuth RegistryAuth
EncodedRegistryAuth string
Constants struct{
OverlayNetworkName string
HelperServiceName string
StorageContainerName string
PrepullImageServiceName string
ServiceScaleTimeout time.Duration
SSHClientPKConfigName string
SSHHostSKSecretName string
}
}
@@ -26,24 +36,42 @@ type RegistryAuth struct {
Password string
}
func GetConfig() Config {
func GetConfig()(Config, error){
var cfg Config;
rawConfig, err := os.ReadFile("./config.json");
rawConfig, err := os.ReadFile("/config.json");
if err != nil{
panic("Failed it load config file." + err.Error());
return cfg, errors.New("Failed it load config file." + err.Error());
}
// Set defaults
cfg.Constants.OverlayNetworkName = "blazenaPohar";
cfg.Constants.HelperServiceName = "blazenaHelper";
cfg.Constants.StorageContainerName = "blazenaStorage";
cfg.Constants.PrepullImageServiceName = "blazenaPrepull";
cfg.Constants.ServiceScaleTimeout = time.Second * 15;
cfg.Constants.SSHClientPKConfigName = "blazenaSSHClientPublicKey";
cfg.Constants.SSHHostSKSecretName = "blazenaSSHHostPrivateKey";
err = json.Unmarshal(rawConfig, &cfg);
if err != nil{
panic("Failed to unmarshal config." + err.Error())
return cfg, errors.New("Failed to unmarshal config: " + err.Error());
}
return cfg;
authConfig := registry.AuthConfig{
Username: cfg.RegistryAuth.Username,
Password: cfg.RegistryAuth.Password,
}
authJSON, err := json.Marshal(authConfig)
if err != nil {
panic("Failed to marshal auth config!"+ err.Error());
}
cfg.EncodedRegistryAuth = base64.StdEncoding.EncodeToString(authJSON);
return cfg, err;
}

View File

@@ -2,10 +2,10 @@ package docker
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"time"
"github.com/docker/docker/api/types/swarm"
@@ -20,44 +20,40 @@ func cleanup(w http.ResponseWriter, r *http.Request){
if !bearerAuth(w, r) {return;}
rawBody, err := io.ReadAll(r.Body);
if err != nil {
panic("Failed to read body!");
}
var bodyDecoded struct{
ServiceId string `json:"serviceId"`
};
err = json.Unmarshal(rawBody, &bodyDecoded);
if err != nil {
panic("Failed to unmarshal json."+ err.Error());
}
listResoult, err := ApiClient.ServiceList(context.Background(), swarm.ServiceListOptions{});
if err != nil {
panic("Failed to list services."+ err.Error());
slog.Error("Failed to list services", slog.Any("propagatedError", err));
os.Exit(1);
}
var helperServiceId string;
var helperServices int;
for _, service := range listResoult{
if service.Spec.Labels["blazena.helper"] != "true" {
continue;
}
helperServiceId = service.ID;
break;
helperServices ++;
}
if helperServiceId == ""{
panic("Helper service not found!");
slog.Warn("Helper service wasn't found");
http.Error(w, "Internal Server Error", http.StatusInternalServerError);
return;
}
if helperServices > 1{
slog.Error("There are more than 1 helper service.");
os.Exit(1);
}
err = ApiClient.ServiceRemove(context.Background(), helperServiceId);
if err != nil {
panic("Failed to remove helper service."+ err.Error());
}
//TODO: add proper wait system
time.Sleep(7*time.Second);
fmt.Fprint(w, bodyDecoded.ServiceId);
fmt.Fprint(w, helperServiceId);
}

View File

@@ -4,15 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"net/http"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
@@ -21,39 +22,48 @@ import (
// Add mutex.
var ApiClient *client.Client;
var scale sync.Map;
var token string = "12345";
var theConfig cfg.Config;
var theShutdownFuncPointer *context.CancelFunc;
type aService struct{
ServiceId string `json:"serviceId"`;
VolumeNames []string `json:"volumeNames"`;
Node string `json:"node"`;
Dependents []string `json:"dependents"`;
}
func Run(Config cfg.Config){
theConfig = Config;
// Before touching the line below think.
theConfig = Config;
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM);
theShutdownFuncPointer = &stop;
var err error;
ApiClient, err = client.NewClientWithOpts(client.FromEnv);
if(err != nil){
panic("Docker client was not able to init from env!" + err.Error());
slog.Error("Failed to create docker client!", slog.String("note", "Try to look into DOCKER_HOST env var or check if socket exists and works"));
os.Exit(1);
}
info, err := ApiClient.Info(context.Background())
if(err != nil){
panic("Error getting info!" + err.Error());
slog.Error("Error getting info from docker socket!", slog.String("note", "This is kind of ping."));
os.Exit(1);
}
if(!info.Swarm.ControlAvailable){
panic("Node is not a swarm manager.");
slog.Error("This node is not a swarm manager!");
os.Exit(1);
}
server := &http.Server{
Addr: ":1234",
}
http.HandleFunc("/prepull", prepullImage);
http.HandleFunc("/services", listServices);
http.HandleFunc("/scale/up", scaleUp);
http.HandleFunc("/scale/down", scaleDown);
@@ -68,7 +78,7 @@ func Run(Config cfg.Config){
stop();
});
ApiClient.NetworkCreate(context.Background(), theConfig.Constants.OverlayNetworkName, network.CreateOptions{
ApiClient.NetworkCreate(context.Background(), Config.Constants.OverlayNetworkName, network.CreateOptions{
Attachable: true,
// Internal: true,
Driver: "overlay",
@@ -84,7 +94,8 @@ func Run(Config cfg.Config){
}
if(err != nil){
panic("Unable to start http server!" + err.Error());
slog.Error("Unable to start http server!", slog.Any("propagatedError", err));
os.Exit(1);
}
}();
@@ -96,7 +107,7 @@ func Run(Config cfg.Config){
fmt.Println("Stopping http server.");
server.Close();
ApiClient.NetworkRemove(context.Background(), theConfig.Constants.OverlayNetworkName);
ApiClient.NetworkRemove(context.Background(), Config.Constants.OverlayNetworkName);
ApiClient.ConfigRemove(context.Background(), "blazenaSSHPublicKey")
ApiClient.SecretRemove(context.Background(), "blazenaSSHHostPrivateKey");
@@ -110,6 +121,7 @@ func bearerAuth(w http.ResponseWriter, r *http.Request)bool {
if authHeader != expected {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, "Unauthorized")
slog.Warn("Unauthorized request received", slog.Any("request", *r));
return false;
}
return true;
@@ -131,6 +143,17 @@ func listServices(w http.ResponseWriter, r *http.Request){
var services []aService;
nodes, err := ApiClient.NodeList(context.Background(), swarm.NodeListOptions{});
var validNodeHostnames []string;
for _, node := range nodes{
validNodeHostnames = append(validNodeHostnames, node.Description.Hostname);
}
SERVICES:
for _, service := range list{
var settings map[string]string = service.Spec.Labels;
@@ -139,12 +162,62 @@ func listServices(w http.ResponseWriter, r *http.Request){
continue;
}
if !contains(validNodeHostnames, settings["blazena.node"]) {
errMsg := "Node with hostname:'"+ settings["blazena.node"] +"' does not exist.";
slog.Warn("Invalid Service Config!", slog.String("serviceId", service.ID), slog.String("errMessage", errMsg));
continue SERVICES;
}
targetVolumes := strings.Split(settings["blazena.volumes"], ",");
var validVolumeNames []string;
for _, mnt := range service.Spec.TaskTemplate.ContainerSpec.Mounts{
if mnt.Type != mount.TypeVolume {
continue
}
validVolumeNames = append(validVolumeNames, mnt.Source);
}
for _, volume := range targetVolumes{
if contains(validVolumeNames, volume){
continue;
}
errMsg := "Volume name '"+ volume + "' is not in the service spec!";
slog.Warn("Invalid Service Config!", slog.String("serviceId", service.ID), slog.String("errMessage", errMsg));
continue SERVICES;
}
var dependents []string;
if(settings["blazena.dependents"] != ""){
dependents = strings.Split(settings["blazena.dependents"], ",");
} else {
dependents = make([]string, 0);
}
var validDependents []string;
for _, x := range list{
validDependents = append(validDependents, x.Spec.Name);
}
slog.Debug("validDependents", slog.Any("value", validDependents));
slog.Debug("dependents", slog.Any("value", dependents));
for _, dependent := range dependents {
if contains(validDependents, dependent){continue;}
errMsg := "Dependent named '"+ dependent +"' was not found in this cluster!";
slog.Warn("Invalid Service Config!", slog.String("serviceId", service.ID), slog.String("errMessage", errMsg));
continue SERVICES;
}
services = append(services, aService{
ServiceId: service.ID,
VolumeNames: targetVolumes,
Node: settings["blazena.node"],
Dependents: dependents,
});
}
@@ -152,7 +225,8 @@ func listServices(w http.ResponseWriter, r *http.Request){
bytes, err := json.Marshal(services);
if err != nil{
panic("Error during response encoding!" + err.Error());
slog.Error("Error during response encoding!", slog.Any("propagatedError", err));
os.Exit(1);
}
fmt.Fprint(w, string(bytes));

View File

@@ -1,11 +1,14 @@
package docker
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"context"
"os"
"github.com/docker/docker/api/types/swarm"
"github.com/rony5394/blazena/shared"
@@ -32,23 +35,35 @@ func exchangeKeys(w http.ResponseWriter, r *http.Request){
if err != nil {
panic("Failed to unmarshal json."+ err.Error());
}
sshPkPem := bodyDecoded.SshPkPem;
sshClientPkPem := bodyDecoded.SshPkPem;
hostKeypair := shared.GenerateSSHKeypair();
encoded, err := json.Marshal(struct{HostPkPem string `json:"hostPkPem"`}{HostPkPem: hostKeypair.Public});
if err != nil {
panic("I wonder how. I wonder why?"+err.Error());
slog.Error("Failed to marshal host pk into response.", slog.Any("propagatedError", err));
os.Exit(42);
}
ApiClient.ConfigCreate(context.Background(), swarm.ConfigSpec{
Data: []byte(sshPkPem),
Annotations: swarm.Annotations{Name: "blazenaSSHPublicKey"},
_, err = ApiClient.ConfigCreate(context.Background(), swarm.ConfigSpec{
Data: []byte(sshClientPkPem),
Annotations: swarm.Annotations{Name: theConfig.Constants.SSHClientPKConfigName},
});
ApiClient.SecretCreate(context.Background(), swarm.SecretSpec{
if err != nil {
slog.Error("Failed to create a config.", slog.Any("propagatedError", err));
os.Exit(1);
}
_, err = ApiClient.SecretCreate(context.Background(), swarm.SecretSpec{
Data: []byte(hostKeypair.Private),
Annotations: swarm.Annotations{Name: "blazenaSSHHostPrivateKey"},
Annotations: swarm.Annotations{Name: theConfig.Constants.SSHHostSKSecretName},
});
if err != nil {
slog.Error("Failed to create a secret.", slog.Any("propagatedError", err));
os.Exit(1);
}
fmt.Fprint(w, string(encoded));
}

View File

@@ -2,17 +2,17 @@ package docker
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"encoding/base64"
"time"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
cfg "github.com/rony5394/blazena/config"
@@ -53,6 +53,7 @@ func prepare(w http.ResponseWriter, r *http.Request){
pullBlazenaImage();
createHelper(theConfig, labels["blazena.node"], bodyDecoded.VolumeId);
//TODO: add proper waiting system.
time.Sleep(7*time.Second);
fmt.Fprint(w, bodyDecoded.ServiceId);
@@ -129,7 +130,7 @@ func createHelper(Config cfg.Config, targetNode string, targetVolume string){
stopGracePeriod := time.Second * 5;
helperCommand := `/usr/sbin/sshd -h /host-key -p 2222 -D`;
sshKeyConfigId, err := getConfigIDByName(ApiClient, "blazenaSSHPublicKey");
sshKeyConfigId, err := getConfigIDByName(ApiClient, theConfig.Constants.SSHClientPKConfigName);
if err != nil {
panic("Docker needs both id and name to mount config for some reason and getting id of it failed!"+err.Error());
@@ -163,7 +164,7 @@ func createHelper(Config cfg.Config, targetNode string, targetVolume string){
Configs: []*swarm.ConfigReference{
&swarm.ConfigReference{
ConfigID: sshKeyConfigId,
ConfigName: "blazenaSSHPublicKey",
ConfigName: theConfig.Constants.SSHClientPKConfigName,
File: &swarm.ConfigReferenceFileTarget{
Name: "/root/.ssh/authorized_keys",
Mode: 0600,
@@ -175,7 +176,7 @@ func createHelper(Config cfg.Config, targetNode string, targetVolume string){
Secrets: []*swarm.SecretReference{
&swarm.SecretReference{
SecretID: sshHostKeySecretId,
SecretName: "blazenaSSHHostPrivateKey",
SecretName: theConfig.Constants.SSHHostSKSecretName,
File: &swarm.SecretReferenceFileTarget{
Name: "/host-key",
Mode: 0600,

78
docker/prepullImage.go Normal file
View File

@@ -0,0 +1,78 @@
package docker
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"time"
"github.com/docker/docker/api/types/swarm"
)
func prepullImage(w http.ResponseWriter, r *http.Request){
if r.Method != http.MethodPost{
w.WriteHeader(http.StatusMethodNotAllowed);
fmt.Fprint(w, "Method Not Allowed");
return;
}
if !bearerAuth(w, r){return;}
sc, err := ApiClient.ServiceCreate(context.Background(), swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: theConfig.Constants.PrepullImageServiceName,
Labels: map[string]string{"blazena.prepull": "true"},
},
Mode: swarm.ServiceMode{
GlobalJob: &swarm.GlobalJob{},
},
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{
Image: theConfig.BlazenaImageUrl,
Command: []string{"sleep", "1s"},
},
},
}, swarm.ServiceCreateOptions{
QueryRegistry: true,
EncodedRegistryAuth: theConfig.EncodedRegistryAuth,
});
if err != nil {
slog.Error("Failed to create prepull service", slog.Any("propagatedError", err));
os.Exit(3);
}
slog.Info("Started Prepull of blazena image.");
go func(){
CHECKLOOP: for {
time.Sleep(3*time.Second);
tasks, err := ApiClient.TaskList(context.Background(), swarm.TaskListOptions{});
if err != nil {
slog.Error("Failed to list tasks.", slog.Any("propagatedError", err));
os.Exit(3);
}
for _, task := range tasks {
if task.ServiceID != sc.ID {continue};
if task.Status.State != "complete" {
time.Sleep(1*time.Second);
continue CHECKLOOP
};
}
err = ApiClient.ServiceRemove(context.Background(), sc.ID);
if err != nil {
slog.Warn("Failed to remove prepull service.", slog.Any("propagatedError", err));
}
slog.Info("Prepull Finished");
break CHECKLOOP;
}
}();
}

View File

@@ -3,7 +3,10 @@ package docker
import (
"context"
"io"
"log/slog"
"net/http"
"os"
"strconv"
"time"
"encoding/json"
@@ -53,16 +56,16 @@ func scaleDown(w http.ResponseWriter, r *http.Request){
newScale := uint64(0);
updatedSpec.Mode.Replicated.Replicas = &newScale;
updatedSpec.Labels["blazena.scaledDown"] = "true";
scale.Store(serviceId, *originalScale);
updatedSpec.Labels["blazena.originalScale"] = strconv.FormatUint(*originalScale, 10);
_, err = ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
if(err != nil){
panic("Failed to update service."+ err.Error());
}
ctx, cancel := context.WithTimeout(context.Background(), theConfig.Constants.ServiceScaleTimeout);
defer cancel();
//TODO: Add proper wait system
time.Sleep(15 * time.Second);
waitForScale(serviceId, ctx, 0);
}
func scaleUp(w http.ResponseWriter, r *http.Request){
@@ -99,22 +102,67 @@ func scaleUp(w http.ResponseWriter, r *http.Request){
return;
}
originalScale, ok := scale.Load(serviceId);
if(!ok){
originalScale := inspectresoult.Spec.Labels["blazena.originalScale"];
if(originalScale == ""){
panic("Its not okay!");
}
originalScaleChecked, ok := originalScale.(uint64);
if(!ok){
panic("Its very not okay!")
originalScaleChecked, err := strconv.ParseUint(originalScale, 10, 64);
if(err != nil){
panic("Its very not okay!"+ err.Error())
}
updatedSpec := inspectresoult.Spec;
updatedSpec.Mode.Replicated.Replicas = &originalScaleChecked;
delete(updatedSpec.Labels, "blazena.scaledDown");
delete(updatedSpec.Labels, "blazena.originalScale");
ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
_, err = ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
//TODO: Add proper wait system
time.Sleep(15 * time.Second);
if err != nil {
slog.Error("Failed to update/scale a service.", slog.Any("propagatedError", err), slog.String("serviceId", serviceId));
os.Exit(1);
}
ctx, cancel := context.WithTimeout(context.Background(), theConfig.Constants.ServiceScaleTimeout);
defer cancel();
waitForScale(serviceId, ctx, int(originalScaleChecked));
}
func waitForScale(serviceId string, ctx context.Context, desiredCount int){
startTime := time.Now();
for ctx.Err() == nil {
tasks, err := ApiClient.TaskList(context.Background(), swarm.TaskListOptions{});
if err != nil {
slog.Error("Failed to list tasks.", slog.Any("propagatedError", err));
os.Exit(1);
}
var running int;
for _, task := range tasks {
if task.ServiceID != serviceId {
continue;
}
if task.Status.State == swarm.TaskStateRunning{
running ++;
}
}
if running == desiredCount {
slog.Debug("Rescaled Service",
slog.String("serviceId", serviceId),
slog.Any("took", time.Since(startTime)),
slog.Any("targetScale", desiredCount),
);
return;
}
time.Sleep(1*time.Second);
}
if ctx.Err() == context.DeadlineExceeded{
slog.Error("Failed to rescale service in given time.", slog.Any("serviceId", serviceId));
}
}

1
go.mod
View File

@@ -19,6 +19,7 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/sys/atomicwriter v0.1.0 // indirect
github.com/moby/term v0.5.2 // indirect

View File

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

View File

@@ -52,7 +52,6 @@ func createStorageContainer(Config cfg.Config, DockerClient *client.Client, sshS
Type: mount.TypeBind,
Source: Config.LocalBasePath,
Target: "/volume",
ReadOnly: true,
},
},
//AutoRemove: true,

View File

@@ -1,10 +1,13 @@
package host
import (
"bytes"
"encoding/json"
"io"
"log/slog"
"net/http"
"bytes"
"os"
cfg "github.com/rony5394/blazena/config"
);
@@ -19,12 +22,15 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
if err != nil {
panic("Failed to marshal body."+ err.Error());
slog.Error("Failed to marshal body.", slog.Any("propagatedError", err), slog.String("note", "Input for this marshal operation is that ssh pk. So the kebab is going on!"))
os.Exit(42);
}
rq, err := http.NewRequest("POST", Config.DockerManagerBaseUrl + "/keys", bytes.NewBuffer(bodyEncoded));
if err != nil{
panic("Failed to create http request"+ err.Error());
slog.Error("Failed to create request", slog.Any("propagatedError", err), slog.String("note", "not send just create the object"));
os.Exit(1);
}
rq.Header.Set("Authorization", "Bearer "+ token);
@@ -32,7 +38,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
rs, err := http.DefaultClient.Do(rq);
if err != nil{
panic("Failed to send http request"+ err.Error());
slog.Error("Failed to send http request", slog.Any("propagatedError", err));
os.Exit(1);
}
defer rs.Body.Close();
@@ -40,7 +47,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
rsBodyRaw, err := io.ReadAll(rs.Body);
if err != nil{
panic("Failed to read response's body!"+err.Error());
slog.Error("Failed to read response body!" , slog.Any("propagatedError", err));
os.Exit(1);
}
var rsBody struct{
@@ -49,7 +57,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
err = json.Unmarshal(rsBodyRaw, &rsBody);
if err != nil{
panic("Failed to unmarshal rsBodyRaw!"+ err.Error());
slog.Error("Failed to unmarshal rsBodyRaw!", slog.Any("propagatedError", err));
os.Exit(1);
}

View File

@@ -5,8 +5,10 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"time"
@@ -23,6 +25,7 @@ type aService struct{
ServiceId string `json:"serviceId"`;
VolumeNames []string `json:"volumeNames"`;
Node string `json:"node"`;
Dependents []string `json:"dependents"`;
}
func Run(Config cfg.Config) {
@@ -38,22 +41,36 @@ func Run(Config cfg.Config) {
sshKeyPair := shared.GenerateSSHKeypair();
sshHostPkPem := exchangeKeys(Config, string(sshKeyPair.Public));
go prepullImage(Config);
createStorageContainer(Config, DockerClient, sshKeyPair.Private, sshHostPkPem);
services := getServices(Config);
for _, service := range services {
fmt.Println("Scaling Down: "+service.ServiceId)
scale(Config, service.ServiceId, false);
fmt.Println("Done!");
for _, volume := range service.VolumeNames{
fmt.Println("Preparing: " + service.ServiceId + " volume: " + volume);
if !prepareService(Config, service, volume) {continue}
fmt.Println("Done!");
// Skiping Host Key Check is temporary.
for _, dependent := range service.Dependents {
slog.Info("Scaling Down Dependent", slog.String("serviceId", service.ServiceId), slog.String("dependentId", dependent));
scale(Config, dependent, false);
slog.Info("Done");
}
slog.Info("Scaling Down", slog.String("serviceId", service.ServiceId));
scale(Config, service.ServiceId, false);
slog.Info("Done");
for _, volume := range service.VolumeNames{
slog.Info("Preparing", slog.String("serviceId", service.ServiceId), slog.String("volumeId", volume));
if !prepareService(Config, service, volume) {continue}
slog.Info("Done");
targetStoragePath, _ := generateStoragePath(Config, service.Node, volume, DockerClient);
sourceStoragePath := "root@tasks."+ Config.Constants.HelperServiceName +":/volume";
slog.Debug("targetStoragePath", slog.String("value", targetStoragePath), slog.String("serviceId", service.ServiceId));
slog.Debug("sourceStoragePath", slog.String("value", sourceStoragePath), slog.String("serviceId", service.ServiceId));
command := `rsync -avz --delete -e "ssh -i /ssh-key -p 2222 -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/expected-host-key" \
root@tasks.`+ Config.Constants.HelperServiceName +`:/volume/ /tmp/` + volume;
`+ sourceStoragePath +" "+ targetStoragePath;
exec, err := DockerClient.ContainerExecCreate(context.Background(), Config.Constants.StorageContainerName, container.ExecOptions{
Cmd: []string{"sh", "-c", command},
@@ -62,30 +79,46 @@ func Run(Config cfg.Config) {
Tty: false,
});
if err != nil {
panic("Failed to create rsync exec!"+err.Error());
slog.Error("Failed to create rsync exec!", slog.Any("propagatedError", err));
os.Exit(1);
}
resp, err := DockerClient.ContainerExecAttach(context.Background(), exec.ID, container.ExecStartOptions{});
if err != nil {
slog.Error("Failed to create container exec!", slog.Any("propagatedError", err));
}
defer resp.Close();
io.Copy(os.Stdout, resp.Reader)
time.Sleep(30*time.Second);
fmt.Println("Cleaning Up: " + service.ServiceId);
slog.Info("Cleaning Up", slog.String("serviceId", service.ServiceId), slog.String("volumeId", volume));
cleanupService(Config, service);
fmt.Println("Done!");
slog.Info("Done!");
}
fmt.Println("Scaling up: "+service.ServiceId);
slog.Info("Scaling Up", slog.String("serviceId", service.ServiceId));
scale(Config, service.ServiceId, true);
fmt.Println("Done!");
slog.Info("Done!");
for _, dependent := range service.Dependents {
slog.Info("Scaling Up Dependent", slog.String("serviceId", service.ServiceId), slog.String("dependentId", dependent));
scale(Config, dependent, true);
slog.Info("Done");
}
}
DockerClient.ContainerRemove(context.Background(), Config.Constants.StorageContainerName, container.RemoveOptions{
Force: true,
});
if !shutdown(Config){panic("Failed to shutdown docker api!");}
if !shutdown(Config){
slog.Error("Failed to shutdown docker api!");
os.Exit(1);
}
slog.Info("Finished whole backup run.");
}
func getServices(Config cfg.Config)[]aService{
@@ -109,6 +142,7 @@ func getServices(Config cfg.Config)[]aService{
if err != nil {
panic("Failed to unmarshal response.");
}
slog.Debug("Services", slog.Any("value", services));
return services;
}
@@ -185,3 +219,95 @@ func addToTar(tw *tar.Writer, filename string, content string) error{
return err;
}
func createIfMissing(targetPath string, DockerClient *client.Client, cfg cfg.Config) error{
const cmd = `#!/bin/sh
set -e
TARGET_PATH=$1
# Remove trailing slash
TARGET_PATH=${TARGET_PATH%/}
CURRENT=""
case "$TARGET_PATH" in
/*) CURRENT="/" ;;
esac
OLD_IFS=$IFS
IFS='/'
for PART in $TARGET_PATH; do
[ -z "$PART" ] && continue
if [ "$CURRENT" = "/" ]; then
NEXT="${CURRENT}${PART}"
else
NEXT="${CURRENT}/${PART}"
fi
if [ ! -e "$NEXT" ]; then
case "$PART" in
@*)
echo "Creating Btrfs subvolume: $NEXT"
btrfs subvolume create "$NEXT"
;;
*)
echo "Creating directory: $NEXT"
mkdir "$NEXT"
;;
esac
else
echo "Already exists: $NEXT"
fi
CURRENT="$NEXT"
done
IFS=$OLD_IFS`;
exec, err := DockerClient.ContainerExecCreate(context.Background(), cfg.Constants.StorageContainerName, container.ExecOptions{
Cmd: []string{"sh", "-c", cmd, "_", targetPath},
AttachStdout: true,
AttachStderr: true,
Tty: false,
});
if err != nil {
panic("Failed to create execute!"+err.Error());
}
resp, err := DockerClient.ContainerExecAttach(context.Background(), exec.ID, container.ExecStartOptions{});
defer resp.Close();
if err != nil {
panic("Failed to atach to exec!"+err.Error());
}
inspect, err := DockerClient.ContainerExecInspect(context.Background(), exec.ID);
if(inspect.ExitCode != 0){
fmt.Println("<resp>");
io.Copy(os.Stdout, resp.Reader);
fmt.Println("</resp>");
return errors.New("Execution did return non zero code!");
}
return nil;
}
func generateStoragePath(cfg cfg.Config, node string, volumeId string, DockerClient *client.Client) (string, error){
var path string;
path += "/volume";
path += "/@"+ node +"/@"+ volumeId;
err := createIfMissing(path, DockerClient, cfg);
if err != nil {
return "", err;
}
return path, nil;
}

30
host/prepullImage.go Normal file
View File

@@ -0,0 +1,30 @@
package host
import (
"bytes"
"log/slog"
"net/http"
"os"
cfg "github.com/rony5394/blazena/config"
);
func prepullImage(Config cfg.Config){
rq, err := http.NewRequest("POST", Config.DockerManagerBaseUrl + "/prepull", bytes.NewBufferString("{}"));
if err != nil{
slog.Error("Failed to create request", slog.Any("propagatedError", err), slog.String("note", "not send just create the object"));
os.Exit(1);
}
rq.Header.Set("Authorization", "Bearer "+ token);
rq.Close = true;
rs, err := http.DefaultClient.Do(rq);
if err != nil{
slog.Error("Failed to send http request", slog.Any("propagatedError", err));
os.Exit(1);
}
defer rs.Body.Close();
}

View File

@@ -37,7 +37,9 @@ func scale(Config cfg.Config, serviceId string, up bool)bool{
rq.Header.Set("Authorization", "Bearer "+ token);
rq.Close = true;
rs, err := http.DefaultClient.Do(rq);
defer rs.Body.Close();
if err == nil {
rs.Body.Close();
}
if err != nil{
panic("Failed to send http request"+ err.Error());

42
main.go
View File

@@ -1,18 +1,49 @@
package main
import (
"log/slog"
"os"
"time"
cfg "github.com/rony5394/blazena/config"
"github.com/rony5394/blazena/docker"
"github.com/rony5394/blazena/host"
cfg "github.com/rony5394/blazena/config"
);
)
//TODO: consider adding blazena.doNotTouch
/*
If the exit code is X then it means Y:
| X | Y |
|----|------------------------------------------------------------------------------------------|
| 0 | Everything should be good, normal exit. |
| 1 | Some common error, but that still mean it is going to crash. |
| 3 | Ask yourself if you are not using dev version in prod. If not then spam the developer. |
| 42 | WHAT THE ACTUAL ***** IS HAPPENING. or assume something is very wrong in the app itself. |
| 69 | [INSERT HERE] |
*/
func main() {
if(len(os.Args) < 2){
panic("Usage: blazena <mode>");
}
var config = cfg.GetConfig();
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}));
slog.SetDefault(logger);
config, err := cfg.GetConfig();
if(err != nil){
slog.Error("Failed to load config!", slog.Any("propagatedError", err.Error()));
os.Exit(1);
}
slog.Debug("Config", slog.Any("Value", config));
startTime := time.Now();
mode := os.Args[1];
switch mode {
@@ -22,7 +53,12 @@ func main() {
case "host":
host.Run(config);
break;
case "pull":
os.Exit(0);
break;
default:
panic("Invalid runtime mode!");
}
slog.Debug("Whole run took", slog.String("time", time.Since(startTime).String()));
}

View File

@@ -1,9 +1,11 @@
package shared
package shared
import (
"crypto/ed25519"
"crypto/rand"
"encoding/pem"
"log/slog"
"os"
"golang.org/x/crypto/ssh"
)
@@ -16,19 +18,22 @@ type Keypair struct {
func GenerateSSHKeypair() Keypair {
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
panic(err)
slog.Error("Failed to generate an ssh keypair.", slog.Any("propagatedError", err));
os.Exit(42);
}
privBlock, err := ssh.MarshalPrivateKey(privateKey, "")
if err != nil {
panic(err)
slog.Error("Failed to marshal private key", slog.Any("propagatedError", err));
os.Exit(42);
}
privPem := pem.EncodeToMemory(privBlock)
sshPubKey, err := ssh.NewPublicKey(publicKey)
if err != nil {
panic(err)
slog.Error("Failed deriving public ssh key from a private one.", slog.Any("propagatedError", err));
os.Exit(42);
}
pubBytes := ssh.MarshalAuthorizedKey(sshPubKey)

28
shared/trace.go Normal file
View File

@@ -0,0 +1,28 @@
package shared
import (
"log/slog"
"github.com/google/uuid"
);
func NewTraceId()string{
return uuid.New().String();
}
func helper(name string, id *string)slog.Attr{
if id == nil{
return slog.String(name, NewTraceId());
}
return slog.String(name, *id);
}
func NewSlogTrace(id *string)slog.Attr{
return helper("trace", id);
}
func NewSlogOperation(id *string)slog.Attr{
return helper("operation", id);
}