Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
88f7b330
Commit
88f7b330
authored
May 08, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer/rpc: Rename Command to follow RPC style
parent
ac83cf65
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
62 deletions
+18
-62
packer/rpc/command.go
packer/rpc/command.go
+10
-10
packer/rpc/command_test.go
packer/rpc/command_test.go
+7
-51
packer/rpc/server.go
packer/rpc/server.go
+1
-1
No files found.
packer/rpc/command.go
View file @
88f7b330
...
...
@@ -5,15 +5,15 @@ import (
"net/rpc"
)
// A C
lientCommand is an implementation of the
Command interface where the
// A C
ommand is an implementation of the packer.
Command interface where the
// command is actually executed over an RPC connection.
type
ClientC
ommand
struct
{
type
c
ommand
struct
{
client
*
rpc
.
Client
}
// A
ServerCommand wraps a
Command and makes it exportable as part
// A
CommandServer wraps a packer.
Command and makes it exportable as part
// of a Golang RPC server.
type
ServerCommand
struct
{
type
CommandServer
struct
{
command
packer
.
Command
}
...
...
@@ -24,11 +24,11 @@ type CommandRunArgs struct {
type
CommandSynopsisArgs
byte
func
Command
(
client
*
rpc
.
Client
)
*
ClientC
ommand
{
return
&
ClientC
ommand
{
client
}
func
Command
(
client
*
rpc
.
Client
)
*
c
ommand
{
return
&
c
ommand
{
client
}
}
func
(
c
*
ClientC
ommand
)
Run
(
env
packer
.
Environment
,
args
[]
string
)
(
result
int
)
{
func
(
c
*
c
ommand
)
Run
(
env
packer
.
Environment
,
args
[]
string
)
(
result
int
)
{
// Create and start the server for the Environment
server
:=
rpc
.
NewServer
()
RegisterEnvironment
(
server
,
env
)
...
...
@@ -42,7 +42,7 @@ func (c *ClientCommand) Run(env packer.Environment, args []string) (result int)
return
}
func
(
c
*
ClientC
ommand
)
Synopsis
()
(
result
string
)
{
func
(
c
*
c
ommand
)
Synopsis
()
(
result
string
)
{
err
:=
c
.
client
.
Call
(
"Command.Synopsis"
,
CommandSynopsisArgs
(
0
),
&
result
)
if
err
!=
nil
{
panic
(
err
)
...
...
@@ -51,7 +51,7 @@ func (c *ClientCommand) Synopsis() (result string) {
return
}
func
(
c
*
ServerCommand
)
Run
(
args
*
CommandRunArgs
,
reply
*
int
)
error
{
func
(
c
*
CommandServer
)
Run
(
args
*
CommandRunArgs
,
reply
*
int
)
error
{
client
,
err
:=
rpc
.
Dial
(
"tcp"
,
args
.
RPCAddress
)
if
err
!=
nil
{
return
err
...
...
@@ -63,7 +63,7 @@ func (c *ServerCommand) Run(args *CommandRunArgs, reply *int) error {
return
nil
}
func
(
c
*
ServerCommand
)
Synopsis
(
args
*
CommandSynopsisArgs
,
reply
*
string
)
error
{
func
(
c
*
CommandServer
)
Synopsis
(
args
*
CommandSynopsisArgs
,
reply
*
string
)
error
{
*
reply
=
c
.
command
.
Synopsis
()
return
nil
}
packer/rpc/command_test.go
View file @
88f7b330
...
...
@@ -3,7 +3,6 @@ package rpc
import
(
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net"
"net/rpc"
"testing"
)
...
...
@@ -25,66 +24,23 @@ func (tc *TestCommand) Synopsis() string {
return
"foo"
}
// This starts a RPC server for the given command listening on the
// given address. The RPC server is ready when "readyChan" receives a message
// and the RPC server will quit when "stopChan" receives a message.
//
// This function should be run in a goroutine.
func
testCommandRPCServer
(
laddr
string
,
command
interface
{},
readyChan
chan
int
,
stopChan
<-
chan
int
)
{
listener
,
err
:=
net
.
Listen
(
"tcp"
,
laddr
)
if
err
!=
nil
{
panic
(
err
)
}
// Close the listener when we exit so that the RPC server ends
defer
listener
.
Close
()
// Start the RPC server
server
:=
rpc
.
NewServer
()
server
.
RegisterName
(
"Command"
,
command
)
go
func
()
{
for
{
conn
,
err
:=
listener
.
Accept
()
if
err
!=
nil
{
// If there is an error, just ignore it.
break
}
go
server
.
ServeConn
(
conn
)
}
}()
// We're ready!
readyChan
<-
1
// Block on waiting to receive from the channel
<-
stopChan
}
func
TestRPCCommand
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
// Create the command
command
:=
new
(
TestCommand
)
serverCommand
:=
&
ServerCommand
{
command
}
// Start the RPC server, and make sure to exit it at the end
// of the test.
readyChan
:=
make
(
chan
int
)
stopChan
:=
make
(
chan
int
)
defer
func
()
{
stopChan
<-
1
}()
go
testCommandRPCServer
(
":1234"
,
serverCommand
,
readyChan
,
stopChan
)
<-
readyChan
// Start the server
server
:=
rpc
.
NewServer
()
RegisterCommand
(
server
,
command
)
address
:=
serveSingleConn
(
server
)
// Create the command client over RPC and run some methods to verify
// we get the proper behavior.
client
,
err
:=
rpc
.
Dial
(
"tcp"
,
":1234"
)
if
err
!=
nil
{
panic
(
err
)
}
client
,
err
:=
rpc
.
Dial
(
"tcp"
,
address
)
assert
.
Nil
(
err
,
"should be no error"
)
clientComm
:=
&
ClientCommand
{
client
}
clientComm
:=
Command
(
client
)
// Test run
runArgs
:=
[]
string
{
"foo"
,
"bar"
}
...
...
packer/rpc/server.go
View file @
88f7b330
...
...
@@ -20,7 +20,7 @@ func RegisterBuilder(s *rpc.Server, b packer.Builder) {
// Registers the appropriate endpoint on an RPC server to serve a
// Packer Command.
func
RegisterCommand
(
s
*
rpc
.
Server
,
c
packer
.
Command
)
{
s
.
RegisterName
(
"Command"
,
&
ServerCommand
{
c
})
s
.
RegisterName
(
"Command"
,
&
CommandServer
{
c
})
}
// Registers the appropriate endpoint on an RPC server to serve a
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment