Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Parse compose file as plain YAML
Browse files Browse the repository at this point in the history
If we don't, parsing into cli/compose/types can be broken by
parameters that we don't want replaced by env

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Oct 2, 2019
1 parent ed1a8c0 commit 6f3e3fe
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 48 deletions.
2 changes: 1 addition & 1 deletion e2e/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestImageList(t *testing.T) {

insertBundles(t, cmd, dir, info)

expected := `APP IMAGE APP NAME
expected := `APP IMAGE APP NAME
%s push-pull
a-simple-app:latest simple
b-simple-app:latest simple
Expand Down
97 changes: 58 additions & 39 deletions internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,28 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"

"github.com/deislabs/cnab-go/bundle"
cnab "github.com/deislabs/cnab-go/driver"
"github.com/docker/app/internal/packager"
"github.com/docker/app/types"
"github.com/docker/buildx/build"
"github.com/docker/buildx/driver"
_ "github.com/docker/buildx/driver/docker" // required to get default driver registered, see driver/docker/factory.go:14
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/compose/loader"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/util/appcontext"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path"
"path/filepath"

cnab "github.com/deislabs/cnab-go/driver"
"github.com/docker/buildx/build"
_ "github.com/docker/buildx/driver/docker" // required to get default driver registered, see driver/docker/factory.go:14
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
composetypes "github.com/docker/cli/cli/compose/types"
"github.com/moby/buildkit/util/appcontext"
"github.com/spf13/cobra"
)

Expand All @@ -37,7 +35,7 @@ type buildOptions struct {
progress string
pull bool
tag string
out string
out string
}

func buildCmd(dockerCli command.Cli) *cobra.Command {
Expand Down Expand Up @@ -83,7 +81,7 @@ func runBuild(dockerCli command.Cli, application string, opt buildOptions) (refe
return nil, err
}

_, err = createInvocationImageBuildOptions(dockerCli, app)
buildopts["invocation-image"], err = createInvocationImageBuildOptions(dockerCli, app)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -220,57 +218,79 @@ func debugSolveResponses(resp map[string]*client.SolveResponse) {
}
}

// parseCompose do parse app compose file and extract buildx targets
// parseCompose do parse app compose file and extract buildx Options
// We don't rely on bake's ReadTargets + TargetsToBuildOpt here as we have to skip environment variable interpolation
func parseCompose(app *types.App, options buildOptions) (map[string]build.Options, error) {

// Fixme can have > 1 composes ?
parsed, err := loader.ParseYAML(app.Composes()[0])
if err != nil {
return nil, err
}
compose, err := loader.Load(composetypes.ConfigDetails{
ConfigFiles: []composetypes.ConfigFile{
{
Config: parsed,
},
},
})
if err != nil {
return nil, err

services, ok := parsed["services"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Invalid compose file: 'services' should be a map")
}

opts := map[string]build.Options{}
for _, service := range compose.Services {
b := service.Build
if b.Context == "" {
continue
for name, cfg := range services {
config, ok := cfg.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Invalid compose file: service %s isn't a map", name)
}

if b.Context, err = filepath.Abs(b.Context); err != nil {
return nil, err
bc, ok := config["build"]
if !ok {
continue
}
var buildContext string
dockerfilePath := "Dockerfile"
if b.Dockerfile != "" {
dockerfilePath = b.Dockerfile
buildargs := map[string]string{}
switch bc.(type) {
case string:
buildContext = bc.(string)
case map[string]interface{}:
buildconfig := bc.(map[string]interface{})
buildContext = buildconfig["context"].(string)
if dockerfile, ok := buildconfig["dockerfile"]; ok {
dockerfilePath = dockerfile.(string)
}
if a, ok := buildconfig["args"]; ok {
switch a.(type) {
case map[string]interface{}:
for k, v := range a.(map[string]interface{}) {
buildargs[k] = v.(string)
}
// FIXME also support the list-style syntax
default:
return nil, fmt.Errorf("Invalid compose file: service %s build args is invalid", name)
}
}
default:
return nil, fmt.Errorf("Invalid compose file: service %s build is invalid", name)
}
dockerfilePath = path.Join(b.Context, dockerfilePath)
opts[service.Name] = build.Options{

// FIXME the compose file we build from x.dockerapp refers to docker context in parent folder.
// Maybe docker app init should update such relative paths accordingly ?
buildContext = path.Join(app.Path, "..", buildContext)
dockerfilePath = path.Join(buildContext, dockerfilePath)
opts[name] = build.Options{
Inputs: build.Inputs{
ContextPath: b.Context,
ContextPath: buildContext,
DockerfilePath: dockerfilePath,
},
// BuildArgs: t.Args, //FIXME introduce build args support in docker app build
BuildArgs: buildargs,
NoCache: options.noCache,
Pull: options.pull,
}
}
return opts, nil
}


type sha struct {
d digest.Digest
}

var _ reference.Named = sha{""}
var _ reference.Digested = sha{""}

Expand All @@ -288,4 +308,3 @@ func (s sha) String() string {
func (s sha) Name() string {
return s.d.String()
}

3 changes: 2 additions & 1 deletion internal/commands/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"context"
"fmt"
"github.com/docker/app/internal/packager"
"io"
"io/ioutil"
"os"
"strings"

"github.com/docker/app/internal/packager"

"github.com/containerd/containerd/platforms"
"github.com/deislabs/cnab-go/bundle"
"github.com/docker/app/internal/log"
Expand Down
15 changes: 8 additions & 7 deletions internal/packager/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"

"github.com/deislabs/cnab-go/bundle"
"github.com/docker/app/internal/store"
"github.com/docker/app/types"
Expand All @@ -15,7 +17,6 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"io/ioutil"
)

func MakeBundleFromApp(dockerCli command.Cli, app *types.App, refOverride reference.NamedTagged) (*bundle.Bundle, error) {
Expand All @@ -28,15 +29,15 @@ func MakeBundleFromApp(dockerCli command.Cli, app *types.App, refOverride refere

buildContext := bytes.NewBuffer(nil)
if err := PackInvocationImageContext(dockerCli, app, buildContext); err != nil {
return nil, err
}
return nil, err
}

logrus.Debugf("Building invocation image %s", invocationImageName)
buildResp, err := dockerCli.Client().ImageBuild(context.TODO(), buildContext, dockertypes.ImageBuildOptions{
Dockerfile: "Dockerfile",
Tags: []string{invocationImageName},
BuildArgs: map[string]*string{},
})
Dockerfile: "Dockerfile",
Tags: []string{invocationImageName},
BuildArgs: map[string]*string{},
})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 6f3e3fe

Please sign in to comment.