Skip to content

Commit

Permalink
Merges changes with upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmccormack committed Nov 25, 2024
2 parents a835519 + 5eb8ac0 commit eb0b430
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 56 deletions.
31 changes: 17 additions & 14 deletions .web-docs/components/builder/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ source "docker" "example" {
export_path = "image.tar"
}
build {
sources = ["source.docker.example"]
}
```

**JSON**

```json
Expand All @@ -42,11 +47,6 @@ source "docker" "example" {
}
```

build {
sources = ["source.docker.example"]
}
```
## Basic Example: Commit

Below is another example, the same as above but instead of exporting the
Expand Down Expand Up @@ -298,23 +298,23 @@ You must specify (only) one of `commit`, `discard`, or `export_path`.

- `platform` (string) - Set platform if server is multi-platform capable

If using `build`, this field will be ignored, as the `platform` option for
this operation will instead have precedence.
This cannot be used at the same time as `build`; instead, use `build.platform`

- `login` (bool) - This is used to login to dockerhub to pull a private base container. For
pushing to dockerhub, see the docker post-processors
- `login` (bool) - This is used to login to a private docker repository (e.g., dockerhub)
to build or pull a private base container. For pushing to a private
repository, see the docker post-processors.

- `login_password` (string) - The password to use to authenticate to login.

- `login_server` (string) - The server address to login to.

- `login_username` (string) - The username to use to authenticate to login.

- `ecr_login` (bool) - Defaults to false. If true, the builder will login in order to pull the
image from Amazon EC2 Container Registry (ECR). The builder only logs in
for the duration of the pull. If true login_server is required and
login, login_username, and login_password will be ignored. For more
information see the section on ECR.
- `ecr_login` (bool) - Defaults to false. If true, the builder will login in order to build or
pull the image from Amazon EC2 Container Registry (ECR). The builder
only logs in for the duration of the build or pull step. If true,
login_server is required and login, login_username, and login_password
will be ignored. For more information see the section on ECR.

<!-- End of code generated from the comments of the Config struct in builder/docker/config.go; -->

