-
Notifications
You must be signed in to change notification settings - Fork 873
Initial build-using-dockerfile implementation #59
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
33002c2
Use imagebuilder's definition of "scratch"
nalind 62ca12b
Vendor openshift/imagebuilder
nalind 6e50133
Add a build-using-dockerfile command
nalind eff24d5
Add a few tests for building images
nalind f3d0a9d
Add a man page for buildah-bud(1)
nalind 390defd
Update bash-completions for build-using-dockerfile
nalind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,176 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/Sirupsen/logrus" | ||
| "github.com/containers/storage/pkg/archive" | ||
| "github.com/projectatomic/buildah/imagebuildah" | ||
| "github.com/urfave/cli" | ||
| ) | ||
|
|
||
| var ( | ||
| budFlags = []cli.Flag{ | ||
| cli.BoolFlag{ | ||
| Name: "quiet, q", | ||
| Usage: "refrain from announcing build instructions", | ||
| }, | ||
| cli.StringFlag{ | ||
| Name: "registry", | ||
| Usage: "prefix to prepend to the image name in order to pull the image", | ||
| Value: DefaultRegistry, | ||
| }, | ||
| cli.BoolTFlag{ | ||
| Name: "pull", | ||
| Usage: "pull the image if not present", | ||
| }, | ||
| cli.BoolFlag{ | ||
| Name: "pull-always", | ||
| Usage: "pull the image, even if a version is present", | ||
| }, | ||
| cli.StringFlag{ | ||
| Name: "signature-policy", | ||
| Usage: "`pathname` of signature policy file (not usually used)", | ||
| }, | ||
| cli.StringSliceFlag{ | ||
| Name: "build-arg", | ||
| Usage: "`argument=value` to supply to the builder", | ||
| }, | ||
| cli.StringFlag{ | ||
| Name: "tag, t", | ||
| Usage: "`tag` to apply to the built image", | ||
| }, | ||
| cli.StringSliceFlag{ | ||
| Name: "file, f", | ||
| Usage: "`pathname or URL` of a Dockerfile", | ||
| }, | ||
| } | ||
| budDescription = "Builds an OCI image using instructions in one or more Dockerfiles." | ||
| budCommand = cli.Command{ | ||
| Name: "build-using-dockerfile", | ||
| Aliases: []string{"bud"}, | ||
| Usage: "Build an image using instructions in a Dockerfile", | ||
| Description: budDescription, | ||
| Flags: budFlags, | ||
| Action: budCmd, | ||
| ArgsUsage: "CONTEXT-DIRECTORY | URL", | ||
| } | ||
| ) | ||
|
|
||
| func budCmd(c *cli.Context) error { | ||
| output := "" | ||
| if c.IsSet("tag") { | ||
| output = c.String("tag") | ||
| } | ||
| registry := DefaultRegistry | ||
| if c.IsSet("registry") { | ||
| registry = c.String("registry") | ||
| } | ||
| pull := true | ||
| if c.IsSet("pull") { | ||
| pull = c.BoolT("pull") | ||
| } | ||
| pullAlways := false | ||
| if c.IsSet("pull-always") { | ||
| pull = c.Bool("pull-always") | ||
| } | ||
| signaturePolicy := "" | ||
| if c.IsSet("signature-policy") { | ||
| signaturePolicy = c.String("signature-policy") | ||
| } | ||
| args := make(map[string]string) | ||
| if c.IsSet("build-arg") { | ||
| for _, arg := range c.StringSlice("build-arg") { | ||
| av := strings.SplitN(arg, "=", 2) | ||
| if len(av) > 1 { | ||
| args[av[0]] = av[1] | ||
| } else { | ||
| delete(args, av[0]) | ||
| } | ||
| } | ||
| } | ||
| quiet := false | ||
| if c.IsSet("quiet") { | ||
| quiet = c.Bool("quiet") | ||
| } | ||
| dockerfiles := []string{} | ||
| if c.IsSet("file") || c.IsSet("f") { | ||
| dockerfiles = c.StringSlice("file") | ||
| } | ||
| contextDir := "" | ||
| cliArgs := c.Args() | ||
| if len(cliArgs) > 0 { | ||
| // The context directory could be a URL. Try to handle that. | ||
| tempDir, subDir, err := imagebuildah.TempDirForURL("", "buildah", cliArgs[0]) | ||
| if err != nil { | ||
| return fmt.Errorf("error prepping temporary context directory: %v", err) | ||
| } | ||
| if tempDir != "" { | ||
| // We had to download it to a temporary directory. | ||
| // Delete it later. | ||
| defer func() { | ||
| if err = os.RemoveAll(tempDir); err != nil { | ||
| logrus.Errorf("error removing temporary directory %q: %v", contextDir, err) | ||
| } | ||
| }() | ||
| contextDir = filepath.Join(tempDir, subDir) | ||
| } else { | ||
| // Nope, it was local. Use it as is. | ||
| absDir, err := filepath.Abs(cliArgs[0]) | ||
| if err != nil { | ||
| return fmt.Errorf("error determining path to directory %q: %v", cliArgs[0], err) | ||
| } | ||
| contextDir = absDir | ||
| } | ||
| cliArgs = cliArgs.Tail() | ||
| } else { | ||
| // No context directory or URL was specified. Try to use the | ||
| // home of the first locally-available Dockerfile. | ||
| for i := range dockerfiles { | ||
| if strings.HasPrefix(dockerfiles[i], "http://") || | ||
| strings.HasPrefix(dockerfiles[i], "https://") || | ||
| strings.HasPrefix(dockerfiles[i], "git://") || | ||
| strings.HasPrefix(dockerfiles[i], "github.com/") { | ||
| continue | ||
| } | ||
| absFile, err := filepath.Abs(dockerfiles[i]) | ||
| if err != nil { | ||
| return fmt.Errorf("error determining path to file %q: %v", dockerfiles[i], err) | ||
| } | ||
| contextDir = filepath.Dir(absFile) | ||
| dockerfiles[i], err = filepath.Rel(contextDir, absFile) | ||
| if err != nil { | ||
| return fmt.Errorf("error determining path to file %q: %v", dockerfiles[i], err) | ||
| } | ||
| break | ||
| } | ||
| } | ||
| if contextDir == "" { | ||
| return fmt.Errorf("no context directory specified, and no dockerfile specified") | ||
| } | ||
| if len(dockerfiles) == 0 { | ||
| dockerfiles = append(dockerfiles, filepath.Join(contextDir, "Dockerfile")) | ||
| } | ||
|
|
||
| store, err := getStore(c) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| options := imagebuildah.BuildOptions{ | ||
| ContextDirectory: contextDir, | ||
| PullIfMissing: pull, | ||
| PullAlways: pullAlways, | ||
| Registry: registry, | ||
| Compression: archive.Gzip, | ||
| Quiet: quiet, | ||
| SignaturePolicyPath: signaturePolicy, | ||
| Args: args, | ||
| Output: output, | ||
| } | ||
|
|
||
| return imagebuildah.BuildDockerfiles(store, options, dockerfiles...) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we reverse this and change it to verbose, and then default it to value so we don't announce build instructions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not strongly opposed to it, but docker build and imagebuilder output this info by default.