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
f0a09ffa
Commit
f0a09ffa
authored
May 03, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Crazy things with RPC servers and stuff
parent
0985d261
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
205 additions
and
0 deletions
+205
-0
packer/rpc/build.go
packer/rpc/build.go
+58
-0
packer/rpc/build_test.go
packer/rpc/build_test.go
+70
-0
packer/rpc/server.go
packer/rpc/server.go
+77
-0
No files found.
packer/rpc/build.go
0 → 100644
View file @
f0a09ffa
package
rpc
import
(
"github.com/mitchellh/packer/packer"
"net/rpc"
)
// An implementation of packer.Build where the build is actually executed
// over an RPC connection.
type
Build
struct
{
client
*
rpc
.
Client
}
// BuildServer wraps a packer.Build implementation and makes it exportable
// as part of a Golang RPC server.
type
BuildServer
struct
{
build
packer
.
Build
}
type
BuildPrepareArgs
interface
{}
type
BuildRunArgs
struct
{
UiRPCAddress
string
}
func
(
b
*
Build
)
Prepare
()
{
b
.
client
.
Call
(
"Build.Prepare"
,
new
(
interface
{}),
new
(
interface
{}))
}
func
(
b
*
Build
)
Run
(
ui
packer
.
Ui
)
{
// Create and start the server for the UI
server
:=
NewServer
()
server
.
RegisterUi
(
ui
)
server
.
Start
()
defer
server
.
Stop
()
args
:=
&
BuildRunArgs
{
server
.
Address
()}
b
.
client
.
Call
(
"Build.Run"
,
args
,
new
(
interface
{}))
}
func
(
b
*
BuildServer
)
Prepare
(
args
*
BuildPrepareArgs
,
reply
*
interface
{})
error
{
b
.
build
.
Prepare
()
*
reply
=
nil
return
nil
}
func
(
b
*
BuildServer
)
Run
(
args
*
BuildRunArgs
,
reply
*
interface
{})
error
{
client
,
err
:=
rpc
.
Dial
(
"tcp"
,
args
.
UiRPCAddress
)
if
err
!=
nil
{
return
err
}
b
.
build
.
Run
(
&
Ui
{
client
})
*
reply
=
nil
return
nil
}
packer/rpc/build_test.go
0 → 100644
View file @
f0a09ffa
package
rpc
import
(
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"testing"
)
type
testBuild
struct
{
prepareCalled
bool
runCalled
bool
runUi
packer
.
Ui
}
func
(
b
*
testBuild
)
Prepare
()
{
b
.
prepareCalled
=
true
}
func
(
b
*
testBuild
)
Run
(
ui
packer
.
Ui
)
{
b
.
runCalled
=
true
b
.
runUi
=
ui
}
func
TestBuildRPC
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
// Create the UI to test
b
:=
new
(
testBuild
)
bServer
:=
&
BuildServer
{
b
}
// Start the RPC server
readyChan
:=
make
(
chan
int
)
stopChan
:=
make
(
chan
int
)
defer
func
()
{
stopChan
<-
1
}()
go
testRPCServer
(
":1234"
,
"Build"
,
bServer
,
readyChan
,
stopChan
)
<-
readyChan
// Create the client over RPC and run some methods to verify it works
client
,
err
:=
rpc
.
Dial
(
"tcp"
,
":1234"
)
if
err
!=
nil
{
panic
(
err
)
}
// Test Prepare
bClient
:=
&
Build
{
client
}
bClient
.
Prepare
()
assert
.
True
(
b
.
prepareCalled
,
"prepare should be called"
)
// Test Run
ui
:=
new
(
testUi
)
bClient
.
Run
(
ui
)
assert
.
True
(
b
.
runCalled
,
"run should be called"
)
// Test the UI given to run, which should be fully functional
if
b
.
runCalled
{
b
.
runUi
.
Say
(
"format"
)
assert
.
True
(
ui
.
sayCalled
,
"say should be called"
)
assert
.
Equal
(
ui
.
sayFormat
,
"format"
,
"format should be correct"
)
}
}
func
TestBuild_ImplementsBuild
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
var
realBuild
packer
.
Build
b
:=
&
Build
{
nil
}
assert
.
Implementor
(
b
,
&
realBuild
,
"should be a Build"
)
}
packer/rpc/server.go
0 → 100644
View file @
f0a09ffa
package
rpc
import
(
"errors"
"github.com/mitchellh/packer/packer"
"net"
"net/rpc"
)
// A Server is a Golang RPC server that has helper methods for automatically
// setting up the endpoints for Packer interfaces.
type
Server
struct
{
server
*
rpc
.
Server
started
bool
doneChan
chan
bool
}
// Creates and returns a new Server.
func
NewServer
()
*
Server
{
return
&
Server
{
server
:
rpc
.
NewServer
(),
started
:
false
,
}
}
func
(
s
*
Server
)
Address
()
string
{
return
":2345"
}
func
(
s
*
Server
)
RegisterUi
(
ui
packer
.
Ui
)
{
s
.
server
.
RegisterName
(
"Ui"
,
&
UiServer
{
ui
})
}
func
(
s
*
Server
)
Start
()
error
{
if
s
.
started
{
return
errors
.
New
(
"Server already started."
)
}
// TODO: Address
address
:=
":2345"
// Mark that we started and setup the channel we'll use to mark exits
s
.
started
=
true
s
.
doneChan
=
make
(
chan
bool
)
// Start the TCP listener and a goroutine responsible for cleaning up the
// listener.
listener
,
_
:=
net
.
Listen
(
"tcp"
,
address
)
go
func
()
{
<-
s
.
doneChan
listener
.
Close
()
}()
// Start accepting connections
go
func
()
{
for
{
conn
,
err
:=
listener
.
Accept
()
if
err
!=
nil
{
break
}
go
s
.
server
.
ServeConn
(
conn
)
}
}()
return
nil
}
func
(
s
*
Server
)
Stop
()
{
if
s
.
started
{
// TODO: There is a race condition here, we need to wait for
// the listener to REALLY close.
s
.
doneChan
<-
true
s
.
started
=
false
s
.
doneChan
=
nil
}
}
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