Skip to content

Commit 22da070

Browse files
authored
Merge pull request #30 from oracle/issue#27-add-filter-capability
Issue#27 add filter capability
2 parents 56bc804 + 542c5d0 commit 22da070

File tree

4 files changed

+191
-37
lines changed

4 files changed

+191
-37
lines changed

core/src/main/python/create.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from wlsdeploy.logging.platform_logger import PlatformLogger
3333
from wlsdeploy.tool.create.domain_creator import DomainCreator
3434
from wlsdeploy.tool.create.domain_typedef import DomainTypedef
35+
from wlsdeploy.tool.util import filter_helper
3536
from wlsdeploy.tool.validate.validator import Validator
3637
from wlsdeploy.util import getcreds
3738
from wlsdeploy.util import variables
@@ -66,6 +67,7 @@
6667
CommandLineArgUtil.PASSPHRASE_SWITCH
6768
]
6869

70+
6971
def __process_args(args):
7072
"""
7173
Process the command-line arguments and prompt the user for any missing information
@@ -95,6 +97,7 @@ def __process_args(args):
9597
domain_typedef.set_model_context(model_context)
9698
return model_context
9799

100+
98101
def __verify_required_args_present(required_arg_map):
99102
"""
100103
Verify that the required args are present.
@@ -111,6 +114,7 @@ def __verify_required_args_present(required_arg_map):
111114
raise ex
112115
return
113116

117+
114118
def __process_java_home_arg(optional_arg_map):
115119
"""
116120
Verify that java_home is set. If not, set it.
@@ -132,6 +136,7 @@ def __process_java_home_arg(optional_arg_map):
132136
optional_arg_map[CommandLineArgUtil.JAVA_HOME_SWITCH] = java_home.getAbsolutePath()
133137
return
134138

139+
135140
def __process_model_args(optional_arg_map):
136141
"""
137142
Verify that either the model_file or archive_file was provided and exists.
@@ -176,6 +181,7 @@ def __process_model_args(optional_arg_map):
176181
raise ex
177182
return
178183

184+
179185
def __process_rcu_args(optional_arg_map, domain_type, domain_typedef):
180186
"""
181187
Determine if the RCU is needed and validate/prompt for any missing information
@@ -234,6 +240,7 @@ def __process_rcu_args(optional_arg_map, domain_type, domain_typedef):
234240
raise ex
235241
return
236242

243+
237244
def __process_encryption_args(optional_arg_map):
238245
"""
239246
Determine if the user is using our encryption and if so, get the passphrase.
@@ -255,6 +262,7 @@ def __process_encryption_args(optional_arg_map):
255262
optional_arg_map[CommandLineArgUtil.PASSPHRASE_SWITCH] = String(passphrase)
256263
return
257264

265+
258266
def __clean_up_temp_files():
259267
"""
260268
If a temporary directory was created to extract the model from the archive, delete the directory and its contents.
@@ -265,6 +273,28 @@ def __clean_up_temp_files():
265273
FileUtils.deleteDirectory(__tmp_model_dir)
266274
__tmp_model_dir = None
267275

276+
277+
def validate_model(model_dictionary, model_context, aliases):
278+
_method_name = 'validate_model'
279+
280+
try:
281+
validator = Validator(model_context, aliases, wlst_mode=__wlst_mode)
282+
283+
# no need to pass the variable file for processing, substitution has already been performed
284+
return_code = validator.validate_in_tool_mode(model_dictionary, variables_file_name=None,
285+
archive_file_name=model_context.get_archive_file_name())
286+
except ValidateException, ex:
287+
__logger.severe('WLSDPLY-20000', _program_name, ex.getLocalizedMessage(), error=ex,
288+
class_name=_class_name, method_name=_method_name)
289+
__clean_up_temp_files()
290+
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
291+
292+
if return_code == Validator.ReturnCode.STOP:
293+
__logger.severe('WLSDPLY-20001', _program_name, class_name=_class_name, method_name=_method_name)
294+
__clean_up_temp_files()
295+
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
296+
297+
268298
def main():
269299
"""
270300
The entry point for the create domain tool.
@@ -306,26 +336,12 @@ def main():
306336
__clean_up_temp_files()
307337
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
308338

309-
try:
310-
aliases = Aliases(model_context, wlst_mode=__wlst_mode)
311-
validator = Validator(model_context, aliases, wlst_mode=__wlst_mode)
339+
aliases = Aliases(model_context, wlst_mode=__wlst_mode)
340+
validate_model(model, model_context, aliases)
312341

