Commit cf09c0eb authored by Nicolas Wavrant's avatar Nicolas Wavrant

free_port: new port shouldn't be returned everytime the instance is processed

parent d21c3415
......@@ -25,6 +25,7 @@
#
##############################################################################
import ConfigParser
import socket
import netaddr
......@@ -39,11 +40,30 @@ class Recipe(object):
"""
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()
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')
self.options = options.copy()
if self.minimum == self.maximum:
self.options['port'] = str(self.minimum)
return
if netaddr.valid_ipv4(self.ip):
self.inet_family = socket.AF_INET
......@@ -51,10 +71,10 @@ class Recipe(object):
self.inet_family = socket.AF_INET6
else:
# address family is unknown, so let's return a general purpose port
self.options['port'] = 0
self.options['port'] = str(0)
return
self.options['port'] = self._getFreePort()
self.options['port'] = str(self._getFreePort())
def _getFreePort(self):
"""
......@@ -77,8 +97,4 @@ class Recipe(object):
return port
def install(self):
pass
def update(self):
pass
install = update = lambda self: []
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment