Skip to content

Commit

Permalink
Allow building (name).Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinChartier committed Jul 18, 2019
1 parent 07c3aad commit 128268d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
44 changes: 23 additions & 21 deletions build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"time"
)

Expand All @@ -25,12 +24,17 @@ type Builder struct {
DoPush bool
}

func (builder *Builder) buildkitSolveOpts(serviceDir, fullImageName string, writer io.WriteCloser) *client.SolveOpt {
func (builder *Builder) buildkitSolveOpts(
serviceDir, dockerfileName, fullImageName string,
writer io.WriteCloser) *client.SolveOpt {
solveOpt := &client.SolveOpt{
LocalDirs: map[string]string{
"context": serviceDir,
"dockerfile": serviceDir,
},
FrontendAttrs: map[string]string{
"filename": dockerfileName,
},
Session: []session.Attachable{authprovider.NewDockerAuthProvider()},
}

Expand Down Expand Up @@ -65,42 +69,41 @@ func (builder *Builder) buildkitSolveOpts(serviceDir, fullImageName string, writ
}

//BuildService builds a specific sevice directory with a specific context
func (builder *Builder) BuildService(ctx context.Context, serviceDir string) error {
serviceName := filepath.Base(serviceDir)
fullImageName := fmt.Sprintf("%s:%s", serviceName, builder.BuildTag)
func (builder *Builder) BuildService(ctx context.Context, service util.BuildableService) error {
fullImageName := fmt.Sprintf("%s:%s", service.Name, builder.BuildTag)
if builder.Registry != "" {
fullImageName = fmt.Sprintf("%s/%s:%s", builder.Registry, serviceName, builder.BuildTag)
fullImageName = fmt.Sprintf("%s/%s:%s", builder.Registry, service.Name, builder.BuildTag)
}
builder.Interface.StartJob(serviceName, fullImageName)
builder.Interface.StartJob(service.Name, fullImageName)
statusChannel := make(chan *client.SolveStatus)

var resultR *io.PipeReader
var resultW *io.PipeWriter
if !builder.DoPush {
resultR, resultW = io.Pipe()
}
buildOpts := builder.buildkitSolveOpts(serviceDir, fullImageName, resultW)
buildOpts := builder.buildkitSolveOpts(service.Dir, service.Dockerfile, fullImageName, resultW)

err := util.RunContextuallyInParallel(
ctx,
func(ctx context.Context) error {
buildkitClient, err := client.New(ctx, BuildkitDaemonAddr, client.WithFailFast())
if err != nil {
builder.Interface.FailJob(serviceName, err)
builder.Logger.Log(serviceName, time.Now(), "Could not connect to build daemon! ", err.Error())
builder.Interface.FailJob(service.Name, err)
builder.Logger.Log(service.Name, time.Now(), "Could not connect to build daemon! ", err.Error())
return err
}
builder.Logger.Log(serviceName, time.Now(), "Starting build of ", serviceDir)
builder.Logger.Log(service.Name, time.Now(), "Starting build of ", service.Dir)
solveStatus, err := buildkitClient.Build(ctx, *buildOpts, "", dockerfile.Build, statusChannel)
if solveStatus != nil {
//TODO if this is null should print a warning that we failed to push
//e.g., when we haven't deployed yet
for k, v := range solveStatus.ExporterResponse {
builder.Logger.Log(serviceName, time.Now(), fmt.Sprintf("exporter: %s=%s", k, v))
builder.Logger.Log(service.Name, time.Now(), fmt.Sprintf("exporter: %s=%s", k, v))
}
}
if err != nil {
builder.Logger.Log(serviceName, time.Now(), "FAILED: ", err.Error())
builder.Logger.Log(service.Name, time.Now(), "FAILED: ", err.Error())
}
return err
},
Expand All @@ -113,8 +116,7 @@ func (builder *Builder) BuildService(ctx context.Context, serviceDir string) err
return err
}
err := util.WaitCmdContextually(ctx, cmd)
resultR.CloseWithError(err)
return err
return resultR.CloseWithError(err)
}
return nil
},
Expand All @@ -129,10 +131,10 @@ func (builder *Builder) BuildService(ctx context.Context, serviceDir string) err
}
for _, status := range status.Statuses {
if status.ID == "pushing layers" {
builder.Interface.SetPushing(serviceName)
builder.Interface.SetPushing(service.Name)
}
}
logErr := builder.Logger.ProcessStatus(serviceName, status)
logErr := builder.Logger.ProcessStatus(service.Name, status)
if logErr != nil {
fmt.Fprintln(os.Stderr, logErr.Error())
}
Expand All @@ -142,11 +144,11 @@ func (builder *Builder) BuildService(ctx context.Context, serviceDir string) err
)

