diff --git a/builder/docker/artifact_import.go b/builder/docker/artifact_import.go index c626a9df3..a53c0a52b 100644 --- a/builder/docker/artifact_import.go +++ b/builder/docker/artifact_import.go @@ -30,19 +30,22 @@ func (a *ImportArtifact) Id() string { } func (a *ImportArtifact) String() string { - tags := a.StateData["docker_tags"] - if tags == nil { - return fmt.Sprintf("Imported Docker image: %s", a.Id()) - } - cast := tags.([]interface{}) - names := []string{} - for _, name := range cast { - if n, ok := name.(string); ok { - names = append(names, n) + var tags []string + switch t := a.StateData["docker_tags"].(type) { + case []string: + tags = t + case []interface{}: + for _, name := range t { + if n, ok := name.(string); ok { + tags = append(tags, n) + } } } - return fmt.Sprintf("Imported Docker image: %s with tags %s", - a.Id(), strings.Join(names, " ")) + if len(tags) > 0 { + return fmt.Sprintf("Imported Docker image: %s with tags %s", + a.Id(), strings.Join(tags, " ")) + } + return fmt.Sprintf("Imported Docker image: %s", a.Id()) } func (a *ImportArtifact) State(name string) interface{} { diff --git a/packer/rpc/post_processor.go b/packer/rpc/post_processor.go index 4b36f2dd4..00d492a0a 100644 --- a/packer/rpc/post_processor.go +++ b/packer/rpc/post_processor.go @@ -99,28 +99,42 @@ func (p *PostProcessorServer) PostProcess(streamId uint32, reply *PostProcessorP if err != nil { return NewBasicError(err) } - defer client.Close() if p.context == nil { p.context, p.contextCancel = context.WithCancel(context.Background()) } - streamId = 0 - artifactResult, keep, forceOverride, err := p.p.PostProcess(p.context, client.Ui(), client.Artifact()) - if err == nil && artifactResult != nil { - streamId = p.mux.NextId() - server := newServerWithMux(p.mux, streamId) - server.RegisterArtifact(artifactResult) - go server.Serve() - } - + artifact := client.Artifact() + artifactResult, keep, forceOverride, err := p.p.PostProcess(p.context, client.Ui(), artifact) *reply = PostProcessorProcessResponse{ Err: NewBasicError(err), Keep: keep, ForceOverride: forceOverride, - StreamId: streamId, + StreamId: 0, + } + if err != nil { + log.Printf("error: %v", err) + client.Close() + return nil } + if artifactResult != artifact { + // Sometimes, the artifact returned by PostProcess is the artifact from + // client.Artifact() and in that case we don't want to close client; + // otherwise the outcome is sort of undetermined. See [GH-9995] for a + // good test file. + defer client.Close() + } + + if artifactResult != nil { + streamId = p.mux.NextId() + reply.StreamId = streamId + server := newServerWithMux(p.mux, streamId) + if err := server.RegisterArtifact(artifactResult); err != nil { + return err + } + go server.Serve() + } return nil } diff --git a/post-processor/docker-push/post-processor.go b/post-processor/docker-push/post-processor.go index 979ea2896..97ffb8627 100644 --- a/post-processor/docker-push/post-processor.go +++ b/post-processor/docker-push/post-processor.go @@ -5,7 +5,6 @@ package dockerpush import ( "context" "fmt" - "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer/builder/docker" "github.com/hashicorp/packer/common" @@ -103,17 +102,21 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact }() } - names := []string{artifact.Id()} - tags := artifact.State("docker_tags") - if tags != nil { - cast := tags.([]interface{}) - for _, name := range cast { + var tags []string + switch t := artifact.State("docker_tags").(type) { + case []string: + tags = t + case []interface{}: + for _, name := range t { if n, ok := name.(string); ok { - names = append(names, n) + tags = append(tags, n) } } } + names := []string{artifact.Id()} + names = append(names, tags...) + // Get the name. for _, name := range names { ui.Message("Pushing: " + name)