2013-05-22 18:35:52 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2019-03-27 07:29:09 -04:00
|
|
|
"log"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-05-22 18:35:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.Provisioner where the provisioner is actually
|
|
|
|
// executed over an RPC connection.
|
|
|
|
type provisioner struct {
|
2019-12-17 05:25:56 -05:00
|
|
|
commonClient
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProvisionerServer wraps a packer.Provisioner implementation and makes it
|
|
|
|
// exportable as part of a Golang RPC server.
|
|
|
|
type ProvisionerServer 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.Provisioner
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProvisionerPrepareArgs struct {
|
2013-06-07 02:14:31 -04:00
|
|
|
Configs []interface{}
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *provisioner) Prepare(configs ...interface{}) error {
|
|
|
|
configs, err := encodeCTYValues(configs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-06-06 20:07:42 -04:00
|
|
|
}
|
2019-12-17 05:25:56 -05:00
|
|
|
args := &ProvisionerPrepareArgs{configs}
|
|
|
|
return p.client.Call(p.endpoint+".Prepare", args, new(interface{}))
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:10:57 -04:00
|
|
|
type ProvisionerProvisionArgs struct {
|
2019-12-12 13:59:44 -05:00
|
|
|
GeneratedData map[string]interface{}
|
2019-07-11 15:10:57 -04:00
|
|
|
StreamID uint32
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (p *provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
|
2013-12-10 14:56:15 -05:00
|
|
|
nextId := p.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(p.mux, nextId)
|
2013-12-10 14:56:15 -05:00
|
|
|
server.RegisterCommunicator(comm)
|
|
|
|
server.RegisterUi(ui)
|
|
|
|
go server.Serve()
|
2013-05-22 18:35:52 -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 provisioner 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 provisioner: %s", err)
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-07-11 15:10:57 -04:00
|
|
|
args := &ProvisionerProvisionArgs{generatedData, nextId}
|
2019-12-17 05:57:09 -05:00
|
|
|
return p.client.Call(p.endpoint+".Provision", args, new(interface{}))
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *interface{}) error {
|
2019-12-17 05:25:56 -05:00
|
|
|
config, err := decodeCTYValues(args.Configs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.p.Prepare(config...)
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:10:57 -04:00
|
|
|
func (p *ProvisionerServer) Provision(args *ProvisionerProvisionArgs, reply *interface{}) error {
|
|
|
|
streamId := args.StreamID
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(p.mux, streamId)
|
2013-05-22 18:35:52 -04:00
|
|
|
if err != nil {
|
2013-12-10 14:56:15 -05:00
|
|
|
return NewBasicError(err)
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
2013-12-10 14:56:15 -05:00
|
|
|
defer client.Close()
|
2013-05-22 18:35:52 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
if p.context == nil {
|
|
|
|
p.context, p.contextCancel = context.WithCancel(context.Background())
|
|
|
|
}
|
2019-12-12 13:59:44 -05:00
|
|
|
if err := p.p.Provision(p.context, client.Ui(), client.Communicator(), args.GeneratedData); err != nil {
|
2013-06-26 20:50:25 -04:00
|
|
|
return NewBasicError(err)
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:35:52 -04:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-27 07:29:09 -04:00
|
|
|
|
|
|
|
func (p *ProvisionerServer) Cancel(args *interface{}, reply *interface{}) error {
|
|
|
|
p.contextCancel()
|
|
|
|
return nil
|
|
|
|
}
|