Skip to content

Commit 6f9f06d

Browse files
add capability to append or update an existing variable file with injector
1 parent 1486e54 commit 6f9f06d

File tree

8 files changed

+94
-51
lines changed

8 files changed

+94
-51
lines changed

core/src/main/python/discover.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from oracle.weblogic.deploy.util import WebLogicDeployToolingVersion
2323
from oracle.weblogic.deploy.util import WLSDeployArchive
2424
from oracle.weblogic.deploy.util import WLSDeployArchiveIOException
25-
from oracle.weblogic.deploy.validate import ValidateException
2625

2726
sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
2827

@@ -37,7 +36,6 @@
3736
from wlsdeploy.tool.discover.multi_tenant_discoverer import MultiTenantDiscoverer
3837
from wlsdeploy.tool.discover.resources_discoverer import ResourcesDiscoverer
3938
from wlsdeploy.tool.discover.topology_discoverer import TopologyDiscoverer
40-
from wlsdeploy.tool.validate.validator import Validator
4139
from wlsdeploy.tool.util import filter_helper
4240
from wlsdeploy.util import wlst_helper
4341
from wlsdeploy.util import model_translator
@@ -369,18 +367,20 @@ def __check_and_customize_model(model, model_context):
369367
if filter_helper.apply_filters(model.get_model(), "discover"):
370368
__logger.info('WLSDPLY-06014', _class_name=_class_name, method_name=_method_name)
371369

372-
inserted, variable_model, variable_file_name = VariableInjector(model.get_model(), model_context, WebLogicHelper(
373-
__logger).get_actual_weblogic_version()).inject_variables_keyword_file()
370+
inserted, variable_model, variable_file_name = VariableInjector(_program_name, model.get_model(), model_context,
371+
WebLogicHelper(
372+
__logger).get_actual_weblogic_version()).\
373+
inject_variables_keyword_file()
374374
if inserted:
375375
model = Model(variable_model)
376-
try:
377-
validator = Validator(model_context, wlst_mode=__wlst_mode)
378-
379-
# no variables are generated by the discover tool
380-
validator.validate_in_tool_mode(model.get_model(), variables_file_name=variable_file_name,
381-
archive_file_name=model_context.get_archive_file_name())
382-
except ValidateException, ex:
383-
__logger.warning('WLSDPLY-06015', ex.getLocalizedMessage(), class_name=_class_name, method_name=_method_name)
376+
# try:
377+
# validator = Validator(model_context, wlst_mode=__wlst_mode)
378+
#
379+
# # no variables are generated by the discover tool
380+
# validator.validate_in_tool_mode(model.get_model(), variables_file_name=variable_file_name,
381+
# archive_file_name=model_context.get_archive_file_name())
382+
# except ValidateException, ex:
383+
# __logger.warning('WLSDPLY-06015', ex.getLocalizedMessage(), class_name=_class_name, method_name=_method_name)
384384
return model
385385

386386

core/src/main/python/encrypt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def __encrypt_model_and_variables(model_context):
190190

191191
if variable_change_count > 0:
192192
try:
193-
variable_helper.write_variables(variables, variable_file)
193+
variable_helper.write_variables(_program_name, variables, variable_file)
194194
__logger.info('WLSDPLY-04209', _program_name, variable_change_count, variable_file,
195195
class_name=_class_name, method_name=_method_name)
196196
except VariableException, ve:

