From 4a33358596e4e88072af4c0d2fc87671368195c2 Mon Sep 17 00:00:00 2001 From: rony5394 <143897221+rony5394@users.noreply.github.com> Date: Thu, 9 Apr 2026 15:32:54 +0200 Subject: [PATCH] Saving to storage. New buildfile. Storing backups in storage. --- Dockerfile | 11 +++--- config/config.go | 2 +- helper.Dockerfile | 4 -- host/createHost.go | 1 - host/host.go | 98 +++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 103 insertions(+), 13 deletions(-) delete mode 100644 helper.Dockerfile diff --git a/Dockerfile b/Dockerfile index 02fb324..0675271 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/config/config.go b/config/config.go index eee570e..be34fec 100644 --- a/config/config.go +++ b/config/config.go @@ -29,7 +29,7 @@ type RegistryAuth struct { func GetConfig() Config { 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()); } diff --git a/helper.Dockerfile b/helper.Dockerfile deleted file mode 100644 index 0fbab05..0000000 --- a/helper.Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM alpine:3 - -RUN mkdir -p /root/.ssh/ -RUN apk add openssh rsync --no-cache diff --git a/host/createHost.go b/host/createHost.go index ebefc1f..8eb52c6 100644 --- a/host/createHost.go +++ b/host/createHost.go @@ -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, diff --git a/host/host.go b/host/host.go index ccf303b..710053a 100644 --- a/host/host.go +++ b/host/host.go @@ -5,6 +5,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -51,9 +52,10 @@ func Run(Config cfg.Config) { if !prepareService(Config, service, volume) {continue} fmt.Println("Done!"); - // Skiping Host Key Check is temporary. + storagePath, _ := generateStoragePath(Config, service.Node, volume, DockerClient); + fmt.Println(storagePath); 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; + root@tasks.`+ Config.Constants.HelperServiceName +`:/volume/ ` +storagePath; exec, err := DockerClient.ContainerExecCreate(context.Background(), Config.Constants.StorageContainerName, container.ExecOptions{ Cmd: []string{"sh", "-c", command}, @@ -185,3 +187,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(""); + io.Copy(os.Stdout, resp.Reader); + fmt.Println(""); + 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; +}