packer-cn/packer/rpc/environment.go

194 lines
4.2 KiB
Go
Raw Normal View History

2013-05-03 18:49:15 -04:00
package rpc
import (
"github.com/mitchellh/packer/packer"
2013-12-10 15:23:42 -05:00
"log"
2013-05-03 18:49:15 -04:00
"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
2013-12-10 15:23:42 -05:00
mux *MuxConn
2013-05-03 18:49:15 -04:00
}
// A EnvironmentServer wraps a packer.Environment and makes it exportable
// as part of a Golang RPC server.
type EnvironmentServer struct {
env packer.Environment
2013-12-10 15:23:42 -05:00
mux *MuxConn
2013-05-03 18:49:15 -04:00
}
2013-05-04 18:58:42 -04:00
type EnvironmentCliArgs struct {
Args []string
}
func (e *Environment) Builder(name string) (b packer.Builder, err error) {
2013-12-10 15:23:42 -05:00
var streamId uint32
err = e.client.Call("Environment.Builder", name, &streamId)
if err != nil {
return
}
client, err := newClientWithMux(e.mux, streamId)
if err != nil {
2013-12-10 15:23:42 -05:00
return nil, err
}
2013-12-10 15:23:42 -05:00
b = client.Builder()
return
}
func (e *Environment) Cache() packer.Cache {
2013-12-10 15:23:42 -05:00
var streamId uint32
if err := e.client.Call("Environment.Cache", new(interface{}), &streamId); err != nil {
panic(err)
}
client, err := newClientWithMux(e.mux, streamId)
if err != nil {
2013-12-10 15:23:42 -05:00
log.Printf("[ERR] Error getting cache client: %s", err)
return nil
}
2013-12-10 15:23:42 -05:00
return client.Cache()
}
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) {
2013-12-10 15:23:42 -05:00
var streamId uint32
err = e.client.Call("Environment.Hook", name, &streamId)
2013-05-11 12:51:49 -04:00
if err != nil {
return
}
client, err := newClientWithMux(e.mux, streamId)
2013-05-11 12:51:49 -04:00
if err != nil {
2013-12-10 15:23:42 -05:00
return nil, err
2013-05-11 12:51:49 -04:00
}
2013-12-10 15:23:42 -05:00
return client.Hook(), nil
2013-05-11 12:51:49 -04:00
}
func (e *Environment) PostProcessor(name string) (p packer.PostProcessor, err error) {
2013-12-10 15:23:42 -05:00
var streamId uint32
err = e.client.Call("Environment.PostProcessor", name, &streamId)
if err != nil {
return
}
client, err := newClientWithMux(e.mux, streamId)
if err != nil {
2013-12-10 15:23:42 -05:00
return nil, err
}
2013-12-10 15:23:42 -05:00
p = client.PostProcessor()
return
}
2013-05-22 18:35:52 -04:00
func (e *Environment) Provisioner(name string) (p packer.Provisioner, err error) {
2013-12-10 15:23:42 -05:00
var streamId uint32
err = e.client.Call("Environment.Provisioner", name, &streamId)
2013-05-22 18:35:52 -04:00
if err != nil {
return
}
client, err := newClientWithMux(e.mux, streamId)
2013-05-22 18:35:52 -04:00
if err != nil {
2013-12-10 15:23:42 -05:00
return nil, err
2013-05-22 18:35:52 -04:00
}
2013-12-10 15:23:42 -05:00
p = client.Provisioner()
2013-05-22 18:35:52 -04:00
return
}
2013-05-04 18:58:42 -04:00
func (e *Environment) Ui() packer.Ui {
2013-12-10 15:23:42 -05:00
var streamId uint32
e.client.Call("Environment.Ui", new(interface{}), &streamId)
2013-05-04 18:58:42 -04:00
client, err := newClientWithMux(e.mux, streamId)
2013-06-15 14:27:09 -04:00
if err != nil {
2013-12-10 15:23:42 -05:00
log.Printf("[ERR] Error connecting to Ui: %s", err)
return nil
2013-06-15 14:27:09 -04:00
}
2013-12-10 15:23:42 -05:00
return client.Ui()
2013-05-04 18:58:42 -04:00
}
2013-12-10 15:23:42 -05:00
func (e *EnvironmentServer) Builder(name string, reply *uint32) error {
builder, err := e.env.Builder(name)
if err != nil {
2013-12-10 15:23:42 -05:00
return NewBasicError(err)
}
2013-12-10 15:23:42 -05:00
*reply = e.mux.NextId()
server := newServerWithMux(e.mux, *reply)
2013-12-10 15:23:42 -05:00
server.RegisterBuilder(builder)
go server.Serve()
return nil
}
2013-12-10 15:23:42 -05:00
func (e *EnvironmentServer) Cache(args *interface{}, reply *uint32) error {
cache := e.env.Cache()
2013-12-10 15:23:42 -05:00
*reply = e.mux.NextId()
server := newServerWithMux(e.mux, *reply)
2013-12-10 15:23:42 -05:00
server.RegisterCache(cache)
go server.Serve()
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-12-10 15:23:42 -05:00
func (e *EnvironmentServer) Hook(name string, reply *uint32) error {
hook, err := e.env.Hook(name)
2013-05-11 12:51:49 -04:00
if err != nil {
2013-12-10 15:23:42 -05:00
return NewBasicError(err)
2013-05-11 12:51:49 -04:00
}
2013-12-10 15:23:42 -05:00
*reply = e.mux.NextId()
server := newServerWithMux(e.mux, *reply)
2013-12-10 15:23:42 -05:00
server.RegisterHook(hook)
go server.Serve()
2013-05-11 12:51:49 -04:00
return nil
}
2013-12-10 15:23:42 -05:00
func (e *EnvironmentServer) PostProcessor(name string, reply *uint32) error {
pp, err := e.env.PostProcessor(name)
if err != nil {
2013-12-10 15:23:42 -05:00
return NewBasicError(err)
}
2013-12-10 15:23:42 -05:00
*reply = e.mux.NextId()
server := newServerWithMux(e.mux, *reply)
2013-12-10 15:23:42 -05:00
server.RegisterPostProcessor(pp)
go server.Serve()
return nil
}
2013-12-10 15:23:42 -05:00
func (e *EnvironmentServer) Provisioner(name string, reply *uint32) error {
prov, err := e.env.Provisioner(name)
2013-05-22 18:35:52 -04:00
if err != nil {
2013-12-10 15:23:42 -05:00
return NewBasicError(err)
2013-05-22 18:35:52 -04:00
}
2013-12-10 15:23:42 -05:00
*reply = e.mux.NextId()
server := newServerWithMux(e.mux, *reply)
2013-12-10 15:23:42 -05:00
server.RegisterProvisioner(prov)
go server.Serve()
2013-05-22 18:35:52 -04:00
return nil
}
2013-12-10 15:23:42 -05:00
func (e *EnvironmentServer) Ui(args *interface{}, reply *uint32) error {
2013-05-04 18:58:42 -04:00
ui := e.env.Ui()
2013-12-10 15:23:42 -05:00
*reply = e.mux.NextId()
server := newServerWithMux(e.mux, *reply)
2013-12-10 15:23:42 -05:00
server.RegisterUi(ui)
go server.Serve()
2013-05-04 18:58:42 -04:00
return nil
}