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
94cdedf4
Commit
94cdedf4
authored
May 03, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move RPC stuff into a new "packer/rpc" package
parent
b1993dc2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
159 additions
and
120 deletions
+159
-120
packer/command.go
packer/command.go
+0
-43
packer/command_test.go
packer/command_test.go
+0
-77
packer/rpc/command.go
packer/rpc/command.go
+47
-0
packer/rpc/command_test.go
packer/rpc/command_test.go
+112
-0
No files found.
packer/command.go
View file @
94cdedf4
package
packer
import
"net/rpc"
// A command is a runnable sub-command of the `packer` application.
// When `packer` is called with the proper subcommand, this will be
// called.
...
...
@@ -19,44 +17,3 @@ type Command interface {
Run
(
env
Environment
,
args
[]
string
)
int
Synopsis
()
string
}
// An RPCCommand is an implementation of the Command interface where the
// command is actually executed over an RPC connection.
type
RPCClientCommand
struct
{
client
*
rpc
.
Client
}
// An RPCServerCommand wraps a Command and makes it exportable as part
// of a Golang RPC server.
type
RPCServerCommand
struct
{
command
Command
}
type
RPCCommandRunArgs
struct
{
Env
Environment
Args
[]
string
}
type
RPCCommandSynopsisArgs
byte
func
(
c
*
RPCClientCommand
)
Run
(
env
Environment
,
args
[]
string
)
(
result
int
)
{
// TODO: Environment
rpcArgs
:=
&
RPCCommandRunArgs
{
nil
,
args
}
c
.
client
.
Call
(
"Command.Run"
,
rpcArgs
,
&
result
)
return
}
func
(
c
*
RPCClientCommand
)
Synopsis
()
(
result
string
)
{
c
.
client
.
Call
(
"Command.Synopsis"
,
RPCCommandSynopsisArgs
(
0
),
&
result
)
return
}
func
(
c
*
RPCServerCommand
)
Run
(
args
*
RPCCommandRunArgs
,
reply
*
int
)
error
{
*
reply
=
c
.
command
.
Run
(
args
.
Env
,
args
.
Args
)
return
nil
}
func
(
c
*
RPCServerCommand
)
Synopsis
(
args
*
RPCCommandSynopsisArgs
,
reply
*
string
)
error
{
*
reply
=
c
.
command
.
Synopsis
()
return
nil
}
packer/command_test.go
View file @
94cdedf4
package
packer
import
(
"cgl.tideland.biz/asserts"
"net"
"net/rpc"
"testing"
)
type
TestCommand
struct
{
runArgs
[]
string
runCalled
bool
...
...
@@ -23,73 +16,3 @@ func (tc *TestCommand) Run(env Environment, args []string) int {
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
:=
&
RPCServerCommand
{
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
// 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
)
}
clientComm
:=
&
RPCClientCommand
{
client
}
runArgs
:=
[]
string
{
"foo"
,
"bar"
}
testEnv
:=
testEnvironment
()
exitCode
:=
clientComm
.
Run
(
testEnv
,
runArgs
)
synopsis
:=
clientComm
.
Synopsis
()
assert
.
Equal
(
command
.
runArgs
,
runArgs
,
"Correct args should be sent"
)
assert
.
Equal
(
exitCode
,
0
,
"Exit code should be correct"
)
assert
.
Equal
(
synopsis
,
"foo"
,
"Synopsis should be correct"
)
}
packer/rpc/command.go
0 → 100644
View file @
94cdedf4
package
rpc
import
(
"github.com/mitchellh/packer/packer"
"net/rpc"
)
// A ClientCommand is an implementation of the Command interface where the
// command is actually executed over an RPC connection.
type
ClientCommand
struct
{
client
*
rpc
.
Client
}
// A ServerCommand wraps a Command and makes it exportable as part
// of a Golang RPC server.
type
ServerCommand
struct
{
command
packer
.
Command
}
type
CommandRunArgs
struct
{
Env
packer
.
Environment
Args
[]
string
}
type
CommandSynopsisArgs
byte
func
(
c
*
ClientCommand
)
Run
(
env
packer
.
Environment
,
args
[]
string
)
(
result
int
)
{
// TODO: Environment
rpcArgs
:=
&
CommandRunArgs
{
nil
,
args
}
c
.
client
.
Call
(
"Command.Run"
,
rpcArgs
,
&
result
)
return
}
func
(
c
*
ClientCommand
)
Synopsis
()
(
result
string
)
{
c
.
client
.
Call
(
"Command.Synopsis"
,
CommandSynopsisArgs
(
0
),
&
result
)
return
}
func
(
c
*
ServerCommand
)
Run
(
args
*
CommandRunArgs
,
reply
*
int
)
error
{
*
reply
=
c
.
command
.
Run
(
args
.
Env
,
args
.
Args
)
return
nil
}
func
(
c
*
ServerCommand
)
Synopsis
(
args
*
CommandSynopsisArgs
,
reply
*
string
)
error
{
*
reply
=
c
.
command
.
Synopsis
()
return
nil
}
packer/rpc/command_test.go
0 → 100644
View file @
94cdedf4
package
rpc
import
(
"bytes"
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net"
"net/rpc"
"testing"
)
type
TestCommand
struct
{
runArgs
[]
string
runCalled
bool
runEnv
packer
.
Environment
}
func
(
tc
*
TestCommand
)
Run
(
env
packer
.
Environment
,
args
[]
string
)
int
{
tc
.
runCalled
=
true
tc
.
runArgs
=
args
tc
.
runEnv
=
env
return
0
}
func
(
tc
*
TestCommand
)
Synopsis
()
string
{
return
"foo"
}
func
testEnvironment
()
packer
.
Environment
{
config
:=
&
packer
.
EnvironmentConfig
{}
config
.
Ui
=
&
packer
.
ReaderWriterUi
{
new
(
bytes
.
Buffer
),
new
(
bytes
.
Buffer
),
}
env
,
err
:=
packer
.
NewEnvironment
(
config
)
if
err
!=
nil
{
panic
(
err
)
}
return
env
}
// 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
// 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
)
}
clientComm
:=
&
ClientCommand
{
client
}
runArgs
:=
[]
string
{
"foo"
,
"bar"
}
testEnv
:=
testEnvironment
()
exitCode
:=
clientComm
.
Run
(
testEnv
,
runArgs
)
synopsis
:=
clientComm
.
Synopsis
()
assert
.
Equal
(
command
.
runArgs
,
runArgs
,
"Correct args should be sent"
)
assert
.
Equal
(
exitCode
,
0
,
"Exit code should be correct"
)
assert
.
Equal
(
synopsis
,
"foo"
,
"Synopsis should be correct"
)
}
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