Skip to content

Commit bf27c98

Browse files
committed
Issue#27 - Add filter capability
1 parent 56bc804 commit bf27c98

File tree

3 files changed

+158
-18
lines changed

3 files changed

+158
-18
lines changed

core/src/main/python/deploy.py

Lines changed: 29 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,29 @@ 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+
# Since the have already performed all variable substitution,
403+
# no need to pass the variable file for processing.
404+
#
405+
return_code = validator.validate_in_tool_mode(model_dictionary, variables_file_name=None,
406+
archive_file_name=model_context.get_archive_file_name())
407+
except ValidateException, ex:
408+
__logger.severe('WLSDPLY-20000', _program_name, ex.getLocalizedMessage(), error=ex,
409+
class_name=_class_name, method_name=_method_name)
410+
__clean_up_temp_files()
411+
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
412+
413+
if return_code == Validator.ReturnCode.STOP:
414+
__logger.severe('WLSDPLY-20001', _program_name, class_name=_class_name, method_name=_method_name)
415+
__clean_up_temp_files()
416+
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
417+
418+
395419
def main():
396420
"""
397421
The python entry point for deployApps.
@@ -433,25 +457,12 @@ def main():
433457
__clean_up_temp_files()
434458
sys.exit(CommandLineArgUtil.PROG_ERROR_EXIT_CODE)
435459

436-
try:
437-
aliases = Aliases(model_context, wlst_mode=__wlst_mode)
438-
validator = Validator(model_context, aliases, wlst_mode=__wlst_mode)
460+
aliases = Aliases(model_context, wlst_mode=__wlst_mode)
461+
validate_model(model_dictionary, model_context, aliases)
439462

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)
450-
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)
463+
if filter_helper.apply_filters(model_dictionary, "deploy"):
464+
# if any filters were applied, re-validate the model
465+
validate_model(model_dictionary, model_context, aliases)
455466

456467
try:
457468
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)