-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2 inital builder. Directory of BuildContext and Project are statical…
…ly at the moment
- Loading branch information
1 parent
100109a
commit a01c795
Showing
15 changed files
with
355 additions
and
4 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
.idea/runConfigurations/go_build_github_com_harbourrocks_harbour_cmd_harbour_build.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/harbourrocks/harbour/pkg/harbourbuild" | ||
"github.com/harbourrocks/harbour/pkg/harbourbuild/configuration" | ||
"github.com/harbourrocks/harbour/pkg/logconfig" | ||
"github.com/harbourrocks/harbour/pkg/redisconfig" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func NewBuildServerCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "harbour-builder", | ||
Long: "The habour.rocks build server manages the building of projects for the registry", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
// load builder config | ||
s := configuration.ParseViperConfig() | ||
|
||
// configure logging | ||
l := logconfig.ParseViperConfig() | ||
logconfig.ConfigureLog(l) | ||
|
||
logrus.Info("Harbour Builder configured") | ||
|
||
// test redis connection | ||
redisconfig.TestConnection(s.Redis) | ||
|
||
return harbourbuild.RunBuildServer(s) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initCobra) | ||
} | ||
|
||
func initCobra() { | ||
viper.AutomaticEnv() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/harbourrocks/harbour/cmd/harbour-build/app" | ||
"os" | ||
) | ||
|
||
func main() { | ||
cmd := app.NewBuildServerCommand() | ||
|
||
if err := cmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package harbourbuild | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
"github.com/harbourrocks/harbour/pkg/harbourbuild/models" | ||
"github.com/jhoonb/archivex" | ||
l "github.com/sirupsen/logrus" | ||
"os" | ||
) | ||
|
||
type Builder struct { | ||
jobChan chan models.BuildJob | ||
cli *client.Client | ||
ctx context.Context | ||
} | ||
|
||
func NewBuilder(jobChan chan models.BuildJob) (Builder, error) { | ||
var builder Builder | ||
ctx := context.Background() | ||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) | ||
if err != nil { | ||
return builder, err | ||
} | ||
|
||
builder = Builder{jobChan: jobChan, cli: cli, ctx: ctx} | ||
return builder, nil | ||
} | ||
|
||
func (b Builder) Start() { | ||
go func() { | ||
for { | ||
select { | ||
case job := <-b.jobChan: | ||
b.buildImage(job) | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (b Builder) buildImage(job models.BuildJob) { | ||
l.Trace("Create Build Context") | ||
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) | ||
if err != nil { | ||
l.WithError(err).Error("Failed to build image") | ||
return | ||
} | ||
} | ||
|
||
func (b Builder) cleanUpAfterBuild(buildContext *os.File) { | ||
l.Trace("Cleaning up") | ||
err := buildContext.Close() | ||
err = os.Remove(buildContext.Name()) | ||
if err != nil { | ||
l.WithError(err).Error("Error while cleaning up build context") | ||
return | ||
} | ||
} | ||
|
||
//TODO Communicate with Harbour SCM in order to receive the path to the project-files | ||
func (b Builder) getProjectPath(project string) (string, error) { | ||
// Just returns a demo path | ||
return fmt.Sprintf("./test/repos/%s", project), nil | ||
} | ||
|
||
func (b Builder) createBuildContext(project string) (*os.File, error) { | ||
|
||
buildContext := fmt.Sprintf("./test/buildcontext/%s.tar", project) | ||
projectPath, err := b.getProjectPath(project) | ||
if err != nil { | ||
l.WithError(err).Error("Failed to receive the project files") | ||
return nil, err | ||
} | ||
|
||
tar := new(archivex.TarFile) | ||
err = tar.Create(buildContext) | ||
err = tar.AddAll(projectPath, false) | ||
err = tar.Close() | ||
|
||
dockerBuildCtx, err := os.Open(buildContext) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return dockerBuildCtx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package configuration | ||
|
||
import ( | ||
"github.com/harbourrocks/harbour/pkg/auth" | ||
"github.com/harbourrocks/harbour/pkg/redisconfig" | ||
) | ||
|
||
type Options struct { | ||
Redis redisconfig.RedisOptions | ||
OIDCConfig auth.OIDCConfig | ||
} | ||
|
||
func NewDefaultOptions() *Options { | ||
s := Options{ | ||
Redis: redisconfig.NewDefaultRedisOptions(), | ||
OIDCConfig: auth.DefaultConfig(), | ||
} | ||
|
||
return &s | ||
} | ||
|
||
func ParseViperConfig() *Options { | ||
s := NewDefaultOptions() | ||
|
||
s.OIDCConfig = auth.ParseViperConfig() | ||
s.Redis = redisconfig.ParseViperConfig() | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/harbourrocks/harbour/pkg/harbourbuild/models" | ||
"github.com/harbourrocks/harbour/pkg/httphandler/traits" | ||
"github.com/harbourrocks/harbour/pkg/redisconfig" | ||
l "github.com/sirupsen/logrus" | ||
"net/http" | ||
) | ||
|
||
type BuilderModel struct { | ||
traits.HttpModel | ||
traits.IdTokenModel | ||
redisconfig.RedisModel | ||
buildChan chan models.BuildJob | ||
} | ||
|
||
func NewBuilderModel(buildChan chan models.BuildJob) BuilderModel { | ||
return BuilderModel{buildChan: buildChan} | ||
} | ||
|
||
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) | ||
if err != nil { | ||
l.WithError(err).Error("Failed to parse build request") | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
b.buildChan <- models.BuildJob{Request: buildRequest} | ||
|
||
l.Trace("Build job enqueued") | ||
|
||
w.WriteHeader(http.StatusAccepted) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package models | ||
|
||
type BuildRequest struct { | ||
Dockerfile string `json:"dockerfile"` | ||
Tags []string `json:"tags"` | ||
Project string `json:"project"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package redis | ||
|
||
import "fmt" | ||
|
||
func BuilderAppKey(buildId int) string { | ||
return fmt.Sprintf("BUILD_%d", buildId) | ||
} |
Oops, something went wrong.