github.com/jerryclinesmith/packer@v0.3.7/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  }
    14  
    15  // ProvisionerServer wraps a packer.Provisioner implementation and makes it
    16  // exportable as part of a Golang RPC server.
    17  type ProvisionerServer struct {
    18  	p packer.Provisioner
    19  }
    20  
    21  type ProvisionerPrepareArgs struct {
    22  	Configs []interface{}
    23  }
    24  
    25  type ProvisionerProvisionArgs struct {
    26  	RPCAddress string
    27  }
    28  
    29  func Provisioner(client *rpc.Client) *provisioner {
    30  	return &provisioner{client}
    31  }
    32  func (p *provisioner) Prepare(configs ...interface{}) (err error) {
    33  	args := &ProvisionerPrepareArgs{configs}
    34  	if cerr := p.client.Call("Provisioner.Prepare", args, &err); cerr != nil {
    35  		err = cerr
    36  	}
    37  
    38  	return
    39  }
    40  
    41  func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
    42  	// TODO: Error handling
    43  	server := rpc.NewServer()
    44  	RegisterCommunicator(server, comm)
    45  	RegisterUi(server, ui)
    46  
    47  	args := &ProvisionerProvisionArgs{serveSingleConn(server)}
    48  	return p.client.Call("Provisioner.Provision", args, new(interface{}))
    49  }
    50  
    51  func (p *provisioner) Cancel() {
    52  	err := p.client.Call("Provisioner.Cancel", new(interface{}), new(interface{}))
    53  	if err != nil {
    54  		log.Printf("Provisioner.Cancel err: %s", err)
    55  	}
    56  }
    57  
    58  func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *error) error {
    59  	*reply = p.p.Prepare(args.Configs...)
    60  	if *reply != nil {
    61  		*reply = NewBasicError(*reply)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func (p *ProvisionerServer) Provision(args *ProvisionerProvisionArgs, reply *interface{}) error {
    68  	client, err := rpc.Dial("tcp", args.RPCAddress)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	comm := Communicator(client)
    74  	ui := &Ui{client}
    75  
    76  	if err := p.p.Provision(ui, comm); err != nil {
    77  		return NewBasicError(err)
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func (p *ProvisionerServer) Cancel(args *interface{}, reply *interface{}) error {
    84  	p.p.Cancel()
    85  	return nil
    86  }