Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos-mynij-dev
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mynij
slapos-mynij-dev
Commits
7ed4b6f8
Commit
7ed4b6f8
authored
Aug 18, 2016
by
Nicolas Wavrant
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
recipe: new recipe to get a free network port
parent
3410e277
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
177 additions
and
0 deletions
+177
-0
setup.py
setup.py
+1
-0
slapos/recipe/free_port.py
slapos/recipe/free_port.py
+102
-0
slapos/test/recipe/test_free_port.py
slapos/test/recipe/test_free_port.py
+74
-0
No files found.
setup.py
View file @
7ed4b6f8
...
...
@@ -111,6 +111,7 @@ setup(name=name,
'erp5testnode = slapos.recipe.erp5testnode:Recipe'
,
'firefox = slapos.recipe.firefox:Recipe'
,
'fontconfig = slapos.recipe.fontconfig:Recipe'
,
'free_port = slapos.recipe.free_port:Recipe'
,
'generate.mac = slapos.recipe.generatemac:Recipe'
,
'generate.password = slapos.recipe.generatepassword:Recipe'
,
'generic.cloudooo = slapos.recipe.generic_cloudooo:Recipe'
,
...
...
slapos/recipe/free_port.py
0 → 100644
View file @
7ed4b6f8
##############################################################################
#
# Copyright (c) 2016 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import
ConfigParser
import
os
import
netaddr
import
socket
class
Recipe
(
object
):
"""
Uses the socket python standard library to get an unused port.
Notice : this recipe may still fail because of race condition : if a new
process spawns and use the picked port before the service for which it has
been generated starts, then the service won't start. Therefore, the result
would be the same giving an already-in-use port to the service.
"""
def
__init__
(
self
,
buildout
,
name
,
options
):
self
.
options
=
options
# If section has already been installed, port is already taken by the
# requested service itself.
# If this check isn't done, a new port would be picked for every upgrade
# of the software release
try
:
parser
=
ConfigParser
.
RawConfigParser
()
if
os
.
path
.
exists
(
buildout
[
'buildout'
][
'installed'
]):
with
open
(
buildout
[
'buildout'
][
'installed'
])
as
config_file
:
parser
.
readfp
(
config_file
)
port
=
parser
.
get
(
name
,
'port'
)
self
.
options
[
'port'
]
=
port
return
except
(
IOError
,
ConfigParser
.
NoSectionError
,
ConfigParser
.
NoOptionError
):
pass
# Otherwise, let's find one
self
.
minimum
=
int
(
options
.
get
(
'minimum'
,
1024
))
self
.
maximum
=
int
(
options
.
get
(
'maximum'
,
49151
))
self
.
ip
=
options
.
get
(
'ip'
)
if
self
.
minimum
==
self
.
maximum
:
self
.
options
[
'port'
]
=
str
(
self
.
minimum
)
return
if
netaddr
.
valid_ipv4
(
self
.
ip
):
self
.
inet_family
=
socket
.
AF_INET
elif
netaddr
.
valid_ipv6
(
self
.
ip
):
self
.
inet_family
=
socket
.
AF_INET6
else
:
# address family is unknown, so let's return a general purpose port
self
.
options
[
'port'
]
=
str
(
0
)
return
self
.
options
[
'port'
]
=
str
(
self
.
_getFreePort
())
def
_getFreePort
(
self
):
"""
Port number will be picked from a given range, smaller port first, then
incremented until a free one is found.
This algorithm thus returns always the same value with the same parameters in
a standard environment.
"""
for
port
in
xrange
(
self
.
minimum
,
self
.
maximum
):
sock
=
socket
.
socket
(
self
.
inet_family
,
socket
.
SOCK_STREAM
)
try
:
sock
.
bind
((
self
.
ip
,
port
))
break
except
socket
.
error
:
continue
finally
:
sock
.
close
()
else
:
port
=
0
return
port
install
=
update
=
lambda
self
:
[]
slapos/test/recipe/test_free_port.py
0 → 100644
View file @
7ed4b6f8
import
mock
import
socket
import
unittest
from
slapos.recipe
import
free_port
class
SocketMock
():
def
__init__
(
self
,
*
args
,
**
kw
):
self
.
args
=
args
self
.
kw
=
kw
pass
def
nothing_happen
(
self
,
*
args
,
**
kw
):
pass
bind
=
close
=
nothing_happen
import
sys
sys
.
modules
[
'socket'
].
socket
=
SocketMock
class
FreePortTest
(
unittest
.
TestCase
):
def
afterSetup
(
self
):
SocketMock
.
bind
=
SocketMock
.
close
=
SocketMock
.
nothing_happen
def
new_recipe
(
self
,
**
kw
):
buildout
=
{
'buildout'
:
{
'bin-directory'
:
''
,
'find-links'
:
''
,
'allow-hosts'
:
''
,
'develop-eggs-directory'
:
''
,
'eggs-directory'
:
''
,
'python'
:
'testpython'
,
'installed'
:
'.installed.cfg'
,
},
'testpython'
:
{
'executable'
:
sys
.
executable
,
},
'slap-connection'
:
{
'computer-id'
:
''
,
'partition-id'
:
''
,
'server-url'
:
''
,
'software-release-url'
:
''
,
}
}
options
=
{
'ip'
:
'127.0.0.1'
,
}
options
.
update
(
kw
)
return
free_port
.
Recipe
(
buildout
=
buildout
,
name
=
'free_port'
,
options
=
options
)
def
test_ifNoBusyPortThenMinPortIsAlwaysReturned
(
self
):
recipe
=
self
.
new_recipe
(
minimum
=
2000
)
self
.
assertEqual
(
recipe
.
options
[
'port'
],
'2000'
)
def
test_iterateUntilFreePortIsFound
(
self
):
def
bindFailExceptOnPort2020
(
socket_instance
,
binding
):
ip
,
port
=
binding
if
port
!=
2020
:
raise
socket
.
error
()
SocketMock
.
bind
=
bindFailExceptOnPort2020
recipe
=
self
.
new_recipe
(
minimum
=
2000
)
self
.
assertEqual
(
recipe
.
options
[
'port'
],
'2020'
)
def
test_returnsPort0IfNoPortIsFreeInRange
(
self
):
def
bindAlwaysFail
(
socket_instance
,
binding
):
raise
socket
.
error
()
SocketMock
.
bind
=
bindAlwaysFail
recipe
=
self
.
new_recipe
(
minimum
=
2000
,
maximum
=
2100
)
self
.
assertEqual
(
recipe
.
options
[
'port'
],
'0'
)
if
__name__
==
'__main__'
:
unittest
.
main
()
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