2014-09-28 19:49:27 +02:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/rpc"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/hashicorp/yamux"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server listens for network connections and then dispenses interface
|
|
|
|
// implementations for Terraform over net/rpc.
|
|
|
|
type Server struct {
|
2014-09-28 19:55:12 +02:00
|
|
|
ProviderFunc ProviderFunc
|
|
|
|
ProvisionerFunc ProvisionerFunc
|
2014-09-28 19:49:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProviderFunc creates terraform.ResourceProviders when they're requested
|
|
|
|
// from the server.
|
|
|
|
type ProviderFunc func() terraform.ResourceProvider
|
|
|
|
|
2014-09-28 19:55:12 +02:00
|
|
|
// ProvisionerFunc creates terraform.ResourceProvisioners when they're requested
|
|
|
|
// from the server.
|
|
|
|
type ProvisionerFunc func() terraform.ResourceProvisioner
|
|
|
|
|
2014-09-28 19:49:27 +02:00
|
|
|
// Accept accepts connections on a listener and serves requests for
|
|
|
|
// each incoming connection. Accept blocks; the caller typically invokes
|
|
|
|
// it in a go statement.
|
|
|
|
func (s *Server) Accept(lis net.Listener) {
|
|
|
|
for {
|
|
|
|
conn, err := lis.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] plugin server: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go s.ServeConn(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeConn runs a single connection.
|
|
|
|
//
|
|
|
|
// ServeConn blocks, serving the connection until the client hangs up.
|
|
|
|
func (s *Server) ServeConn(conn io.ReadWriteCloser) {
|
|
|
|
// First create the yamux server to wrap this connection
|
|
|
|
mux, err := yamux.Server(conn, nil)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
log.Printf("[ERR] plugin: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept the control connection
|
|
|
|
control, err := mux.Accept()
|
|
|
|
if err != nil {
|
|
|
|
mux.Close()
|
|
|
|
log.Printf("[ERR] plugin: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the broker and start it up
|
|
|
|
broker := newMuxBroker(mux)
|
|
|
|
go broker.Run()
|
|
|
|
|
|
|
|
// Use the control connection to build the dispenser and serve the
|
|
|
|
// connection.
|
|
|
|
server := rpc.NewServer()
|
|
|
|
server.RegisterName("Dispenser", &dispenseServer{
|
2014-09-28 19:55:12 +02:00
|
|
|
ProviderFunc: s.ProviderFunc,
|
|
|
|
ProvisionerFunc: s.ProvisionerFunc,
|
2014-09-28 19:49:27 +02:00
|
|
|
|
|
|
|
broker: broker,
|
|
|
|
})
|
|
|
|
server.ServeConn(control)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispenseServer dispenses variousinterface implementations for Terraform.
|
|
|
|
type dispenseServer struct {
|
2014-09-28 19:55:12 +02:00
|
|
|
ProviderFunc ProviderFunc
|
|
|
|
ProvisionerFunc ProvisionerFunc
|
2014-09-28 19:49:27 +02:00
|
|
|
|
|
|
|
broker *muxBroker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dispenseServer) ResourceProvider(
|
|
|
|
args interface{}, response *uint32) error {
|
|
|
|
id := d.broker.NextId()
|
|
|
|
*response = id
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
conn, err := d.broker.Accept(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Plugin dispense: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-29 09:23:17 +02:00
|
|
|
serve(conn, "ResourceProvider", &ResourceProviderServer{
|
|
|
|
Broker: d.broker,
|
2014-09-28 19:49:27 +02:00
|
|
|
Provider: d.ProviderFunc(),
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-28 19:55:12 +02:00
|
|
|
func (d *dispenseServer) ResourceProvisioner(
|
|
|
|
args interface{}, response *uint32) error {
|
|
|
|
id := d.broker.NextId()
|
|
|
|
*response = id
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
conn, err := d.broker.Accept(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Plugin dispense: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-29 09:23:17 +02:00
|
|
|
serve(conn, "ResourceProvisioner", &ResourceProvisionerServer{
|
2014-09-28 19:55:12 +02:00
|
|
|
Provisioner: d.ProvisionerFunc(),
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-29 09:23:17 +02:00
|
|
|
func acceptAndServe(mux *muxBroker, id uint32, n string, v interface{}) {
|
|
|
|
conn, err := mux.Accept(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Plugin acceptAndServe: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
serve(conn, n, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serve(conn io.ReadWriteCloser, name string, v interface{}) {
|
2014-09-28 19:49:27 +02:00
|
|
|
server := rpc.NewServer()
|
|
|
|
if err := server.RegisterName(name, v); err != nil {
|
|
|
|
log.Printf("[ERR] Plugin dispense: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
server.ServeConn(conn)
|
|
|
|
}
|