WIP: with docker action

This commit is contained in:
Kasper Juul Hermansen 2022-09-18 00:53:41 +02:00
parent 8c6eea468e
commit c2d1a7b89e
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
4 changed files with 59 additions and 9 deletions

View File

@ -3,6 +3,4 @@ FROM debian:bullseye-slim
# Kraken relies on this path being the specified path
WORKDIR /src/work/
COPY . .
RUN echo "# README docker" >> README.md
RUN echo "# README docker" > README.md

View File

@ -8,4 +8,4 @@ select:
# organisation: "cibus"
actions:
- type: docker-build
entry: "Dockerfile"
entry: Dockerfile

View File

@ -31,6 +31,7 @@ func (a *Action) Execute(ctx context.Context, area *storage.Area) error {
zap.L().Debug("Execution done")
case "docker-build":
zap.L().Debug("Building docker-build")
runCmd, err := builders.NewDockerBuild(zap.L()).Build(ctx, a.SchemaPath, action.Entry)
if err != nil {
return err

View File

@ -2,20 +2,23 @@ package builders
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"os"
"os/exec"
"go.uber.org/zap"
"go.uber.org/zap/zapio"
)
type DockerBuild struct {
logger *zap.Logger
}
func NewDockerBuild(logger *zap.Logger) *Go {
return &Go{logger: logger}
func NewDockerBuild(logger *zap.Logger) *DockerBuild {
return &DockerBuild{logger: logger}
}
type DockerRunCommand func(ctx context.Context, victimPath string) error
@ -27,7 +30,41 @@ func (g *DockerBuild) Build(ctx context.Context, modulePath, entryPath string) (
return nil, errors.New("could not find entry")
}
g.logger.Debug("Go binary built!")
b := make([]byte, 20)
_, err := rand.Reader.Read(b)
if err != nil {
return nil, err
}
tag := hex.EncodeToString(b)
buildDockerCmd := fmt.Sprintf("(cd %s; docker build -f %s --tag kraken/%s .)", modulePath, entryPath, tag)
g.logger.Debug("Running command", zap.String("command", buildDockerCmd))
cmd := exec.CommandContext(
ctx,
"/bin/bash",
"-c",
buildDockerCmd,
)
debugwriter := &zapio.Writer{
Log: g.logger,
Level: zap.DebugLevel,
}
defer debugwriter.Close()
cmd.Stdout = debugwriter
cmd.Stderr = debugwriter
err = cmd.Start()
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, err
}
g.logger.Debug("Docker image built!")
return func(ctx context.Context, victimPath string) error {
g.logger.Debug("Executing script", zap.String("victim", victimPath))
@ -36,9 +73,23 @@ func (g *DockerBuild) Build(ctx context.Context, modulePath, entryPath string) (
ctx,
"/bin/bash",
"-c",
fmt.Sprintf("(cd %s; docker build)", modulePath),
fmt.Sprintf("docker run --rm -v %s/:/src/work/ kraken/%s", victimPath, tag),
)
return cmd.Run()
runDockerWriter := &zapio.Writer{
Log: g.logger,
Level: zap.DebugLevel,
}
defer runDockerWriter.Close()
cmd.Stdout = runDockerWriter
cmd.Stderr = runDockerWriter
err = cmd.Start()
if err != nil {
return err
}
return cmd.Wait()
}, nil
}