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
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
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
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
2013-05-03 18:49:15 -04:00
|
|
|
}
|
2013-05-04 18:58:42 -04:00
|
|
|
|
2013-05-07 23:42:49 -04:00
|
|
|
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)
|
2013-05-07 23:42:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-05-05 18:12:55 -04:00
|
|
|
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(e.mux, streamId)
|
2013-05-07 23:42:49 -04:00
|
|
|
if err != nil {
|
2013-12-10 15:23:42 -05:00
|
|
|
return nil, err
|
2013-05-07 23:42:49 -04:00
|
|
|
}
|
2013-12-10 15:23:42 -05:00
|
|
|
b = client.Builder()
|
2013-05-07 23:42:49 -04:00
|
|
|
return
|
2013-05-05 18:12:55 -04:00
|
|
|
}
|
|
|
|
|
2013-06-10 00:09:09 -04:00
|
|
|
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 {
|
2013-06-10 00:09:09 -04:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(e.mux, streamId)
|
2013-06-10 00:09:09 -04:00
|
|
|
if err != nil {
|
2013-12-10 15:23:42 -05:00
|
|
|
log.Printf("[ERR] Error getting cache client: %s", err)
|
|
|
|
return nil
|
2013-06-10 00:09:09 -04:00
|
|
|
}
|
2013-12-10 15:23:42 -05:00
|
|
|
return client.Cache()
|
2013-06-10 00:09:09 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:35:32 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-18 13:05:45 -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)
|
2013-06-18 13:05:45 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(e.mux, streamId)
|
2013-06-18 13:05:45 -04:00
|
|
|
if err != nil {
|
2013-12-10 15:23:42 -05:00
|
|
|
return nil, err
|
2013-06-18 13:05:45 -04:00
|
|
|
}
|
2013-12-10 15:23:42 -05:00
|
|
|
p = client.PostProcessor()
|
2013-06-18 13:05:45 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:35:32 -05:00
|
|
|
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
|
|
|
|
2013-12-19 20:35:32 -05: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)
|
2013-05-07 23:42:49 -04:00
|
|
|
if err != nil {
|
2013-12-10 15:23:42 -05:00
|
|
|
return NewBasicError(err)
|
2013-05-07 23:42:49 -04:00
|
|
|
}
|
2013-05-05 18:12:55 -04:00
|
|
|
|
2013-12-10 15:23:42 -05:00
|
|
|
*reply = e.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(e.mux, *reply)
|
2013-12-10 15:23:42 -05:00
|
|
|
server.RegisterBuilder(builder)
|
|
|
|
go server.Serve()
|
2013-05-05 18:12:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-10 15:23:42 -05:00
|
|
|
func (e *EnvironmentServer) Cache(args *interface{}, reply *uint32) error {
|
2013-06-10 00:09:09 -04:00
|
|
|
cache := e.env.Cache()
|
|
|
|
|
2013-12-10 15:23:42 -05:00
|
|
|
*reply = e.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(e.mux, *reply)
|
2013-12-10 15:23:42 -05:00
|
|
|
server.RegisterCache(cache)
|
|
|
|
go server.Serve()
|
2013-06-10 00:09:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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()
|
2013-12-19 20:35:32 -05:00
|
|
|
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)
|
2013-06-18 13:05:45 -04:00
|
|
|
if err != nil {
|
2013-12-10 15:23:42 -05:00
|
|
|
return NewBasicError(err)
|
2013-06-18 13:05:45 -04:00
|
|
|
}
|
|
|
|
|
2013-12-10 15:23:42 -05:00
|
|
|
*reply = e.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(e.mux, *reply)
|
2013-12-10 15:23:42 -05:00
|
|
|
server.RegisterPostProcessor(pp)
|
|
|
|
go server.Serve()
|
2013-06-18 13:05:45 -04:00
|
|
|
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()
|
2013-12-19 20:35:32 -05:00
|
|
|
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()
|
2013-12-19 20:35:32 -05:00
|
|
|
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
|
|
|
|
}
|