From e65115a7a05a11573c264c1db0a08f07cae54498 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 22 Mar 2019 14:56:02 +0100 Subject: [PATCH] contextualize post-processor --- packer/build.go | 2 +- packer/plugin/post_processor.go | 5 +++-- packer/plugin/post_processor_test.go | 3 ++- packer/post_processor.go | 4 +++- packer/post_processor_mock.go | 4 +++- packer/rpc/post_processor.go | 7 ++++--- packer/rpc/post_processor_test.go | 5 +++-- post-processor/alicloud-import/post-processor.go | 3 ++- post-processor/amazon-import/post-processor.go | 3 ++- post-processor/artifice/post-processor.go | 3 ++- post-processor/checksum/post-processor.go | 3 ++- post-processor/checksum/post-processor_test.go | 5 +++-- post-processor/compress/benchmark.go | 1 + post-processor/compress/post-processor.go | 3 ++- post-processor/compress/post-processor_test.go | 5 +++-- .../digitalocean-import/post-processor.go | 5 +++-- post-processor/docker-import/post-processor.go | 3 ++- post-processor/docker-push/post-processor.go | 7 ++++--- post-processor/docker-push/post-processor_test.go | 9 +++++---- post-processor/docker-save/post-processor.go | 7 ++++--- post-processor/docker-tag/post-processor.go | 5 +++-- post-processor/docker-tag/post-processor_test.go | 7 ++++--- .../googlecompute-export/post-processor.go | 5 +++-- .../googlecompute-import/post-processor.go | 3 ++- post-processor/manifest/post-processor.go | 3 ++- post-processor/shell-local/post-processor.go | 4 +++- post-processor/vagrant-cloud/post-processor.go | 14 +++----------- post-processor/vagrant/post-processor.go | 3 ++- post-processor/vagrant/post-processor_test.go | 5 +++-- post-processor/vsphere-template/post-processor.go | 4 ++-- post-processor/vsphere/post-processor.go | 3 ++- 31 files changed, 83 insertions(+), 60 deletions(-) diff --git a/packer/build.go b/packer/build.go index 7585b97bf..b82da85ef 100644 --- a/packer/build.go +++ b/packer/build.go @@ -260,7 +260,7 @@ PostProcessorRunSeqLoop: builderUi.Say(fmt.Sprintf("Running post-processor: %s", corePP.processorType)) ts := CheckpointReporter.AddSpan(corePP.processorType, "post-processor", corePP.config) - artifact, keep, err := corePP.processor.PostProcess(ppUi, priorArtifact) + artifact, keep, err := corePP.processor.PostProcess(ctx, ppUi, priorArtifact) ts.End(err) if err != nil { errors = append(errors, fmt.Errorf("Post-processor failed: %s", err)) diff --git a/packer/plugin/post_processor.go b/packer/plugin/post_processor.go index b65c76623..478906af2 100644 --- a/packer/plugin/post_processor.go +++ b/packer/plugin/post_processor.go @@ -1,6 +1,7 @@ package plugin import ( + "context" "log" "github.com/hashicorp/packer/packer" @@ -20,13 +21,13 @@ func (c *cmdPostProcessor) Configure(config ...interface{}) error { return c.p.Configure(config...) } -func (c *cmdPostProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) { +func (c *cmdPostProcessor) PostProcess(ctx context.Context, ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) { defer func() { r := recover() c.checkExit(r, nil) }() - return c.p.PostProcess(ui, a) + return c.p.PostProcess(ctx, ui, a) } func (c *cmdPostProcessor) checkExit(p interface{}, cb func()) { diff --git a/packer/plugin/post_processor_test.go b/packer/plugin/post_processor_test.go index a1e2f0f65..66c774ac6 100644 --- a/packer/plugin/post_processor_test.go +++ b/packer/plugin/post_processor_test.go @@ -1,6 +1,7 @@ package plugin import ( + "context" "os/exec" "testing" @@ -13,7 +14,7 @@ func (helperPostProcessor) Configure(...interface{}) error { return nil } -func (helperPostProcessor) PostProcess(packer.Ui, packer.Artifact) (packer.Artifact, bool, error) { +func (helperPostProcessor) PostProcess(context.Context, packer.Ui, packer.Artifact) (packer.Artifact, bool, error) { return nil, false, nil } diff --git a/packer/post_processor.go b/packer/post_processor.go index 3def4a0dd..93c3f4231 100644 --- a/packer/post_processor.go +++ b/packer/post_processor.go @@ -1,5 +1,7 @@ package packer +import "context" + // A PostProcessor is responsible for taking an artifact of a build // and doing some sort of post-processing to turn this into another // artifact. An example of a post-processor would be something that takes @@ -14,5 +16,5 @@ type PostProcessor interface { // PostProcess takes a previously created Artifact and produces another // Artifact. If an error occurs, it should return that error. If `keep` // is to true, then the previous artifact is forcibly kept. - PostProcess(Ui, Artifact) (a Artifact, keep bool, err error) + PostProcess(context.Context, Ui, Artifact) (a Artifact, keep bool, err error) } diff --git a/packer/post_processor_mock.go b/packer/post_processor_mock.go index 591e4b876..e7d365cca 100644 --- a/packer/post_processor_mock.go +++ b/packer/post_processor_mock.go @@ -1,5 +1,7 @@ package packer +import "context" + // MockPostProcessor is an implementation of PostProcessor that can be // used for tests. type MockPostProcessor struct { @@ -22,7 +24,7 @@ func (t *MockPostProcessor) Configure(configs ...interface{}) error { return t.ConfigureError } -func (t *MockPostProcessor) PostProcess(ui Ui, a Artifact) (Artifact, bool, error) { +func (t *MockPostProcessor) PostProcess(ctx context.Context, ui Ui, a Artifact) (Artifact, bool, error) { t.PostProcessCalled = true t.PostProcessArtifact = a t.PostProcessUi = ui diff --git a/packer/rpc/post_processor.go b/packer/rpc/post_processor.go index 0107558de..1d068ae50 100644 --- a/packer/rpc/post_processor.go +++ b/packer/rpc/post_processor.go @@ -1,6 +1,7 @@ package rpc import ( + "context" "net/rpc" "github.com/hashicorp/packer/packer" @@ -39,7 +40,7 @@ func (p *postProcessor) Configure(raw ...interface{}) (err error) { return } -func (p *postProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) { +func (p *postProcessor) PostProcess(ctx context.Context, ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) { nextId := p.mux.NextId() server := newServerWithMux(p.mux, nextId) server.RegisterArtifact(a) @@ -72,7 +73,7 @@ func (p *PostProcessorServer) Configure(args *PostProcessorConfigureArgs, reply return err } -func (p *PostProcessorServer) PostProcess(streamId uint32, reply *PostProcessorProcessResponse) error { +func (p *PostProcessorServer) PostProcess(ctx context.Context, streamId uint32, reply *PostProcessorProcessResponse) error { client, err := newClientWithMux(p.mux, streamId) if err != nil { return NewBasicError(err) @@ -80,7 +81,7 @@ func (p *PostProcessorServer) PostProcess(streamId uint32, reply *PostProcessorP defer client.Close() streamId = 0 - artifactResult, keep, err := p.p.PostProcess(client.Ui(), client.Artifact()) + artifactResult, keep, err := p.p.PostProcess(ctx, client.Ui(), client.Artifact()) if err == nil && artifactResult != nil { streamId = p.mux.NextId() server := newServerWithMux(p.mux, streamId) diff --git a/packer/rpc/post_processor_test.go b/packer/rpc/post_processor_test.go index 683b6dc16..9858608c4 100644 --- a/packer/rpc/post_processor_test.go +++ b/packer/rpc/post_processor_test.go @@ -1,6 +1,7 @@ package rpc import ( + "context" "reflect" "testing" @@ -24,7 +25,7 @@ func (pp *TestPostProcessor) Configure(v ...interface{}) error { return nil } -func (pp *TestPostProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) { +func (pp *TestPostProcessor) PostProcess(ctx context.Context, ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) { pp.ppCalled = true pp.ppArtifact = a pp.ppArtifactId = a.Id() @@ -65,7 +66,7 @@ func TestPostProcessorRPC(t *testing.T) { IdValue: "ppTestId", } ui := new(testUi) - artifact, _, err := ppClient.PostProcess(ui, a) + artifact, _, err := ppClient.PostProcess(context.Background(), ui, a) if err != nil { t.Fatalf("err: %s", err) } diff --git a/post-processor/alicloud-import/post-processor.go b/post-processor/alicloud-import/post-processor.go index 57344321c..0b89b5038 100644 --- a/post-processor/alicloud-import/post-processor.go +++ b/post-processor/alicloud-import/post-processor.go @@ -1,6 +1,7 @@ package alicloudimport import ( + "context" "fmt" "log" "strconv" @@ -118,7 +119,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { var err error // Render this key since we didn't in the configure phase diff --git a/post-processor/amazon-import/post-processor.go b/post-processor/amazon-import/post-processor.go index 2fbab390d..19c9d3a82 100644 --- a/post-processor/amazon-import/post-processor.go +++ b/post-processor/amazon-import/post-processor.go @@ -1,6 +1,7 @@ package amazonimport import ( + "context" "fmt" "log" "os" @@ -119,7 +120,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { var err error session, err := p.config.Session() diff --git a/post-processor/artifice/post-processor.go b/post-processor/artifice/post-processor.go index 030b52c07..39595fdc9 100644 --- a/post-processor/artifice/post-processor.go +++ b/post-processor/artifice/post-processor.go @@ -1,6 +1,7 @@ package artifice import ( + "context" "fmt" "strings" @@ -48,7 +49,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { if len(artifact.Files()) > 0 { ui.Say(fmt.Sprintf("Discarding artifact files: %s", strings.Join(artifact.Files(), ", "))) } diff --git a/post-processor/checksum/post-processor.go b/post-processor/checksum/post-processor.go index 70295c878..dcfc63053 100644 --- a/post-processor/checksum/post-processor.go +++ b/post-processor/checksum/post-processor.go @@ -1,6 +1,7 @@ package checksum import ( + "context" "crypto/md5" "crypto/sha1" "crypto/sha256" @@ -95,7 +96,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { files := artifact.Files() var h hash.Hash diff --git a/post-processor/checksum/post-processor_test.go b/post-processor/checksum/post-processor_test.go index f2b91aca9..8f80adc6f 100644 --- a/post-processor/checksum/post-processor_test.go +++ b/post-processor/checksum/post-processor_test.go @@ -2,6 +2,7 @@ package checksum import ( "bytes" + "context" "fmt" "io/ioutil" "os" @@ -67,7 +68,7 @@ func setup(t *testing.T) (packer.Ui, packer.Artifact, error) { } // Run the file builder - artifact, err := builder.Run(ui, nil) + artifact, err := builder.Run(context.Background(), ui, nil) if err != nil { return nil, nil, fmt.Errorf("Failed to build artifact: %s", err) } @@ -98,7 +99,7 @@ func testChecksum(t *testing.T, config string) packer.Artifact { checksum.config.PackerBuildName = "vanilla" checksum.config.PackerBuilderType = "file" - artifactOut, _, err := checksum.PostProcess(ui, artifact) + artifactOut, _, err := checksum.PostProcess(context.Background(), ui, artifact) if err != nil { t.Fatalf("Failed to checksum artifact: %s", err) } diff --git a/post-processor/compress/benchmark.go b/post-processor/compress/benchmark.go index 3b97ad342..185a23f05 100644 --- a/post-processor/compress/benchmark.go +++ b/post-processor/compress/benchmark.go @@ -15,6 +15,7 @@ import ( "github.com/biogo/hts/bgzf" "github.com/klauspost/pgzip" "github.com/pierrec/lz4" + "github.com/ulikunitz/xz" ) type Compressor struct { diff --git a/post-processor/compress/post-processor.go b/post-processor/compress/post-processor.go index 5ac213a76..ee73b1f91 100644 --- a/post-processor/compress/post-processor.go +++ b/post-processor/compress/post-processor.go @@ -4,6 +4,7 @@ import ( "archive/tar" "archive/zip" "compress/gzip" + "context" "fmt" "io" "os" @@ -100,7 +101,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { // These are extra variables that will be made available for interpolation. p.config.ctx.Data = map[string]string{ diff --git a/post-processor/compress/post-processor_test.go b/post-processor/compress/post-processor_test.go index a3c6a8232..7b7384a17 100644 --- a/post-processor/compress/post-processor_test.go +++ b/post-processor/compress/post-processor_test.go @@ -2,6 +2,7 @@ package compress import ( "compress/gzip" + "context" "fmt" "io/ioutil" "os" @@ -207,7 +208,7 @@ func setup(t *testing.T) (packer.Ui, packer.Artifact, error) { } // Run the file builder - artifact, err := builder.Run(ui, nil) + artifact, err := builder.Run(context.Background(), ui, nil) if err != nil { return nil, nil, fmt.Errorf("Failed to build artifact: %s", err) } @@ -238,7 +239,7 @@ func testArchive(t *testing.T, config string) packer.Artifact { compressor.config.PackerBuildName = "vanilla" compressor.config.PackerBuilderType = "file" - artifactOut, _, err := compressor.PostProcess(ui, artifact) + artifactOut, _, err := compressor.PostProcess(context.Background(), ui, artifact) if err != nil { t.Fatalf("Failed to compress artifact: %s", err) } diff --git a/post-processor/digitalocean-import/post-processor.go b/post-processor/digitalocean-import/post-processor.go index d7dcd867b..00c7dd463 100644 --- a/post-processor/digitalocean-import/post-processor.go +++ b/post-processor/digitalocean-import/post-processor.go @@ -3,12 +3,13 @@ package digitaloceanimport import ( "context" "fmt" - "golang.org/x/oauth2" "log" "os" "strings" "time" + "golang.org/x/oauth2" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" @@ -136,7 +137,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { var err error p.config.ObjectName, err = interpolate.Render(p.config.ObjectName, &p.config.ctx) diff --git a/post-processor/docker-import/post-processor.go b/post-processor/docker-import/post-processor.go index eac920f06..c561ee9f7 100644 --- a/post-processor/docker-import/post-processor.go +++ b/post-processor/docker-import/post-processor.go @@ -1,6 +1,7 @@ package dockerimport import ( + "context" "fmt" "github.com/hashicorp/packer/builder/docker" @@ -43,7 +44,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { switch artifact.BuilderId() { case docker.BuilderId, artifice.BuilderId: break diff --git a/post-processor/docker-push/post-processor.go b/post-processor/docker-push/post-processor.go index 2ffc3e5a5..2136555bf 100644 --- a/post-processor/docker-push/post-processor.go +++ b/post-processor/docker-push/post-processor.go @@ -1,14 +1,15 @@ package dockerpush import ( + "context" "fmt" "github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/post-processor/docker-import" - "github.com/hashicorp/packer/post-processor/docker-tag" + dockerimport "github.com/hashicorp/packer/post-processor/docker-import" + dockertag "github.com/hashicorp/packer/post-processor/docker-tag" "github.com/hashicorp/packer/template/interpolate" ) @@ -51,7 +52,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { if artifact.BuilderId() != dockerimport.BuilderId && artifact.BuilderId() != dockertag.BuilderId { err := fmt.Errorf( diff --git a/post-processor/docker-push/post-processor_test.go b/post-processor/docker-push/post-processor_test.go index 1eee102cb..24e595507 100644 --- a/post-processor/docker-push/post-processor_test.go +++ b/post-processor/docker-push/post-processor_test.go @@ -2,11 +2,12 @@ package dockerpush import ( "bytes" + "context" "testing" "github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/post-processor/docker-import" + dockerimport "github.com/hashicorp/packer/post-processor/docker-import" ) func testConfig() map[string]interface{} { @@ -41,7 +42,7 @@ func TestPostProcessor_PostProcess(t *testing.T) { IdValue: "foo/bar", } - result, keep, err := p.PostProcess(testUi(), artifact) + result, keep, err := p.PostProcess(context.Background(), testUi(), artifact) if _, ok := result.(packer.Artifact); !ok { t.Fatal("should be instance of Artifact") } @@ -71,7 +72,7 @@ func TestPostProcessor_PostProcess_portInName(t *testing.T) { IdValue: "localhost:5000/foo/bar", } - result, keep, err := p.PostProcess(testUi(), artifact) + result, keep, err := p.PostProcess(context.Background(), testUi(), artifact) if _, ok := result.(packer.Artifact); !ok { t.Fatal("should be instance of Artifact") } @@ -101,7 +102,7 @@ func TestPostProcessor_PostProcess_tags(t *testing.T) { IdValue: "hashicorp/ubuntu:precise", } - result, keep, err := p.PostProcess(testUi(), artifact) + result, keep, err := p.PostProcess(context.Background(), testUi(), artifact) if _, ok := result.(packer.Artifact); !ok { t.Fatal("should be instance of Artifact") } diff --git a/post-processor/docker-save/post-processor.go b/post-processor/docker-save/post-processor.go index 6cb348bf8..4b16cd68f 100644 --- a/post-processor/docker-save/post-processor.go +++ b/post-processor/docker-save/post-processor.go @@ -1,6 +1,7 @@ package dockersave import ( + "context" "fmt" "os" @@ -8,8 +9,8 @@ import ( "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/post-processor/docker-import" - "github.com/hashicorp/packer/post-processor/docker-tag" + dockerimport "github.com/hashicorp/packer/post-processor/docker-import" + dockertag "github.com/hashicorp/packer/post-processor/docker-tag" "github.com/hashicorp/packer/template/interpolate" ) @@ -45,7 +46,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { if artifact.BuilderId() != dockerimport.BuilderId && artifact.BuilderId() != dockertag.BuilderId { err := fmt.Errorf( diff --git a/post-processor/docker-tag/post-processor.go b/post-processor/docker-tag/post-processor.go index e9311d12f..1b7e60733 100644 --- a/post-processor/docker-tag/post-processor.go +++ b/post-processor/docker-tag/post-processor.go @@ -1,13 +1,14 @@ package dockertag import ( + "context" "fmt" "github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/post-processor/docker-import" + dockerimport "github.com/hashicorp/packer/post-processor/docker-import" "github.com/hashicorp/packer/template/interpolate" ) @@ -45,7 +46,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { if artifact.BuilderId() != BuilderId && artifact.BuilderId() != dockerimport.BuilderId { err := fmt.Errorf( diff --git a/post-processor/docker-tag/post-processor_test.go b/post-processor/docker-tag/post-processor_test.go index fef434ee9..7c376c6cc 100644 --- a/post-processor/docker-tag/post-processor_test.go +++ b/post-processor/docker-tag/post-processor_test.go @@ -2,11 +2,12 @@ package dockertag import ( "bytes" + "context" "testing" "github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/packer" - "github.com/hashicorp/packer/post-processor/docker-import" + dockerimport "github.com/hashicorp/packer/post-processor/docker-import" ) func testConfig() map[string]interface{} { @@ -48,7 +49,7 @@ func TestPostProcessor_PostProcess(t *testing.T) { IdValue: "1234567890abcdef", } - result, keep, err := p.PostProcess(testUi(), artifact) + result, keep, err := p.PostProcess(context.Background(), testUi(), artifact) if _, ok := result.(packer.Artifact); !ok { t.Fatal("should be instance of Artifact") } @@ -87,7 +88,7 @@ func TestPostProcessor_PostProcess_Force(t *testing.T) { IdValue: "1234567890abcdef", } - result, keep, err := p.PostProcess(testUi(), artifact) + result, keep, err := p.PostProcess(context.Background(), testUi(), artifact) if _, ok := result.(packer.Artifact); !ok { t.Fatal("should be instance of Artifact") } diff --git a/post-processor/googlecompute-export/post-processor.go b/post-processor/googlecompute-export/post-processor.go index de56eb7cb..aacb1de6f 100644 --- a/post-processor/googlecompute-export/post-processor.go +++ b/post-processor/googlecompute-export/post-processor.go @@ -1,6 +1,7 @@ package googlecomputeexport import ( + "context" "fmt" "strings" @@ -75,7 +76,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { if artifact.BuilderId() != googlecompute.BuilderId { err := fmt.Errorf( "Unknown artifact type: %s\nCan only export from Google Compute Engine builder artifacts.", @@ -165,7 +166,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac // Run the steps. p.runner = common.NewRunner(steps, p.config.PackerConfig, ui) - p.runner.Run(state) + p.runner.Run(ctx, state) result := &Artifact{paths: p.config.Paths} diff --git a/post-processor/googlecompute-import/post-processor.go b/post-processor/googlecompute-import/post-processor.go index 4dfb1fee9..0f393791c 100644 --- a/post-processor/googlecompute-import/post-processor.go +++ b/post-processor/googlecompute-import/post-processor.go @@ -1,6 +1,7 @@ package googlecomputeimport import ( + "context" "fmt" "net/http" "os" @@ -94,7 +95,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { client, err := googlecompute.NewClientGCE(&p.config.Account) if err != nil { return nil, false, err diff --git a/post-processor/manifest/post-processor.go b/post-processor/manifest/post-processor.go index 8833f2abe..0f8966968 100644 --- a/post-processor/manifest/post-processor.go +++ b/post-processor/manifest/post-processor.go @@ -1,6 +1,7 @@ package manifest import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -56,7 +57,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, source packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, source packer.Artifact) (packer.Artifact, bool, error) { artifact := &Artifact{} var err error diff --git a/post-processor/shell-local/post-processor.go b/post-processor/shell-local/post-processor.go index c761a19f4..af468e2b2 100644 --- a/post-processor/shell-local/post-processor.go +++ b/post-processor/shell-local/post-processor.go @@ -1,6 +1,8 @@ package shell_local import ( + "context" + sl "github.com/hashicorp/packer/common/shell-local" "github.com/hashicorp/packer/packer" ) @@ -35,7 +37,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return sl.Validate(&p.config) } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { // this particular post-processor doesn't do anything with the artifact // except to return it. diff --git a/post-processor/vagrant-cloud/post-processor.go b/post-processor/vagrant-cloud/post-processor.go index e11e8df8e..799ed0107 100644 --- a/post-processor/vagrant-cloud/post-processor.go +++ b/post-processor/vagrant-cloud/post-processor.go @@ -5,8 +5,8 @@ package vagrantcloud import ( + "context" "fmt" - "log" "os" "strings" @@ -117,7 +117,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { if _, ok := builtins[artifact.BuilderId()]; !ok { return nil, false, fmt.Errorf( "Unknown artifact type, requires box from vagrant post-processor or vagrant builder: %s", artifact.BuilderId()) @@ -177,7 +177,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac // Run the steps p.runner = common.NewRunner(steps, p.config.PackerConfig, ui) - p.runner.Run(state) + p.runner.Run(ctx, state) // If there was an error, return that if rawErr, ok := state.GetOk("error"); ok { @@ -187,14 +187,6 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac return NewArtifact(providerName, p.config.Tag), true, nil } -// Runs a cleanup if the post processor fails to upload -func (p *PostProcessor) Cancel() { - if p.runner != nil { - log.Println("Cancelling the step runner...") - p.runner.Cancel() - } -} - // converts a packer builder name to the corresponding vagrant // provider func providerFromBuilderName(name string) string { diff --git a/post-processor/vagrant/post-processor.go b/post-processor/vagrant/post-processor.go index 72c2ab440..042f2287b 100644 --- a/post-processor/vagrant/post-processor.go +++ b/post-processor/vagrant/post-processor.go @@ -5,6 +5,7 @@ package vagrant import ( "compress/flate" + "context" "fmt" "io/ioutil" "os" @@ -158,7 +159,7 @@ func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui p return NewArtifact(name, outputPath), provider.KeepInputArtifact(), nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { name, ok := builtins[artifact.BuilderId()] if !ok { diff --git a/post-processor/vagrant/post-processor_test.go b/post-processor/vagrant/post-processor_test.go index 8a5368737..6061fd103 100644 --- a/post-processor/vagrant/post-processor_test.go +++ b/post-processor/vagrant/post-processor_test.go @@ -3,6 +3,7 @@ package vagrant import ( "bytes" "compress/flate" + "context" "io/ioutil" "os" "strings" @@ -151,7 +152,7 @@ func TestPostProcessorPostProcess_badId(t *testing.T) { BuilderIdValue: "invalid.packer", } - _, _, err := testPP(t).PostProcess(testUi(), artifact) + _, _, err := testPP(t).PostProcess(context.Background(), testUi(), artifact) if !strings.Contains(err.Error(), "artifact type") { t.Fatalf("err: %s", err) } @@ -181,7 +182,7 @@ func TestPostProcessorPostProcess_vagrantfileUserVariable(t *testing.T) { a := &packer.MockArtifact{ BuilderIdValue: "packer.parallels", } - a2, _, err := p.PostProcess(testUi(), a) + a2, _, err := p.PostProcess(context.Background(), testUi(), a) if a2 != nil { for _, fn := range a2.Files() { defer os.Remove(fn) diff --git a/post-processor/vsphere-template/post-processor.go b/post-processor/vsphere-template/post-processor.go index 9f0b7f271..9defa78ca 100644 --- a/post-processor/vsphere-template/post-processor.go +++ b/post-processor/vsphere-template/post-processor.go @@ -91,7 +91,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { if _, ok := builtins[artifact.BuilderId()]; !ok { return nil, false, fmt.Errorf("The Packer vSphere Template post-processor "+ "can only take an artifact from the VMware-iso builder, built on "+ @@ -133,7 +133,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac NewStepMarkAsTemplate(artifact), } runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state) - runner.Run(state) + runner.Run(ctx, state) if rawErr, ok := state.GetOk("error"); ok { return nil, false, rawErr.(error) } diff --git a/post-processor/vsphere/post-processor.go b/post-processor/vsphere/post-processor.go index 4102b4138..983e1d628 100644 --- a/post-processor/vsphere/post-processor.go +++ b/post-processor/vsphere/post-processor.go @@ -2,6 +2,7 @@ package vsphere import ( "bytes" + "context" "fmt" "io" "log" @@ -114,7 +115,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return nil } -func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { +func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { if _, ok := builtins[artifact.BuilderId()]; !ok { return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId()) }