packer/rpc: remove unused methods

This commit is contained in:
Mitchell Hashimoto 2013-12-10 13:26:07 -08:00
parent 8d4ba1fc2b
commit ce2304c948
15 changed files with 131 additions and 250 deletions

View File

@ -18,10 +18,6 @@ type ArtifactServer struct {
artifact packer.Artifact artifact packer.Artifact
} }
func Artifact(client *rpc.Client) *artifact {
return &artifact{client: client}
}
func (a *artifact) BuilderId() (result string) { func (a *artifact) BuilderId() (result string) {
a.client.Call(a.endpoint+".BuilderId", new(interface{}), &result) a.client.Call(a.endpoint+".BuilderId", new(interface{}), &result)
return return

View File

@ -37,5 +37,5 @@ func TestArtifactRPC(t *testing.T) {
} }
func TestArtifact_Implements(t *testing.T) { func TestArtifact_Implements(t *testing.T) {
var _ packer.Artifact = Artifact(nil) var _ packer.Artifact = new(artifact)
} }

View File

@ -24,10 +24,6 @@ type BuildPrepareResponse struct {
Error error Error error
} }
func Build(client *rpc.Client) *build {
return &build{client: client}
}
func (b *build) Name() (result string) { func (b *build) Name() (result string) {
b.client.Call("Build.Name", new(interface{}), &result) b.client.Call("Build.Name", new(interface{}), &result)
return return

View File

@ -154,5 +154,5 @@ func TestBuildPrepare_Warnings(t *testing.T) {
} }
func TestBuild_ImplementsBuild(t *testing.T) { func TestBuild_ImplementsBuild(t *testing.T) {
var _ packer.Build = Build(nil) var _ packer.Build = new(build)
} }

View File

@ -29,10 +29,6 @@ type BuilderPrepareResponse struct {
Error error Error error
} }
func Builder(client *rpc.Client) *builder {
return &builder{client: client}
}
func (b *builder) Prepare(config ...interface{}) ([]string, error) { func (b *builder) Prepare(config ...interface{}) ([]string, error) {
var resp BuilderPrepareResponse var resp BuilderPrepareResponse
cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &resp) cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &resp)

View File

@ -141,5 +141,5 @@ func TestBuilderCancel(t *testing.T) {
} }
func TestBuilder_ImplementsBuilder(t *testing.T) { func TestBuilder_ImplementsBuilder(t *testing.T) {
var _ packer.Builder = Builder(nil) var _ packer.Builder = new(builder)
} }

View File

