Yuvipanda has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/204009

Change subject: tools: Have uwsgi and nodejs attempt to pick a random port
......................................................................

tools: Have uwsgi and nodejs attempt to pick a random port

Instead of always using the uid for port.

Bug: T93046
Change-Id: Ib9395817f431d73820e08b893355327298f6963c
---
M modules/toollabs/files/portgrabber.py
M modules/toollabs/files/tool-nodejs
M modules/toollabs/files/tool-uwsgi-python
3 files changed, 23 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/09/204009/1

diff --git a/modules/toollabs/files/portgrabber.py 
b/modules/toollabs/files/portgrabber.py
index bc8eb6d..acd8960 100644
--- a/modules/toollabs/files/portgrabber.py
+++ b/modules/toollabs/files/portgrabber.py
@@ -7,16 +7,29 @@
 socks = []
 
 
-def getProxies():
+def get_proxies():
     """Return the list of proxies to register with."""
     with open('/etc/portgrabber.yaml', 'r') as f:
         config = yaml.safe_load(f)
         return config['proxies']
 
 
+def get_open_port():
+    """Tries to get a random open port to listen on
+
+    It does this by starting to listen on a socket, letting the kernel 
determine
+    the open port. It then immediately closes it and returns the port.
+    """
+    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    s.bind(('', 0))
+    port = s.getsockname()[1]
+    s.close()
+    return port
+
+
 def register(port):
     """Register with the proxies."""
-    for proxy in getProxies():
+    for proxy in get_proxies():
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sock.connect((proxy, 8282))
         sock.sendall(".*\nhttp://%s:%u\n"; % (socket.getfqdn(), port))
diff --git a/modules/toollabs/files/tool-nodejs 
b/modules/toollabs/files/tool-nodejs
index aaa8558..46e1b61 100644
--- a/modules/toollabs/files/tool-nodejs
+++ b/modules/toollabs/files/tool-nodejs
@@ -8,19 +8,17 @@
 
 import portgrabber
 
-pwd_entry = pwd.getpwuid(os.getuid())  # Use uid as reserved port for each tool
-TOOL = re.sub(r'^tools.', '', pwd_entry.pw_name) # Tool users are of form 
tools.TOOLNAME
-
-PORT = os.getuid()
+# Attempt to get an open port
+port = portgrabber.get_open_port()
 
 # Connect to the proxylistener instances on the web proxies and notify
 # them where requests for the tool need to be routed to.
-portgrabber.register(PORT)
+portgrabber.register(port)
 
 os.chdir(os.path.expanduser('~/www/js'))
 
 # Pass in all of our parent environment too
 # npm doesn't like it too much if you give it an almost empty environment
-os.environ['TOOL_WEB_PORT'] = str(PORT)
+os.environ['TOOL_WEB_PORT'] = str(port)
 
 os.execv('/usr/bin/npm', ['/usr/bin/npm', 'start'])
diff --git a/modules/toollabs/files/tool-uwsgi-python 
b/modules/toollabs/files/tool-uwsgi-python
index 05561c0..80c92fc 100755
--- a/modules/toollabs/files/tool-uwsgi-python
+++ b/modules/toollabs/files/tool-uwsgi-python
@@ -8,12 +8,13 @@
 pwd_entry = pwd.getpwuid(os.getuid())  # Use uid as reserved port for eacht ool
 TOOL = re.sub(r'^tools.', '', pwd_entry.pw_name) # Tool users are of form 
tools.TOOLNAME
 
-PORT = os.getuid()
+# Attempt to get an open port
+port = portgrabber.get_open_port()
 
 args = [
     '/usr/bin/uwsgi',
     '--plugin', 'python',
-    '--http-socket', ':' + str(PORT),
+    '--http-socket', ':' + str(port),
     '--chdir', os.path.expanduser('~/www/python/src'),
     '--logto', os.path.expanduser('~/uwsgi.log'),
     '--callable', 'app',
@@ -32,6 +33,6 @@
 
 # Connect to the proxylistener instances on the web proxies and notify
 # them where requests for the tool need to be routed to.
-portgrabber.register(PORT)
+portgrabber.register(port)
 
 os.execv('/usr/bin/uwsgi', args)

-- 
To view, visit https://gerrit.wikimedia.org/r/204009
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib9395817f431d73820e08b893355327298f6963c
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Yuvipanda <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to