2013-05-05 00:26:30 -04:00
|
|
|
// The packer/plugin package provides the functionality required for writing
|
|
|
|
// Packer plugins in the form of static binaries that are then executed and
|
|
|
|
// run. It also contains the functions necessary to run these external plugins.
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-05-05 19:25:32 -04:00
|
|
|
"net"
|
|
|
|
"net/rpc"
|
2013-05-05 00:26:30 -04:00
|
|
|
"os"
|
|
|
|
packrpc "github.com/mitchellh/packer/packer/rpc"
|
|
|
|
)
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
// This serves a single RPC connection on the given RPC server on
|
|
|
|
// a random port.
|
|
|
|
func serve(server *rpc.Server) (err error) {
|
|
|
|
listener, err := net.Listen("tcp", ":2345")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer listener.Close()
|
2013-05-05 00:26:30 -04:00
|
|
|
|
|
|
|
// Output the address to stdout
|
2013-05-05 19:25:32 -04:00
|
|
|
fmt.Println(":2345")
|
2013-05-05 00:26:30 -04:00
|
|
|
os.Stdout.Sync()
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
// Accept a connection
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve a single connection
|
|
|
|
server.ServeConn(conn)
|
|
|
|
return
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
// Serves a command from a plugin.
|
2013-05-05 00:26:30 -04:00
|
|
|
func ServeCommand(command packer.Command) {
|
2013-05-05 19:25:32 -04:00
|
|
|
server := rpc.NewServer()
|
|
|
|
packrpc.RegisterCommand(server, command)
|
|
|
|
|
|
|
|
if err := serve(server); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|