2013-06-18 16:44:57 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2019-03-27 07:29:09 -04:00
|
|
|
"log"
|
2017-03-28 21:02:51 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-18 16:44:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.PostProcessor where the PostProcessor is actually
|
|
|
|
// executed over an RPC connection.
|
|
|
|
type postProcessor struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
commonClient
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostProcessorServer wraps a packer.PostProcessor implementation and makes it
|
|
|
|
// exportable as part of a Golang RPC server.
|
|
|
|
type PostProcessorServer struct {
|
2019-03-27 07:29:09 -04:00
|
|
|
context context.Context
|
|
|
|
contextCancel func()
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
commonServer
|
|
|
|
p packer.PostProcessor
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 17:59:23 -04:00
|
|
|
type PostProcessorConfigureArgs struct {
|
|
|
|
Configs []interface{}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:44:57 -04:00
|
|
|
type PostProcessorProcessResponse struct {
|
2019-04-02 19:51:58 -04:00
|
|
|
Err *BasicError
|
|
|
|
Keep bool
|
|
|
|
ForceOverride bool
|
|
|
|
StreamId uint32
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *postProcessor) Configure(raw ...interface{}) error {
|
|
|
|
raw, err := encodeCTYValues(raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
args := &PostProcessorConfigureArgs{Configs: raw}
|
|
|
|
return p.client.Call(p.endpoint+".Configure", args, new(interface{}))
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
func (p *postProcessor) PostProcess(ctx context.Context, ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, bool, error) {
|
2013-12-09 22:07:36 -05:00
|
|
|
nextId := p.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(p.mux, nextId)
|
2013-12-09 22:07:36 -05:00
|
|
|
server.RegisterArtifact(a)
|
|
|
|
server.RegisterUi(ui)
|
|
|
|
go server.Serve()
|
2013-06-18 16:44:57 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
done := make(chan interface{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Printf("Cancelling post-processor after context cancellation %v", ctx.Err())
|
2019-12-17 05:25:56 -05:00
|
|
|
if err := p.client.Call(p.endpoint+".Cancel", new(interface{}), new(interface{})); err != nil {
|
2019-03-27 07:29:09 -04:00
|
|
|
log.Printf("Error cancelling post-processor: %s", err)
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-06-18 16:44:57 -04:00
|
|
|
var response PostProcessorProcessResponse
|
2019-12-17 05:25:56 -05:00
|
|
|
if err := p.client.Call(p.endpoint+".PostProcess", nextId, &response); err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if response.Err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, response.Err
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-12-09 22:07:36 -05:00
|
|
|
if response.StreamId == 0 {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, nil
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(p.mux, response.StreamId)
|
2013-12-09 22:07:36 -05:00
|
|
|
if err != nil {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, err
|
2013-12-09 22:07:36 -05:00
|
|
|
}
|
|
|
|
|
2019-04-02 19:51:58 -04:00
|
|
|
return client.Artifact(), response.Keep, response.ForceOverride, nil
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *PostProcessorServer) Configure(args *PostProcessorConfigureArgs, reply *interface{}) (err error) {
|
|
|
|
config, err := decodeCTYValues(args.Configs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = p.p.Configure(config...)
|
2014-04-28 18:18:31 -04:00
|
|
|
return err
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
func (p *PostProcessorServer) PostProcess(streamId uint32, reply *PostProcessorProcessResponse) error {
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(p.mux, streamId)
|
2013-12-09 22:07:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return NewBasicError(err)
|
2013-12-09 18:44:00 -05:00
|
|
|
}
|
2013-06-18 16:44:57 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
if p.context == nil {
|
|
|
|
p.context, p.contextCancel = context.WithCancel(context.Background())
|
|
|
|
}
|
|
|
|
|
2020-10-05 08:27:55 -04:00
|
|
|
artifact := client.Artifact()
|
|
|
|
artifactResult, keep, forceOverride, err := p.p.PostProcess(p.context, client.Ui(), artifact)
|
2013-06-18 16:44:57 -04:00
|
|
|
*reply = PostProcessorProcessResponse{
|
2019-04-02 19:51:58 -04:00
|
|
|
Err: NewBasicError(err),
|
|
|
|
Keep: keep,
|
|
|
|
ForceOverride: forceOverride,
|
2020-10-05 08:27:55 -04:00
|
|
|
StreamId: 0,
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %v", err)
|
|
|
|
client.Close()
|
|
|
|
return nil
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2020-10-05 08:27:55 -04:00
|
|
|
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()
|
|
|
|
}
|
2013-06-18 16:44:57 -04:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-27 07:29:09 -04:00
|
|
|
|
|
|
|
func (b *PostProcessorServer) Cancel(args *interface{}, reply *interface{}) error {
|
|
|
|
if b.contextCancel != nil {
|
|
|
|
b.contextCancel()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|