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"
|
2019-03-26 10:21:07 -04:00
|
|
|
"net/rpc"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/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 {
|
|
|
|
client *rpc.Client
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
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()
|
|
|
|
|
2013-12-10 14:56:15 -05:00
|
|
|
p packer.Provisioner
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
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
|
|
|
}
|
|
|
|
|
2013-06-06 20:07:42 -04:00
|
|
|
func (p *provisioner) Prepare(configs ...interface{}) (err error) {
|
2013-06-06 20:01:12 -04:00
|
|
|
args := &ProvisionerPrepareArgs{configs}
|
2019-03-26 10:21:07 -04:00
|
|
|
if cerr := p.client.Call("Provisioner.Prepare", args, new(interface{})); cerr != nil {
|
2013-06-06 20:07:42 -04:00
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 12:37:59 -04:00
|
|
|
func (p *provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData *packer.ProvisionHookData) 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())
|
|
|
|
if err := p.client.Call("Provisioner.Cancel", new(interface{}), new(interface{})); err != nil {
|
|
|
|
log.Printf("Error cancelling provisioner: %s", err)
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-03-26 10:21:07 -04:00
|
|
|
return p.client.Call("Provisioner.Provision", nextId, 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 {
|
2014-04-29 00:36:49 -04:00
|
|
|
return p.p.Prepare(args.Configs...)
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 12:37:59 -04:00
|
|
|
func (p *ProvisionerServer) Provision(streamId uint32, reply *interface{}, generatedData *packer.ProvisionHookData) error {
|
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-07-11 12:37:59 -04:00
|
|
|
if err := p.p.Provision(p.context, client.Ui(), client.Communicator(), 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
|
|
|
|
}
|