Expand Down Expand Up @@ -393,6 +393,9 @@ source "docker" "example" {

Defaults to the directory from which we invoke packer.

- `arguments` (map[string]string) - A mapping of additional build args to provide. The key of
the object is the argument name, the value is the argument value.

- `platform` (string) - Set platform if server is multi-platform capable

- `pull` (boolean) - Pull the image when building the base docker image.
Expand Down
3 changes: 3 additions & 0 deletions builder/docker/builder_acc_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package docker

import (
Expand Down
15 changes: 8 additions & 7 deletions builder/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,21 @@ type Config struct {
// This cannot be used at the same time as `build`; instead, use `build.platform`
Platform string `mapstructure:"platform" required:"false"`

// This is used to login to dockerhub to pull a private base container. For
// pushing to dockerhub, see the docker post-processors
// This is used to login to a private docker repository (e.g., dockerhub)
// to build or pull a private base container. For pushing to a private
// repository, see the docker post-processors.
Login bool `mapstructure:"login" required:"false"`
// The password to use to authenticate to login.
LoginPassword string `mapstructure:"login_password" required:"false"`
// The server address to login to.
LoginServer string `mapstructure:"login_server" required:"false"`
// The username to use to authenticate to login.
LoginUsername string `mapstructure:"login_username" required:"false"`
// Defaults to false. If true, the builder will login in order to pull the
// image from Amazon EC2 Container Registry (ECR). The builder only logs in
// for the duration of the pull. If true login_server is required and
// login, login_username, and login_password will be ignored. For more
// information see the section on ECR.
// Defaults to false. If true, the builder will login in order to build or
// pull the image from Amazon EC2 Container Registry (ECR). The builder
// only logs in for the duration of the build or pull step. If true,
// login_server is required and login, login_username, and login_password
// will be ignored. For more information see the section on ECR.
EcrLogin bool `mapstructure:"ecr_login" required:"false"`
AwsAccessConfig `mapstructure:",squash"`

Expand Down
14 changes: 12 additions & 2 deletions builder/docker/dockerfile_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ type DockerfileBootstrapConfig struct {
//
// Defaults to the directory from which we invoke packer.
BuildDir string `mapstructure:"build_dir"`


// A mapping of additional build args to provide. The key of
// the object is the argument name, the value is the argument value.
Arguments map[string]string `mapstructure:"arguments" required:"false"`

// Set platform if server is multi-platform capable
Platform string `mapstructure:"platform" required:"false"`

// Pull the image when building the base docker image.
//
// Note: defaults to true, to disable this, explicitly set it to false.
Expand Down Expand Up @@ -101,6 +105,12 @@ func (c DockerfileBootstrapConfig) BuildArgs() []string {
retArgs = append(retArgs, "--compress")
}

// Loops through map of build arguments to add to build command
for key, value := range c.Arguments {
arg := key + "=" + value
retArgs = append(retArgs, "--build-arg", arg)
}

return append(retArgs, c.BuildDir)
}

Expand Down
12 changes: 7 additions & 5 deletions builder/docker/dockerfile_config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions builder/docker/driver_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (d *DockerDriver) Build(args []string) (string, error) {
imageIdFilePath := imageIdFile.Name()
imageIdFile.Close()

log.Printf("Building container with args: %v", args)
cmd := exec.Command(d.Executable, "build")
cmd.Args = append(cmd.Args, "--iidfile", imageIdFilePath)
cmd.Args = append(cmd.Args, args...)
Expand Down
61 changes: 47 additions & 14 deletions builder/docker/step_build.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package docker

import (
"context"
"errors"
"fmt"
"reflect"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
Expand All @@ -22,28 +23,60 @@ func (s *stepBuild) Run(ctx context.Context, state multistep.StateBag) multistep

ui := state.Get("ui").(packer.Ui)
driver := state.Get("driver").(Driver)
ui.Say("Building base image...")

imageId, err := driver.Build(s.buildArgs.BuildArgs())
if err != nil {
config, ok := state.Get("config").(*Config)
if !ok {
err := fmt.Errorf("error encountered obtaining docker config")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

ui.Sayf("Finished building base image %q", imageId)
ui.Say("Building base image...")

cfg, ok := state.GetOk("config")
if !ok {
state.Put("error", errors.New("missing config in state; this is a docker plugin bug, please report upstream"))
return multistep.ActionHalt
if config.EcrLogin {
ui.Message("Fetching ECR credentials...")

username, password, err := config.EcrGetLogin(config.LoginServer)
if err != nil {
err := fmt.Errorf("Error fetching ECR credentials: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

config.LoginUsername = username
config.LoginPassword = password
}

config, ok := cfg.(*Config)
if !ok {
state.Put("error", fmt.Errorf("config object set but type (%s) doesn't match. This is a docker plugin bug, please report upstream", reflect.TypeOf(cfg).String()))
if config.Login || config.EcrLogin {
ui.Message("Logging in...")
err := driver.Login(
config.LoginServer,
config.LoginUsername,
config.LoginPassword)
if err != nil {
err := fmt.Errorf("Error logging in: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

defer func() {
ui.Message("Logging out...")
if err := driver.Logout(config.LoginServer); err != nil {
ui.Error(fmt.Sprintf("Error logging out: %s", err))
}
}()
}

imageId, err := driver.Build(s.buildArgs.BuildArgs())
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}

ui.Sayf("Finished building base image %q", imageId)

config.Image = imageId

s.ran = true
Expand Down
15 changes: 8 additions & 7 deletions docs-partials/builder/docker/Config-not-required.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,20 @@

This cannot be used at the same time as `build`; instead, use `build.platform`

- `login` (bool) - This is used to login to dockerhub to pull a private base container. For
pushing to dockerhub, see the docker post-processors
- `login` (bool) - This is used to login to a private docker repository (e.g., dockerhub)
to build or pull a private base container. For pushing to a private
repository, see the docker post-processors.

- `login_password` (string) - The password to use to authenticate to login.

- `login_server` (string) - The server address to login to.

- `login_username` (string) - The username to use to authenticate to login.

- `ecr_login` (bool) - Defaults to false. If true, the builder will login in order to pull the
image from Amazon EC2 Container Registry (ECR). The builder only logs in
for the duration of the pull. If true login_server is required and
login, login_username, and login_password will be ignored. For more
information see the section on ECR.
- `ecr_login` (bool) - Defaults to false. If true, the builder will login in order to build or
pull the image from Amazon EC2 Container Registry (ECR). The builder
only logs in for the duration of the build or pull step. If true,
login_server is required and login, login_username, and login_password
will be ignored. For more information see the section on ECR.

<!-- End of code generated from the comments of the Config struct in builder/docker/config.go; -->
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

Defaults to the directory from which we invoke packer.

- `arguments` (map[string]string) - A mapping of additional build args to provide. The key of
the object is the argument name, the value is the argument value.

- `platform` (string) - Set platform if server is multi-platform capable

- `pull` (boolean) - Pull the image when building the base docker image.
Expand Down
10 changes: 5 additions & 5 deletions docs/builders/docker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ source "docker" "example" {
export_path = "image.tar"
}
build {
sources = ["source.docker.example"]
}
```

**JSON**

```json
Expand All @@ -53,11 +58,6 @@ source "docker" "example" {
}
```

build {
sources = ["source.docker.example"]
}
```
## Basic Example: Commit

Below is another example, the same as above but instead of exporting the
Expand Down
4 changes: 2 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package version
import "github.com/hashicorp/packer-plugin-sdk/version"

var (
Version = "1.1.0"
VersionPrerelease = ""
Version = "1.1.1"
VersionPrerelease = "dev"
VersionMetadata = ""
PluginVersion = version.NewPluginVersion(Version, VersionPrerelease, VersionMetadata)
)

0 comments on commit eb0b430

Please sign in to comment.