Skip to content

Commit 6ec7f77

Browse files
authored
Issue #559 Implement environment variable lookup in model (#570)
1 parent 3d0da7c commit 6ec7f77

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

core/src/main/python/wlsdeploy/util/variables.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
_logger = platform_logger.PlatformLogger('wlsdeploy.variables')
2626
_file_variable_pattern = re.compile("@@FILE:[\w.\\\/:-]+@@")
2727
_property_pattern = re.compile("(@@PROP:([\\w.-]+)@@)")
28+
_environment_pattern = re.compile("(@@ENV:([\\w.-]+)@@)")
2829
_file_nested_variable_pattern = re.compile("@@FILE:@@[\w]+@@[\w.\\\/:-]+@@")
2930

3031

@@ -234,6 +235,23 @@ def _substitute(text, variables, model_context):
234235
value = variables[key]
235236
text = text.replace(token, value)
236237

238+
# check environment variables before @@FILE:/dir/@@ENV:name@@.txt@@
239+
matches = _environment_pattern.findall(text)
240+
for token, key in matches:
241+
# log, or throw an exception if key is not found.
242+
if not os.environ.has_key(key):
243+
if model_context.get_validation_method() == 'strict':
244+
_logger.severe('WLSDPLY-01737', key, class_name=_class_name, method_name=method_name)
245+
ex = exception_helper.create_variable_exception('WLSDPLY-01737', key)
246+
_logger.throwing(ex, class_name=_class_name, method_name=method_name)
247+
raise ex
248+
else:
249+
_logger.info('WLSDPLY-01737', key, class_name=_class_name, method_name=method_name)
250+
continue
251+
252+
value = os.environ.get(key)
253+
text = text.replace(token, value)
254+
237255
tokens = _file_variable_pattern.findall(text)
238256
if tokens:
239257
for token in tokens:

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ WLSDPLY-01733=Variable file {0} cannot be read: {1}
293293
WLSDPLY-01734=No value in variable file {0}
294294
WLSDPLY-01735=Variable substitution for {0} is deprecated, use @@PROP:{1}@@
295295
WLSDPLY-01736=Default variable file name {0}
296+
WLSDPLY-01737=Environment variable "{0}" was not found
296297

297298
# wlsdeploy/util/weblogic_helper.py
298299
WLSDPLY-01740=Encryption failed: Unable to locate SerializedSystemIni

core/src/test/python/variables_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"""
55
import unittest
66

7+
import os
8+
79
import wlsdeploy.util.variables as variables
810
from oracle.weblogic.deploy.util import VariableException
911
from wlsdeploy.util.model_context import ModelContext
@@ -88,6 +90,27 @@ def testFileVariableNotFound(self):
8890
else:
8991
self.fail('Test must raise VariableException when variable file is not found')
9092

93+
def testEnvironmentVariable(self):
94+
os.environ['envVariable'] = 'the-admin-user'
95+
model = {'domainInfo': {'AdminUserName': '@@ENV:envVariable@@'}}
96+
variables.substitute(model, {}, self.model_context)
97+
self.assertEqual(model['domainInfo']['AdminUserName'], 'the-admin-user')
98+
99+
def testFileVariableWithEnvironmentVariable(self):
100+
os.environ['variableDir'] = self._resources_dir
101+
model = {'domainInfo': {'AdminUserName': '@@FILE:@@ENV:variableDir@@/' + self._file_variable_name + '@@'}}
102+
variables.substitute(model, {}, self.model_context)
103+
self.assertEqual(model['domainInfo']['AdminUserName'], 'file-variable-value')
104+
105+
def testEnvironmentVariableNotFound(self):
106+
try:
107+
model = {'domainInfo': {'AdminUserName': '@@ENV:notaVariable@@'}}
108+
variables.substitute(model, {}, self.model_context)
109+
except VariableException:
110+
pass
111+
else:
112+
self.fail('Test must raise VariableException when variable file is not found')
113+
91114

92115
if __name__ == '__main__':
93116
unittest.main()

0 commit comments

Comments
 (0)