Skip to content

Commit d57a636

Browse files
Merge pull request #218 from oracle/jira-wdt-36-problems-resolving-file
Jira wdt 36 problems resolving file
2 parents d83434a + 7e9be69 commit d57a636

File tree

6 files changed

+51
-26
lines changed

6 files changed

+51
-26
lines changed

core/src/main/python/create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def main(args):
362362
variable_map = {}
363363
if model_context.get_variable_file():
364364
variable_map = variables.load_variables(model_context.get_variable_file())
365-
variables.substitute(model, variable_map)
365+
variables.substitute(model, variable_map, model_context)
366366
except VariableException, ex:
367367
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
368368
class_name=_class_name, method_name=_method_name)

core/src/main/python/deploy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def main(args):
448448
variable_map = {}
449449
if model_context.get_variable_file():
450450
variable_map = variables.load_variables(model_context.get_variable_file())
451-
variables.substitute(model_dictionary, variable_map)
451+
variables.substitute(model_dictionary, variable_map, model_context)
452452
except VariableException, ex:
453453
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
454454
class_name=_class_name, method_name=_method_name)

core/src/main/python/update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def main(args):
466466
variable_map = {}
467467
if model_context.get_variable_file():
468468
variable_map = variables.load_variables(model_context.get_variable_file())
469-
variables.substitute(model_dictionary, variable_map)
469+
variables.substitute(model_dictionary, variable_map, model_context)
470470
except VariableException, ex:
471471
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
472472
class_name=_class_name, method_name=_method_name)

