2013-05-22 18:35:52 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-08-31 02:21:15 -04:00
|
|
|
"log"
|
2013-05-22 18:35:52 -04:00
|
|
|
"net/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.Provisioner where the provisioner is actually
|
|
|
|
// executed over an RPC connection.
|
|
|
|
type provisioner struct {
|
|
|
|
client *rpc.Client
|
2013-12-10 14:56:15 -05:00
|
|
|
mux *MuxConn
|
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 {
|
2013-12-10 14:56:15 -05:00
|
|
|
p packer.Provisioner
|
|
|
|
mux *MuxConn
|
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}
|
2013-06-06 20:07:42 -04:00
|
|
|
if cerr := p.client.Call("Provisioner.Prepare", args, &err); cerr != nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
2013-06-26 20:50:25 -04:00
|
|
|
func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
2013-12-10 14:56:15 -05:00
|
|
|
nextId := p.mux.NextId()
|
|
|
|
server := NewServerWithMux(p.mux, nextId)
|
|
|
|
server.RegisterCommunicator(comm)
|
|
|
|
server.RegisterUi(ui)
|
|
|
|
go server.Serve()
|
2013-05-22 18:35:52 -04:00
|
|
|
|
2013-12-10 14:56:15 -05:00
|
|
|
return p.client.Call("Provisioner.Provision", nextId, new(interface{}))
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 02:21:15 -04:00
|
|
|
func (p *provisioner) Cancel() {
|
|
|
|
err := p.client.Call("Provisioner.Cancel", new(interface{}), new(interface{}))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Provisioner.Cancel err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 20:07:42 -04:00
|
|
|
func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *error) error {
|
|
|
|
*reply = p.p.Prepare(args.Configs...)
|
2013-06-20 15:55:11 -04:00
|
|
|
if *reply != nil {
|
|
|
|
*reply = NewBasicError(*reply)
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:35:52 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-10 14:56:15 -05:00
|
|
|
func (p *ProvisionerServer) Provision(streamId uint32, reply *interface{}) error {
|
|
|
|
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
|
|
|
|
2013-12-10 14:56:15 -05:00
|
|
|
if err := p.p.Provision(client.Ui(), client.Communicator()); err != nil {
|
2013-06-26 20:50:25 -04:00
|
|
|
return NewBasicError(err)
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:35:52 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-08-31 02:21:15 -04:00
|
|
|
|
|
|
|
func (p *ProvisionerServer) Cancel(args *interface{}, reply *interface{}) error {
|
|
|
|
p.p.Cancel()
|
|
|
|
return nil
|
|
|
|
}
|