@ -17,10 +17,6 @@ type CacheServer struct {
cache packer.Cache cache packer.Cache
} }
func Cache(client *rpc.Client) *cache {
return &cache{client}
}
type CacheRLockResponse struct { type CacheRLockResponse struct {
Path string Path string
Exists bool Exists bool

View File

@ -39,11 +39,7 @@ func (t *testCache) RUnlock(key string) {
} }
func TestCache_Implements(t *testing.T) { func TestCache_Implements(t *testing.T) {
var raw interface{} var _ packer.Cache = new(cache)
raw = Cache(nil)
if _, ok := raw.(packer.Cache); !ok {
t.Fatal("Cache must be a cache.")
}
} }
func TestCacheRPC(t *testing.T) { func TestCacheRPC(t *testing.T) {

View File

@ -26,10 +26,6 @@ type CommandRunArgs struct {
type CommandSynopsisArgs byte type CommandSynopsisArgs byte
func Command(client *rpc.Client) *command {
return &command{client: client}
}
func (c *command) Help() (result string) { func (c *command) Help() (result string) {
err := c.client.Call("Command.Help", new(interface{}), &result) err := c.client.Call("Command.Help", new(interface{}), &result)
if err != nil { if err != nil {

View File

@ -67,5 +67,5 @@ func TestRPCCommand(t *testing.T) {
} }
func TestCommand_Implements(t *testing.T) { func TestCommand_Implements(t *testing.T) {
var _ packer.Command = Command(nil) var _ packer.Command = new(command)
} }

View File

@ -26,10 +26,6 @@ type HookRunArgs struct {
StreamId uint32 StreamId uint32
} }
func Hook(client *rpc.Client) *hook {
return &hook{client: client}
}
func (h *hook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) error { func (h *hook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) error {
nextId := h.mux.NextId() nextId := h.mux.NextId()
server := NewServerWithMux(h.mux, nextId) server := NewServerWithMux(h.mux, nextId)

View File

@ -24,9 +24,6 @@ type ProvisionerPrepareArgs struct {
Configs []interface{} Configs []interface{}
} }
func Provisioner(client *rpc.Client) *provisioner {
return &provisioner{client: client}
}
func (p *provisioner) Prepare(configs ...interface{}) (err error) { func (p *provisioner) Prepare(configs ...interface{}) (err error) {
args := &ProvisionerPrepareArgs{configs} args := &ProvisionerPrepareArgs{configs}
if cerr := p.client.Call("Provisioner.Prepare", args, &err); cerr != nil { if cerr := p.client.Call("Provisioner.Prepare", args, &err); cerr != nil {

View File

@ -43,5 +43,5 @@ func TestProvisionerRPC(t *testing.T) {
} }
func TestProvisioner_Implements(t *testing.T) { func TestProvisioner_Implements(t *testing.T) {
var _ packer.Provisioner = Provisioner(nil) var _ packer.Provisioner = new(provisioner)
} }

View File

@ -1,88 +1,155 @@
package rpc package rpc
import ( import (
"fmt"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io"
"log"
"net/rpc" "net/rpc"
"sync/atomic"
) )
// Registers the appropriate endpoint on an RPC server to serve an var endpointId uint64
// Artifact.
func RegisterArtifact(s *rpc.Server, a packer.Artifact) { const (
registerComponent(s, "Artifact", &ArtifactServer{a}, false) DefaultArtifactEndpoint string = "Artifact"
DefaultBuildEndpoint = "Build"
DefaultBuilderEndpoint = "Builder"
DefaultCacheEndpoint = "Cache"
DefaultCommandEndpoint = "Command"
DefaultCommunicatorEndpoint = "Communicator"
DefaultEnvironmentEndpoint = "Environment"
DefaultHookEndpoint = "Hook"
DefaultPostProcessorEndpoint = "PostProcessor"
DefaultProvisionerEndpoint = "Provisioner"
DefaultUiEndpoint = "Ui"
)
// Server represents an RPC server for Packer. This must be paired on
// the other side with a Client.
type Server struct {
mux *MuxConn
streamId uint32
server *rpc.Server
} }
// Registers the appropriate endpoint on an RPC server to serve a // NewServer returns a new Packer RPC server.
// Packer Build. func NewServer(conn io.ReadWriteCloser) *Server {
func RegisterBuild(s *rpc.Server, b packer.Build) { return NewServerWithMux(NewMuxConn(conn), 0)
registerComponent(s, "Build", &BuildServer{build: b}, false)
} }
// Registers the appropriate endpoint on an RPC server to serve a func NewServerWithMux(mux *MuxConn, streamId uint32) *Server {
// Packer Builder. return &Server{
func RegisterBuilder(s *rpc.Server, b packer.Builder) { mux: mux,
registerComponent(s, "Builder", &BuilderServer{builder: b}, false) streamId: streamId,
server: rpc.NewServer(),
}
} }
// Registers the appropriate endpoint on an RPC server to serve a func (s *Server) Close() error {
// Packer Cache. return s.mux.Close()
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 func (s *Server) RegisterArtifact(a packer.Artifact) {
// Packer Command. s.server.RegisterName(DefaultArtifactEndpoint, &ArtifactServer{
func RegisterCommand(s *rpc.Server, c packer.Command) { artifact: a,
registerComponent(s, "Command", &CommandServer{command: c}, false) })
} }
// Registers the appropriate endpoint on an RPC server to serve a func (s *Server) RegisterBuild(b packer.Build) {
// Packer Communicator. s.server.RegisterName(DefaultBuildEndpoint, &BuildServer{
func RegisterCommunicator(s *rpc.Server, c packer.Communicator) { build: b,
registerComponent(s, "Communicator", &CommunicatorServer{c: c}, false) mux: s.mux,
})
} }
// Registers the appropriate endpoint on an RPC server to serve a func (s *Server) RegisterBuilder(b packer.Builder) {
// Packer Environment s.server.RegisterName(DefaultBuilderEndpoint, &BuilderServer{
func RegisterEnvironment(s *rpc.Server, e packer.Environment) { builder: b,
registerComponent(s, "Environment", &EnvironmentServer{env: e}, false) mux: s.mux,
})
} }
// Registers the appropriate endpoint on an RPC server to serve a func (s *Server) RegisterCache(c packer.Cache) {
// Hook. s.server.RegisterName(DefaultCacheEndpoint, &CacheServer{
func RegisterHook(s *rpc.Server, h packer.Hook) { cache: c,
registerComponent(s, "Hook", &HookServer{hook: h}, false) })
} }
// Registers the appropriate endpoing on an RPC server to serve a func (s *Server) RegisterCommand(c packer.Command) {
// PostProcessor. s.server.RegisterName(DefaultCommandEndpoint, &CommandServer{
func RegisterPostProcessor(s *rpc.Server, p packer.PostProcessor) { command: c,
registerComponent(s, "PostProcessor", &PostProcessorServer{p: p}, false) mux: s.mux,
})
} }
// Registers the appropriate endpoint on an RPC server to serve a packer.Provisioner func (s *Server) RegisterCommunicator(c packer.Communicator) {
func RegisterProvisioner(s *rpc.Server, p packer.Provisioner) { s.server.RegisterName(DefaultCommunicatorEndpoint, &CommunicatorServer{
registerComponent(s, "Provisioner", &ProvisionerServer{p: p}, false) c: c,
mux: s.mux,
})
} }
// Registers the appropriate endpoint on an RPC server to serve a func (s *Server) RegisterEnvironment(b packer.Environment) {
// Packer UI s.server.RegisterName(DefaultEnvironmentEndpoint, &EnvironmentServer{
func RegisterUi(s *rpc.Server, ui packer.Ui) { env: b,
registerComponent(s, "Ui", &UiServer{ui}, false) mux: s.mux,
})
} }
func serveSingleConn(s *rpc.Server) string { func (s *Server) RegisterHook(h packer.Hook) {
l := netListenerInRange(portRangeMin, portRangeMax) s.server.RegisterName(DefaultHookEndpoint, &HookServer{
hook: h,
mux: s.mux,
})
}
// Accept a single connection in a goroutine and then exit func (s *Server) RegisterPostProcessor(p packer.PostProcessor) {
go func() { s.server.RegisterName(DefaultPostProcessorEndpoint, &PostProcessorServer{
defer l.Close() mux: s.mux,
conn, err := l.Accept() p: p,
})
}
func (s *Server) RegisterProvisioner(p packer.Provisioner) {
s.server.RegisterName(DefaultProvisionerEndpoint, &ProvisionerServer{
mux: s.mux,
p: p,
})
}
func (s *Server) RegisterUi(ui packer.Ui) {
s.server.RegisterName(DefaultUiEndpoint, &UiServer{
ui: ui,
})
}
// ServeConn serves a single connection over the RPC server. It is up
// to the caller to obtain a proper io.ReadWriteCloser.
func (s *Server) Serve() {
// Accept a connection on stream ID 0, which is always used for
// normal client to server connections.
stream, err := s.mux.Accept(s.streamId)
defer stream.Close()
if err != nil { if err != nil {
panic(err) log.Printf("[ERR] Error retrieving stream for serving: %s", err)
return
} }
s.ServeConn(conn) s.server.ServeConn(stream)
}() }
return l.Addr().String() // 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(server *rpc.Server, name string, rcvr interface{}, id bool) string {
endpoint := name
if id {
fmt.Sprintf("%s.%d", endpoint, atomic.AddUint64(&endpointId, 1))
}
server.RegisterName(endpoint, rcvr)
return endpoint
} }

