kjuulh
46cc160764
updated roadmap updated roadmap Add a basic readme (#15) Co-authored-by: kjuulh <contact@kjuulh.io> Reviewed-on: kjuulh/kraken#15 fix readme Add readme with stuff more roadmap items update formatting formatting 2 more setup with semantic with semantic 2 revert add releaserc with releaser with kraken Update roadmap rename rename feature/move-command (#18) Co-authored-by: kjuulh <contact@kjuulh.io> Reviewed-on: #18 fix/require-two-pushes (#20) Co-authored-by: kjuulh <contact@kjuulh.io> Reviewed-on: #20
96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
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) *DockerBuild {
|
|
return &DockerBuild{logger: logger}
|
|
}
|
|
|
|
type DockerRunCommand func(ctx context.Context, victimPath string) error
|
|
|
|
func (g *DockerBuild) Build(ctx context.Context, modulePath, entryPath string) (DockerRunCommand, error) {
|
|
g.logger.Debug("Building docker image", zap.String("actiondir", modulePath), zap.String("entry", entryPath))
|
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s/%s", modulePath, entryPath)); os.IsNotExist(err) {
|
|
return nil, errors.New("could not find entry")
|
|
}
|
|
|
|
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 octopush/%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))
|
|
|
|
cmd := exec.CommandContext(
|
|
ctx,
|
|
"/bin/bash",
|
|
"-c",
|
|
fmt.Sprintf("docker run --rm -v %s/:/src/work/ octopush/%s", victimPath, tag),
|
|
)
|
|
|
|
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
|
|
}
|