From 064a944655b48e0d0ad84af5d9c2bbd6cacd62b7 Mon Sep 17 00:00:00 2001 From: rony5394 <143897221+rony5394@users.noreply.github.com> Date: Thu, 12 Mar 2026 22:48:02 +0100 Subject: [PATCH] Added image autopull. --- .gitignore | 1 + Dockerfile | 5 ++++- config.json | 14 -------------- config/config.go | 7 +++++++ docker/docker.go | 5 ++++- docker/prepare.go | 27 ++++++++++++++++++++++++++- host/host.go | 30 ++++++++++++++++++++++++++---- main.go | 2 +- 8 files changed, 69 insertions(+), 22 deletions(-) delete mode 100644 config.json diff --git a/.gitignore b/.gitignore index 96906dd..21124d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ ssh-key ssh-key.pub ssk-key.tar +config.json diff --git a/Dockerfile b/Dockerfile index 101ec8a..02fb324 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,9 +12,12 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o /blazena FROM docker.io/library/alpine:3.3 +RUN apk add openssh rsync --no-cache COPY --from=builder /blazena / EXPOSE 1234 ENV MODE=invalid -CMD /blazena $MODE +WORKDIR /root/.ssh + +CMD ["/blazena", "$MODE"] diff --git a/config.json b/config.json deleted file mode 100644 index 9be098c..0000000 --- a/config.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "Nodes": { - "rpi":{ - "Ip": "192.168.1.175", - "DockerVolumePath": "/var/lib/docker/volumes" - }, - "arnost":{ - "Ip": "192.168.1.9", - "DockerVolumePath": "/home/blazena/volumes" - } - }, - "DockerManagerBaseUrl": "http://localhost:1234", - "LocalBasePath": "/archive/@backups" -} diff --git a/config/config.go b/config/config.go index de87a52..7ac9026 100644 --- a/config/config.go +++ b/config/config.go @@ -7,4 +7,11 @@ type Config struct { } DockerManagerBaseUrl string LocalBasePath string + BlazenaImageUrl string + RegistryAuth RegistryAuth }; + +type RegistryAuth struct { + Username string + Password string +} diff --git a/docker/docker.go b/docker/docker.go index 56b2dad..fca53f1 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -16,12 +16,14 @@ import ( "github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" + cfg "github.com/rony5394/blazena/config" ) // Add mutex. var ApiClient *client.Client; var scale sync.Map; var token string = "12345"; +var theConfig cfg.Config; type aService struct{ ServiceId string `json:"serviceId"`; @@ -29,7 +31,8 @@ type aService struct{ Node string `json:"node"`; } -func Run(){ +func Run(Config cfg.Config){ + theConfig = Config; ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM); var err error; diff --git a/docker/prepare.go b/docker/prepare.go index 0754613..2c4e88b 100644 --- a/docker/prepare.go +++ b/docker/prepare.go @@ -6,10 +6,14 @@ import ( "fmt" "io" "net/http" + "encoding/base64" "time" + "os" "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" ) func prepare(w http.ResponseWriter, r *http.Request){ @@ -41,6 +45,27 @@ func prepare(w http.ResponseWriter, r *http.Request){ panic("Failed to inspect service."+ err.Error()); } + authConfig := registry.AuthConfig{ + Username: theConfig.RegistryAuth.Username, + Password: theConfig.RegistryAuth.Password, + } + + authJSON, err := json.Marshal(authConfig) + if err != nil { + panic("Failed to marshal auth config!"+ err.Error()); + } + + authString := base64.URLEncoding.EncodeToString(authJSON); + + ipc, err := ApiClient.ImagePull(context.Background(), theConfig.BlazenaImageUrl, image.PullOptions{RegistryAuth: authString}); + if err != nil { + panic("Failed to pull blazena image!"+ err.Error()); + } + defer ipc.Close(); + + io.Copy(os.Stdout, ipc); + + labels := inspectResoults.Spec.Labels; maxConcurrent := uint64(1); @@ -65,7 +90,7 @@ func prepare(w http.ResponseWriter, r *http.Request){ }, TaskTemplate: swarm.TaskSpec{ ContainerSpec: &swarm.ContainerSpec{ - Image: "registry.lan.ronycloud.dev/blazena/helper:latest", + Image: theConfig.BlazenaImageUrl, Command: []string{"sh", "-c", helperCommand}, Mounts: []mount.Mount{ mount.Mount{ diff --git a/host/host.go b/host/host.go index de9ab5e..9c3303f 100644 --- a/host/host.go +++ b/host/host.go @@ -7,13 +7,16 @@ import ( "fmt" "io" "net/http" + "encoding/base64" "os" "time" "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/strslice" + "github.com/docker/docker/api/types/registry" "github.com/docker/docker/client" v1 "github.com/opencontainers/image-spec/specs-go/v1" cfg "github.com/rony5394/blazena/config" @@ -52,8 +55,7 @@ func Run(Config cfg.Config) { fmt.Println("Done!"); // Skiping Host Key Check is temporary. - command := `apk add --no-cache rsync openssh-client && \ - rsync -avz --delete -e "ssh -i /ssh-key -p 2222 -o StrictHostKeyChecking=no" \ + command := `rsync -avz --delete -e "ssh -i /ssh-key -p 2222 -o StrictHostKeyChecking=no" \ root@tasks.BlazenaHelper:/volume/ /tmp/` + volume; @@ -232,12 +234,32 @@ func scale(Config cfg.Config, serviceId string, up bool)bool{ } func createStorageContainer(Config cfg.Config, DockerClient *client.Client){ + authConfig := registry.AuthConfig{ + Username: Config.RegistryAuth.Username, + Password: Config.RegistryAuth.Password, + } + + authJSON, err := json.Marshal(authConfig) + if err != nil { + panic("Failed to marshal auth config!"+ err.Error()); + } + + authString := base64.URLEncoding.EncodeToString(authJSON); + + ipc, err := DockerClient.ImagePull(context.Background(), Config.BlazenaImageUrl, image.PullOptions{RegistryAuth: authString}); + if err != nil { + panic("Failed to pull blazena image!"+ err.Error()); + } + defer ipc.Close(); + + io.Copy(os.Stdout, ipc); + cr, err := DockerClient.ContainerCreate(context.Background(), &container.Config{ - Image: "docker.io/library/alpine:3.3", + Image: Config.BlazenaImageUrl, Labels: map[string]string{ "blazena.storage": "true", }, - Cmd: strslice.StrSlice{"sleep", "1h"}, + Cmd: strslice.StrSlice{"sleep", "3h"}, }, &container.HostConfig{ Mounts: []mount.Mount{ mount.Mount{ diff --git a/main.go b/main.go index 012e3c1..cf4e8a6 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ func main() { mode := os.Args[1]; switch mode { case "docker": - docker.Run(); + docker.Run(Config); break; case "host": host.Run(Config);