github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/provisioner.go (about)

     1  package rpc
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"log"
     6  	"net/rpc"
     7  )
     8  
     9  // An implementation of packer.Provisioner where the provisioner is actually
    10  // executed over an RPC connection.
    11  type provisioner struct {
    12  	client *rpc.Client
    13  	mux    *muxBroker
    14  }
    15  
    16  // ProvisionerServer wraps a packer.Provisioner implementation and makes it
    17  // exportable as part of a Golang RPC server.
    18  type ProvisionerServer struct {
    19  	p   packer.Provisioner
    20  	mux *muxBroker
    21  }
    22  
    23  type ProvisionerPrepareArgs struct {
    24  	Configs []interface{}
    25  }
    26  
    27  func (p *provisioner) Prepare(configs ...interface{}) (err error) {
    28  	args := &ProvisionerPrepareArgs{configs}
    29  	if cerr := p.client.Call("Provisioner.Prepare", args, new(interface{})); cerr != nil {
    30  		err = cerr
    31  	}
    32  
    33  	return
    34  }
    35  
    36  func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
    37  	nextId := p.mux.NextId()
    38  	server := newServerWithMux(p.mux, nextId)
    39  	server.RegisterCommunicator(comm)
    40  	server.RegisterUi(ui)
    41  	go server.Serve()
    42  
    43  	return p.client.Call("Provisioner.Provision", nextId, new(interface{}))
    44  }
    45  
    46  func (p *provisioner) Cancel() {
    47  	err := p.client.Call("Provisioner.Cancel", new(interface{}), new(interface{}))
    48  	if err != nil {
    49  		log.Printf("Provisioner.Cancel err: %s", err)
    50  	}
    51  }
    52  
    53  func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *interface{}) error {
    54  	return p.p.Prepare(args.Configs...)
    55  }
    56  
    57  func (p *ProvisionerServer) Provision(streamId uint32, reply *interface{}) error {
    58  	client, err := newClientWithMux(p.mux, streamId)
    59  	if err != nil {
    60  		return NewBasicError(err)
    61  	}
    62  	defer client.Close()
    63  
    64  	if err := p.p.Provision(client.Ui(), client.Communicator()); err != nil {
    65  		return NewBasicError(err)
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func (p *ProvisionerServer) Cancel(args *interface{}, reply *interface{}) error {
    72  	p.p.Cancel()
    73  	return nil
    74  }