core/src/main/python/wlsdeploy/tool/validate/validator.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,17 @@ def __validate_model_file(self, model_dict, variables_file_name, archive_file_na
222222
if self._model_file_name is not None:
223223
self._logger.info('WLSDPLY-05003', self._model_file_name, class_name=_class_name, method_name=_method_name)
224224

225-
if variables_file_name is not None:
226-
self._logger.info('WLSDPLY-05004', variables_file_name, class_name=_class_name, method_name=_method_name)
227-
try:
225+
try:
226+
if variables_file_name is not None:
227+
self._logger.info('WLSDPLY-05004', variables_file_name, class_name=_class_name, method_name=_method_name)
228228
self._variable_properties = variables.load_variables(variables_file_name)
229-
variables.substitute(model_dict, self._variable_properties)
230-
except VariableException, ve:
231-
ex = exception_helper.create_validate_exception('WLSDPLY-20004', 'validateModel',
232-
ve.getLocalizedMessage(), error=ve)
233-
self._logger.throwing(ex, class_name=_class_name, method_name=_method_name)
234-
raise ex
229+
230+
variables.substitute(model_dict, self._variable_properties, self._model_context)
231+
except VariableException, ve:
232+
ex = exception_helper.create_validate_exception('WLSDPLY-20004', 'validateModel',
233+
ve.getLocalizedMessage(), error=ve)
234+
self._logger.throwing(ex, class_name=_class_name, method_name=_method_name)
235+
raise ex
235236

236237
if archive_file_name is not None:
237238
self._logger.info('WLSDPLY-05005', archive_file_name, class_name=_class_name, method_name=_method_name)

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
_variable_pattern = re.compile("\\$\\{[\w.-]+\\}")
2626
_file_variable_pattern = re.compile("@@FILE:[\w.\\\/:-]+@@")
2727
_property_pattern = re.compile("@@PROP:[\w.-]+@@")
28+
_file_nested_variable_pattern = re.compile("@@FILE:@@[\w]+@@[\w.\\\/:-]+@@")
2829

2930

3031
def load_variables(file_path):
@@ -126,20 +127,22 @@ def get_variable_names(text):
126127
return names
127128

128129

129-
def substitute(dictionary, variables):
130+
def substitute(dictionary, variables, model_context):
130131
"""
131132
Substitute fields in the specified dictionary with variable values.
132133
:param dictionary: the dictionary in which to substitute variables
133134
:param variables: a dictionary of variables for substitution
135+
:param model_context: used to resolve variables in file paths
134136
"""
135-
_process_node(dictionary, variables)
137+
_process_node(dictionary, variables, model_context)
136138

137139

138-
def _process_node(nodes, variables):
140+
def _process_node(nodes, variables, model_context):
139141
"""
140142
Process variables in the node.
141143
:param nodes: the dictionary to process
142144
:param variables: the variables to use
145+
:param model_context: used to resolve variables in file paths
143146
"""
144147
# iterate over copy to avoid concurrent change for add/delete
145148
if type(nodes) is OrderedDict:
@@ -150,22 +153,23 @@ def _process_node(nodes, variables):
150153
value = nodes[key]
151154

152155
# if the key changes with substitution, remove old key and map value to new key
153-
new_key = _substitute(key, variables)
156+
new_key = _substitute(key, variables, model_context)
154157
if new_key is not key:
155158
nodes.pop(key)
156159
nodes[new_key] = value
157160

158161
if isinstance(value, dict):
159-
_process_node(value, variables)
162+
_process_node(value, variables, model_context)
160163
elif type(value) is str:
161-
nodes[key] = _substitute(value, variables)
164+
nodes[key] = _substitute(value, variables, model_context)
162165

163166

164-
def _substitute(text, variables):
167+
def _substitute(text, variables, model_context):
165168
"""
166169
Substitute the variable placeholders with the variable value.
167170
:param text: the text to process for variable placeholders
168171
:param variables: the variables to use
172+
:param model_context: used to resolve variables in file paths
169173
:return: the replaced text
170174
"""
171175
method_name = '_substitute'
@@ -185,6 +189,8 @@ def _substitute(text, variables):
185189

186190
# skip lookups for text with no @@
187191
if '@@' in text:
192+
193+
# do properties first, to cover the case @@FILE:/dir/@@PROP:name@@.txt@@
188194
tokens = _property_pattern.findall(text)
189195
if tokens:
190196
for token in tokens:
@@ -204,6 +210,15 @@ def _substitute(text, variables):
204210
value = _read_value_from_file(path)
205211
text = text.replace(token, value)
206212

213+
# special case for @@FILE:@@ORACLE_HOME@@/dir/name.txt@@
214+
tokens = _file_nested_variable_pattern.findall(text)
215+
if tokens:
216+
for token in tokens:
217+
path = token[7:-2]
218+
path = model_context.replace_token_string(path)
219+
value = _read_value_from_file(path)
220+
text = text.replace(token, value)
221+
207222
return text
208223

209224

core/src/test/python/variables_test.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import wlsdeploy.util.variables as variables
88
from oracle.weblogic.deploy.util import VariableException
9+
from wlsdeploy.util.model_context import ModelContext
910
from wlsdeploy.util.model_translator import FileToPython
1011

1112

@@ -19,14 +20,17 @@ class VariablesTestCase(unittest.TestCase):
1920
def setUp(self):
2021
self.name = 'VariablesTestCase'
2122

23+
# create a context with resource directory as Oracle home, to support @@ORACLE_HOME@@ resolution
24+
self.model_context = ModelContext("test", {'-oracle_home': self._resources_dir})
25+
2226
def testReadVariables(self):
2327
variable_map = variables.load_variables(self._variables_file)
2428
self.assertEqual(variable_map['my-abc'], 'xyz')
2529

2630
def testSubstituteYaml(self):
2731
model = FileToPython(self._resources_dir + '/variables-test.yaml', self._use_ordering).parse()
2832
variable_map = variables.load_variables(self._variables_file)
29-
variables.substitute(model, variable_map)
33+
variables.substitute(model, variable_map, self.model_context)
3034
self.assertEqual(model['topology']['Name'], 'xyz123')
3135
self.assertEqual(model['topology']['Server']['s1']['ListenPort'], '1009')
3236
self.assertEqual(model['topology']['Server']['s2']['Cluster'], 'myCluster')
@@ -36,7 +40,7 @@ def testSubstituteYaml(self):
3640
def testSubstituteJson(self):
3741
model = FileToPython(self._resources_dir + '/variables-test.json', self._use_ordering).parse()
3842
variable_map = variables.load_variables(self._variables_file)
39-
variables.substitute(model, variable_map)
43+
variables.substitute(model, variable_map, self.model_context)
4044
self.assertEqual(model['topology']['Name'], 'xyz123')
4145
self.assertEqual(model['topology']['Server']['s1']['ListenPort'], '1009')
4246
self.assertEqual(model['topology']['Server']['s2']['Cluster'], 'myCluster')
@@ -51,7 +55,7 @@ def testVariableNotFound(self):
5155
model = FileToPython(self._resources_dir + '/variables-test.json', self._use_ordering).parse()
5256
model['topology']['Name'] = '${bad.variable}'
5357
variable_map = variables.load_variables(self._variables_file)
54-
variables.substitute(model, variable_map)
58+
variables.substitute(model, variable_map, self.model_context)
5559
self.assertEqual(model['topology']['Name'], '${bad.variable}')
5660

5761
def testPropertyNotFound(self):
@@ -62,7 +66,7 @@ def testPropertyNotFound(self):
6266
model = FileToPython(self._resources_dir + '/variables-test.json', self._use_ordering).parse()
6367
model['topology']['Name'] = '@@PROP:bad.variable@@'
6468
variable_map = variables.load_variables(self._variables_file)
65-
variables.substitute(model, variable_map)
69+
variables.substitute(model, variable_map, self.model_context)
6670
except VariableException:
6771
pass
6872
else:
@@ -71,19 +75,24 @@ def testPropertyNotFound(self):
7175
def testFileVariable(self):
7276
path = self._resources_dir + '/' + self._file_variable_name
7377
model = {'domainInfo': {'AdminUserName': '@@FILE:' + path + '@@'}}
74-
variables.substitute(model, {})
78+
variables.substitute(model, {}, self.model_context)
7579
self.assertEqual(model['domainInfo']['AdminUserName'], 'file-variable-value')
7680

7781
def testFileVariableWithVariable(self):
7882
model = {'domainInfo': {'AdminUserName': '@@FILE:${variable_dir}/' + self._file_variable_name + '@@'}}
79-
variables.substitute(model, {'variable_dir': self._resources_dir})
83+
variables.substitute(model, {'variable_dir': self._resources_dir}, self.model_context)
84+
self.assertEqual(model['domainInfo']['AdminUserName'], 'file-variable-value')
85+
86+
def testFileVariableWithConstant(self):
87+
model = {'domainInfo': {'AdminUserName': '@@FILE:@@ORACLE_HOME@@/' + self._file_variable_name + '@@'}}
88+
variables.substitute(model, {}, self.model_context)
8089
self.assertEqual(model['domainInfo']['AdminUserName'], 'file-variable-value')
8190

8291
def testFileVariableNotFound(self):
8392
try:
8493
path = self._resources_dir + '/no-file.txt'
8594
model = {'domainInfo': {'AdminUserName': '@@FILE:' + path + '@@'}}
86-
variables.substitute(model, {})
95+
variables.substitute(model, {}, self.model_context)
8796
self.assertEqual(model['domainInfo']['AdminUserName'], 'file-variable-value')
8897
except VariableException:
8998
pass

0 commit comments

Comments
 (0)