Skip to content

Commit ccc67a9

Browse files
committed
Issue#54 - Add capability to inline variables by specifying @@file:/<dir>/<name>@@ for variable value. Fixes #54
1 parent 0cbc9c1 commit ccc67a9

File tree

6 files changed

+82
-16
lines changed

6 files changed

+82
-16
lines changed

core/src/main/python/create.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,16 @@ def main():
326326
__clean_up_temp_files()
327327
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
328328

329-
if model_context.get_variable_file():
330-
try:
329+
try:
330+
variable_map = {}
331+
if model_context.get_variable_file():
331332
variable_map = variables.load_variables(model_context.get_variable_file())
332-
variables.substitute(model, variable_map)
333-
except VariableException, ex:
334-
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
335-
class_name=_class_name, method_name=_method_name)
336-
__clean_up_temp_files()
337-
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
333+
variables.substitute(model, variable_map)
334+
except VariableException, ex:
335+
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
336+
class_name=_class_name, method_name=_method_name)
337+
__clean_up_temp_files()
338+
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
338339

339340
aliases = Aliases(model_context, wlst_mode=__wlst_mode)
340341
validate_model(model, model_context, aliases)

core/src/main/python/deploy.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -445,15 +445,16 @@ def main():
445445
__clean_up_temp_files()
446446
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
447447

448-
if model_context.get_variable_file():
449-
try:
448+
try:
449+
variable_map = {}
450+
if model_context.get_variable_file():
450451
variable_map = variables.load_variables(model_context.get_variable_file())
451-
variables.substitute(model_dictionary, variable_map)
452-
except VariableException, ex:
453-
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
454-
class_name=_class_name, method_name=_method_name)
455-
__clean_up_temp_files()
456-
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
452+
variables.substitute(model_dictionary, variable_map)
453+
except VariableException, ex:
454+
__logger.severe('WLSDPLY-20004', _program_name, ex.getLocalizedMessage(), error=ex,
455+
class_name=_class_name, method_name=_method_name)
456+
__clean_up_temp_files()
457+
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
457458

458459
aliases = Aliases(model_context, wlst_mode=__wlst_mode)
459460
validate_model(model_dictionary, model_context, aliases)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
"""
55
import re
66

7+
from java.io import BufferedReader
78
from java.io import FileInputStream
89
from java.io import FileOutputStream
10+
from java.io import FileReader
911
from java.io import IOException
1012
from java.util import Properties
1113

@@ -17,6 +19,7 @@
1719
_class_name = "variables"
1820
_logger = platform_logger.PlatformLogger('wlsdeploy.variables')
1921
_variable_pattern = re.compile("\\$\\{[\w.-]+\\}")
22+
_file_variable_pattern = re.compile("@@FILE:[\w.\\\/:-]+@@")
2023

2124

2225
def load_variables(file_path):
@@ -150,5 +153,38 @@ def _substitute(text, variables):
150153
value = variables[key]
151154
text = text.replace(token, value)
152155

156+
if '@@FILE:' in text:
157+
tokens = _file_variable_pattern.findall(text)
158+
if tokens:
159+
for token in tokens:
160+
path = token[7:-2]
161+
value = _read_value_from_file(path)
162+
text = text.replace(token, value)
163+
153164
return text
154165

166+
167+
def _read_value_from_file(file_path):
168+
"""
169+
Read a single text value from the first line in the specified file.
170+
:param file_path: the file from which to read the value
171+
:return: the text value
172+
:raises BundleAwareException if an error occurs while reading the value
173+
"""
174+
method_name = '_read_value_from_file'
175+
176+
try:
177+
file_reader = BufferedReader(FileReader(file_path))
178+
line = file_reader.readLine()
179+
file_reader.close()
180+
except IOException, e:
181+
ex = exception_helper.create_variable_exception('WLSDPLY-01733', file_path, e.getLocalizedMessage(), error=e)
182+
_logger.throwing(ex, class_name=_class_name, method_name=method_name)
183+
raise ex
184+
185+
if line is None:
186+
ex = exception_helper.create_variable_exception('WLSDPLY-01734', file_path)
187+
_logger.throwing(ex, class_name=_class_name, method_name=method_name)
188+
raise ex
189+
190+
return str(line).strip()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ WLSDPLY-01720=to_boolean() method called with non-boolean value {0} so returning
247247
WLSDPLY-01730=Failed to load variables file {0}: {1}
248248
WLSDPLY-01731=Variables updated after encryption
249249
WLSDPLY-01732=Variable {0} is not found in properties file
250+
WLSDPLY-01733=Variable file {0} cannot be read: {1}
251+
WLSDPLY-01734=No value in variable file {0}
250252

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

core/src/test/python/variables_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
class VariablesTestCase(unittest.TestCase):
1313
_resources_dir = '../../test-classes'
1414
_variables_file = _resources_dir + '/variables.properties'
15+
_file_variable_name = 'file-variable.txt'
16+
_file_variable_path = _resources_dir + '/' + _file_variable_name
1517
_use_ordering = True
1618

1719
def setUp(self):
@@ -48,5 +50,28 @@ def testVariableNotFound(self):
4850
else:
4951
self.fail('Test must raise VariableException when variable is not found')
5052

53+
def testFileVariable(self):
54+
path = self._resources_dir + '/' + self._file_variable_name
55+
model = {'domainInfo': {'AdminUserName': '@@FILE:' + path + '@@'}}
56+
variables.substitute(model, {})
57+
self.assertEqual(model['domainInfo']['AdminUserName'], 'file-variable-value')
58+
59+
def testFileVariableWithVariable(self):
60+
model = {'domainInfo': {'AdminUserName': '@@FILE:${variable_dir}/' + self._file_variable_name + '@@'}}
61+
variables.substitute(model, {'variable_dir': self._resources_dir})
62+
self.assertEqual(model['domainInfo']['AdminUserName'], 'file-variable-value')
63+
64+
def testFileVariableNotFound(self):
65+
try:
66+
path = self._resources_dir + '/no-file.txt'
67+
model = {'domainInfo': {'AdminUserName': '@@FILE:' + path + '@@'}}
68+
variables.substitute(model, {})
69+
self.assertEqual(model['domainInfo']['AdminUserName'], 'file-variable-value')
70+
except VariableException:
71+
pass
72+
else:
73+
self.fail('Test must raise VariableException when variable file is not found')
74+
75+
5176
if __name__ == '__main__':
5277
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file-variable-value

0 commit comments

Comments
 (0)