packer-cn/packer/rpc/server.go

109 lines
3.1 KiB
Go

package rpc
import (
"fmt"
"github.com/mitchellh/packer/packer"
"net/rpc"
"sync/atomic"
)
// This keeps track of the endpoint ID to use when registering artifacts.
var endpointId uint64 = 0
// Registers the appropriate endpoint on an RPC server to serve an
// Artifact.
func RegisterArtifact(s *rpc.Server, a packer.Artifact) {
registerComponent(s, "Artifact", &ArtifactServer{a}, false)
}
// Registers the appropriate endpoint on an RPC server to serve a
// Packer Build.
func RegisterBuild(s *rpc.Server, b packer.Build) {
registerComponent(s, "Build", &BuildServer{b}, false)
}
// Registers the appropriate endpoint on an RPC server to serve a
// Packer Builder.
func RegisterBuilder(s *rpc.Server, b packer.Builder) {
registerComponent(s, "Builder", &BuilderServer{b}, false)
}
// Registers the appropriate endpoint on an RPC server to serve a
// Packer Cache.
func RegisterCache(s *rpc.Server, c packer.Cache) {
registerComponent(s, "Cache", &CacheServer{c}, false)
}
// Registers the appropriate endpoint on an RPC server to serve a
// Packer Command.
func RegisterCommand(s *rpc.Server, c packer.Command) {
registerComponent(s, "Command", &CommandServer{c}, false)
}
// Registers the appropriate endpoint on an RPC server to serve a
// Packer Communicator.
func RegisterCommunicator(s *rpc.Server, c packer.Communicator) {
registerComponent(s, "Communicator", &CommunicatorServer{c}, false)
}
// Registers the appropriate endpoint on an RPC server to serve a
// Packer Environment
func RegisterEnvironment(s *rpc.Server, e packer.Environment) {
registerComponent(s, "Environment", &EnvironmentServer{e}, false)
}
// Registers the appropriate endpoint on an RPC server to serve a
// Hook.
func RegisterHook(s *rpc.Server, h packer.Hook) {
registerComponent(s, "Hook", &HookServer{h}, false)
}
// Registers the appropriate endpoing on an RPC server to serve a
// PostProcessor.
func RegisterPostProcessor(s *rpc.Server, p packer.PostProcessor) {
registerComponent(s, "PostProcessor", &PostProcessorServer{p}, false)
}
// Registers the appropriate endpoint on an RPC server to serve a packer.Provisioner
func RegisterProvisioner(s *rpc.Server, p packer.Provisioner) {
registerComponent(s, "Provisioner", &ProvisionerServer{p}, false)
}
// Registers the appropriate endpoint on an RPC server to serve a
// Packer UI
func RegisterUi(s *rpc.Server, ui packer.Ui) {
registerComponent(s, "Ui", &UiServer{ui}, false)
}
// registerComponent registers a single Packer RPC component onto
// the RPC server. If id is true, then a unique ID number will be appended
// onto the end of the endpoint.
//
// The endpoint name is returned.
func registerComponent(s *rpc.Server, name string, rcvr interface{}, id bool) string {
endpoint := name
if id {
fmt.Sprintf("%s.%d", endpoint, atomic.AddUint64(&endpointId, 1))
}
s.RegisterName(endpoint, rcvr)
return endpoint
}
func serveSingleConn(s *rpc.Server) string {
l := netListenerInRange(portRangeMin, portRangeMax)
// Accept a single connection in a goroutine and then exit
go func() {
defer l.Close()
conn, err := l.Accept()
if err != nil {
panic(err)
}
s.ServeConn(conn)
}()
return l.Addr().String()
}