Skip to content

Commit

Permalink
#2 saving build to redis. Updating build during execution of build
Browse files Browse the repository at this point in the history
  • Loading branch information
BrobotDubstep committed May 11, 2020
1 parent 91fe2b1 commit 6d5ffd4
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 21 deletions.
39 changes: 30 additions & 9 deletions pkg/harbourbuild/builder.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package harbourbuild

import (
"bytes"
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/harbourrocks/harbour/pkg/harbourbuild/models"
"github.com/harbourrocks/harbour/pkg/redisconfig"
"github.com/jhoonb/archivex"
l "github.com/sirupsen/logrus"
"io"
"os"
"strings"
)

type Builder struct {
Expand All @@ -17,6 +21,7 @@ type Builder struct {
ctx context.Context
ctxPath string
repoPath string
redisconfig.RedisModel
}

func NewBuilder(jobChan chan models.BuildJob, ctxPath string, repoPath string) (Builder, error) {
Expand All @@ -43,33 +48,50 @@ func (b Builder) Start() {
}

func (b Builder) buildImage(job models.BuildJob) {
l.Trace("Create Build Context")

redisConfig := b.GetRedisConfig()
redisClient := redisconfig.OpenClient(redisConfig)
redisClient.HSet(job.BuildKey, "build_status", "Running")

buildCtx, err := b.createBuildContext(job.Request.Project)
if err != nil {
l.WithError(err).Error("Failed to create build context")
return
}

defer b.cleanUpAfterBuild(buildCtx)

l.Trace("Create Build Options")
opt := types.ImageBuildOptions{
Tags: job.Request.Tags,
Dockerfile: job.Request.Dockerfile,
}

l.Trace("Build Image")
_, err = b.cli.ImageBuild(b.ctx, buildCtx, opt)
resp, err := b.cli.ImageBuild(b.ctx, buildCtx, opt)
if err != nil {
l.WithError(err).Error("Failed to build image")
return
}

defer b.cleanUpAfterBuild(buildCtx, resp.Body)

buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
l.WithError(err).Error("Failed to parse response body")
}

logs := buf.String()
if strings.Contains(logs, "errorDetail") {
redisClient.HSet(job.BuildKey, "build_status", "Failed", "logs", logs)
return
}

redisClient.HSet(job.BuildKey, "build_status", "Success", "logs", logs)
l.Trace("Image was built")
}

func (b Builder) cleanUpAfterBuild(buildContext *os.File) {
l.Trace("Cleaning up")
func (b Builder) cleanUpAfterBuild(buildContext *os.File, logs io.ReadCloser) {
err := buildContext.Close()
err = os.Remove(buildContext.Name())
err = logs.Close()
if err != nil {
l.WithError(err).Error("Error while cleaning up build context")
return
Expand All @@ -83,7 +105,6 @@ func (b Builder) getProjectPath(project string) (string, error) {
}

func (b Builder) createBuildContext(project string) (*os.File, error) {

buildContext := fmt.Sprintf(b.ctxPath+"%s.tar", project)
projectPath, err := b.getProjectPath(project)
if err != nil {
Expand Down
31 changes: 28 additions & 3 deletions pkg/harbourbuild/handler/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package handler

import (
"encoding/json"
"github.com/google/uuid"
"github.com/harbourrocks/harbour/pkg/harbourbuild/models"
"github.com/harbourrocks/harbour/pkg/harbourbuild/redis"
"github.com/harbourrocks/harbour/pkg/httphandler/traits"
"github.com/harbourrocks/harbour/pkg/redisconfig"
l "github.com/sirupsen/logrus"
Expand All @@ -23,7 +25,6 @@ func NewBuilderModel(buildChan chan models.BuildJob) BuilderModel {
func (b BuilderModel) Handle() {
w := b.GetResponse()
req := b.GetRequest()
//redisConfig := b.GetRedisConfig()
var buildRequest models.BuildRequest
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&buildRequest)
Expand All @@ -33,9 +34,33 @@ func (b BuilderModel) Handle() {
return
}

b.buildChan <- models.BuildJob{Request: buildRequest}
buildKey, err := b.createBuildEntry(buildRequest)
if err != nil {
l.WithError(err).Error("Failed to save build to redis")
return
}

l.Trace("Build job enqueued")
b.buildChan <- models.BuildJob{Request: buildRequest, BuildKey: buildKey}

l.Trace("Build job enqueued")
w.WriteHeader(http.StatusAccepted)
}

func (b BuilderModel) createBuildEntry(request models.BuildRequest) (string, error) {
redisConfig := b.GetRedisConfig()
buildId := uuid.New()
buildKey := redis.BuildAppKey(buildId.String())

client := redisconfig.OpenClient(redisConfig)
err := client.HSet(buildKey,
"project", request.Project,
"commit", request.Commit,
"logs", nil,
"repository", request.Project,
"build_status", "Pending").Err()
if err != nil {
return buildKey, err
}

return buildKey, nil
}
10 changes: 10 additions & 0 deletions pkg/harbourbuild/models/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type Build struct {
BuildId string
Project string
Commit string
Logs string
Repository string
BuildStatus string
}
3 changes: 2 additions & 1 deletion pkg/harbourbuild/models/buildjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package models
// BuildJob represents a job send to the Builder with a channel.
// Contains all information required for build a image.
type BuildJob struct {
Request BuildRequest
Request BuildRequest
BuildKey string
}
1 change: 1 addition & 0 deletions pkg/harbourbuild/models/buildrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type BuildRequest struct {
Dockerfile string `json:"dockerfile"`
Tags []string `json:"tags"`
Project string `json:"project"`
Commit string `json:"commit"`
}
7 changes: 7 additions & 0 deletions pkg/harbourbuild/redis/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package redis

import "fmt"

func BuildAppKey(buildId string) string {
return fmt.Sprintf("BUILD_%s", buildId)
}
7 changes: 0 additions & 7 deletions pkg/harbourbuild/redis/builder.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/harbourbuild/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ func RunBuildServer(o *configuration.Options) error {
logrus.Fatal(err)
return err
}
redisconfig.AddRedis(&builder, o.Redis)
builder.Start()
logrus.Info("Started Harbour builder ")

http.HandleFunc("/build/test", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc("/build", func(w http.ResponseWriter, r *http.Request) {
logrus.Trace(r)
model := handler.NewBuilderModel(buildChan)
traits.AddHttp(&model, r, w, o.OIDCConfig)
Expand Down

0 comments on commit 6d5ffd4

Please sign in to comment.