contextualize post-processor

This commit is contained in:
Adrien Delorme 2019-03-22 14:56:02 +01:00
parent cccbd7f316
commit e65115a7a0
31 changed files with 83 additions and 60 deletions

View File

@ -260,7 +260,7 @@ PostProcessorRunSeqLoop:
builderUi.Say(fmt.Sprintf("Running post-processor: %s", corePP.processorType)) builderUi.Say(fmt.Sprintf("Running post-processor: %s", corePP.processorType))
ts := CheckpointReporter.AddSpan(corePP.processorType, "post-processor", corePP.config) 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) ts.End(err)
if err != nil { if err != nil {
errors = append(errors, fmt.Errorf("Post-processor failed: %s", err)) errors = append(errors, fmt.Errorf("Post-processor failed: %s", err))

View File

@ -1,6 +1,7 @@
package plugin package plugin
import ( import (
"context"
"log" "log"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
@ -20,13 +21,13 @@ func (c *cmdPostProcessor) Configure(config ...interface{}) error {
return c.p.Configure(config...) 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() { defer func() {
r := recover() r := recover()
c.checkExit(r, nil) 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()) { func (c *cmdPostProcessor) checkExit(p interface{}, cb func()) {

View File

@ -1,6 +1,7 @@
package plugin package plugin
import ( import (
"context"
"os/exec" "os/exec"
"testing" "testing"
@ -13,7 +14,7 @@ func (helperPostProcessor) Configure(...interface{}) error {
return nil 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 return nil, false, nil
} }

View File

@ -1,5 +1,7 @@
package packer package packer
import "context"
// A PostProcessor is responsible for taking an artifact of a build // A PostProcessor is responsible for taking an artifact of a build
// and doing some sort of post-processing to turn this into another // and doing some sort of post-processing to turn this into another
// artifact. An example of a post-processor would be something that takes // 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 // PostProcess takes a previously created Artifact and produces another
// Artifact. If an error occurs, it should return that error. If `keep` // Artifact. If an error occurs, it should return that error. If `keep`
// is to true, then the previous artifact is forcibly kept. // 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)
} }

View File

@ -1,5 +1,7 @@
package packer package packer
import "context"
// MockPostProcessor is an implementation of PostProcessor that can be // MockPostProcessor is an implementation of PostProcessor that can be
// used for tests. // used for tests.
type MockPostProcessor struct { type MockPostProcessor struct {
@ -22,7 +24,7 @@ func (t *MockPostProcessor) Configure(configs ...interface{}) error {
return t.ConfigureError 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.PostProcessCalled = true
t.PostProcessArtifact = a t.PostProcessArtifact = a
t.PostProcessUi = ui t.PostProcessUi = ui

View File

@ -1,6 +1,7 @@
package rpc package rpc
import ( import (
"context"
"net/rpc" "net/rpc"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
@ -39,7 +40,7 @@ func (p *postProcessor) Configure(raw ...interface{}) (err error) {
return 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() nextId := p.mux.NextId()
server := newServerWithMux(p.mux, nextId) server := newServerWithMux(p.mux, nextId)
server.RegisterArtifact(a) server.RegisterArtifact(a)
@ -72,7 +73,7 @@ func (p *PostProcessorServer) Configure(args *PostProcessorConfigureArgs, reply
return err 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) client, err := newClientWithMux(p.mux, streamId)
if err != nil { if err != nil {
return NewBasicError(err) return NewBasicError(err)
@ -80,7 +81,7 @@ func (p *PostProcessorServer) PostProcess(streamId uint32, reply *PostProcessorP
defer client.Close() defer client.Close()
streamId = 0 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 { if err == nil && artifactResult != nil {
streamId = p.mux.NextId() streamId = p.mux.NextId()
server := newServerWithMux(p.mux, streamId) server := newServerWithMux(p.mux, streamId)

View File

@ -1,6 +1,7 @@
package rpc package rpc
import ( import (
"context"
"reflect" "reflect"
"testing" "testing"
@ -24,7 +25,7 @@ func (pp *TestPostProcessor) Configure(v ...interface{}) error {
return nil 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.ppCalled = true
pp.ppArtifact = a pp.ppArtifact = a
pp.ppArtifactId = a.Id() pp.ppArtifactId = a.Id()
@ -65,7 +66,7 @@ func TestPostProcessorRPC(t *testing.T) {
IdValue: "ppTestId", IdValue: "ppTestId",
} }
ui := new(testUi) ui := new(testUi)
artifact, _, err := ppClient.PostProcess(ui, a) artifact, _, err := ppClient.PostProcess(context.Background(), ui, a)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }

View File

@ -1,6 +1,7 @@
package alicloudimport package alicloudimport
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
@ -118,7 +119,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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 var err error
// Render this key since we didn't in the configure phase // Render this key since we didn't in the configure phase

View File

@ -1,6 +1,7 @@
package amazonimport package amazonimport
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -119,7 +120,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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 var err error
session, err := p.config.Session() session, err := p.config.Session()

View File

@ -1,6 +1,7 @@
package artifice package artifice
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
@ -48,7 +49,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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 { if len(artifact.Files()) > 0 {
ui.Say(fmt.Sprintf("Discarding artifact files: %s", strings.Join(artifact.Files(), ", "))) ui.Say(fmt.Sprintf("Discarding artifact files: %s", strings.Join(artifact.Files(), ", ")))
} }

View File

@ -1,6 +1,7 @@
package checksum package checksum
import ( import (
"context"
"crypto/md5" "crypto/md5"
"crypto/sha1" "crypto/sha1"
"crypto/sha256" "crypto/sha256"
@ -95,7 +96,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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() files := artifact.Files()
var h hash.Hash var h hash.Hash

View File

@ -2,6 +2,7 @@ package checksum
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -67,7 +68,7 @@ func setup(t *testing.T) (packer.Ui, packer.Artifact, error) {
} }
// Run the file builder // Run the file builder
artifact, err := builder.Run(ui, nil) artifact, err := builder.Run(context.Background(), ui, nil)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("Failed to build artifact: %s", err) 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.PackerBuildName = "vanilla"
checksum.config.PackerBuilderType = "file" checksum.config.PackerBuilderType = "file"
artifactOut, _, err := checksum.PostProcess(ui, artifact) artifactOut, _, err := checksum.PostProcess(context.Background(), ui, artifact)
if err != nil { if err != nil {
t.Fatalf("Failed to checksum artifact: %s", err) t.Fatalf("Failed to checksum artifact: %s", err)
} }

View File

@ -15,6 +15,7 @@ import (
"github.com/biogo/hts/bgzf" "github.com/biogo/hts/bgzf"
"github.com/klauspost/pgzip" "github.com/klauspost/pgzip"
"github.com/pierrec/lz4" "github.com/pierrec/lz4"
"github.com/ulikunitz/xz"
) )
type Compressor struct { type Compressor struct {

View File

@ -4,6 +4,7 @@ import (
"archive/tar" "archive/tar"
"archive/zip" "archive/zip"
"compress/gzip" "compress/gzip"
"context"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -100,7 +101,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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. // These are extra variables that will be made available for interpolation.
p.config.ctx.Data = map[string]string{ p.config.ctx.Data = map[string]string{

View File

@ -2,6 +2,7 @@ package compress
import ( import (
"compress/gzip" "compress/gzip"
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -207,7 +208,7 @@ func setup(t *testing.T) (packer.Ui, packer.Artifact, error) {
} }
// Run the file builder // Run the file builder
artifact, err := builder.Run(ui, nil) artifact, err := builder.Run(context.Background(), ui, nil)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("Failed to build artifact: %s", err) 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.PackerBuildName = "vanilla"
compressor.config.PackerBuilderType = "file" compressor.config.PackerBuilderType = "file"
artifactOut, _, err := compressor.PostProcess(ui, artifact) artifactOut, _, err := compressor.PostProcess(context.Background(), ui, artifact)
if err != nil { if err != nil {
t.Fatalf("Failed to compress artifact: %s", err) t.Fatalf("Failed to compress artifact: %s", err)
} }

View File

@ -3,12 +3,13 @@ package digitaloceanimport
import ( import (
"context" "context"
"fmt" "fmt"
"golang.org/x/oauth2"
"log" "log"
"os" "os"
"strings" "strings"
"time" "time"
"golang.org/x/oauth2"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
@ -136,7 +137,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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 var err error
p.config.ObjectName, err = interpolate.Render(p.config.ObjectName, &p.config.ctx) p.config.ObjectName, err = interpolate.Render(p.config.ObjectName, &p.config.ctx)

View File

@ -1,6 +1,7 @@
package dockerimport package dockerimport
import ( import (
"context"
"fmt" "fmt"
"github.com/hashicorp/packer/builder/docker" "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() { switch artifact.BuilderId() {
case docker.BuilderId, artifice.BuilderId: case docker.BuilderId, artifice.BuilderId:
break break

View File

@ -1,14 +1,15 @@
package dockerpush package dockerpush
import ( import (
"context"
"fmt" "fmt"
"github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/builder/docker"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer" "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/post-processor/docker-tag" dockertag "github.com/hashicorp/packer/post-processor/docker-tag"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
) )
@ -51,7 +52,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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 && if artifact.BuilderId() != dockerimport.BuilderId &&
artifact.BuilderId() != dockertag.BuilderId { artifact.BuilderId() != dockertag.BuilderId {
err := fmt.Errorf( err := fmt.Errorf(

View File

@ -2,11 +2,12 @@ package dockerpush
import ( import (
"bytes" "bytes"
"context"
"testing" "testing"
"github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/builder/docker"
"github.com/hashicorp/packer/packer" "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{} { func testConfig() map[string]interface{} {
@ -41,7 +42,7 @@ func TestPostProcessor_PostProcess(t *testing.T) {
IdValue: "foo/bar", 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 { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }
@ -71,7 +72,7 @@ func TestPostProcessor_PostProcess_portInName(t *testing.T) {
IdValue: "localhost:5000/foo/bar", 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 { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }
@ -101,7 +102,7 @@ func TestPostProcessor_PostProcess_tags(t *testing.T) {
IdValue: "hashicorp/ubuntu:precise", 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 { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }

View File

@ -1,6 +1,7 @@
package dockersave package dockersave
import ( import (
"context"
"fmt" "fmt"
"os" "os"
@ -8,8 +9,8 @@ import (
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer" "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/post-processor/docker-tag" dockertag "github.com/hashicorp/packer/post-processor/docker-tag"
"github.com/hashicorp/packer/template/interpolate" "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 && if artifact.BuilderId() != dockerimport.BuilderId &&
artifact.BuilderId() != dockertag.BuilderId { artifact.BuilderId() != dockertag.BuilderId {
err := fmt.Errorf( err := fmt.Errorf(

View File

@ -1,13 +1,14 @@
package dockertag package dockertag
import ( import (
"context"
"fmt" "fmt"
"github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/builder/docker"
"github.com/hashicorp/packer/common" "github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer" "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" "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 && if artifact.BuilderId() != BuilderId &&
artifact.BuilderId() != dockerimport.BuilderId { artifact.BuilderId() != dockerimport.BuilderId {
err := fmt.Errorf( err := fmt.Errorf(

View File

@ -2,11 +2,12 @@ package dockertag
import ( import (
"bytes" "bytes"
"context"
"testing" "testing"
"github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/builder/docker"
"github.com/hashicorp/packer/packer" "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{} { func testConfig() map[string]interface{} {
@ -48,7 +49,7 @@ func TestPostProcessor_PostProcess(t *testing.T) {
IdValue: "1234567890abcdef", IdValue: "1234567890abcdef",
} }
result, keep, err := p.PostProcess(testUi(), artifact) result, keep, err := p.PostProcess(context.Background(), testUi(), artifact)
if _, ok := result.(packer.Artifact); !ok { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }
@ -87,7 +88,7 @@ func TestPostProcessor_PostProcess_Force(t *testing.T) {
IdValue: "1234567890abcdef", IdValue: "1234567890abcdef",
} }
result, keep, err := p.PostProcess(testUi(), artifact) result, keep, err := p.PostProcess(context.Background(), testUi(), artifact)
if _, ok := result.(packer.Artifact); !ok { if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact") t.Fatal("should be instance of Artifact")
} }

View File

@ -1,6 +1,7 @@
package googlecomputeexport package googlecomputeexport
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
@ -75,7 +76,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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 { if artifact.BuilderId() != googlecompute.BuilderId {
err := fmt.Errorf( err := fmt.Errorf(
"Unknown artifact type: %s\nCan only export from Google Compute Engine builder artifacts.", "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. // Run the steps.
p.runner = common.NewRunner(steps, p.config.PackerConfig, ui) p.runner = common.NewRunner(steps, p.config.PackerConfig, ui)
p.runner.Run(state) p.runner.Run(ctx, state)
result := &Artifact{paths: p.config.Paths} result := &Artifact{paths: p.config.Paths}

View File

@ -1,6 +1,7 @@
package googlecomputeimport package googlecomputeimport
import ( import (
"context"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@ -94,7 +95,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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) client, err := googlecompute.NewClientGCE(&p.config.Account)
if err != nil { if err != nil {
return nil, false, err return nil, false, err

View File

@ -1,6 +1,7 @@
package manifest package manifest
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -56,7 +57,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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{} artifact := &Artifact{}
var err error var err error

View File

@ -1,6 +1,8 @@
package shell_local package shell_local
import ( import (
"context"
sl "github.com/hashicorp/packer/common/shell-local" sl "github.com/hashicorp/packer/common/shell-local"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
@ -35,7 +37,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return sl.Validate(&p.config) 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 // this particular post-processor doesn't do anything with the artifact
// except to return it. // except to return it.

View File

@ -5,8 +5,8 @@
package vagrantcloud package vagrantcloud
import ( import (
"context"
"fmt" "fmt"
"log"
"os" "os"
"strings" "strings"
@ -117,7 +117,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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 { if _, ok := builtins[artifact.BuilderId()]; !ok {
return nil, false, fmt.Errorf( return nil, false, fmt.Errorf(
"Unknown artifact type, requires box from vagrant post-processor or vagrant builder: %s", artifact.BuilderId()) "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 // Run the steps
p.runner = common.NewRunner(steps, p.config.PackerConfig, ui) 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 there was an error, return that
if rawErr, ok := state.GetOk("error"); ok { 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 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 // converts a packer builder name to the corresponding vagrant
// provider // provider
func providerFromBuilderName(name string) string { func providerFromBuilderName(name string) string {

View File

@ -5,6 +5,7 @@ package vagrant
import ( import (
"compress/flate" "compress/flate"
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -158,7 +159,7 @@ func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui p
return NewArtifact(name, outputPath), provider.KeepInputArtifact(), nil 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()] name, ok := builtins[artifact.BuilderId()]
if !ok { if !ok {

View File

@ -3,6 +3,7 @@ package vagrant
import ( import (
"bytes" "bytes"
"compress/flate" "compress/flate"
"context"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
@ -151,7 +152,7 @@ func TestPostProcessorPostProcess_badId(t *testing.T) {
BuilderIdValue: "invalid.packer", BuilderIdValue: "invalid.packer",
} }
_, _, err := testPP(t).PostProcess(testUi(), artifact) _, _, err := testPP(t).PostProcess(context.Background(), testUi(), artifact)
if !strings.Contains(err.Error(), "artifact type") { if !strings.Contains(err.Error(), "artifact type") {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -181,7 +182,7 @@ func TestPostProcessorPostProcess_vagrantfileUserVariable(t *testing.T) {
a := &packer.MockArtifact{ a := &packer.MockArtifact{
BuilderIdValue: "packer.parallels", BuilderIdValue: "packer.parallels",
} }
a2, _, err := p.PostProcess(testUi(), a) a2, _, err := p.PostProcess(context.Background(), testUi(), a)
if a2 != nil { if a2 != nil {
for _, fn := range a2.Files() { for _, fn := range a2.Files() {
defer os.Remove(fn) defer os.Remove(fn)

View File

@ -91,7 +91,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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 { if _, ok := builtins[artifact.BuilderId()]; !ok {
return nil, false, fmt.Errorf("The Packer vSphere Template post-processor "+ return nil, false, fmt.Errorf("The Packer vSphere Template post-processor "+
"can only take an artifact from the VMware-iso builder, built on "+ "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), NewStepMarkAsTemplate(artifact),
} }
runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state) runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state)
runner.Run(state) runner.Run(ctx, state)
if rawErr, ok := state.GetOk("error"); ok { if rawErr, ok := state.GetOk("error"); ok {
return nil, false, rawErr.(error) return nil, false, rawErr.(error)
} }

View File

@ -2,6 +2,7 @@ package vsphere
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -114,7 +115,7 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return nil 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 { if _, ok := builtins[artifact.BuilderId()]; !ok {
return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId()) return nil, false, fmt.Errorf("Unknown artifact type, can't build box: %s", artifact.BuilderId())
} }