Added image autopull.

This commit is contained in:
rony5394
2026-03-12 22:48:02 +01:00
parent 2dda7dbb38
commit 064a944655
8 changed files with 69 additions and 22 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
ssh-key
ssh-key.pub
ssk-key.tar
config.json

View File

@@ -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"]

View File

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

View File

@@ -7,4 +7,11 @@ type Config struct {
}
DockerManagerBaseUrl string
LocalBasePath string
BlazenaImageUrl string
RegistryAuth RegistryAuth
};
type RegistryAuth struct {
Username string
Password string
}

View File

@@ -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;

View File

@@ -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{

View File

@@ -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{

View File

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