Skip to content

Commit 7acaca7

Browse files
diningPhilosopher64Prabhakar Kumar
authored andcommitted
Set ddux environment variables
1 parent d837dec commit 7acaca7

File tree

8 files changed

+111
-9
lines changed

8 files changed

+111
-9
lines changed

matlab_proxy/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,34 @@ def get_executable_name():
2626
str: Name of the executable
2727
"""
2828
return "matlab-proxy-app"
29+
30+
31+
def __get_matlab_proxy_base_ddux_value():
32+
"""Returns the DDUX value for MATLAB use when launched its by matlab-proxy
33+
34+
Returns:
35+
str : DDUX value for MATLAB use.
36+
"""
37+
38+
return "MATLAB_PROXY:BASE:V1"
39+
40+
41+
def get_mwi_ddux_value(extension_name):
42+
"""Returns DDUX value for matlab-proxy based on the context from which
43+
it is being launched from.
44+
45+
Args:
46+
extension_name (str): The name of the extension/environment
47+
48+
Returns:
49+
str: DDUX value for matlab-proxy based on the environment.
50+
"""
51+
matlab_proxy_ddux_value = __get_matlab_proxy_base_ddux_value()
52+
53+
if extension_name == get_default_config_name():
54+
return matlab_proxy_ddux_value
55+
else:
56+
variant = extension_name.upper().strip()
57+
variant = variant.replace(" ", "_").replace("-", "_")
58+
mwi_ddux_value = matlab_proxy_ddux_value.replace("BASE", variant)
59+
return mwi_ddux_value

matlab_proxy/app.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ async def cleanup_background_tasks(app):
513513

514514

515515
# config is has a default initializer because it needs to be callable without inputs from ADEV servers
516-
def create_app(config=matlab_proxy.get_default_config_name()):
516+
def create_app(config_name=matlab_proxy.get_default_config_name()):
517517
"""Creates the web server and adds the routes,settings and env_config to the server.
518518
519519
Returns:
@@ -522,7 +522,9 @@ def create_app(config=matlab_proxy.get_default_config_name()):
522522
app = web.Application()
523523

524524
# Get application settings
525-
app["settings"] = settings.get(config, dev=mwi_env.is_development_mode_enabled())
525+
app["settings"] = settings.get(
526+
config_name, dev=mwi_env.is_development_mode_enabled()
527+
)
526528

527529
# Initialise application state
528530
app["state"] = AppState(app["settings"])
@@ -564,9 +566,9 @@ def main():
564566

565567
# The integration needs to be called with --config flag.
566568
# Parse the passed cli arguments.
567-
desired_configuration = util.parse_cli_args()["config"]
569+
desired_configuration_name = util.parse_cli_args()["config"]
568570

569-
app = create_app(desired_configuration)
571+
app = create_app(desired_configuration_name)
570572

571573
loop = asyncio.get_event_loop()
572574

matlab_proxy/app_state.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import socket
1212
import errno
1313
from collections import deque
14+
import matlab_proxy
1415
from matlab_proxy.util import mw, mwi_logger
1516
from matlab_proxy.util.mwi_exceptions import (
1617
LicensingError,
@@ -502,8 +503,10 @@ async def start_matlab(self, restart_matlab=False):
502503
matlab_env["MATLAB_LOG_DIR"] = str(self.matlab_ready_file_dir)
503504
# For r2020b, r2021a
504505
matlab_env["MW_CD_ANYWHERE_ENABLED"] = "true"
505-
# For > r2021b
506+
# For >= r2021b
506507
matlab_env["MW_CD_ANYWHERE_DISABLED"] = "false"
508+
matlab_env["MW_CONTEXT_TAGS"] = self.settings.get("mw_context_tags")
509+
507510
if self.licensing["type"] == "mhlm":
508511
matlab_env["MLM_WEB_LICENSE"] = "true"
509512
matlab_env["MLM_WEB_USER_CRED"] = access_token_data["token"]

matlab_proxy/default_configuration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# Use a single word for extension_name
1010
# It will be used as a flag when launching the integration.
1111
# NOTE: This name must be used when setting the entrypoint for matlab_proxy in setup.py
12+
# Use '-' or '_' seperated values if more than 1 word is used.
13+
# Ex: Hello-World, Alice_Bob.
1214
"extension_name": matlab_proxy.get_default_config_name(),
1315
# This value will be used in various places on the website UI.
1416
# Ensure that this is not more than 3 words.

matlab_proxy/settings.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ def get_dev_settings(config):
7474
"ssl_context": None,
7575
"mwi_logs_root_dir": mwi_config_folder / "ports",
7676
"mwi_proxy_lock_file_name": "mwi_proxy.lock",
77+
"mw_context_tags": get_mw_context_tags(matlab_proxy.get_default_config_name()),
7778
}
7879

7980

80-
def get(config=matlab_proxy.get_default_config_name(), dev=False):
81+
def get(config_name=matlab_proxy.get_default_config_name(), dev=False):
8182
"""Returns the settings specific to the environment in which the server is running in
8283
If the environment variable 'TEST' is set to true, will make some changes to the dev settings.
8384
@@ -90,7 +91,7 @@ def get(config=matlab_proxy.get_default_config_name(), dev=False):
9091
"""
9192

9293
if dev:
93-
settings = get_dev_settings(config)
94+
settings = get_dev_settings(config_name)
9495