313-
# Since the have already performed all variable substitution,
314-
# no need to pass the variable file for processing.
315-
#
316-
return_code = validator.validate_in_tool_mode(model,
317-
variables_file_name=None,
318-
archive_file_name=model_context.get_archive_file_name())
319-
except ValidateException, ex:
320-
__logger.severe('WLSDPLY-20000', _program_name, ex.getLocalizedMessage(), error=ex,
321-
class_name=_class_name, method_name=_method_name)
322-
__clean_up_temp_files()
323-
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
324-
325-
if return_code == Validator.ReturnCode.STOP:
326-
__logger.severe('WLSDPLY-20001', _program_name, class_name=_class_name, method_name=_method_name)
327-
__clean_up_temp_files()
328-
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
342+
if filter_helper.apply_filters(model, "create"):
343+
# if any filters were applied, re-validate the model
344+
validate_model(model, model_context, aliases)
329345

330346
try:
331347
creator = DomainCreator(model, model_context, aliases)

core/src/main/python/deploy.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from wlsdeploy.tool.deploy import deployer_utils
3636
from wlsdeploy.tool.deploy import model_deployer
3737
from wlsdeploy.tool.validate.validator import Validator
38+
from wlsdeploy.tool.util import filter_helper
3839
from wlsdeploy.tool.util.wlst_helper import WlstHelper
3940
from wlsdeploy.util import getcreds
4041
from wlsdeploy.util import variables
@@ -392,6 +393,27 @@ def __clean_up_temp_files():
392393
__tmp_model_dir = None
393394

394395

