Skip to content
Closed
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ BUILDFLAGS := -tags "$(AUTOTAGS) $(TAGS)"

all: buildah docs

buildah: *.go cmd/buildah/*.go
buildah: *.go imagebuildah/*.go cmd/buildah/*.go
go build -o buildah $(BUILDFLAGS) ./cmd/buildah

.PHONY: clean
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ Then to install buildah follow the steps in this example:
**Future goals include:**
* docs
* more CI tests
* additional CLI commands (build?)
* additional CLI commands (?)
176 changes: 176 additions & 0 deletions cmd/buildah/bud.go
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",
Copy link
Member

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?

Copy link
Member Author

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.

},
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...)
}
6 changes: 3 additions & 3 deletions cmd/buildah/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func updateConfig(builder *buildah.Builder, c *cli.Context) {
if c.IsSet("user") {
builder.User = c.String("user")
}
if c.IsSet("port") {
if c.IsSet("port") || c.IsSet("p") {
if builder.Expose == nil {
builder.Expose = make(map[string]interface{})
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func updateConfig(builder *buildah.Builder, c *cli.Context) {
builder.Volumes = append(builder.Volumes, volSpec...)
}
}
if c.IsSet("label") {
if c.IsSet("label") || c.IsSet("l") {
if builder.Labels == nil {
builder.Labels = make(map[string]string)
}
Expand All @@ -149,7 +149,7 @@ func updateConfig(builder *buildah.Builder, c *cli.Context) {
if c.IsSet("workingdir") {
builder.Workdir = c.String("workingdir")
}
if c.IsSet("annotation") {
if c.IsSet("annotation") || c.IsSet("a") {
if builder.Annotations == nil {
builder.Annotations = make(map[string]string)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/buildah/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func main() {
umountCommand,
imagesCommand,
rmiCommand,
budCommand,
}
err := app.Run(os.Args)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,15 @@ func (b *Builder) updatedConfig() []byte {
}
return updatedImageConfig
}

// UpdatedEnv returns the environment list from the source image, with the
// builder's own list appended to it.
func (b *Builder) UpdatedEnv() []string {
config := b.updatedConfig()
image := ociv1.Image{}
if err := json.Unmarshal(config, &image); err != nil {
logrus.Errorf("error parsing updated image information")
return []string{}
}
return append(image.Config.Env, b.Env...)
}
49 changes: 46 additions & 3 deletions contrib/completions/bash/buildah
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,47 @@ return 1
esac
}

_buildah_bud() {
local boolean_options="
--help
-h
--pull
--pull-always
"

local options_with_args="
--registry
--signature-policy
--runtime
--runtime-flag
--tag
-t
--file
-f
--build-arg
"

local all_options="$options_with_args $boolean_options"

case "$prev" in
--runtime)
COMPREPLY=($(compgen -W 'runc runv' -- "$cur"))
;;
$(__buildah_to_extglob "$options_with_args"))
return
;;
esac

case "$cur" in
-*)
COMPREPLY=($(compgen -W "$boolean_options $options_with_args" -- "$cur"))
;;
esac
}

_buildah_build_using_dockerfile() {
_buildah_bud "$@"
}

_buildah_run() {
local boolean_options="
Expand Down Expand Up @@ -437,7 +478,7 @@ return 1
-q
--noheading
-n
--no-truncate
--notruncate
"

local options_with_args="
Expand All @@ -460,7 +501,7 @@ return 1
-q
--noheading
-n
--no-truncate
--notruncate
"

local options_with_args="
Expand Down Expand Up @@ -507,6 +548,8 @@ return 1

local commands=(
add
bud
build-using-dockerfile
commit
config
containers
Expand Down Expand Up @@ -561,7 +604,7 @@ done

local binary="${words[0]}"

local completions_func=_buildah_${command}
local completions_func=_buildah_${command/-/_}
declare -F $completions_func >/dev/null && $completions_func

eval "$previous_extglob_setting"
Expand Down
Loading