9596
# If running tests using Pytest, it will set environment variable TEST to true before running tests.
9697
# Will make test env specific changes before returning the settings.
@@ -159,7 +160,7 @@ def get(config=matlab_proxy.get_default_config_name(), dev=False):
159160
"mhlm_api_endpoint": f"https://licensing{ws_env_suffix}.mathworks.com/mls/service/v1/entitlement/list",
160161
"mwa_login": f"https://login{ws_env_suffix}.mathworks.com",
161162
"mwi_custom_http_headers": mwi_custom_http_headers.get(),
162-
"env_config": mwi_validators.validate_env_config(config),
163+
"env_config": mwi_validators.validate_env_config(config_name),
163164
"ssl_context": get_ssl_context(
164165
ssl_cert_file=ssl_cert_file, ssl_key_file=ssl_key_file
165166
),
@@ -168,9 +169,34 @@ def get(config=matlab_proxy.get_default_config_name(), dev=False):
168169
"mwi_logs_root_dir": mwi_config_folder / "ports",
169170
# Name of the lock file which will be created by this instance of matlab-proxy process.
170171
"mwi_proxy_lock_file_name": "mwi_proxy.lock",
172+
"mw_context_tags": get_mw_context_tags(config_name),
171173
}
172174

173175

176+
def get_mw_context_tags(extension_name):
177+
"""Returns a string which combines existing MW_CONTEXT_TAGS value and context tags
178+
specific to where matlab-proxy is being launched from.
179+
180+
Returns:
181+
str: Which combines existing MW_CONTEXT_TAGS with one from matlab-proxy.
182+
"""
183+
existing_mw_context_tags = os.getenv("MW_CONTEXT_TAGS", "")
184+
185+
if existing_mw_context_tags:
186+
logger.debug(f'Existing MW_CONTEXT_TAGS:"{existing_mw_context_tags}"')
187+
existing_mw_context_tags += ","
188+
189+
mwi_context_tags = matlab_proxy.get_mwi_ddux_value(extension_name)
190+
logger.debug(f'DDUX value for matlab-proxy "{mwi_context_tags}"')
191+
192+
combined_context_tags = existing_mw_context_tags + mwi_context_tags
193+
logger.debug(
194+
f'Combined DDUX value to be used for MATLAB process: "{combined_context_tags}"'
195+
)
196+
197+
return combined_context_tags
198+
199+
174200
def create_xvfb_cmd():
175201
"""Creates the Xvfb command with a write descriptor.
176202

matlab_proxy/util/mwi_validators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def validate_base_url(base_url):
143143

144144
def validate_env_config(config):
145145
"""Validates config passed with available "matlab_proxy_configs" entry point in the same
146-
python environment.
146+
python environment. Computes DDUX value for MATLAB use.
147147
148148
Args:
149149
config (str): Name of the configuration to use.
@@ -169,6 +169,7 @@ def validate_env_config(config):
169169

170170
logger.debug(f"Successfully validated provided {config} configuration")
171171
return env_config
172+
172173
else:
173174
logger.error(
174175
f"{config} is not a valid config. Available configs are : {list(available_configs.keys())}"

tests/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
11
# Copyright 2020 The MathWorks, Inc.
2+
import matlab_proxy
3+
4+
5+
def test_get_mwi_ddux_value():
6+
"""Tests ddux value for matlab-proxy with different extension names"""
7+
expected_result = matlab_proxy.__get_matlab_proxy_base_ddux_value()
8+
actual_result = matlab_proxy.get_mwi_ddux_value(
9+
matlab_proxy.get_default_config_name()
10+
)
11+
12+
assert expected_result == actual_result
13+
14+
expected_result = "MATLAB_PROXY:HELLO_WORLD:V1"
15+
actual_result = matlab_proxy.get_mwi_ddux_value("hello world")
16+
17+
assert expected_result == actual_result
18+
19+
actual_result = matlab_proxy.get_mwi_ddux_value(" \n \t hello-world \n")
20+
assert expected_result == actual_result

tests/test_settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright 2021 The MathWorks, Inc.
22

3+
import matlab_proxy
34
import matlab_proxy.settings as settings
45
from matlab_proxy import mwi_environment_variables as mwi_env
56
import pytest
@@ -148,3 +149,20 @@ def test_get_dev_false(patch_env_variables, mock_shutil_which):
148149
assert _settings["matlab_cmd"][0] == "matlab"
149150
assert os.path.isdir(_settings["matlab_path"])
150151
assert _settings["matlab_protocol"] == "https"
152+
153+
154+
def test_get_mw_context_tags(monkeypatch):
155+
"""Tests get_mw_context_tags() function to return appropriate MW_CONTEXT_TAGS"""
156+
157+
# Monkeypatch env var MW_CONTEXT_TAGS to check for if condition
158+
dockerhub_mw_context_tags = "MATLAB:DOCKERHUB:V1"
159+
monkeypatch.setenv("MW_CONTEXT_TAGS", dockerhub_mw_context_tags)
160+
161+
extension_name = matlab_proxy.get_default_config_name()
162+
163+
matlab_proxy_base_context_tags = matlab_proxy.get_mwi_ddux_value(extension_name)
164+
expected_result = f"{dockerhub_mw_context_tags},{matlab_proxy_base_context_tags}"
165+
166+
actual_result = settings.get_mw_context_tags(extension_name)
167+
168+
assert expected_result == actual_result

0 commit comments

Comments
 (0)