2013-05-03 18:49:15 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
|
|
|
)
|
|
|
|
|
2013-05-04 18:58:42 -04:00
|
|
|
// A Environment is an implementation of the packer.Environment interface
|
2013-05-03 18:49:15 -04:00
|
|
|
// where the actual environment is executed over an RPC connection.
|
2013-05-04 18:58:42 -04:00
|
|
|
type Environment struct {
|
2013-05-03 18:49:15 -04:00
|
|
|
client *rpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// A EnvironmentServer wraps a packer.Environment and makes it exportable
|
|
|
|
// as part of a Golang RPC server.
|
|
|
|
type EnvironmentServer struct {
|
|
|
|
env packer.Environment
|
|
|
|
}
|
2013-05-04 18:58:42 -04:00
|
|
|
|
|
|
|
type EnvironmentCliArgs struct {
|
|
|
|
Args []string
|
|
|
|
}
|
|
|
|
|
2013-05-05 18:12:55 -04:00
|
|
|
func (e *Environment) Builder(name string) packer.Builder {
|
|
|
|
var reply string
|
|
|
|
e.client.Call("Environment.Builder", name, &reply)
|
|
|
|
|
|
|
|
// TODO: error handling
|
|
|
|
client, _ := rpc.Dial("tcp", reply)
|
|
|
|
return &Builder{client}
|
|
|
|
}
|
|
|
|
|
2013-05-04 18:58:42 -04:00
|
|
|
func (e *Environment) Cli(args []string) (result int) {
|
|
|
|
rpcArgs := &EnvironmentCliArgs{args}
|
|
|
|
e.client.Call("Environment.Cli", rpcArgs, &result)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Environment) Ui() packer.Ui {
|
|
|
|
var reply string
|
|
|
|
e.client.Call("Environment.Ui", new(interface{}), &reply)
|
|
|
|
|
|
|
|
// TODO: error handling
|
|
|
|
client, _ := rpc.Dial("tcp", reply)
|
|
|
|
return &Ui{client}
|
|
|
|
}
|
|
|
|
|
2013-05-05 18:12:55 -04:00
|
|
|
func (e *EnvironmentServer) Builder(name *string, reply *string) error {
|
|
|
|
builder := e.env.Builder(*name)
|
|
|
|
|
|
|
|
// Wrap it
|
|
|
|
server := NewServer()
|
|
|
|
server.RegisterBuilder(builder)
|
|
|
|
server.StartSingle()
|
|
|
|
|
|
|
|
*reply = server.Address()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-04 18:58:42 -04:00
|
|
|
func (e *EnvironmentServer) Cli(args *EnvironmentCliArgs, reply *int) error {
|
|
|
|
*reply = e.env.Cli(args.Args)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EnvironmentServer) Ui(args *interface{}, reply *string) error {
|
|
|
|
ui := e.env.Ui()
|
|
|
|
|
|
|
|
// Wrap it
|
|
|
|
server := NewServer()
|
|
|
|
server.RegisterUi(ui)
|
|
|
|
server.StartSingle()
|
|
|
|
|
|
|
|
*reply = server.Address()
|
|
|
|
return nil
|
|
|
|
}
|