core/src/main/python/variable_inject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def __inject(model, model_context):
177177
:param model_context: the model context
178178
:return: True if variables were inserted into model: The updated model
179179
"""
180-
__kwargs[variable_injector.VARIABLE_FILE_APPEND_ARG] = True
181-
inserted, variable_model, variable_file_name = VariableInjector(model, model_context,
180+
__kwargs[variable_injector.VARIABLE_FILE_APPEND_ARG] = variable_injector.VARIABLE_FILE_UPDATE
181+
inserted, variable_model, variable_file_name = VariableInjector(_program_name, model, model_context,
182182
WebLogicHelper(
183183
__logger).get_actual_weblogic_version()). \
184184
inject_variables_keyword_file(**__kwargs)

core/src/main/python/wlsdeploy/tool/util/variable_injector.py

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import oracle.weblogic.deploy.aliases.AliasException as AliasException
1212
import oracle.weblogic.deploy.json.JsonException as JsonException
13-
import oracle.weblogic.deploy.util.PyOrderedDict as PyOrderedDict
1413
import oracle.weblogic.deploy.util.VariableException as VariableException
1514

1615
import wlsdeploy.util.model as model_sections
@@ -33,6 +32,9 @@
3332
VARIABLE_FILE_NAME_ARG = 'variable_file_name'
3433
VARIABLE_FILE_NAME = 'variables.json'
3534
VARIABLE_FILE_APPEND_ARG = 'append_to_variables'
35+
VARIABLE_FILE_APPEND = 'append'
36+
VARIABLE_FILE_UPDATE = 'update'
37+
VARIABLE_FILE_APPEND_VALS = [VARIABLE_FILE_APPEND, VARIABLE_FILE_UPDATE]
3638
# custom keyword in model injector file
3739
CUSTOM_KEYWORD = 'CUSTOM'
3840
KEYWORD_FILES = 'file-list'
@@ -59,7 +61,15 @@
5961

6062
class VariableInjector(object):
6163

62-
def __init__(self, model, model_context=None, version=None):
64+
def __init__(self, program_name, model, model_context=None, version=None):
65+
"""
66+
Construct an instance of the injector with the model and information used by the injector.
67+
:param program_name: name of the calling tool
68+
:param model: to be updated with variables
69+
:param model_context: context with command line information
70+
:param version: of model if model context is not provided
71+
"""
72+
self.__program_name = program_name
6373
self.__original = copy.deepcopy(model)
6474
self.__model = model
6575
self.__model_context = model_context
@@ -86,7 +96,7 @@ def inject_variables_keyword_file(self, **kwargs):
8696
_logger.entering(class_name=_class_name, method_name=_method_name)
8797

8898
variable_injector_location_file = _get_variable_injector_file_name(**kwargs)
89-
variables_injector_dictionary = _load_variables_file(variable_injector_location_file)
99+
variables_injector_dictionary = _load_variable_injector_file(variable_injector_location_file)
90100
variable_keywords_location_file = _get_variable_keywords_file_name(**kwargs)
91101
keywords_dictionary = _load_keywords_file(variable_keywords_location_file)
92102

@@ -101,12 +111,14 @@ def inject_variables_keyword_file(self, **kwargs):
101111
else:
102112
_logger.info('WLSDPLY-19533', variable_injector_location_file, class_name=_class_name,
103113
method_name=_method_name)
104-
variable_file_location = self._replace_tokens(variable_file_location)
114+
append, stage_dictionary = _load_variable_file(variable_file_location, **kwargs)
105115
injector_file_list = _create_injector_file_list(variables_injector_dictionary, keywords_dictionary,
106116
_get_keyword_files_location(**kwargs))
107117
variables_file_dictionary = self.inject_variables_keyword_dictionary(injector_file_list)
108-
variables_inserted = _write_variables_file(variables_file_dictionary, variable_file_location,
109-
_get_append_to_variable_file(**kwargs))
118+
if variables_file_dictionary:
119+
stage_dictionary.update(variables_file_dictionary)
120+
variables_inserted = self._write_variables_file(stage_dictionary, variable_file_location,
121+
append)
110122
if variables_inserted:
111123
_logger.info('WLSDPLY-19518', variable_file_location, class_name=_class_name,
112124
method_name=_method_name)
@@ -124,20 +136,20 @@ def inject_variables_keyword_file(self, **kwargs):
124136
def inject_variables_keyword_dictionary(self, injector_file_list):
125137
"""
126138
Takes a variable keyword dictionary and returns a variables for file in a dictionary
127-
:param injector_file_list:
128-
:return:
139+
:param injector_file_list: list of injector files for processing variable injection
140+
:return: variables_dictionary containing the variable properties to persist to the variable file
129141
"""
130142
_method_name = 'inject_variables_keyword_dictionary'
131143
_logger.entering(injector_file_list, class_name=_class_name, method_name=_method_name)
132-
variables_dictionary = PyOrderedDict()
144+
variable_dictionary = dict()
133145
for filename in injector_file_list:
134146
injector_dictionary = _load_injector_file(self._replace_tokens(filename))
135147
entries = self.inject_variables(injector_dictionary)
136148
if entries:
137149
_logger.finer('WLSDPLY-19513', filename, class_name=_class_name, method_name=_method_name)
138-
variables_dictionary.update(entries)
139-
_logger.exiting(class_name=_class_name, method_name=_method_name, result=variables_dictionary)
140-
return variables_dictionary
150+
variable_dictionary.update(entries)
151+
_logger.exiting(class_name=_class_name, method_name=_method_name, result=variable_dictionary)
152+
return variable_dictionary
141153

142154
def inject_variables(self, injector_dictionary):
143155
"""
@@ -422,7 +434,7 @@ def _log_mbean_not_found(self, mbean, replacement, location):
422434
code = ValidationCodes.INVALID
423435
try:
424436
code, __ = self.__aliases.is_valid_model_folder_name(location, mbean)
425-
except AliasException, ae:
437+
except AliasException:
426438
pass
427439
if code == ValidationCodes.INVALID:
428440
_logger.warning('WLSDPLY-19515', mbean, replacement, location.get_folder_path(),
@@ -444,8 +456,46 @@ def _get_variable_file_name(self, variables_injector_dictionary, **kwargs):
444456
_logger.finer('WLSDPLY-19521', variable_file_location, class_name=_class_name, method_name=_method_name)
445457
else:
446458
variable_file_location = variables.get_default_variable_file_name(self.__model_context)
459+
if variable_file_location:
460+
variable_file_location = self._replace_tokens(variable_file_location)
447461
return variable_file_location
448462

463+
def _write_variables_file(self, variables_dictionary, variables_file_name, append):
464+
_method_name = '_write_variables_file'
465+
_logger.entering(variables_dictionary, variables_file_name, class_name=_class_name, method_name=_method_name)
466+
467+
written = False
468+
if variables_dictionary:
469+
try:
470+
variables.write_variables(self.__program_name, variables_dictionary, variables_file_name, append)
471+
written = True
472+
except VariableException, ve:
473+
_logger.warning('WLSDPLY-19507', variables_file_name, ve.getLocalizedMessage(), class_name=_class_name,
474+
method_name=_method_name)
475+
_logger.exiting(class_name=_class_name, method_name=_method_name, result=written)
476+
return written
477+
478+
479+
def _load_variable_file(variable_file_location, **kwargs):
480+
_method_name = '_load_variable_file'
481+
append = False
482+
variable_dictionary = dict()
483+
if VARIABLE_FILE_APPEND_ARG in kwargs and kwargs[VARIABLE_FILE_APPEND_ARG] in VARIABLE_FILE_APPEND_VALS:
484+
_logger.fine('append argument found {0}', kwargs[VARIABLE_FILE_APPEND_ARG] )
485+
if kwargs[VARIABLE_FILE_APPEND_ARG] == VARIABLE_FILE_APPEND:
486+
_logger.fine('WLSDPLY-19536', variable_file_location, class_name=_class_name, method_name=_method_name)
487+
append = True
488+
elif kwargs[VARIABLE_FILE_APPEND_ARG] == VARIABLE_FILE_UPDATE and os.path.isfile(variable_file_location):
489+
_logger.fine('WLSDPLY-19534', variable_file_location, class_name=_class_name, method_name=_method_name)
490+
try:
491+
variable_dictionary = variables.load_variables(variable_file_location)
492+
except VariableException, ve:
493+
_logger.warning('WLSDPLY-19537', variable_file_location, ve.getLocalizedMessage(),
494+
class_name=_class_name, method_name=_method_name)
495+
else:
496+
_logger.fine('WLSDPLY-19535', variable_file_location, class_name=_class_name, method_name=_method_name)
497+
return append, variable_dictionary
498+
449499

450500
def _get_variable_injector_file_name(**kwargs):
451501
variable_injector_file_name = VARIABLE_INJECTOR_FILE_NAME
@@ -467,7 +517,7 @@ def _get_variable_keywords_file_name(**kwargs):
467517
return os.path.join(_wlsdeploy_location, DEFAULT_FILE_LOCATION, variable_keywords_file_name)
468518

469519

470-
def _load_variables_file(variable_injector_location):
520+
def _load_variable_injector_file(variable_injector_location):
471521
_method_name = '_load_variables_file'
472522
_logger.entering(variable_injector_location, class_name=_class_name, method_name=_method_name)
473523
variables_dictionary = None
@@ -555,20 +605,6 @@ def _get_append_to_variable_file(**kwargs):
555605
return False
556606

557607

558-
def _write_variables_file(variables_dictionary, variables_file_name, append):
559-
_method_name = '_write_variables_file'
560-
_logger.entering(variables_dictionary, variables_file_name, class_name=_class_name, method_name=_method_name)
561-
562-
written = False
563-
if variables_dictionary:
564-
try:
565-
variables.write_variables(variables_dictionary, variables_file_name, append)
566-
written = True
567-
except VariableException, ve:
568-
_logger.warning('WLSDPLY-19507', variables_file_name, ve.getLocalizedMessage(), class_name=_class_name,
569-
method_name=_method_name)
570-
_logger.exiting(class_name=_class_name, method_name=_method_name, result=written)
571-
return written
572608

573609

574610
def _format_variable_value(value):

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,23 @@ def load_variables(file_path):
5858
return variable_map
5959

6060

61-
def write_variables(variable_map, file_path, append=False):
61+
def write_variables(program_name, variable_map, file_path, append=False):
6262
"""
6363
Write the dictionary of variables to the specified file.
64+
:param program_name: name of tool that invoked the method which will be written to the variable properties file
6465
:param variable_map: the dictionary of variables
6566
:param file_path: the file to which to write the properties
6667
:param append: defaults to False. Append properties to the end of file
6768
:raises VariableException if an error occurs while storing the variables in the file
6869
"""
6970
_method_name = 'write_variables'
70-
_logger.entering(file_path, append, class_name=_class_name, method_name=_method_name)
71+
_logger.entering(program_name, file_path, append, class_name=_class_name, method_name=_method_name)
7172
props = Properties()
7273
for key in variable_map:
7374
value = variable_map[key]
7475
props.setProperty(key, value)
7576

76-
comment = exception_helper.get_message('WLSDPLY-01731')
77+
comment = exception_helper.get_message('WLSDPLY-01731', program_name)
7778
output_stream = None
7879
try:
7980
output_stream = FileOutputStream(File(file_path), Boolean(append))

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ WLSDPLY-01720=to_boolean() method called with non-boolean value {0} so returning
247247

248248
# wlsdeploy/util/variables.py
249249
WLSDPLY-01730=Failed to load variables file {0}: {1}
250-
WLSDPLY-01731=Variables updated
250+
WLSDPLY-01731=Variables updated by {0}
251251
WLSDPLY-01732=Variable {0} is not found in properties file
252252
WLSDPLY-01733=Variable file {0} cannot be read: {1}
253253
WLSDPLY-01734=No value in variable file {0}
@@ -1163,6 +1163,10 @@ WLSDPLY-19531=Invalid location {0} for variable injection into attribute {1} : {
11631163
WLSDPLY-19532=No model variable injector file {0}
11641164
WLSDPLY-19533=Will inject variable replacement strings into model using keyword directions found in model \
11651165
variable injector file {0}
1166+
WLSDPLY-19534=Update existing variable file {0} with injected variables
1167+
WLSDPLY-19535=Create new variable file {0} for injected variables
1168+
WLSDPLY-19536=Append existing variable file {0} with injected variables
1169+
WLSDPLY-19537=Unable to load variables from existing file {0} and will overwrite the file : {1}
11661170

11671171
# wlsdeploy/tool/variable_inject.py
11681172
WLSDPLY-19600=Use model variable injector file {0} from command line arguments

core/src/test/python/variable_injector_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
33
The Universal Permissive License (UPL), Version 1.0
44
"""
5+
import os
56
import unittest
67

78
import wlsdeploy.util.variables as variables
@@ -12,7 +13,7 @@
1213

1314
class VariableFileHelperTest(unittest.TestCase):
1415
_resources_dir = '../../test-classes'
15-
_variable_file = _resources_dir + '/variables.properties'
16+
_variable_file = _resources_dir + '/variable.injector.test.properties'
1617
_model_file = _resources_dir + '/variable_insertion.yaml'
1718
_variable_injector_keyword = 'variable_injector_keyword.json'
1819
_variable_injector_custom = 'variable_injector_custom.json'
@@ -21,7 +22,7 @@ class VariableFileHelperTest(unittest.TestCase):
2122
def setUp(self):
2223
self.name = VariableFileHelperTest
2324
self._model = FileToPython(self._model_file).parse()
24-
self._helper = VariableInjector(self._model, None, '12.2.1.3')
25+
self._helper = VariableInjector(self.name, self._model, None, '12.2.1.3')
2526

2627
def testSingleVariableReplacement(self):
2728
replacement_dict = dict()
@@ -223,9 +224,10 @@ def testWithVariableHelperKeywords(self):
223224
variable_injector_path_name=self._resources_dir,
224225
variable_injector_file_name=self._variable_injector_keyword,
225226
variable_keywords_path_name=self._resources_dir, variable_keywords_file_name=self._keywords_file)
226-
self.assertEqual(True, inserted)
227227
self.assertEqual(self._variable_file, variable_file_name)
228+
self.assertEqual(True, inserted)
228229
actual = variables.load_variables(self._variable_file)
230+
print actual
229231
self._compare_to_expected_dictionary(expected, actual)
230232

231233
def _compare_to_expected_dictionary(self, expected, actual):

core/src/test/resources/variable_injector_keyword.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"variable_file_name": "../../test-classes/variables.properties",
2+
"variable_file_name": "../../test-classes/variable.injector.test.properties",
33
"PORT": {},
44
"URL": {},
55
"CREDENTIALS": {}

0 commit comments

Comments
 (0)