View File

@ -1,155 +0,0 @@
package rpc
import (
"fmt"
"github.com/mitchellh/packer/packer"
"io"
"log"
"net/rpc"
"sync/atomic"
)
var endpointId uint64
const (
DefaultArtifactEndpoint string = "Artifact"
DefaultBuildEndpoint = "Build"
DefaultBuilderEndpoint = "Builder"
DefaultCacheEndpoint = "Cache"
DefaultCommandEndpoint = "Command"
DefaultCommunicatorEndpoint = "Communicator"
DefaultEnvironmentEndpoint = "Environment"
DefaultHookEndpoint = "Hook"
DefaultPostProcessorEndpoint = "PostProcessor"
DefaultProvisionerEndpoint = "Provisioner"
DefaultUiEndpoint = "Ui"
)
// Server represents an RPC server for Packer. This must be paired on
// the other side with a Client.
type Server struct {
mux *MuxConn
streamId uint32
server *rpc.Server
}
// NewServer returns a new Packer RPC server.
func NewServer(conn io.ReadWriteCloser) *Server {
return NewServerWithMux(NewMuxConn(conn), 0)
}
func NewServerWithMux(mux *MuxConn, streamId uint32) *Server {
return &Server{
mux: mux,
streamId: streamId,
server: rpc.NewServer(),
}
}
func (s *Server) Close() error {
return s.mux.Close()
}
func (s *Server) RegisterArtifact(a packer.Artifact) {
s.server.RegisterName(DefaultArtifactEndpoint, &ArtifactServer{
artifact: a,
})
}
func (s *Server) RegisterBuild(b packer.Build) {
s.server.RegisterName(DefaultBuildEndpoint, &BuildServer{
build: b,
mux: s.mux,
})
}
func (s *Server) RegisterBuilder(b packer.Builder) {
s.server.RegisterName(DefaultBuilderEndpoint, &BuilderServer{
builder: b,
mux: s.mux,
})
}
func (s *Server) RegisterCache(c packer.Cache) {
s.server.RegisterName(DefaultCacheEndpoint, &CacheServer{
cache: c,
})
}
func (s *Server) RegisterCommand(c packer.Command) {
s.server.RegisterName(DefaultCommandEndpoint, &CommandServer{
command: c,
mux: s.mux,
})
}
func (s *Server) RegisterCommunicator(c packer.Communicator) {
s.server.RegisterName(DefaultCommunicatorEndpoint, &CommunicatorServer{
c: c,
mux: s.mux,
})
}
func (s *Server) RegisterEnvironment(b packer.Environment) {
s.server.RegisterName(DefaultEnvironmentEndpoint, &EnvironmentServer{
env: b,
mux: s.mux,
})
}
func (s *Server) RegisterHook(h packer.Hook) {
s.server.RegisterName(DefaultHookEndpoint, &HookServer{
hook: h,
mux: s.mux,
})
}
func (s *Server) RegisterPostProcessor(p packer.PostProcessor) {
s.server.RegisterName(DefaultPostProcessorEndpoint, &PostProcessorServer{
mux: s.mux,
p: p,
})
}
func (s *Server) RegisterProvisioner(p packer.Provisioner) {
s.server.RegisterName(DefaultProvisionerEndpoint, &ProvisionerServer{
mux: s.mux,
p: p,
})
}
func (s *Server) RegisterUi(ui packer.Ui) {
s.server.RegisterName(DefaultUiEndpoint, &UiServer{
ui: ui,
})
}
// ServeConn serves a single connection over the RPC server. It is up
// to the caller to obtain a proper io.ReadWriteCloser.
func (s *Server) Serve() {
// Accept a connection on stream ID 0, which is always used for
// normal client to server connections.
stream, err := s.mux.Accept(s.streamId)
defer stream.Close()
if err != nil {
log.Printf("[ERR] Error retrieving stream for serving: %s", err)
return
}
s.server.ServeConn(stream)
}
// 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(server *rpc.Server, name string, rcvr interface{}, id bool) string {
endpoint := name
if id {
fmt.Sprintf("%s.%d", endpoint, atomic.AddUint64(&endpointId, 1))
}
server.RegisterName(endpoint, rcvr)
return endpoint
}