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
9262a85e
Commit
9262a85e
authored
May 12, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer/rpc: Communicator.Upload
parent
daa431af
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
3 deletions
+65
-3
packer/rpc/communicator.go
packer/rpc/communicator.go
+41
-2
packer/rpc/communicator_test.go
packer/rpc/communicator_test.go
+24
-1
No files found.
packer/rpc/communicator.go
View file @
9262a85e
...
@@ -34,6 +34,11 @@ type CommunicatorStartResponse struct {
...
@@ -34,6 +34,11 @@ type CommunicatorStartResponse struct {
RemoteCommandAddress
string
RemoteCommandAddress
string
}
}
type
CommunicatorUploadArgs
struct
{
Path
string
ReaderAddress
string
}
func
Communicator
(
client
*
rpc
.
Client
)
*
communicator
{
func
Communicator
(
client
*
rpc
.
Client
)
*
communicator
{
return
&
communicator
{
client
}
return
&
communicator
{
client
}
}
}
...
@@ -87,8 +92,30 @@ func (c *communicator) Start(cmd string) (rc *packer.RemoteCommand, err error) {
...
@@ -87,8 +92,30 @@ func (c *communicator) Start(cmd string) (rc *packer.RemoteCommand, err error) {
return
return
}
}
func
(
c
*
communicator
)
Upload
(
string
,
io
.
Reader
)
error
{
func
(
c
*
communicator
)
Upload
(
path
string
,
r
io
.
Reader
)
(
err
error
)
{
return
nil
readerL
:=
netListenerInRange
(
portRangeMin
,
portRangeMax
)
if
readerL
==
nil
{
err
=
errors
.
New
(
"couldn't allocate listener for upload reader"
)
return
}
// Make sure at the end of this call, we close the listener
defer
readerL
.
Close
()
// Pipe the reader through to the connection
go
serveSingleCopy
(
"uploadReader"
,
readerL
,
nil
,
r
)
args
:=
CommunicatorUploadArgs
{
path
,
readerL
.
Addr
()
.
String
(),
}
cerr
:=
c
.
client
.
Call
(
"Communicator.Upload"
,
&
args
,
&
err
)
if
cerr
!=
nil
{
err
=
cerr
}
return
}
}
func
(
c
*
communicator
)
Download
(
string
,
io
.
Writer
)
error
{
func
(
c
*
communicator
)
Download
(
string
,
io
.
Writer
)
error
{
...
@@ -133,6 +160,18 @@ func (c *CommunicatorServer) Start(cmd *string, reply *CommunicatorStartResponse
...
@@ -133,6 +160,18 @@ func (c *CommunicatorServer) Start(cmd *string, reply *CommunicatorStartResponse
return
return
}
}
func
(
c
*
CommunicatorServer
)
Upload
(
args
*
CommunicatorUploadArgs
,
reply
*
interface
{})
(
err
error
)
{
readerC
,
err
:=
net
.
Dial
(
"tcp"
,
args
.
ReaderAddress
)
if
err
!=
nil
{
return
}
defer
readerC
.
Close
()
err
=
c
.
c
.
Upload
(
args
.
Path
,
readerC
)
return
}
func
(
rc
*
RemoteCommandServer
)
Wait
(
args
*
interface
{},
reply
*
int
)
error
{
func
(
rc
*
RemoteCommandServer
)
Wait
(
args
*
interface
{},
reply
*
int
)
error
{
rc
.
rc
.
Wait
()
rc
.
rc
.
Wait
()
*
reply
=
rc
.
rc
.
ExitStatus
*
reply
=
rc
.
rc
.
ExitStatus
...
...
packer/rpc/communicator_test.go
View file @
9262a85e
...
@@ -18,6 +18,10 @@ type testCommunicator struct {
...
@@ -18,6 +18,10 @@ type testCommunicator struct {
startErr
*
io
.
PipeWriter
startErr
*
io
.
PipeWriter
startExited
*
bool
startExited
*
bool
startExitStatus
*
int
startExitStatus
*
int
uploadCalled
bool
uploadPath
string
uploadReader
io
.
Reader
}
}
func
(
t
*
testCommunicator
)
Start
(
cmd
string
)
(
*
packer
.
RemoteCommand
,
error
)
{
func
(
t
*
testCommunicator
)
Start
(
cmd
string
)
(
*
packer
.
RemoteCommand
,
error
)
{
...
@@ -45,7 +49,11 @@ func (t *testCommunicator) Start(cmd string) (*packer.RemoteCommand, error) {
...
@@ -45,7 +49,11 @@ func (t *testCommunicator) Start(cmd string) (*packer.RemoteCommand, error) {
return
rc
,
nil
return
rc
,
nil
}
}
func
(
t
*
testCommunicator
)
Upload
(
string
,
io
.
Reader
)
error
{
func
(
t
*
testCommunicator
)
Upload
(
path
string
,
reader
io
.
Reader
)
error
{
t
.
uploadCalled
=
true
t
.
uploadPath
=
path
t
.
uploadReader
=
reader
return
nil
return
nil
}
}
...
@@ -99,6 +107,21 @@ func TestCommunicatorRPC(t *testing.T) {
...
@@ -99,6 +107,21 @@ func TestCommunicatorRPC(t *testing.T) {
*
c
.
startExited
=
true
*
c
.
startExited
=
true
rc
.
Wait
()
rc
.
Wait
()
assert
.
Equal
(
rc
.
ExitStatus
,
42
,
"should have proper exit status"
)
assert
.
Equal
(
rc
.
ExitStatus
,
42
,
"should have proper exit status"
)
// Test that we can upload things
uploadR
,
uploadW
:=
io
.
Pipe
()
err
=
remote
.
Upload
(
"foo"
,
uploadR
)
assert
.
Nil
(
err
,
"should not error"
)
assert
.
True
(
c
.
uploadCalled
,
"should be called"
)
assert
.
Equal
(
c
.
uploadPath
,
"foo"
,
"should be correct path"
)
return
// Test the upload reader
uploadW
.
Write
([]
byte
(
"uploadfoo
\n
"
))
bufUpR
:=
bufio
.
NewReader
(
c
.
uploadReader
)
data
,
err
=
bufUpR
.
ReadString
(
'\n'
)
assert
.
Nil
(
err
,
"should not error"
)
assert
.
Equal
(
data
,
"uploadfoo
\n
"
,
"should have the proper data"
)
}
}
func
TestCommunicator_ImplementsCommunicator
(
t
*
testing
.
T
)
{
func
TestCommunicator_ImplementsCommunicator
(
t
*
testing
.
T
)
{
...
...
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