Skip to content

Commit 2535f20

Browse files
committed
adding filtering for prepare model
1 parent df2d794 commit 2535f20

File tree

13 files changed

+133
-12
lines changed

13 files changed

+133
-12
lines changed

core/src/main/java/oracle/weblogic/deploy/util/WLSDeployArchive.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle Corporation and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
*/
55
package oracle.weblogic.deploy.util;
@@ -854,6 +854,15 @@ public void extractDomainBinScript(String archivePath, File extractToLocation) t
854854
extractFileFromZip(archivePath, ARCHIVE_DOM_BIN_TARGET_DIR, "", extractToLocation);
855855
LOGGER.exiting(CLASS, METHOD);
856856
}
857+
858+
public void removeDomainBinScripts() throws WLSDeployArchiveIOException {
859+
final String METHOD = "removeDomainBinScripts";
860+
861+
LOGGER.entering(CLASS, METHOD);
862+
getZipFile().removeZipEntries(ARCHIVE_DOM_BIN_TARGET_DIR + ZIP_SEP);
863+
LOGGER.exiting(CLASS, METHOD);
864+
}
865+
857866
/**
858867
* This method adds a classpath library to the archive. If a library with the same name already exists, this
859868
* method assumes that the new one also needs to be added so it changes the name to prevent conflicts by adding

core/src/main/python/prepare_model.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
]
3737

3838
__optional_arguments = [
39-
CommandLineArgUtil.VARIABLE_FILE_SWITCH
39+
CommandLineArgUtil.VARIABLE_FILE_SWITCH,
40+
CommandLineArgUtil.ARCHIVE_FILE_SWITCH
4041
]
4142

4243

@@ -55,8 +56,9 @@ def __process_args(args):
5556
target_configuration_helper.process_target_arguments(argument_map)
5657

5758
model_context = ModelContext(_program_name, argument_map)
58-
# override this check, since archive file is not supplied here
59-
model_context.get_validate_configuration().set_allow_unresolved_archive_references(True)
59+
if model_context.get_archive_file_name() is None:
60+
# override this check, if archive file is not supplied
61+
model_context.get_validate_configuration().set_allow_unresolved_archive_references(True)
6062
return model_context
6163

6264

core/src/main/python/wlsdeploy/tool/discover/domain_info_discoverer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def get_user_env_scripts(self):
115115
_method_name = 'get_user_env_scripts'
116116
_logger.entering(class_name=_class_name, method_name=_method_name)
117117
entries = []
118-
if self._model_context.is_targetted_config():
118+
if self._model_context.get_target_configuration().exclude_domain_bin_contents():
119119
_logger.fine('WLSDPLY-06427', class_name=_class_name, method_name=_method_name)
120120
else:
121121
archive_file = self._model_context.get_archive_file()

core/src/main/python/wlsdeploy/tool/prepare/model_preparer.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
from wlsdeploy.aliases.aliases import Aliases
1414
from wlsdeploy.aliases.location_context import LocationContext
1515
from wlsdeploy.aliases.model_constants import DOMAIN_INFO
16+
from wlsdeploy.aliases.model_constants import DOMAIN_SCRIPTS
1617
from wlsdeploy.aliases.wlst_modes import WlstModes
1718
from wlsdeploy.exception import exception_helper
1819
from wlsdeploy.exception.expection_types import ExceptionType
1920
from wlsdeploy.logging.platform_logger import PlatformLogger
2021
from wlsdeploy.tool.util import filter_helper
22+
from wlsdeploy.tool.util.archive_helper import ArchiveHelper
2123
from wlsdeploy.tool.util.credential_injector import CredentialInjector
2224
from wlsdeploy.tool.util.variable_injector import VARIABLE_FILE_UPDATE
2325
from wlsdeploy.tool.util.variable_injector import VariableInjector
@@ -221,6 +223,10 @@ def _apply_filter_and_inject_variable(self, model_dict, model_context):
221223
else:
222224
credential_properties = self.credential_injector.get_variable_cache()
223225

226+
if target_config.exclude_domain_bin_contents() and DOMAIN_INFO in model_dict \
227+
and DOMAIN_SCRIPTS in model_dict[DOMAIN_INFO]:
228+
del model_dict[DOMAIN_INFO][DOMAIN_SCRIPTS]
229+
224230
variable_injector = VariableInjector(_program_name, model_dict, model_context,
225231
WebLogicHelper(self._logger).get_actual_weblogic_version(),
226232
credential_properties)
@@ -279,6 +285,23 @@ def _clean_variable_files(self, merged_model_dictionary):
279285
self._logger.warning('WLSDPLY-05803', e.getLocalizedMessage(),
280286
class_name=_class_name, method_name=_method_name)
281287

288+
def _clean_archive_files(self):
289+
"""
290+
Remove any content necessary from the archive file.
291+
"""
292+
_method_name = '_clean_archive_files'
293+
294+
archive_file_name = self.model_context.get_archive_file_name()
295+
if archive_file_name is None:
296+
return
297+
298+
# If the archive file(s) exist, always make a copy even if we aren't filtering them
299+
archive_helper = ArchiveHelper(archive_file_name, None, self._logger, exception_helper.ExceptionType.PREPARE)
300+
archive_helper = archive_helper.copy_archives_to_target_directory(self.model_context.get_output_dir())
301+
302+
if self.model_context.get_target_configuration().exclude_domain_bin_contents():
303+
archive_helper.remove_domain_scripts()
304+
282305
def prepare_models(self):
283306
"""
284307
Replace password attributes in each model file with secret tokens, and write each model.
@@ -327,9 +350,10 @@ def prepare_models(self):
327350
if variable_file is not None and not os.path.exists(variable_file):
328351
variable_file = None
329352

353+
archive_file_name = self.model_context.get_archive_file_name()
330354
return_code = validator.validate_in_tool_mode(model_dictionary,
331355
variables_file_name=variable_file,
332-
archive_file_name=None)
356+
archive_file_name=archive_file_name)
333357

334358
if return_code == Validator.ReturnCode.STOP:
335359
ex = exception_helper.create_prepare_exception('WLSDPLY-05804', model_file_name)
@@ -359,6 +383,7 @@ def prepare_models(self):
359383
filter_helper.apply_filters(merged_model_dictionary, "discover", self.model_context)
360384
self.credential_injector.filter_unused_credentials(merged_model_dictionary)
361385
self._clean_variable_files(merged_model_dictionary)
386+
self._clean_archive_files()
362387

363388
# use a merged, substituted, filtered model to get domain name and create additional target output.
364389
full_model_dictionary = cla_helper.load_model(_program_name, self.model_context, self._aliases,

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"""
2-
Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
2+
Copyright (c) 2017, 2022, Oracle Corporation and/or its affiliates. All rights reserved.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
5+
import os
6+
import shutil
7+
58
from java.io import File
69
from java.lang import IllegalArgumentException
710
from java.lang import IllegalStateException
@@ -359,6 +362,25 @@ def extract_domain_bin_script(self, script_path):
359362
self.__logger.exiting(class_name=self.__class_name, method_name=_method_name)
360363
return
361364

365+
def remove_domain_scripts(self):
366+
"""
367+
Remove any domain bin scripts from the archive.
368+
:raises: BundleAwareException of the appropriate type: if an error occurs
369+
"""
370+
_method_name = 'remove_domain_scripts'
371+
372+
self.__logger.entering(class_name=self.__class_name, method_name=_method_name)
373+
for archive_file in self.__archive_files:
374+
try:
375+
archive_file.removeDomainBinScripts()
376+
except (WLSDeployArchiveIOException, IllegalArgumentException), e:
377+
ex = exception_helper.create_exception(self.__exception_type, 'WLSDPLY-19311',
378+
archive_file.getArchiveFileName(), e.getLocalizedMessage(),
379+
error=e)
380+
self.__logger.throwing(ex, class_name=self.__class_name, method_name=_method_name)
381+
raise ex
382+
self.__logger.exiting(class_name=self.__class_name, method_name=_method_name)
383+
362384
def get_archive_entries(self):
363385
"""
364386
Get the entries from all the archives.
@@ -439,6 +461,46 @@ def get_manifest(self, source_path):
439461
return archive.getManifest(source_path)
440462
return None
441463

464+
def copy_archives_to_target_directory(self, target_directory):
465+
"""
466+
Makes a copy of the specified archive file(s) to the target directory.
467+
:param target_directory: the directory to which the archive file(s) will be copied
468+
:return: new ArchiveHelper for the new archive file(s)
469+
:raises: BundleAwareException of the appropriate type: if an error occurred while copying the file
470+
"""
471+
_method_name = 'copy_archives_to_target_directory'
472+
self.__logger.entering(target_directory, class_name=self.__class_name, method_name=_method_name)
473+
474+
file_names = self.__archive_files_text.split(CommandLineArgUtil.ARCHIVE_FILES_SEPARATOR)
475+
new_file_names = []
476+
for file_name in file_names:
477+
new_file_name = self._copy_file(file_name, target_directory)
478+
new_file_names.append(new_file_name)
479+
new_archive_file_text = CommandLineArgUtil.ARCHIVE_FILES_SEPARATOR.join(new_file_names)
480+
return ArchiveHelper(new_archive_file_text, self.__domain_home, self.__logger, self.__exception_type)
481+
482+
def _copy_file(self, source_file_name, target_directory):
483+
"""
484+
Make a copy of the source file to the target directory
485+
:param source_file_name: source file name
486+
:param target_directory: target directory name
487+
:return: file name of the new file in the target directory
488+
:raises: BundleAwareException of the appropriate type: if an error occurred while copying the file
489+
"""
490+
_method_name = '_copy_file'
491+
492+
self.__logger.entering(source_file_name, target_directory, class_name=self.__class_name, method_name=_method_name)
493+
base_name = os.path.basename(source_file_name)
494+
target_file_name = os.path.join(target_directory, base_name)
495+
try:
496+
shutil.copyfile(source_file_name, target_file_name)
497+
except Exception, e:
498+
ex = exception_helper.create_exception(self.__exception_type, 'WLSDPLY-19312', source_file_name,
499+
target_file_name, e.getLocalizedMessage(), error=e)
500+
self.__logger.throwing(ex, class_name=self.__class_name, method_name=_method_name)
501+
raise ex
502+
return target_file_name
503+
442504
def _find_archive_for_path(self, path, required=False):
443505
"""
444506
Find the archive file containing the specified path.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import oracle.weblogic.deploy.util.FileUtils as JFileUtils
1919

2020
from wlsdeploy.exception import exception_helper
21+
from wlsdeploy.json.json_translator import JsonToPython
2122
from wlsdeploy.logging.platform_logger import PlatformLogger
2223
from wlsdeploy.util import path_utils
2324
from wlsdeploy.util import validate_configuration
@@ -1157,8 +1158,9 @@ def _validate_target_arg(self, value):
11571158
else:
11581159
try:
11591160
# verify the file is in proper format
1160-
file_handle = open(target_configuration_file)
1161-
config_dictionary = eval(file_handle.read())
1161+
# file_handle = open(target_configuration_file)
1162+
# config_dictionary = eval(file_handle.read())
1163+
config_dictionary = JsonToPython(target_configuration_file).parse()
11621164
target_configuration = TargetConfiguration(config_dictionary)
11631165
validation_method = target_configuration.get_validation_method()
11641166
if (validation_method is not None) and \

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from oracle.weblogic.deploy.util import XPathUtil
1414
from wlsdeploy.aliases.model_constants import MODEL_LIST_DELIMITER
1515
from wlsdeploy.aliases.wlst_modes import WlstModes
16+
from wlsdeploy.json.json_translator import JsonToPython
1617
from wlsdeploy.logging import platform_logger
1718
from wlsdeploy.util import validate_configuration
1819
from wlsdeploy.util.cla_utils import CommandLineArgUtil
@@ -644,8 +645,7 @@ def get_target_configuration(self):
644645
if self._target:
645646
target_configuration_file = self.get_target_configuration_file()
646647
if os.path.exists(target_configuration_file):
647-
file_handle = open(target_configuration_file)
648-
configuration_dict = eval(file_handle.read())
648+
configuration_dict = JsonToPython(target_configuration_file).parse()
649649

650650
self._target_configuration = TargetConfiguration(configuration_dict)
651651

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
# Overrides the Kubernetes secret name for the WebLogic admin user credential
1616
WLS_CREDENTIALS_NAME = "wls_credentials_name"
1717

18+
# Determines whether the domainBin contents should be excluded
19+
EXCLUDE_DOMAIN_BIN_CONTENTS = "exclude_domain_bin_contents"
20+
1821
# put secret tokens in the model, and build a script to create the secrets.
1922
SECRETS_METHOD = 'secrets'
2023

@@ -134,3 +137,15 @@ def manages_credentials(self):
134137
:return: True if credential values are managed, False otherwise
135138
"""
136139
return self.get_credentials_method() in [SECRETS_METHOD, CONFIG_OVERRIDES_SECRETS_METHOD]
140+
141+
def exclude_domain_bin_contents(self):
142+
"""
143+
Determine if the contents of the domain's bin directory should be
144+
excluded from the model and archive. If True, these files will be
145+
excluded and not go into the model or archive file.
146+
:return: True if the domain bin contents should be excluded, False otherwise
147+
"""
148+
result = dictionary_utils.get_element(self.config_dictionary, EXCLUDE_DOMAIN_BIN_CONTENTS)
149+
if result is None:
150+
result = False
151+
return result

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
@@ -1521,6 +1521,8 @@ WLSDPLY-19307=Unable to extract classpath libraries from archive file {0} to dom
15211521
WLSDPLY-19308=Failed to extract script to domain bin {0} because it does not exist in archive file {1}
15221522
WLSDPLY-19309=Unable to extract from domain bin {0} from archive file {1}: {2}
15231523
WLSDPLY-19310=Unable to extract user custom files from archive file {0} to domain directory {1}: {2}
1524+
WLSDPLY-19311=Unable to remove domain bin scripts from archive file{0}: {1}
1525+
WLSDPLY-19312=Unable to copy file {0} to {1}: {2}
15241526

15251527
# wlsdeploy/tool/util/topology_helper.py
15261528
WLSDPLY-19400=Creating placeholder for server template {0}

core/src/main/targetconfigs/vz/target.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"validation_method" : "lax",
1010
"credentials_method" : "secrets",
1111
"credentials_output_method" : "script",
12+
"exclude_domain_bin_contents": true,
1213
"wls_credentials_name" : "__weblogic-credentials__",
1314
"additional_secrets": "runtime-encryption-secret",
1415
"additional_output" : "vz-application.yaml"

0 commit comments

Comments
 (0)