packer-cn/packer/rpc/environment.go

114 lines
2.2 KiB
Go
Raw Normal View History

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
}
func (e *Environment) Builder(name string) (b packer.Builder, err error) {
var reply string
err = e.client.Call("Environment.Builder", name, &reply)
if err != nil {
return
}
client, err := rpc.Dial("tcp", reply)
if err != nil {
return
}
b = Builder(client)
return
}
func (e *Environment) Cli(args []string) (result int, err error) {
2013-05-04 18:58:42 -04:00
rpcArgs := &EnvironmentCliArgs{args}
err = e.client.Call("Environment.Cli", rpcArgs, &result)
2013-05-04 18:58:42 -04:00
return
}
2013-05-11 12:51:49 -04:00
func (e *Environment) Hook(name string) (h packer.Hook, err error) {
var reply string
err = e.client.Call("Environment.Hook", name, &reply)
if err != nil {
return
}
2013-05-11 14:11:40 -04:00
client, err := rpc.Dial("tcp", reply)
2013-05-11 12:51:49 -04:00
if err != nil {
return
}
2013-05-11 14:11:40 -04:00
h = Hook(client)
2013-05-11 12:51:49 -04:00
return
}
2013-05-04 18:58:42 -04:00
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}
}
func (e *EnvironmentServer) Builder(name *string, reply *string) error {
builder, err := e.env.Builder(*name)
if err != nil {
return err
}
// Wrap it
server := rpc.NewServer()
RegisterBuilder(server, builder)
*reply = serveSingleConn(server)
return nil
}
func (e *EnvironmentServer) Cli(args *EnvironmentCliArgs, reply *int) (err error) {
*reply, err = e.env.Cli(args.Args)
return
2013-05-04 18:58:42 -04:00
}
2013-05-11 12:51:49 -04:00
func (e *EnvironmentServer) Hook(name *string, reply *string) error {
2013-05-11 14:11:40 -04:00
hook, err := e.env.Hook(*name)
2013-05-11 12:51:49 -04:00
if err != nil {
return err
}
// Wrap it
server := rpc.NewServer()
2013-05-11 14:11:40 -04:00
RegisterHook(server, hook)
2013-05-11 12:51:49 -04:00
*reply = serveSingleConn(server)
return nil
}
2013-05-04 18:58:42 -04:00
func (e *EnvironmentServer) Ui(args *interface{}, reply *string) error {
ui := e.env.Ui()
// Wrap it
server := rpc.NewServer()
RegisterUi(server, ui)
2013-05-04 18:58:42 -04:00
*reply = serveSingleConn(server)
2013-05-04 18:58:42 -04:00
return nil
}