if err == nil {
builder.Interface.SucceedJob(serviceName)
builder.Logger.Log(serviceName, time.Now(), "Build succeeded!")
builder.Interface.SucceedJob(service.Name)
builder.Logger.Log(service.Name, time.Now(), "Build succeeded!")
} else if err != context.Canceled {
builder.Interface.FailJob(serviceName, err)
builder.Logger.Log(serviceName, time.Now(), "Build failed! ", err.Error())
builder.Interface.FailJob(service.Name, err)
builder.Logger.Log(service.Name, time.Now(), "Build failed! ", err.Error())
}
return err
}
12 changes: 8 additions & 4 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ func buildCommandAction(cliContext *cli.Context) error {

buildTag := cliContext.String("tag")
if buildTag == "" {
buildTag, err = git.GetCurrentTreeHash(buildRoot, services...)
var serviceDirs []string
for _, service := range services {
serviceDirs = append(serviceDirs, service.Dir)
}
buildTag, err = git.GetCurrentTreeHash(buildRoot, serviceDirs...)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
Expand Down Expand Up @@ -112,12 +116,12 @@ func buildCommandAction(cliContext *cli.Context) error {
DoPush: cliContext.Bool("push"),
}

for _, serviceDir := range services {
finalServiceDir := serviceDir
for _, service := range services {
finalService := service
jobs = append(jobs, func(ctx context.Context) error {
return builder.BuildService(
ctx,
finalServiceDir,
finalService,
)
})
}
Expand Down
6 changes: 5 additions & 1 deletion commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ func runTemplater(folderIn, folderOut, templaterImage, namespace string) error {
if err != nil {
return err
}
buildTag, err := git.GetCurrentTreeHash(shl.GetSanicRoot(), services...)
var serviceDirectories []string
for _, service := range services {
serviceDirectories = append(serviceDirectories, service.Dir)
}
buildTag, err := git.GetCurrentTreeHash(shl.GetSanicRoot(), serviceDirectories...)
if err != nil {
return err
}
Expand Down
24 changes: 21 additions & 3 deletions util/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ package util
import (
"os"
"path/filepath"
"strings"
)

type BuildableService struct {
Dir string
Dockerfile string
Name string
}

//FindServices finds all of the buildable services in the given directory
//(e.g., folders which contain a Dockerfile)
func FindServices(dir string, ignorePaths []string) ([]string, error) {
func FindServices(dir string, ignorePaths []string) ([]BuildableService, error) {
dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}

var ret []string
var ret []BuildableService

err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
for _, ignorePath := range ignorePaths {
Expand All @@ -23,7 +30,18 @@ func FindServices(dir string, ignorePaths []string) ([]string, error) {
}

if info.Name() == "Dockerfile" {
ret = append(ret, filepath.Dir(path))
ret = append(ret, BuildableService{
Dir: filepath.Dir(path),
Dockerfile: "Dockerfile",
Name: filepath.Base(filepath.Dir(path)),
})
} else if strings.HasSuffix(info.Name(), ".Dockerfile") {
name := filepath.Base(filepath.Dir(path))+"-"+strings.TrimSuffix(info.Name(),".Dockerfile")
ret = append(ret, BuildableService{
Dir: filepath.Dir(path),
Dockerfile: info.Name(),
Name: name,
})
}
return nil
})
Expand Down

0 comments on commit 128268d

Please sign in to comment.