Browse Source

[IMP] port select

Gogs 7 years ago
parent
commit
811e886d58
2 changed files with 32 additions and 1 deletions
  1. 14 0
      api/utils/docker_api.py
  2. 18 1
      api/utils/odoo_api.py

+ 14 - 0
api/utils/docker_api.py

@@ -60,6 +60,20 @@ def get_all_containers():
 
 '''
 '''
+def get_all_external_ports():
+    ports = []
+
+    for m in map(lambda x: x['attrs']['NetworkSettings']['Ports'], get_all_containers()):
+        for _, v in m.iteritems():
+            if not v:
+                continue
+            
+            port = map(lambda x: x['HostPort'], v)
+            ports.extend(port)
+
+    return ports
+'''
+'''
 def run_container(image=None, name=None, ports=[], volumes=None, net=None):
     if not name:
         return False

+ 18 - 1
api/utils/odoo_api.py

@@ -3,10 +3,12 @@ from django.conf import settings
 from random import randint
 from jinja2 import Environment, PackageLoader, select_autoescape
 from api.utils.docker_api import (
+    get_all_external_ports,
     execute_command,
     run_container,
     copy_in
 )
+from api.utils.logger import info
 from api.utils.command import execute
 import os
 import socket
@@ -39,7 +41,7 @@ def randomize_port():
     ports = settings.ODOO_PORTS_RANGE
     port = 0
 
-    while not check_port(port):
+    while not check_port_from_docker(port):
         time.sleep(1)
         port = randint(ports[0], ports[1])
 
@@ -52,17 +54,32 @@ def check_port(port=0):
         return False
 
     ok = False
+    info('cheking port %d at %s' % (settings.EXTERNAL_IP, port))
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
     try:
         s.bind((settings.EXTERNAL_IP, port))
+        info('%s checked at %s is success' % (settings.EXTERNAL_IP, port))
         ok = True
     except socket.error:
+        info('%s checked at %s is failed' % (settings.EXTERNAL_IP, port))
         ok = False
+
     
     s.close()
     return ok
 
+'''
+'''
+def check_port_from_docker(port=0):
+    if port == 0:
+        return False
+
+    if port in get_all_external_ports():
+        return False
+    
+    return True
+
 '''
 '''
 def check_name(name=None):