Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add builder subcommand #965

Merged
merged 5 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ func testWithoutSpecificBuilderRequirement(
})
})

when("suggest-builders", func() {
when("builder suggest", func() {
it("displays suggested builders", func() {
output := pack.RunSuccessfully("suggest-builders")
output := pack.RunSuccessfully("builder", "suggest")

assertOutput := assertions.NewOutputAssertionManager(t, output)
assertOutput.IncludesSuggestedBuildersHeading()
Expand Down Expand Up @@ -751,10 +751,22 @@ func testAcceptance(

builderConfigPath := createBuilderPack.FixtureManager().FixtureLocation("invalid_builder.toml")

output, err := pack.Run(
"create-builder", "some-builder:build",
"--config", builderConfigPath,
var (
output string
err error
)
if createBuilderPack.Supports("builder create") {
output, err = createBuilderPack.Run(
"builder", "create", "some-builder:build",
"--config", builderConfigPath,
)
} else {
output, err = createBuilderPack.Run(
"create-builder", "some-builder:build",
"--config", builderConfigPath,
)
}

assert.NotNil(err)
assert.Contains(output, "invalid builder toml")
})
Expand Down Expand Up @@ -2416,11 +2428,20 @@ func createComplexBuilder(t *testing.T,
bldr := registryConfig.RepoName("test/builder-" + h.RandString(10))

// CREATE BUILDER
output := pack.RunSuccessfully(
"create-builder", bldr,
"-c", builderConfigFile.Name(),
"--no-color",
)
var output string
if pack.Supports("builder create") {
output = pack.RunSuccessfully(
"builder", "create", bldr,
"-c", builderConfigFile.Name(),
"--no-color",
)
} else {
output = pack.RunSuccessfully(
"create-builder", bldr,
"-c", builderConfigFile.Name(),
"--no-color",
)
}

assert.Contains(output, fmt.Sprintf("Successfully created builder image '%s'", bldr))
assert.Succeeds(h.PushImage(dockerCli, bldr, registryConfig))
Expand Down Expand Up @@ -2505,11 +2526,21 @@ func createBuilder(
bldr := registryConfig.RepoName("test/builder-" + h.RandString(10))

// CREATE BUILDER
output := pack.RunSuccessfully(
"create-builder", bldr,
"-c", builderConfigFile.Name(),
"--no-color",
)
var output string
if pack.Supports("builder create") {
output = pack.RunSuccessfully(
"builder", "create", bldr,
"-c", builderConfigFile.Name(),
"--no-color",
)
} else {
output = pack.RunSuccessfully(
"create-builder", bldr,
"-c", builderConfigFile.Name(),
"--no-color",
)
}

assert.Contains(output, fmt.Sprintf("Successfully created builder image '%s'", bldr))
assert.Succeeds(h.PushImage(dockerCli, bldr, registryConfig))

Expand Down
2 changes: 1 addition & 1 deletion acceptance/invoke/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (i *PackInvoker) Supports(command string) bool {
output, err := i.baseCmd(cmdParts...).CombinedOutput()
i.assert.Nil(err)

return strings.Contains(string(output), search)
return strings.Contains(string(output), search) && !strings.Contains(string(output), "Unknown help topic")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since many of the command descriptions include "builder", it was falsely returning true, even though the initial line said "Unknown help topic". This ensures that we don't get false positives.

}

type Feature int
Expand Down
3 changes: 3 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {

rootCmd.AddCommand(commands.SetDefaultBuilder(logger, cfg, &packClient))
rootCmd.AddCommand(commands.InspectBuilder(logger, cfg, &packClient, writer.NewFactory()))
//nolint:staticcheck
rootCmd.AddCommand(commands.SuggestBuilders(logger, &packClient))
//nolint:staticcheck
rootCmd.AddCommand(commands.TrustBuilder(logger, cfg))
//nolint:staticcheck
rootCmd.AddCommand(commands.UntrustBuilder(logger, cfg))
//nolint:staticcheck
rootCmd.AddCommand(commands.ListTrustedBuilders(logger, cfg))
//nolint:staticcheck
rootCmd.AddCommand(commands.CreateBuilder(logger, cfg, &packClient))

rootCmd.AddCommand(commands.PackageBuildpack(logger, cfg, &packClient, buildpackage.NewConfigReader()))
Expand Down Expand Up @@ -107,6 +109,7 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {

rootCmd.AddCommand(commands.NewConfigCommand(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.NewStackCommand(logger))
rootCmd.AddCommand(commands.NewBuilderCommand(logger, cfg, &packClient))

rootCmd.Version = pack.Version
rootCmd.SetVersionTemplate(`{{.Version}}{{"\n"}}`)
Expand Down
22 changes: 22 additions & 0 deletions internal/commands/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package commands

import (
"github.com/spf13/cobra"

"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/logging"
)

func NewBuilderCommand(logger logging.Logger, cfg config.Config, client PackClient) *cobra.Command {
cmd := &cobra.Command{
Use: "builder",
Aliases: []string{"builders"},
Short: "Interact with builders",
RunE: nil,
}

cmd.AddCommand(BuilderCreate(logger, cfg, client))
cmd.AddCommand(BuilderSuggest(logger, client))
AddHelpFlag(cmd, "builder")
return cmd
}
100 changes: 100 additions & 0 deletions internal/commands/builder_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package commands

import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpacks/pack"
"github.com/buildpacks/pack/builder"
pubcfg "github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/logging"
)

// BuilderCreateFlags define flags provided to the CreateBuilder command
type BuilderCreateFlags struct {
BuilderTomlPath string
Publish bool
Registry string
Policy string
}

// CreateBuilder creates a builder image, based on a builder config
func BuilderCreate(logger logging.Logger, cfg config.Config, client PackClient) *cobra.Command {
var flags BuilderCreateFlags

cmd := &cobra.Command{
Use: "create <image-name> --config <builder-config-path>",
Args: cobra.ExactArgs(1),
Short: "Create builder image",
Example: "pack builder create my-builder:bionic --config ./builder.toml",
Long: `A builder is an image that bundles all the bits and information on how to build your apps, such as buildpacks, an implementation of the lifecycle, and a build-time environment that pack uses when executing the lifecycle. When building an app, you can use community builders; you can see our suggestions by running

pack builders suggest

Creating a custom builder allows you to control what buildpacks are used and what image apps are based on. For more on how to create a builder, see: https://buildpacks.io/docs/operator-guide/create-a-builder/.
`,
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
if err := validateCreateFlags(&flags, cfg); err != nil {
return err
}

pullPolicy, err := pubcfg.ParsePullPolicy(flags.Policy)
if err != nil {
return errors.Wrapf(err, "parsing pull policy %s", flags.Policy)
}

builderConfig, warns, err := builder.ReadConfig(flags.BuilderTomlPath)
if err != nil {
return errors.Wrap(err, "invalid builder toml")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
return errors.Wrap(err, "invalid builder toml")
return errors.Wrap(err, "invalid builder TOML")

}
for _, w := range warns {
logger.Warnf("builder configuration: %s", w)
}

imageName := args[0]
if err := client.CreateBuilder(cmd.Context(), pack.CreateBuilderOptions{
BuilderName: imageName,
Config: builderConfig,
Publish: flags.Publish,
Registry: flags.Registry,
PullPolicy: pullPolicy,
}); err != nil {
return err
}
logger.Infof("Successfully created builder image %s", style.Symbol(imageName))
logging.Tip(logger, "Run %s to use this builder", style.Symbol(fmt.Sprintf("pack build <image-name> --builder %s", imageName)))
return nil
}),
}

cmd.Flags().StringVarP(&flags.Registry, "buildpack-registry", "R", cfg.DefaultRegistryName, "Buildpack Registry by name")
if !cfg.Experimental {
cmd.Flags().MarkHidden("buildpack-registry")
}
cmd.Flags().StringVarP(&flags.BuilderTomlPath, "config", "c", "", "Path to builder TOML file (required)")
cmd.Flags().BoolVar(&flags.Publish, "publish", false, "Publish to registry")
cmd.Flags().StringVar(&flags.Policy, "pull-policy", "", "Pull policy to use. Accepted values are always, never, and if-not-present. The default is always")

AddHelpFlag(cmd, "create")
return cmd
}

func validateCreateFlags(flags *BuilderCreateFlags, cfg config.Config) error {
if flags.Publish && flags.Policy == pubcfg.PullNever.String() {
return errors.Errorf("--publish and --pull-policy never cannot be used together. The --publish flag requires the use of remote images.")
}

if flags.Registry != "" && !cfg.Experimental {
return pack.NewExperimentError("Support for buildpack registries is currently experimental.")
}

if flags.BuilderTomlPath == "" {
return errors.Errorf("Please provide a builder config path, using --config.")
}

return nil
}
Loading