Saving to storage.

New buildfile.
Storing backups in storage.
This commit is contained in:
rony5394
2026-04-09 15:32:54 +02:00
parent ec797025e5
commit 4a33358596
5 changed files with 103 additions and 13 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

@@ -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());
}

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

@@ -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("<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;
}