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 @@ ...@@ -25,6 +25,7 @@
# #
############################################################################## ##############################################################################
import ConfigParser
import socket import socket
import netaddr import netaddr
...@@ -39,11 +40,30 @@ class Recipe(object): ...@@ -39,11 +40,30 @@ class Recipe(object):
""" """
def __init__(self, buildout, name, options): 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.minimum = int(options.get('minimum', 1024))
self.maximum = int(options.get('maximum', 49151)) self.maximum = int(options.get('maximum', 49151))
self.ip = options.get('ip') 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): if netaddr.valid_ipv4(self.ip):
self.inet_family = socket.AF_INET self.inet_family = socket.AF_INET
...@@ -51,10 +71,10 @@ class Recipe(object): ...@@ -51,10 +71,10 @@ class Recipe(object):
self.inet_family = socket.AF_INET6 self.inet_family = socket.AF_INET6
else: else:
# address family is unknown, so let's return a general purpose port # address family is unknown, so let's return a general purpose port
self.options['port'] = 0 self.options['port'] = str(0)
return return
self.options['port'] = self._getFreePort() self.options['port'] = str(self._getFreePort())
def _getFreePort(self): def _getFreePort(self):
""" """
...@@ -77,8 +97,4 @@ class Recipe(object): ...@@ -77,8 +97,4 @@ class Recipe(object):
return port return port
def install(self): install = update = lambda self: []
pass \ No newline at end of file
def update(self):
pass
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