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
dbe53602
Commit
dbe53602
authored
May 04, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ability to get a net listener in a given port range
parent
f0a09ffa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
0 deletions
+61
-0
packer/rpc/build.go
packer/rpc/build.go
+1
-0
packer/rpc/port.go
packer/rpc/port.go
+30
-0
packer/rpc/port_test.go
packer/rpc/port_test.go
+30
-0
No files found.
packer/rpc/build.go
View file @
dbe53602
...
...
@@ -29,6 +29,7 @@ func (b *Build) Prepare() {
func
(
b
*
Build
)
Run
(
ui
packer
.
Ui
)
{
// Create and start the server for the UI
// TODO: Error handling
server
:=
NewServer
()
server
.
RegisterUi
(
ui
)
server
.
Start
()
...
...
packer/rpc/port.go
0 → 100644
View file @
dbe53602
package
rpc
import
(
"fmt"
"net"
)
var
portRangeMin
int
=
0
var
portRangeMax
int
=
0
// This sets the port range that the RPC stuff will use when creating
// new temporary servers. Some RPC calls require the creation of temporary
// RPC servers. These allow you to pick a range these bind to.
func
PortRange
(
min
,
max
int
)
{
portRangeMin
=
min
portRangeMax
=
max
}
// This finds an open port in the given range and returns a listener
// bound to that port.
func
netListenerInRange
(
min
,
max
int
)
net
.
Listener
{
for
port
:=
min
;
port
<=
max
;
port
++
{
l
,
err
:=
net
.
Listen
(
"tcp"
,
fmt
.
Sprintf
(
":%d"
,
port
))
if
err
==
nil
{
return
l
}
}
return
nil
}
packer/rpc/port_test.go
0 → 100644
View file @
dbe53602
package
rpc
import
(
"cgl.tideland.biz/asserts"
"net"
"strings"
"testing"
)
func
addrPort
(
address
net
.
Addr
)
string
{
parts
:=
strings
.
Split
(
address
.
String
(),
":"
)
return
parts
[
len
(
parts
)
-
1
]
}
func
Test_netListenerInRange
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
// Verify it selects an open port
L1000
,
err
:=
net
.
Listen
(
"tcp"
,
":10000"
)
defer
L1000
.
Close
()
assert
.
Nil
(
err
,
"should be able to bind to port 10000"
)
L
:=
netListenerInRange
(
10000
,
10005
)
assert
.
NotNil
(
L
,
"should have a listener"
)
assert
.
Equal
(
addrPort
(
L
.
Addr
()),
"10001"
,
"should bind to open port"
)
// Returns nil if there are no open ports
L
=
netListenerInRange
(
10000
,
10000
)
assert
.
Nil
(
L
,
"should not get a listener"
)
}
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