diff --git a/builder/alicloud/ecs/builder.go b/builder/alicloud/ecs/builder.go index 83c0d1242..ec38ba38d 100644 --- a/builder/alicloud/ecs/builder.go +++ b/builder/alicloud/ecs/builder.go @@ -3,8 +3,7 @@ package ecs import ( - "log" - + "context" "fmt" "github.com/hashicorp/packer/common" @@ -76,7 +75,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { client, err := b.config.Client() if err != nil { @@ -205,7 +204,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -227,13 +226,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} - func (b *Builder) chooseNetworkType() InstanceNetWork { if b.isVpcNetRequired() { return VpcNet diff --git a/builder/alicloud/ecs/step_create_tags.go b/builder/alicloud/ecs/step_create_tags.go index 75dc602ff..5988dd3b4 100644 --- a/builder/alicloud/ecs/step_create_tags.go +++ b/builder/alicloud/ecs/step_create_tags.go @@ -3,6 +3,7 @@ package ecs import ( "context" "fmt" + "github.com/denverdino/aliyungo/common" "github.com/denverdino/aliyungo/ecs" "github.com/hashicorp/packer/helper/multistep" diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index f6433eefd..5d2fb4c90 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -5,8 +5,8 @@ package chroot import ( + "context" "errors" - "log" "runtime" "github.com/aws/aws-sdk-go/service/ec2" @@ -190,7 +190,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warns, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { if runtime.GOOS != "linux" { return nil, errors.New("The amazon-chroot builder only works on Linux environments.") } @@ -303,7 +303,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -324,10 +324,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/amazon/chroot/step_chroot_provision.go b/builder/amazon/chroot/step_chroot_provision.go index be8667077..4545105b0 100644 --- a/builder/amazon/chroot/step_chroot_provision.go +++ b/builder/amazon/chroot/step_chroot_provision.go @@ -12,7 +12,7 @@ import ( type StepChrootProvision struct { } -func (s *StepChrootProvision) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { +func (s *StepChrootProvision) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { hook := state.Get("hook").(packer.Hook) mountPath := state.Get("mount_path").(string) ui := state.Get("ui").(packer.Ui) @@ -26,7 +26,7 @@ func (s *StepChrootProvision) Run(_ context.Context, state multistep.StateBag) m // Provision log.Println("Running the provision hook") - if err := hook.Run(packer.HookProvision, ui, comm, nil); err != nil { + if err := hook.Run(ctx, packer.HookProvision, ui, comm, nil); err != nil { state.Put("error", err) return multistep.ActionHalt } diff --git a/builder/amazon/chroot/step_create_volume_test.go b/builder/amazon/chroot/step_create_volume_test.go index 03a34bfd5..a522d5026 100644 --- a/builder/amazon/chroot/step_create_volume_test.go +++ b/builder/amazon/chroot/step_create_volume_test.go @@ -1,10 +1,11 @@ package chroot import ( + "testing" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/stretchr/testify/assert" - "testing" ) func buildTestRootDevice() *ec2.BlockDeviceMapping { diff --git a/builder/amazon/chroot/step_register_ami_test.go b/builder/amazon/chroot/step_register_ami_test.go index 8c686b4bc..8861ae704 100644 --- a/builder/amazon/chroot/step_register_ami_test.go +++ b/builder/amazon/chroot/step_register_ami_test.go @@ -1,9 +1,10 @@ package chroot import ( + "testing" + amazon "github.com/hashicorp/packer/builder/amazon/common" "github.com/hashicorp/packer/common" - "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" diff --git a/builder/amazon/common/step_source_ami_info_test.go b/builder/amazon/common/step_source_ami_info_test.go index e0734bda1..4941180cd 100644 --- a/builder/amazon/common/step_source_ami_info_test.go +++ b/builder/amazon/common/step_source_ami_info_test.go @@ -1,10 +1,11 @@ package common import ( + "testing" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/stretchr/testify/assert" - "testing" ) func TestStepSourceAmiInfo_PVImage(t *testing.T) { diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 248c22623..e196cb6d4 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -6,8 +6,8 @@ package ebs import ( + "context" "fmt" - "log" "github.com/aws/aws-sdk-go/service/ec2" awscommon "github.com/hashicorp/packer/builder/amazon/common" @@ -85,7 +85,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { session, err := b.config.Session() if err != nil { @@ -245,7 +245,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) @@ -265,10 +265,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/amazon/ebssurrogate/builder.go b/builder/amazon/ebssurrogate/builder.go index cab8550ae..12899cad4 100644 --- a/builder/amazon/ebssurrogate/builder.go +++ b/builder/amazon/ebssurrogate/builder.go @@ -3,9 +3,9 @@ package ebssurrogate import ( + "context" "errors" "fmt" - "log" "github.com/aws/aws-sdk-go/service/ec2" awscommon "github.com/hashicorp/packer/builder/amazon/common" @@ -100,7 +100,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { session, err := b.config.Session() if err != nil { return nil, err @@ -270,7 +270,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -290,10 +290,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return nil, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/amazon/ebsvolume/builder.go b/builder/amazon/ebsvolume/builder.go index 0a5daeb44..93c425377 100644 --- a/builder/amazon/ebsvolume/builder.go +++ b/builder/amazon/ebsvolume/builder.go @@ -3,8 +3,8 @@ package ebsvolume import ( + "context" "fmt" - "log" "github.com/aws/aws-sdk-go/service/ec2" awscommon "github.com/hashicorp/packer/builder/amazon/common" @@ -89,7 +89,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { session, err := b.config.Session() if err != nil { return nil, err @@ -210,7 +210,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -226,10 +226,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { ui.Say(fmt.Sprintf("Created Volumes: %s", artifact)) return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index c9510278c..86fac7538 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -3,9 +3,9 @@ package instance import ( + "context" "errors" "fmt" - "log" "os" "strings" @@ -170,7 +170,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { session, err := b.config.Session() if err != nil { return nil, err @@ -321,7 +321,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -342,10 +342,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/azure/arm/azure_error_response_test.go b/builder/azure/arm/azure_error_response_test.go index 2e4f7f1ac..d27c2192a 100644 --- a/builder/azure/arm/azure_error_response_test.go +++ b/builder/azure/arm/azure_error_response_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/approvals/go-approval-tests" + approvaltests "github.com/approvals/go-approval-tests" "github.com/hashicorp/packer/common/json" ) diff --git a/builder/azure/arm/builder.go b/builder/azure/arm/builder.go index 096fa322c..9e73c9181 100644 --- a/builder/azure/arm/builder.go +++ b/builder/azure/arm/builder.go @@ -24,10 +24,9 @@ import ( ) type Builder struct { - config *Config - stateBag multistep.StateBag - runner multistep.Runner - ctxCancel context.CancelFunc + config *Config + stateBag multistep.StateBag + runner multistep.Runner } const ( @@ -51,12 +50,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, errs } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { ui.Say("Running builder ...") - ctx, cancel := context.WithCancel(context.Background()) - b.ctxCancel = cancel + ctx, cancel := context.WithCancel(ctx) defer cancel() // User's intent to use MSI is indicated with empty subscription id, tenant, client id, client cert, client secret and jwt. @@ -259,7 +257,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(b.stateBag) + b.runner.Run(ctx, b.stateBag) // Report any errors. if rawErr, ok := b.stateBag.GetOk(constants.Error); ok { @@ -324,17 +322,6 @@ func (b *Builder) isPrivateNetworkCommunication() bool { return b.config.VirtualNetworkName != "" } -func (b *Builder) Cancel() { - if b.ctxCancel != nil { - log.Printf("Cancelling Azure builder...") - b.ctxCancel() - } - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} - func equalLocation(location1, location2 string) bool { return strings.EqualFold(canonicalizeLocation(location1), canonicalizeLocation(location2)) } diff --git a/builder/azure/arm/builder_acc_test.go b/builder/azure/arm/builder_acc_test.go index 8f0c605f9..124ee939e 100644 --- a/builder/azure/arm/builder_acc_test.go +++ b/builder/azure/arm/builder_acc_test.go @@ -24,8 +24,9 @@ import ( "testing" "fmt" - builderT "github.com/hashicorp/packer/helper/builder/testing" "os" + + builderT "github.com/hashicorp/packer/helper/builder/testing" ) const DeviceLoginAcceptanceTest = "DEVICELOGIN_TEST" diff --git a/builder/azure/arm/step_snapshot_data_disks_test.go b/builder/azure/arm/step_snapshot_data_disks_test.go index 66eb2115e..9f34f0cce 100644 --- a/builder/azure/arm/step_snapshot_data_disks_test.go +++ b/builder/azure/arm/step_snapshot_data_disks_test.go @@ -3,9 +3,10 @@ package arm import ( "context" "fmt" + "testing" + "github.com/hashicorp/packer/builder/azure/common/constants" "github.com/hashicorp/packer/helper/multistep" - "testing" ) func TestStepSnapshotDataDisksShouldFailIfSnapshotFails(t *testing.T) { diff --git a/builder/azure/arm/step_snapshot_os_disk.go b/builder/azure/arm/step_snapshot_os_disk.go index 001bb5a45..614b1a5cd 100644 --- a/builder/azure/arm/step_snapshot_os_disk.go +++ b/builder/azure/arm/step_snapshot_os_disk.go @@ -3,6 +3,7 @@ package arm import ( "context" "fmt" + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute" "github.com/Azure/go-autorest/autorest/to" "github.com/hashicorp/packer/builder/azure/common/constants" diff --git a/builder/azure/arm/step_snapshot_os_disk_test.go b/builder/azure/arm/step_snapshot_os_disk_test.go index 48d2211d1..7148a6eb0 100644 --- a/builder/azure/arm/step_snapshot_os_disk_test.go +++ b/builder/azure/arm/step_snapshot_os_disk_test.go @@ -3,9 +3,10 @@ package arm import ( "context" "fmt" + "testing" + "github.com/hashicorp/packer/builder/azure/common/constants" "github.com/hashicorp/packer/helper/multistep" - "testing" ) func TestStepSnapshotOSDiskShouldFailIfSnapshotFails(t *testing.T) { diff --git a/builder/azure/arm/template_factory_test.go b/builder/azure/arm/template_factory_test.go index ac00d33fd..af003f0c6 100644 --- a/builder/azure/arm/template_factory_test.go +++ b/builder/azure/arm/template_factory_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-02-01/resources" - "github.com/approvals/go-approval-tests" + approvaltests "github.com/approvals/go-approval-tests" "github.com/hashicorp/packer/builder/azure/common/constants" "github.com/hashicorp/packer/builder/azure/common/template" ) diff --git a/builder/azure/common/template/template_builder_test.go b/builder/azure/common/template/template_builder_test.go index 5de8ce4ae..482107c34 100644 --- a/builder/azure/common/template/template_builder_test.go +++ b/builder/azure/common/template/template_builder_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute" - "github.com/approvals/go-approval-tests" + approvaltests "github.com/approvals/go-approval-tests" ) // Ensure that a Linux template is configured as expected. diff --git a/builder/cloudstack/builder.go b/builder/cloudstack/builder.go index eae4d2db3..c40b112a3 100644 --- a/builder/cloudstack/builder.go +++ b/builder/cloudstack/builder.go @@ -1,6 +1,7 @@ package cloudstack import ( + "context" "fmt" "github.com/hashicorp/packer/common" @@ -31,7 +32,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } // Run implements the packer.Builder interface. -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { b.ui = ui // Create a CloudStack API client. @@ -91,7 +92,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Configure the runner and run the steps. b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -113,11 +114,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -// Cancel the step runner. -func (b *Builder) Cancel() { - if b.runner != nil { - b.ui.Say("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index 428cebee9..4b5f6e36d 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -35,7 +35,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, &apiTokenSource{ AccessToken: b.config.APIToken, })) @@ -100,7 +100,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -121,10 +121,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/docker/builder.go b/builder/docker/builder.go index f6a6e4765..81ff00c71 100644 --- a/builder/docker/builder.go +++ b/builder/docker/builder.go @@ -1,6 +1,7 @@ package docker import ( + "context" "log" "github.com/hashicorp/packer/common" @@ -29,7 +30,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { driver := &DockerDriver{Ctx: &b.config.ctx, Ui: ui} if err := driver.Verify(); err != nil { return nil, err @@ -82,7 +83,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -108,10 +109,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/docker/communicator_test.go b/builder/docker/communicator_test.go index 588c21562..6bd76f90b 100644 --- a/builder/docker/communicator_test.go +++ b/builder/docker/communicator_test.go @@ -1,6 +1,7 @@ package docker import ( + "context" "crypto/sha256" "io/ioutil" "os" @@ -75,7 +76,7 @@ func TestUploadDownload(t *testing.T) { hook := &packer.DispatchHook{Mapping: hooks} // Run things - artifact, err := builder.Run(ui, hook) + artifact, err := builder.Run(context.Background(), ui, hook) if err != nil { t.Fatalf("Error running build %s", err) } @@ -164,7 +165,7 @@ func TestLargeDownload(t *testing.T) { hook := &packer.DispatchHook{Mapping: hooks} // Run things - artifact, err := builder.Run(ui, hook) + artifact, err := builder.Run(context.Background(), ui, hook) if err != nil { t.Fatalf("Error running build %s", err) } @@ -272,7 +273,7 @@ func TestFixUploadOwner(t *testing.T) { } hook := &packer.DispatchHook{Mapping: hooks} - artifact, err := builder.Run(ui, hook) + artifact, err := builder.Run(context.Background(), ui, hook) if err != nil { t.Fatalf("Error running build %s", err) } diff --git a/builder/file/builder.go b/builder/file/builder.go index 3ff69b07e..3ac6e05a8 100644 --- a/builder/file/builder.go +++ b/builder/file/builder.go @@ -6,6 +6,7 @@ any virtualization or network resources, it's very fast and useful for testing. */ import ( + "context" "fmt" "io" "io/ioutil" @@ -33,7 +34,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } // Run is where the actual build should take place. It takes a Build and a Ui. -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { artifact := new(FileArtifact) if b.config.Source != "" { @@ -69,9 +70,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -// Cancel cancels a possibly running Builder. This should block until -// the builder actually cancels and cleans up after itself. -func (b *Builder) Cancel() { - b.runner.Cancel() -} diff --git a/builder/googlecompute/builder.go b/builder/googlecompute/builder.go index 19043868f..9a2b4fecc 100644 --- a/builder/googlecompute/builder.go +++ b/builder/googlecompute/builder.go @@ -3,6 +3,7 @@ package googlecompute import ( + "context" "fmt" "log" @@ -33,7 +34,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { // Run executes a googlecompute Packer build and returns a packer.Artifact // representing a GCE machine image. -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { driver, err := NewDriverGCE( ui, b.config.ProjectId, &b.config.Account) if err != nil { @@ -82,7 +83,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps. b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { @@ -102,9 +103,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Cancel. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/hcloud/builder.go b/builder/hcloud/builder.go index 432337ab1..92f9f474b 100644 --- a/builder/hcloud/builder.go +++ b/builder/hcloud/builder.go @@ -1,8 +1,8 @@ package hcloud import ( + "context" "fmt" - "log" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" @@ -31,7 +31,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { opts := []hcloud.ClientOption{ hcloud.WithToken(b.config.HCloudToken), hcloud.WithEndpoint(b.config.Endpoint), @@ -67,7 +67,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Run the steps b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) @@ -85,10 +85,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/hyperone/artifact.go b/builder/hyperone/artifact.go index 1436637c1..5b61e5895 100644 --- a/builder/hyperone/artifact.go +++ b/builder/hyperone/artifact.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "github.com/hyperonecom/h1-client-go" + openapi "github.com/hyperonecom/h1-client-go" ) type Artifact struct { diff --git a/builder/hyperone/builder.go b/builder/hyperone/builder.go index 43b0afbb8..2199623f5 100644 --- a/builder/hyperone/builder.go +++ b/builder/hyperone/builder.go @@ -1,8 +1,8 @@ package hyperone import ( + "context" "fmt" - "log" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" @@ -50,7 +50,7 @@ type wrappedCommandTemplate struct { Command string } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { wrappedCommand := func(command string) (string, error) { ctx := b.config.ctx ctx.Data = &wrappedCommandTemplate{Command: command} @@ -97,7 +97,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) @@ -111,10 +111,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the runner...") - b.runner.Cancel() - } -} diff --git a/builder/hyperone/step_create_image.go b/builder/hyperone/step_create_image.go index edb693e40..923566cd1 100644 --- a/builder/hyperone/step_create_image.go +++ b/builder/hyperone/step_create_image.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" - "github.com/hyperonecom/h1-client-go" + openapi "github.com/hyperonecom/h1-client-go" ) type stepCreateImage struct { diff --git a/builder/hyperone/step_create_vm.go b/builder/hyperone/step_create_vm.go index 1ffd06d35..155f383a0 100644 --- a/builder/hyperone/step_create_vm.go +++ b/builder/hyperone/step_create_vm.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" - "github.com/hyperonecom/h1-client-go" + openapi "github.com/hyperonecom/h1-client-go" ) type stepCreateVM struct { diff --git a/builder/hyperone/step_create_vm_from_disk.go b/builder/hyperone/step_create_vm_from_disk.go index 068ed2864..b81a93b32 100644 --- a/builder/hyperone/step_create_vm_from_disk.go +++ b/builder/hyperone/step_create_vm_from_disk.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" - "github.com/hyperonecom/h1-client-go" + openapi "github.com/hyperonecom/h1-client-go" ) type stepCreateVMFromDisk struct { diff --git a/builder/hyperone/step_create_vm_test.go b/builder/hyperone/step_create_vm_test.go index d6b6a44a4..0c7b3e087 100644 --- a/builder/hyperone/step_create_vm_test.go +++ b/builder/hyperone/step_create_vm_test.go @@ -3,7 +3,7 @@ package hyperone import ( "testing" - "github.com/hyperonecom/h1-client-go" + openapi "github.com/hyperonecom/h1-client-go" "github.com/stretchr/testify/assert" ) diff --git a/builder/hyperone/step_detach_disk.go b/builder/hyperone/step_detach_disk.go index 3f96e8acc..0a1451ae1 100644 --- a/builder/hyperone/step_detach_disk.go +++ b/builder/hyperone/step_detach_disk.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" - "github.com/hyperonecom/h1-client-go" + openapi "github.com/hyperonecom/h1-client-go" ) type stepDetachDisk struct { diff --git a/builder/hyperone/step_stop_vm.go b/builder/hyperone/step_stop_vm.go index b43befd60..7d83334d4 100644 --- a/builder/hyperone/step_stop_vm.go +++ b/builder/hyperone/step_stop_vm.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" - "github.com/hyperonecom/h1-client-go" + openapi "github.com/hyperonecom/h1-client-go" ) type stepStopVM struct{} diff --git a/builder/hyperone/utils.go b/builder/hyperone/utils.go index b6172a685..cb76ccff9 100644 --- a/builder/hyperone/utils.go +++ b/builder/hyperone/utils.go @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" - "github.com/hyperonecom/h1-client-go" + openapi "github.com/hyperonecom/h1-client-go" ) func formatOpenAPIError(err error) string { diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index 77f5a9255..004c6e9ee 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -1,6 +1,7 @@ package iso import ( + "context" "errors" "fmt" "log" @@ -356,7 +357,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { // Run executes a Packer build and returns a packer.Artifact representing // a Hyperv appliance. -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Create the driver that we'll use to communicate with Hyperv driver, err := hypervcommon.NewHypervPS4Driver() if err != nil { @@ -505,7 +506,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps. b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { @@ -525,12 +526,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Cancel. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} func appendWarnings(slice []string, data ...string) []string { m := len(slice) diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index d56d8b908..c9041c3fe 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -1,6 +1,7 @@ package vmcx import ( + "context" "errors" "fmt" "log" @@ -372,7 +373,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { // Run executes a Packer build and returns a packer.Artifact representing // a Hyperv appliance. -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Create the driver that we'll use to communicate with Hyperv driver, err := hypervcommon.NewHypervPS4Driver() if err != nil { @@ -527,7 +528,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps. b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { @@ -547,12 +548,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Cancel. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} func appendWarnings(slice []string, data ...string) []string { m := len(slice) diff --git a/builder/lxc/builder.go b/builder/lxc/builder.go index 0dcd9578b..5c8747c7e 100644 --- a/builder/lxc/builder.go +++ b/builder/lxc/builder.go @@ -1,7 +1,7 @@ package lxc import ( - "log" + "context" "os" "path/filepath" @@ -33,7 +33,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { wrappedCommand := func(command string) (string, error) { b.config.ctx.Data = &wrappedCommandTemplate{Command: command} return interpolate.Render(b.config.CommandWrapper, &b.config.ctx) @@ -58,7 +58,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -86,10 +86,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/lxc/step_provision.go b/builder/lxc/step_provision.go index ac33eb301..a3ecf8332 100644 --- a/builder/lxc/step_provision.go +++ b/builder/lxc/step_provision.go @@ -11,7 +11,7 @@ import ( // StepProvision provisions the instance within a chroot. type StepProvision struct{} -func (s *StepProvision) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { +func (s *StepProvision) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { hook := state.Get("hook").(packer.Hook) config := state.Get("config").(*Config) mountPath := state.Get("mount_path").(string) @@ -28,7 +28,7 @@ func (s *StepProvision) Run(_ context.Context, state multistep.StateBag) multist // Provision log.Println("Running the provision hook") - if err := hook.Run(packer.HookProvision, ui, comm, nil); err != nil { + if err := hook.Run(ctx, packer.HookProvision, ui, comm, nil); err != nil { state.Put("error", err) return multistep.ActionHalt } diff --git a/builder/lxd/builder.go b/builder/lxd/builder.go index 4ed07ea46..ce1656217 100644 --- a/builder/lxd/builder.go +++ b/builder/lxd/builder.go @@ -1,7 +1,7 @@ package lxd import ( - "log" + "context" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/multistep" @@ -31,7 +31,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { wrappedCommand := func(command string) (string, error) { b.config.ctx.Data = &wrappedCommandTemplate{Command: command} return interpolate.Render(b.config.CommandWrapper, &b.config.ctx) @@ -52,7 +52,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -65,10 +65,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/lxd/step_provision.go b/builder/lxd/step_provision.go index 509006d77..25d85059e 100644 --- a/builder/lxd/step_provision.go +++ b/builder/lxd/step_provision.go @@ -11,7 +11,7 @@ import ( // StepProvision provisions the container type StepProvision struct{} -func (s *StepProvision) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { +func (s *StepProvision) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { hook := state.Get("hook").(packer.Hook) config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) @@ -25,7 +25,7 @@ func (s *StepProvision) Run(_ context.Context, state multistep.StateBag) multist // Provision log.Println("Running the provision hook") - if err := hook.Run(packer.HookProvision, ui, comm, nil); err != nil { + if err := hook.Run(ctx, packer.HookProvision, ui, comm, nil); err != nil { state.Put("error", err) return multistep.ActionHalt } diff --git a/builder/ncloud/builder.go b/builder/ncloud/builder.go index 7170ad419..36702c933 100644 --- a/builder/ncloud/builder.go +++ b/builder/ncloud/builder.go @@ -1,6 +1,8 @@ package ncloud import ( + "context" + ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" @@ -27,7 +29,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { ui.Message("Creating Naver Cloud Platform Connection ...") conn := ncloud.NewConnection(b.config.AccessKey, b.config.SecretKey) @@ -92,7 +94,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, b.stateBag) - b.runner.Run(b.stateBag) + b.runner.Run(ctx, b.stateBag) // If there was an error, return that if rawErr, ok := b.stateBag.GetOk("Error"); ok { @@ -108,7 +110,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - b.runner.Cancel() -} diff --git a/builder/null/builder.go b/builder/null/builder.go index 0706b0e3e..ea7ac2343 100644 --- a/builder/null/builder.go +++ b/builder/null/builder.go @@ -1,7 +1,7 @@ package null import ( - "log" + "context" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" @@ -26,7 +26,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { steps := []multistep.Step{} if b.config.CommConfig.Type != "none" { @@ -51,7 +51,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -62,10 +62,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { artifact := &NullArtifact{} return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/oneandone/builder.go b/builder/oneandone/builder.go index 2a59b0b1e..a67329c6d 100644 --- a/builder/oneandone/builder.go +++ b/builder/oneandone/builder.go @@ -1,9 +1,9 @@ package oneandone import ( + "context" "errors" "fmt" - "log" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" @@ -28,7 +28,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { state := new(multistep.BasicStateBag) @@ -55,7 +55,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) @@ -77,10 +77,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/openstack/builder.go b/builder/openstack/builder.go index 786c8c130..24921656c 100644 --- a/builder/openstack/builder.go +++ b/builder/openstack/builder.go @@ -4,8 +4,8 @@ package openstack import ( + "context" "fmt" - "log" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" @@ -65,7 +65,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { computeClient, err := b.config.computeV2Client() if err != nil { return nil, fmt.Errorf("Error initializing compute client: %s", err) @@ -155,7 +155,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -176,10 +176,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/oracle/classic/builder.go b/builder/oracle/classic/builder.go index 1bf221171..eb0ce97d8 100644 --- a/builder/oracle/classic/builder.go +++ b/builder/oracle/classic/builder.go @@ -1,8 +1,8 @@ package classic import ( + "context" "fmt" - "log" "os" "github.com/hashicorp/go-cleanhttp" @@ -41,7 +41,7 @@ func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { loggingEnabled := os.Getenv("PACKER_OCI_CLASSIC_LOGGING") != "" httpClient := cleanhttp.DefaultClient() config := &opc.Config{ @@ -176,7 +176,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -199,9 +199,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Cancel terminates a running build. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/oracle/oci/builder.go b/builder/oracle/oci/builder.go index 0379c8354..bf34eafc5 100644 --- a/builder/oracle/oci/builder.go +++ b/builder/oracle/oci/builder.go @@ -3,8 +3,8 @@ package oci import ( + "context" "fmt" - "log" ocommon "github.com/hashicorp/packer/builder/oracle/common" "github.com/hashicorp/packer/common" @@ -36,7 +36,7 @@ func (b *Builder) Prepare(rawConfig ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { driver, err := NewDriverOCI(b.config) if err != nil { return nil, err @@ -77,7 +77,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -105,9 +105,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Cancel terminates a running build. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/parallels/iso/builder.go b/builder/parallels/iso/builder.go index 16cbd32c9..287a47361 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -1,9 +1,9 @@ package iso import ( + "context" "errors" "fmt" - "log" parallelscommon "github.com/hashicorp/packer/builder/parallels/common" "github.com/hashicorp/packer/common" @@ -141,7 +141,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Create the driver that we'll use to communicate with Parallels driver, err := parallelscommon.NewDriver() if err != nil { @@ -237,7 +237,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -255,10 +255,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return parallelscommon.NewArtifact(b.config.OutputDir) } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/parallels/pvm/builder.go b/builder/parallels/pvm/builder.go index 001801849..a19e6b833 100644 --- a/builder/parallels/pvm/builder.go +++ b/builder/parallels/pvm/builder.go @@ -1,9 +1,9 @@ package pvm import ( + "context" "errors" "fmt" - "log" parallelscommon "github.com/hashicorp/packer/builder/parallels/common" "github.com/hashicorp/packer/common" @@ -32,7 +32,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { // Run executes a Packer build and returns a packer.Artifact representing // a Parallels appliance. -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Create the driver that we'll use to communicate with Parallels driver, err := parallelscommon.NewDriver() if err != nil { @@ -116,7 +116,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps. b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { @@ -136,9 +136,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Cancel. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/profitbricks/builder.go b/builder/profitbricks/builder.go index 9891b17d8..dd678c35e 100644 --- a/builder/profitbricks/builder.go +++ b/builder/profitbricks/builder.go @@ -1,8 +1,8 @@ package profitbricks import ( + "context" "fmt" - "log" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" @@ -27,7 +27,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { state := new(multistep.BasicStateBag) state.Put("config", b.config) @@ -54,7 +54,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { config := state.Get("config").(*Config) b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) @@ -65,10 +65,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index 5ca3f555b..89e8b6943 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -1,6 +1,7 @@ package qemu import ( + "context" "errors" "fmt" "log" @@ -358,7 +359,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Create the driver that we'll use to communicate with Qemu driver, err := b.newDriver(b.config.QemuBinary) if err != nil { @@ -460,7 +461,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -507,13 +508,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} - func (b *Builder) newDriver(qemuBinary string) (Driver, error) { qemuPath, err := exec.LookPath(qemuBinary) if err != nil { diff --git a/builder/scaleway/builder.go b/builder/scaleway/builder.go index 68c600693..418f945fb 100644 --- a/builder/scaleway/builder.go +++ b/builder/scaleway/builder.go @@ -4,9 +4,9 @@ package scaleway import ( + "context" "errors" "fmt" - "log" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/communicator" @@ -33,7 +33,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { client, err := api.NewScalewayAPI(b.config.Organization, b.config.Token, b.config.UserAgent, b.config.Region) if err != nil { @@ -69,7 +69,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) @@ -99,10 +99,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/tencentcloud/cvm/builder.go b/builder/tencentcloud/cvm/builder.go index 06b52ca39..a0a12f348 100644 --- a/builder/tencentcloud/cvm/builder.go +++ b/builder/tencentcloud/cvm/builder.go @@ -1,6 +1,7 @@ package cvm import ( + "context" "fmt" "log" @@ -58,7 +59,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { cvmClient, vpcClient, err := b.config.Client() if err != nil { return nil, err @@ -124,7 +125,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } b.runner = common.NewRunner(steps, b.config.PackerConfig, ui) - b.runner.Run(state) + b.runner.Run(ctx, state) if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) @@ -141,10 +142,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } return artifact, nil } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/tencentcloud/cvm/run_config_test.go b/builder/tencentcloud/cvm/run_config_test.go index 7657f2d2e..9f2dc5b35 100644 --- a/builder/tencentcloud/cvm/run_config_test.go +++ b/builder/tencentcloud/cvm/run_config_test.go @@ -1,10 +1,11 @@ package cvm import ( - "github.com/hashicorp/packer/helper/communicator" "io/ioutil" "os" "testing" + + "github.com/hashicorp/packer/helper/communicator" ) func testConfig() *TencentCloudRunConfig { diff --git a/builder/triton/builder.go b/builder/triton/builder.go index 4ec8ff771..4e40116c5 100644 --- a/builder/triton/builder.go +++ b/builder/triton/builder.go @@ -1,7 +1,7 @@ package triton import ( - "log" + "context" "github.com/hashicorp/go-multierror" "github.com/hashicorp/packer/common" @@ -45,7 +45,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, errs.ErrorOrNil() } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { config := b.config driver, err := NewDriverTriton(ui, config) @@ -77,7 +77,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -100,9 +100,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Cancel cancels a possibly running Builder. This should block until // the builder actually cancels and cleans up after itself. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/vagrant/builder.go b/builder/vagrant/builder.go index 33470a9fa..964325648 100644 --- a/builder/vagrant/builder.go +++ b/builder/vagrant/builder.go @@ -1,9 +1,9 @@ package vagrant import ( + "context" "errors" "fmt" - "log" "os" "path/filepath" "strings" @@ -162,7 +162,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { // Run executes a Packer build and returns a packer.Artifact representing // a VirtualBox appliance. -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Create the driver that we'll use to communicate with VirtualBox VagrantCWD, err := filepath.Abs(b.config.OutputDir) if err != nil { @@ -243,7 +243,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps. b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { @@ -263,9 +263,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Cancel. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/virtualbox/common/driver_4_2.go b/builder/virtualbox/common/driver_4_2.go index 8b6402c3a..17e2499de 100644 --- a/builder/virtualbox/common/driver_4_2.go +++ b/builder/virtualbox/common/driver_4_2.go @@ -3,7 +3,6 @@ package common import ( "bytes" "fmt" - versionUtil "github.com/hashicorp/go-version" "log" "os/exec" "regexp" @@ -11,6 +10,8 @@ import ( "strings" "time" + versionUtil "github.com/hashicorp/go-version" + packer "github.com/hashicorp/packer/common" ) diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 0a32fbd45..5a94a216e 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -1,9 +1,9 @@ package iso import ( + "context" "errors" "fmt" - "log" "strings" vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" @@ -193,7 +193,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Create the driver that we'll use to communicate with VirtualBox driver, err := vboxcommon.NewDriver() if err != nil { @@ -321,7 +321,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -339,10 +339,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return vboxcommon.NewArtifact(b.config.OutputDir) } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index ec0c7cb10..aa3546c7b 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -1,9 +1,9 @@ package ovf import ( + "context" "errors" "fmt" - "log" vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" "github.com/hashicorp/packer/common" @@ -32,7 +32,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { // Run executes a Packer build and returns a packer.Artifact representing // a VirtualBox appliance. -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Create the driver that we'll use to communicate with VirtualBox driver, err := vboxcommon.NewDriver() if err != nil { @@ -160,7 +160,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps. b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { @@ -180,9 +180,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Cancel. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 136e8b987..fb4c4967b 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -1,9 +1,9 @@ package iso import ( + "context" "errors" "fmt" - "log" "time" vmwcommon "github.com/hashicorp/packer/builder/vmware/common" @@ -30,7 +30,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return warnings, nil } -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig, b.config.VMName) if err != nil { return nil, fmt.Errorf("Failed creating VMware driver: %s", err) @@ -180,7 +180,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run! b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -200,10 +200,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return vmwcommon.NewArtifact(b.config.RemoteType, b.config.Format, exportOutputPath, b.config.VMName, b.config.SkipExport, b.config.KeepRegistered, state) } - -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/builder/vmware/iso/step_create_vmx_test.go b/builder/vmware/iso/step_create_vmx_test.go index 301b44c97..e9f32e217 100644 --- a/builder/vmware/iso/step_create_vmx_test.go +++ b/builder/vmware/iso/step_create_vmx_test.go @@ -2,6 +2,7 @@ package iso import ( "bytes" + "context" "fmt" "io/ioutil" "math" @@ -174,7 +175,7 @@ func setupVMwareBuild(t *testing.T, builderConfig map[string]string, provisioner } // and then finally build it - artifacts, err := b.Run(ui) + artifacts, err := b.Run(context.Background(), ui) if err != nil { t.Fatalf("Failed to build artifact: %s", err) } diff --git a/builder/vmware/vmx/builder.go b/builder/vmware/vmx/builder.go index 40080d078..fd4cfa1b9 100644 --- a/builder/vmware/vmx/builder.go +++ b/builder/vmware/vmx/builder.go @@ -1,6 +1,7 @@ package vmx import ( + "context" "errors" "fmt" "log" @@ -33,7 +34,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { // Run executes a Packer build and returns a packer.Artifact representing // a VMware image. -func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { driver, err := vmwcommon.NewDriver(&b.config.DriverConfig, &b.config.SSHConfig, b.config.VMName) if err != nil { return nil, fmt.Errorf("Failed creating VMware driver: %s", err) @@ -173,7 +174,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { // Run the steps. b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state) - b.runner.Run(state) + b.runner.Run(ctx, state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { @@ -196,9 +197,3 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { } // Cancel. -func (b *Builder) Cancel() { - if b.runner != nil { - log.Println("Cancelling the step runner...") - b.runner.Cancel() - } -} diff --git a/command/build.go b/command/build.go index 87331c3ae..9072b9ea4 100644 --- a/command/build.go +++ b/command/build.go @@ -2,6 +2,7 @@ package command import ( "bytes" + "context" "fmt" "log" "os" @@ -152,7 +153,7 @@ func (c *BuildCommand) Run(args []string) int { for _, b := range builds { // Increment the waitgroup so we wait for this item to finish properly wg.Add(1) - // buildCtx, cancelCtx := ctx.WithCancel() + buildCtx, cancelCtx := context.WithCancel(context.Background()) // Handle interrupts for this build sigCh := make(chan os.Signal, 1) @@ -165,8 +166,7 @@ func (c *BuildCommand) Run(args []string) int { interrupted = true log.Printf("Stopping build: %s after receiving %s", b.Name(), sig) - b.Cancel() - //cancelCtx() + cancelCtx() log.Printf("Build cancelled: %s", b.Name()) }(b) @@ -177,7 +177,7 @@ func (c *BuildCommand) Run(args []string) int { name := b.Name() log.Printf("Starting build run: %s", name) ui := buildUis[name] - runArtifacts, err := b.Run(ui) + runArtifacts, err := b.Run(buildCtx, ui) if err != nil { ui.Error(fmt.Sprintf("Build '%s' errored: %s", name, err)) diff --git a/helper/builder/testing/testing.go b/helper/builder/testing/testing.go index d760de033..223615fa3 100644 --- a/helper/builder/testing/testing.go +++ b/helper/builder/testing/testing.go @@ -1,6 +1,7 @@ package testing import ( + "context" "fmt" "io/ioutil" "log" @@ -150,7 +151,7 @@ func Test(t TestT, c TestCase) { Writer: ioutil.Discard, ErrorWriter: ioutil.Discard, } - artifacts, err := build.Run(ui) + artifacts, err := build.Run(context.Background(), ui) if err != nil { t.Fatal(fmt.Sprintf("Run error:\n\n%s", err)) goto TEARDOWN diff --git a/packer/build.go b/packer/build.go index bc78c4f0f..7585b97bf 100644 --- a/packer/build.go +++ b/packer/build.go @@ -1,6 +1,7 @@ package packer import ( + "context" "fmt" "log" "sync" @@ -53,11 +54,8 @@ type Build interface { // Run runs the actual builder, returning an artifact implementation // of what is built. If anything goes wrong, an error is returned. - Run(Ui) ([]Artifact, error) - - // Cancel will cancel a running build. This will block until the build - // is actually completely canceled. - Cancel() + // Run can be context cancelled. + Run(context.Context, Ui) ([]Artifact, error) // SetDebug will enable/disable debug mode. Debug mode is always // enabled by adding the additional key "packer_debug" to boolean @@ -180,7 +178,7 @@ func (b *coreBuild) Prepare() (warn []string, err error) { } // Runs the actual build. Prepare must be called prior to running this. -func (b *coreBuild) Run(originalUi Ui) ([]Artifact, error) { +func (b *coreBuild) Run(ctx context.Context, originalUi Ui) ([]Artifact, error) { if !b.prepareCalled { panic("Prepare must be called first") } @@ -235,7 +233,7 @@ func (b *coreBuild) Run(originalUi Ui) ([]Artifact, error) { log.Printf("Running builder: %s", b.builderType) ts := CheckpointReporter.AddSpan(b.builderType, "builder", b.builderConfig) - builderArtifact, err := b.builder.Run(builderUi, hook) + builderArtifact, err := b.builder.Run(ctx, builderUi, hook) ts.End(err) if err != nil { return nil, err @@ -348,8 +346,3 @@ func (b *coreBuild) SetOnError(val string) { b.onError = val } - -// Cancels the build if it is running. -func (b *coreBuild) Cancel() { - b.builder.Cancel() -} diff --git a/packer/build_test.go b/packer/build_test.go index 686f9ba40..c581f2a2f 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -1,6 +1,7 @@ package packer import ( + "context" "reflect" "testing" ) @@ -176,7 +177,8 @@ func TestBuild_Run(t *testing.T) { build := testBuild() build.Prepare() - artifacts, err := build.Run(ui) + ctx := context.Background() + artifacts, err := build.Run(ctx, ui) if err != nil { t.Fatalf("err: %s", err) } @@ -225,7 +227,7 @@ func TestBuild_Run_Artifacts(t *testing.T) { build.postProcessors = [][]coreBuildPostProcessor{} build.Prepare() - artifacts, err := build.Run(ui) + artifacts, err := build.Run(context.Background(), ui) if err != nil { t.Fatalf("err: %s", err) } @@ -250,7 +252,7 @@ func TestBuild_Run_Artifacts(t *testing.T) { } build.Prepare() - artifacts, err = build.Run(ui) + artifacts, err = build.Run(context.Background(), ui) if err != nil { t.Fatalf("err: %s", err) } @@ -278,7 +280,7 @@ func TestBuild_Run_Artifacts(t *testing.T) { } build.Prepare() - artifacts, err = build.Run(ui) + artifacts, err = build.Run(context.Background(), ui) if err != nil { t.Fatalf("err: %s", err) } @@ -308,7 +310,7 @@ func TestBuild_Run_Artifacts(t *testing.T) { } build.Prepare() - artifacts, err = build.Run(ui) + artifacts, err = build.Run(context.Background(), ui) if err != nil { t.Fatalf("err: %s", err) } @@ -335,7 +337,7 @@ func TestBuild_Run_Artifacts(t *testing.T) { } build.Prepare() - artifacts, err = build.Run(ui) + artifacts, err = build.Run(context.Background(), ui) if err != nil { t.Fatalf("err: %s", err) } @@ -363,12 +365,15 @@ func TestBuild_RunBeforePrepare(t *testing.T) { } }() - testBuild().Run(testUi()) + testBuild().Run(context.Background(), testUi()) } func TestBuild_Cancel(t *testing.T) { build := testBuild() - build.Cancel() + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + build.Run(ctx, nil) builder := build.builder.(*MockBuilder) if !builder.CancelCalled { diff --git a/packer/core_test.go b/packer/core_test.go index 1d6c87e4c..52251c9fd 100644 --- a/packer/core_test.go +++ b/packer/core_test.go @@ -1,6 +1,7 @@ package packer import ( + "context" "os" "path/filepath" "reflect" @@ -67,7 +68,7 @@ func TestCoreBuild_basic(t *testing.T) { t.Fatalf("err: %s", err) } - artifact, err := build.Run(nil) + artifact, err := build.Run(context.Background(), nil) if err != nil { t.Fatalf("err: %s", err) } @@ -97,7 +98,7 @@ func TestCoreBuild_basicInterpolated(t *testing.T) { t.Fatalf("err: %s", err) } - artifact, err := build.Run(nil) + artifact, err := build.Run(context.Background(), nil) if err != nil { t.Fatalf("err: %s", err) } @@ -230,7 +231,7 @@ func TestCoreBuild_prov(t *testing.T) { t.Fatalf("err: %s", err) } - artifact, err := build.Run(nil) + artifact, err := build.Run(context.Background(), nil) if err != nil { t.Fatalf("err: %s", err) } @@ -264,7 +265,7 @@ func TestCoreBuild_provSkip(t *testing.T) { t.Fatalf("err: %s", err) } - artifact, err := build.Run(nil) + artifact, err := build.Run(context.Background(), nil) if err != nil { t.Fatalf("err: %s", err) } @@ -298,7 +299,7 @@ func TestCoreBuild_provSkipInclude(t *testing.T) { t.Fatalf("err: %s", err) } - artifact, err := build.Run(nil) + artifact, err := build.Run(context.Background(), nil) if err != nil { t.Fatalf("err: %s", err) } @@ -332,7 +333,7 @@ func TestCoreBuild_provOverride(t *testing.T) { t.Fatalf("err: %s", err) } - artifact, err := build.Run(nil) + artifact, err := build.Run(context.Background(), nil) if err != nil { t.Fatalf("err: %s", err) } @@ -381,7 +382,7 @@ func TestCoreBuild_postProcess(t *testing.T) { t.Fatalf("err: %s", err) } - artifact, err := build.Run(ui) + artifact, err := build.Run(context.Background(), ui) if err != nil { t.Fatalf("err: %s", err) } diff --git a/packer/plugin/builder.go b/packer/plugin/builder.go index 360294fd1..44af7dd67 100644 --- a/packer/plugin/builder.go +++ b/packer/plugin/builder.go @@ -1,6 +1,7 @@ package plugin import ( + "context" "log" "github.com/hashicorp/packer/packer" @@ -20,22 +21,13 @@ func (b *cmdBuilder) Prepare(config ...interface{}) ([]string, error) { return b.builder.Prepare(config...) } -func (b *cmdBuilder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *cmdBuilder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { defer func() { r := recover() b.checkExit(r, nil) }() - return b.builder.Run(ui, hook) -} - -func (b *cmdBuilder) Cancel() { - defer func() { - r := recover() - b.checkExit(r, nil) - }() - - b.builder.Cancel() + return b.builder.Run(ctx, ui, hook) } func (c *cmdBuilder) checkExit(p interface{}, cb func()) { diff --git a/packer/rpc/build.go b/packer/rpc/build.go index 87c82cfbb..916ea7138 100644 --- a/packer/rpc/build.go +++ b/packer/rpc/build.go @@ -1,6 +1,7 @@ package rpc import ( + "context" "net/rpc" "github.com/hashicorp/packer/packer" @@ -43,7 +44,7 @@ func (b *build) Prepare() ([]string, error) { return resp.Warnings, err } -func (b *build) Run(ui packer.Ui) ([]packer.Artifact, error) { +func (b *build) Run(ctx context.Context, ui packer.Ui) ([]packer.Artifact, error) { nextId := b.mux.NextId() server := newServerWithMux(b.mux, nextId) server.RegisterUi(ui) @@ -105,14 +106,14 @@ func (b *BuildServer) Prepare(args *interface{}, resp *BuildPrepareResponse) err return nil } -func (b *BuildServer) Run(streamId uint32, reply *[]uint32) error { +func (b *BuildServer) Run(ctx context.Context, streamId uint32, reply *[]uint32) error { client, err := newClientWithMux(b.mux, streamId) if err != nil { return NewBasicError(err) } defer client.Close() - artifacts, err := b.build.Run(client.Ui()) + artifacts, err := b.build.Run(ctx, client.Ui()) if err != nil { return NewBasicError(err) } @@ -146,6 +147,6 @@ func (b *BuildServer) SetOnError(val *string, reply *interface{}) error { } func (b *BuildServer) Cancel(args *interface{}, reply *interface{}) error { - b.build.Cancel() + panic("cancel !") return nil } diff --git a/packer/rpc/build_test.go b/packer/rpc/build_test.go index 523baceea..4084c04ec 100644 --- a/packer/rpc/build_test.go +++ b/packer/rpc/build_test.go @@ -1,6 +1,7 @@ package rpc import ( + "context" "errors" "reflect" "testing" @@ -34,7 +35,11 @@ func (b *testBuild) Prepare() ([]string, error) { return b.prepareWarnings, nil } -func (b *testBuild) Run(ui packer.Ui) ([]packer.Artifact, error) { +func (b *testBuild) Run(ctx context.Context, ui packer.Ui) ([]packer.Artifact, error) { + go func() { + <-ctx.Done() + b.cancelCalled = true + }() b.runCalled = true b.runUi = ui @@ -69,6 +74,8 @@ func TestBuild(t *testing.T) { server.RegisterBuild(b) bClient := client.Build() + ctx, cancel := context.WithCancel(context.Background()) + // Test Name bClient.Name() if !b.nameCalled { @@ -83,7 +90,7 @@ func TestBuild(t *testing.T) { // Test Run ui := new(testUi) - artifacts, err := bClient.Run(ui) + artifacts, err := bClient.Run(ctx, ui) if !b.runCalled { t.Fatal("run should be called") } @@ -102,7 +109,7 @@ func TestBuild(t *testing.T) { // Test run with an error b.errRunResult = true - _, err = bClient.Run(ui) + _, err = bClient.Run(ctx, ui) if err == nil { t.Fatal("should error") } @@ -126,7 +133,7 @@ func TestBuild(t *testing.T) { } // Test Cancel - bClient.Cancel() + cancel() if !b.cancelCalled { t.Fatal("should be called") } diff --git a/packer/rpc/builder.go b/packer/rpc/builder.go index fc68313b9..b8ba27b35 100644 --- a/packer/rpc/builder.go +++ b/packer/rpc/builder.go @@ -1,7 +1,7 @@ package rpc import ( - "log" + "context" "net/rpc" "github.com/hashicorp/packer/packer" @@ -44,7 +44,7 @@ func (b *builder) Prepare(config ...interface{}) ([]string, error) { return resp.Warnings, err } -func (b *builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { +func (b *builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { nextId := b.mux.NextId() server := newServerWithMux(b.mux, nextId) server.RegisterHook(hook) @@ -68,12 +68,6 @@ func (b *builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) { return client.Artifact(), nil } -func (b *builder) Cancel() { - if err := b.client.Call("Builder.Cancel", new(interface{}), new(interface{})); err != nil { - log.Printf("Error cancelling builder: %s", err) - } -} - func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareResponse) error { warnings, err := b.builder.Prepare(args.Configs...) *reply = BuilderPrepareResponse{ @@ -83,14 +77,14 @@ func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareR return nil } -func (b *BuilderServer) Run(streamId uint32, reply *uint32) error { +func (b *BuilderServer) Run(ctx context.Context, streamId uint32, reply *uint32) error { client, err := newClientWithMux(b.mux, streamId) if err != nil { return NewBasicError(err) } defer client.Close() - artifact, err := b.builder.Run(client.Ui(), client.Hook()) + artifact, err := b.builder.Run(ctx, client.Ui(), client.Hook()) if err != nil { return NewBasicError(err) } @@ -106,8 +100,3 @@ func (b *BuilderServer) Run(streamId uint32, reply *uint32) error { return nil } - -func (b *BuilderServer) Cancel(args *interface{}, reply *interface{}) error { - b.builder.Cancel() - return nil -} diff --git a/packer/rpc/builder_test.go b/packer/rpc/builder_test.go index 3fb496f5c..bbea2bb79 100644 --- a/packer/rpc/builder_test.go +++ b/packer/rpc/builder_test.go @@ -1,6 +1,7 @@ package rpc import ( + "context" "reflect" "testing" @@ -69,7 +70,7 @@ func TestBuilderRun(t *testing.T) { // Test Run hook := &packer.MockHook{} ui := &testUi{} - artifact, err := bClient.Run(ui, hook) + artifact, err := bClient.Run(context.Background(), ui, hook) if err != nil { t.Fatalf("err: %s", err) } @@ -95,7 +96,7 @@ func TestBuilderRun_nilResult(t *testing.T) { hook := &packer.MockHook{} ui := &testUi{} - artifact, err := bClient.Run(ui, hook) + artifact, err := bClient.Run(context.Background(), ui, hook) if artifact != nil { t.Fatalf("bad: %#v", artifact) } @@ -116,7 +117,7 @@ func TestBuilderRun_ErrResult(t *testing.T) { hook := &packer.MockHook{} ui := &testUi{} - artifact, err := bClient.Run(ui, hook) + artifact, err := bClient.Run(context.Background(), ui, hook) if artifact != nil { t.Fatalf("bad: %#v", artifact) } @@ -133,7 +134,10 @@ func TestBuilderCancel(t *testing.T) { server.RegisterBuilder(b) bClient := client.Builder() - bClient.Cancel() + ctx, cancel := context.WithCancel(context.Background()) + cancel() + bClient.Run(ctx, nil, nil) + if !b.CancelCalled { t.Fatal("cancel should be called") }