2013-05-04 16:47:11 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2013-06-03 18:00:35 -04:00
|
|
|
"encoding/gob"
|
2013-08-22 14:53:54 -04:00
|
|
|
"fmt"
|
2013-05-04 16:47:11 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-03 18:00:35 -04:00
|
|
|
"log"
|
|
|
|
"net"
|
2013-05-04 16:47:11 -04:00
|
|
|
"net/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.Builder where the builder is actually executed
|
|
|
|
// over an RPC connection.
|
2013-05-09 00:05:35 -04:00
|
|
|
type builder struct {
|
2013-05-04 16:47:11 -04:00
|
|
|
client *rpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuilderServer wraps a packer.Builder implementation and makes it exportable
|
|
|
|
// as part of a Golang RPC server.
|
|
|
|
type BuilderServer struct {
|
|
|
|
builder packer.Builder
|
|
|
|
}
|
|
|
|
|
|
|
|
type BuilderPrepareArgs struct {
|
2013-06-14 15:27:50 -04:00
|
|
|
Configs []interface{}
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type BuilderRunArgs struct {
|
2013-06-03 18:00:35 -04:00
|
|
|
RPCAddress string
|
|
|
|
ResponseAddress string
|
|
|
|
}
|
|
|
|
|
|
|
|
type BuilderRunResponse struct {
|
2013-06-12 18:58:02 -04:00
|
|
|
Err error
|
2013-05-04 16:47:11 -04:00
|
|
|
RPCAddress string
|
|
|
|
}
|
|
|
|
|
2013-05-09 00:05:35 -04:00
|
|
|
func Builder(client *rpc.Client) *builder {
|
|
|
|
return &builder{client}
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:27:50 -04:00
|
|
|
func (b *builder) Prepare(config ...interface{}) (err error) {
|
2013-05-09 16:59:33 -04:00
|
|
|
cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &err)
|
|
|
|
if cerr != nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
|
2013-05-09 13:54:42 -04:00
|
|
|
return
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-06-12 18:58:02 -04:00
|
|
|
func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
2013-05-04 16:47:11 -04:00
|
|
|
// Create and start the server for the Build and UI
|
2013-05-05 20:38:50 -04:00
|
|
|
server := rpc.NewServer()
|
2013-06-10 01:00:47 -04:00
|
|
|
RegisterCache(server, cache)
|
2013-05-11 13:31:30 -04:00
|
|
|
RegisterHook(server, hook)
|
2013-06-10 01:00:47 -04:00
|
|
|
RegisterUi(server, ui)
|
2013-05-04 16:47:11 -04:00
|
|
|
|
2013-06-03 18:00:35 -04:00
|
|
|
// Create a server for the response
|
|
|
|
responseL := netListenerInRange(portRangeMin, portRangeMax)
|
2013-06-12 18:58:02 -04:00
|
|
|
runResponseCh := make(chan *BuilderRunResponse)
|
2013-06-03 18:00:35 -04:00
|
|
|
go func() {
|
|
|
|
defer responseL.Close()
|
|
|
|
|
2013-08-23 16:57:26 -04:00
|
|
|
var response BuilderRunResponse
|
|
|
|
defer func() { runResponseCh <- &response }()
|
|
|
|
|
2013-06-03 18:00:35 -04:00
|
|
|
conn, err := responseL.Accept()
|
|
|
|
if err != nil {
|
2013-08-23 16:57:26 -04:00
|
|
|
response.Err = err
|
|
|
|
return
|
2013-06-03 18:00:35 -04:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
decoder := gob.NewDecoder(conn)
|
|
|
|
if err := decoder.Decode(&response); err != nil {
|
2013-08-22 14:45:58 -04:00
|
|
|
response.Err = fmt.Errorf("Error waiting for Run: %s", err)
|
2013-06-03 18:00:35 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
args := &BuilderRunArgs{
|
|
|
|
serveSingleConn(server),
|
|
|
|
responseL.Addr().String(),
|
|
|
|
}
|
2013-05-22 01:10:21 -04:00
|
|
|
|
2013-06-03 18:00:35 -04:00
|
|
|
if err := b.client.Call("Builder.Run", args, new(interface{})); err != nil {
|
2013-06-12 18:58:02 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := <-runResponseCh
|
|
|
|
if response.Err != nil {
|
|
|
|
return nil, response.Err
|
2013-05-22 01:13:29 -04:00
|
|
|
}
|
|
|
|
|
2013-06-12 18:58:02 -04:00
|
|
|
if response.RPCAddress == "" {
|
|
|
|
return nil, nil
|
2013-06-05 18:36:26 -04:00
|
|
|
}
|
|
|
|
|
2013-06-12 18:58:02 -04:00
|
|
|
client, err := rpc.Dial("tcp", response.RPCAddress)
|
2013-05-22 01:13:29 -04:00
|
|
|
if err != nil {
|
2013-06-12 18:58:02 -04:00
|
|
|
return nil, err
|
2013-05-22 01:13:29 -04:00
|
|
|
}
|
|
|
|
|
2013-06-12 18:58:02 -04:00
|
|
|
return Artifact(client), nil
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 17:44:34 -04:00
|
|
|
func (b *builder) Cancel() {
|
2013-06-03 18:30:09 -04:00
|
|
|
if err := b.client.Call("Builder.Cancel", new(interface{}), new(interface{})); err != nil {
|
2013-08-23 16:58:00 -04:00
|
|
|
log.Printf("Error cancelling builder: %s", err)
|
2013-06-03 18:30:09 -04:00
|
|
|
}
|
2013-06-03 17:44:34 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 13:54:42 -04:00
|
|
|
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error {
|
2013-06-14 15:27:50 -04:00
|
|
|
err := b.builder.Prepare(args.Configs...)
|
2013-05-09 16:59:33 -04:00
|
|
|
if err != nil {
|
|
|
|
*reply = NewBasicError(err)
|
|
|
|
}
|
|
|
|
|
2013-05-04 16:47:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-03 18:00:35 -04:00
|
|
|
func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error {
|
2013-05-04 16:47:11 -04:00
|
|
|
client, err := rpc.Dial("tcp", args.RPCAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-06-03 18:00:35 -04:00
|
|
|
responseC, err := net.Dial("tcp", args.ResponseAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-22 01:13:29 -04:00
|
|
|
|
2013-06-03 18:00:35 -04:00
|
|
|
responseWriter := gob.NewEncoder(responseC)
|
|
|
|
|
|
|
|
// Run the build in a goroutine so we don't block the RPC connection
|
2013-06-04 01:32:04 -04:00
|
|
|
go func() {
|
2013-06-03 18:00:35 -04:00
|
|
|
defer responseC.Close()
|
|
|
|
|
2013-06-10 01:00:47 -04:00
|
|
|
cache := Cache(client)
|
2013-06-03 18:00:35 -04:00
|
|
|
hook := Hook(client)
|
|
|
|
ui := &Ui{client}
|
2013-06-12 18:58:02 -04:00
|
|
|
artifact, responseErr := b.builder.Run(ui, hook, cache)
|
2013-06-05 18:36:26 -04:00
|
|
|
responseAddress := ""
|
2013-06-03 18:00:35 -04:00
|
|
|
|
2013-06-12 18:58:02 -04:00
|
|
|
if responseErr == nil && artifact != nil {
|
2013-06-05 18:36:26 -04:00
|
|
|
// Wrap the artifact
|
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterArtifact(server, artifact)
|
|
|
|
responseAddress = serveSingleConn(server)
|
|
|
|
}
|
2013-06-03 18:00:35 -04:00
|
|
|
|
2013-06-12 18:58:02 -04:00
|
|
|
if responseErr != nil {
|
|
|
|
responseErr = NewBasicError(responseErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := responseWriter.Encode(&BuilderRunResponse{responseErr, responseAddress})
|
|
|
|
if err != nil {
|
2013-08-23 16:57:26 -04:00
|
|
|
log.Printf("BuildServer.Run error: %s", err)
|
2013-06-12 18:58:02 -04:00
|
|
|
}
|
2013-06-03 18:00:35 -04:00
|
|
|
}()
|
2013-05-04 16:47:11 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2013-06-03 18:30:09 -04:00
|
|
|
|
2013-06-04 01:32:04 -04:00
|
|
|
func (b *BuilderServer) Cancel(args *interface{}, reply *interface{}) error {
|
2013-06-03 18:30:09 -04:00
|
|
|
b.builder.Cancel()
|
|
|
|
return nil
|
|
|
|
}
|