File tree Expand file tree Collapse file tree 4 files changed +62
-7
lines changed Expand file tree Collapse file tree 4 files changed +62
-7
lines changed Original file line number Diff line number Diff line change @@ -407,6 +407,8 @@ def main():
407
407
408
408
logger = mwi_logger .get (init = True )
409
409
410
+ app = create_app ()
411
+
410
412
logger .info ("Starting MATLAB proxy-app" )
411
413
logger .info (
412
414
f" with base_url: { os .environ [mwi_env .get_env_name_base_url ()]} and "
@@ -417,8 +419,6 @@ def main():
417
419
f"\n The webdesktop can be accessed on http://localhost:{ os .environ [mwi_env .get_env_name_app_port ()]} { os .environ [mwi_env .get_env_name_base_url ()]} /index.html"
418
420
)
419
421
420
- app = create_app ()
421
-
422
422
loop = asyncio .get_event_loop ()
423
423
424
424
# Override default loggers
Original file line number Diff line number Diff line change @@ -120,7 +120,9 @@ def get(dev=False):
120
120
],
121
121
"create_xvfb_cmd" : create_xvfb_cmd ,
122
122
"base_url" : os .environ [mwi_env .get_env_name_base_url ()],
123
- "app_port" : os .environ [mwi_env .get_env_name_app_port ()],
123
+ "app_port" : mwi_validators .validate_app_port_is_free (
124
+ os .environ [mwi_env .get_env_name_app_port ()]
125
+ ),
124
126
"host_interface" : os .environ .get (mwi_env .get_env_name_app_host ()),
125
127
"mwapikey" : str (uuid .uuid4 ()),
126
128
"matlab_protocol" : "https" ,
Original file line number Diff line number Diff line change 10
10
11
11
Exceptions are thrown to signal failure.
12
12
"""
13
+ from . import mwi_logger
14
+ from jupyter_matlab_proxy import mwi_environment_variables as mwi_env
15
+ import sys
16
+
17
+ logger = mwi_logger .get ()
13
18
14
19
15
20
def validate_mlm_license_file (nlm_conn_str ):
@@ -20,11 +25,8 @@ def validate_mlm_license_file(nlm_conn_str):
20
25
"""
21
26
import re
22
27
import os
23
- from . import mwi_logger
24
28
from .mwi_exceptions import NetworkLicensingError
25
29
26
- logger = mwi_logger .get ()
27
-
28
30
if nlm_conn_str is None :
29
31
return None
30
32
@@ -59,3 +61,34 @@ def validate_mlm_license_file(nlm_conn_str):
59
61
60
62
# Validation passed
61
63
return nlm_conn_str
64
+
65
+
66
+ def validate_app_port_is_free (port ):
67
+ """Validates and returns port if its free else will error out and exit.
68
+
69
+ Args:
70
+ port (str|int): Port number either as a string or an integer.
71
+
72
+ Raises:
73
+ e: socket.error
74
+
75
+ Returns:
76
+ Boolean: True if provided port is occupied else False.
77
+ """
78
+ import socket , errno
79
+
80
+ try :
81
+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
82
+ s .bind (("" , int (port )))
83
+ s .close ()
84
+
85
+ # Was able to allocate port. Validation passed.
86
+ return port
87
+ except socket .error as e :
88
+ if e .errno == errno .EADDRINUSE :
89
+ logger .error (
90
+ f"The port { port } is not available. Please set another value for the environment variable { mwi_env .get_env_name_app_port ()} "
91
+ )
92
+ sys .exit (1 )
93
+ else :
94
+ raise e
Original file line number Diff line number Diff line change 2
2
"""Tests for functions in jupyter_matlab_proxy/util/mwi_validators.py
3
3
"""
4
4
5
- import pytest , os , tempfile
5
+ import pytest , os , tempfile , socket
6
6
from jupyter_matlab_proxy .util import mwi_validators
7
7
from jupyter_matlab_proxy import mwi_environment_variables as mwi_env
8
8
from jupyter_matlab_proxy .util .mwi_exceptions import NetworkLicensingError
@@ -50,3 +50,23 @@ def test_get_with_environment_variables(monkeypatch):
50
50
assert conn_str == str (path )
51
51
finally :
52
52
os .remove (path )
53
+
54
+
55
+ def test_validate_app_port_is_free_false ():
56
+ """Test to validate if supplied app port is free"""
57
+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
58
+ s .bind (("" , 0 ))
59
+ port = s .getsockname ()[1 ]
60
+ with pytest .raises (SystemExit ) as e :
61
+ mwi_validators .validate_app_port_is_free (port )
62
+ assert e .value .code == 1
63
+ s .close ()
64
+
65
+
66
+ def test_validate_app_port_is_free_true ():
67
+ """Test to validate if supplied app port is free"""
68
+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
69
+ s .bind (("" , 0 ))
70
+ port = s .getsockname ()[1 ]
71
+ s .close ()
72
+ assert mwi_validators .validate_app_port_is_free (port ) == port
You can’t perform that action at this time.
0 commit comments