396+
def validate_model(model_dictionary, model_context, aliases):
397+
_method_name = 'validate_model'
398+
399+
try:
400+
validator = Validator(model_context, aliases, wlst_mode=__wlst_mode)
401+
402+
# no need to pass the variable file for processing, substitution has already been performed
403+
return_code = validator.validate_in_tool_mode(model_dictionary, variables_file_name=None,
404+
archive_file_name=model_context.get_archive_file_name())
405+
except ValidateException, ex:
406+
__logger.severe('WLSDPLY-20000', _program_name, ex.getLocalizedMessage(), error=ex,
407+
class_name=_class_name, method_name=_method_name)
408+
__clean_up_temp_files()
409+
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
410+
411+
if return_code == Validator.ReturnCode.STOP:
412+
__logger.severe('WLSDPLY-20001', _program_name, class_name=_class_name, method_name=_method_name)
413+
__clean_up_temp_files()
414+
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
415+
416+
395417
def main():
396418
"""
397419
The python entry point for deployApps.
@@ -433,25 +455,12 @@ def main():
433455
__clean_up_temp_files()
434456
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
435457

436-
try:
437-
aliases = Aliases(model_context, wlst_mode=__wlst_mode)
438-
validator = Validator(model_context, aliases, wlst_mode=__wlst_mode)
439-
440-
# Since the have already performed all variable substitution,
441-
# no need to pass the variable file for processing.
442-
#
443-
return_code = validator.validate_in_tool_mode(model_dictionary, variables_file_name=None,
444-
archive_file_name=model_context.get_archive_file_name())
445-
except ValidateException, ex:
446-
__logger.severe('WLSDPLY-20000', _program_name, ex.getLocalizedMessage(), error=ex,
447-
class_name=_class_name, method_name=_method_name)
448-
__clean_up_temp_files()
449-
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
458+
aliases = Aliases(model_context, wlst_mode=__wlst_mode)
459+
validate_model(model_dictionary, model_context, aliases)
450460

451-
if return_code == Validator.ReturnCode.STOP:
452-
__logger.severe('WLSDPLY-20001', _program_name, class_name=_class_name, method_name=_method_name)
453-
__clean_up_temp_files()
454-
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
461+
if filter_helper.apply_filters(model_dictionary, "deploy"):
462+
# if any filters were applied, re-validate the model
463+
validate_model(model_dictionary, model_context, aliases)
455464

456465
try:
457466
model = Model(model_dictionary)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
"""
2+
Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3+
The Universal Permissive License (UPL), Version 1.0
4+
"""
5+
import imp
6+
import javaos as os
7+
import sys
8+
9+
from wlsdeploy.logging.platform_logger import PlatformLogger
10+
from wlsdeploy.util import dictionary_utils
11+
from wlsdeploy.util.model_translator import FileToPython
12+
13+
__class_name = 'filter_helper'
14+
__logger = PlatformLogger('wlsdeploy.wlst')
15+
__filter_file_location = os.path.join(os.environ.get('WLSDEPLOY_HOME'), 'lib', 'model_filters.json')
16+
17+
__id_filter_map = {
18+
# 'filterId': filter_method
19+
}
20+
21+
22+
def apply_filters(model, tool_type):
23+
"""
24+
Apply any filters configured for the specified tool type to the specified model.
25+
:param model: the model to be filtered
26+
:param tool_type: the name of the filter tool type
27+
:return: True if any filter was applied, False otherwise
28+
:raises: BundleAwareException of the specified type: if an error occurs
29+
"""
30+
_method_name = 'apply_filters'
31+
32+
filter_applied = False
33+
34+
try:
35+
if os.path.isfile(__filter_file_location):
36+
filters_dictionary = FileToPython(__filter_file_location).parse()
37+
38+
if tool_type in filters_dictionary:
39+
filter_list = filters_dictionary[tool_type]
40+
for filter in filter_list:
41+
filter_applied = _apply_filter(model, filter) or filter_applied
42+
else:
43+
__logger.info('WLSDPLY-20016', tool_type, __filter_file_location, class_name=__class_name,
44+
method_name=_method_name)
45+
else:
46+
__logger.info('WLSDPLY-20017', __filter_file_location, class_name=__class_name, method_name=_method_name)
47+
except Exception, ex:
48+
__logger.severe('WLSDPLY-20018', str(ex), error=ex, class_name=__class_name, method_name=_method_name)
49+
50+
return filter_applied
51+
52+
53+
def _apply_filter(model, the_filter):
54+
"""
55+
Apply the specified filter to the specified model.
56+
:param model: the model to be filtered
57+
:param filter: a dictionary containing the filter parameters
58+
:return: True if the specified filter was applied, False otherwise
59+
:raises: BundleAwareException of the specified type: if an error occurs
60+
"""
61+
_method_name = '_apply_filter'
62+
id = dictionary_utils.get_element(the_filter, 'id')
63+
if id is not None:
64+
return _apply_id_filter(model, id)
65+
66+
path = dictionary_utils.get_element(the_filter, 'path')
67+
if path is not None:
68+
return _apply_path_filter(model, path)
69+
70+
__logger.severe('WLSDPLY-20019', str(__filter_file_location), class_name=__class_name, method_name=_method_name)
71+
return False
72+
73+
74+
def _apply_id_filter(model, id):
75+
"""
76+
Apply the specified ID filter to the specified model.
77+
:param model: the model to be filtered
78+
:param id: the ID of the filter to be applied
79+
:return: True if the specified filter was applied, False otherwise
80+
:raises: BundleAwareException of the specified type: if an error occurs
81+
"""
82+
_method_name = '_apply_id_filter'
83+
84+
filter_method = dictionary_utils.get_element(__id_filter_map, id)
85+
if filter_method is None:
86+
__logger.severe('WLSDPLY-20020', str(id), class_name=__class_name, method_name=_method_name)
87+
return False
88+
else:
89+
filter_method(model)
90+
return True
91+
92+
93+
def _apply_path_filter(model, script_path):
94+
"""
95+
Apply the specified path filter to the specified model.
96+
:param model: the model to be filtered
97+
:param script_path: the path of the filter to be applied
98+
:return: True if the specified filter was applied, False otherwise
99+
:raises: BundleAwareException of the specified type: if an error occurs
100+
"""
101+
_method_name = '_apply_path_filter'
102+
103+
if not os.path.isfile(script_path):
104+
__logger.severe('WLSDPLY-20021', str(script_path), class_name=__class_name, method_name=_method_name)
105+
return False
106+
107+
python_path = os.path.dirname(script_path)
108+
path_present = python_path in sys.path
109+
if not path_present:
110+
sys.path.insert(0, python_path)
111+
112+
try:
113+
module = imp.load_source('filter_script', script_path)
114+
module.filter_model(model)
115+
if not path_present:
116+
sys.path.remove(python_path)
117+
return True
118+
119+
except Exception, ex:
120+
__logger.severe('WLSDPLY-20022', str(script_path), error=ex, class_name=__class_name, method_name=_method_name)
121+
122+
return False

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,3 +1107,10 @@ WLSDPLY-20012=No WLST value for model attribute {0} value {1}
11071107
WLSDPLY-20013=Updating {0}
11081108
WLSDPLY-20014={0} requires the archive file argument {1} to be an existing file: {2}
11091109
WLSDPLY-20015={0} requires a model file to run but neither the {1} or {2} argument were provided
1110+
WLSDPLY-20016=No filters of type {0} found in filter configuration file {1}
1111+
WLSDPLY-20017=No filter configuration file {0}
1112+
WLSDPLY-20018=Error applying filter configuration: {0}
1113+
WLSDPLY-20019=Filter entry in {0} has neither ID or path
1114+
WLSDPLY-20020=Filter ID {0} is invalid
1115+
WLSDPLY-20021=Filter path {0} does not exist
1116+
WLSDPLY-20022=Error loading filter path {0}

0 commit comments

Comments
 (0)