| 
1 | 1 | """  | 
2 |  | -Copyright (c) 2019, 2020, Oracle Corporation and/or its affiliates.  All rights reserved.  | 
 | 2 | +Copyright (c) 2019, 2020, Oracle Corporation and/or its affiliates.  | 
3 | 3 | Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.  | 
4 | 4 | 
  | 
5 | 5 | Utility CLS methods shared by multiple tools.  | 
6 | 6 | """  | 
 | 7 | +import os  | 
 | 8 | + | 
 | 9 | +from java.io import File  | 
7 | 10 | from java.io import IOException  | 
8 | 11 | from java.lang import IllegalArgumentException  | 
9 | 12 | from java.lang import String  | 
10 | 13 | from oracle.weblogic.deploy.util import FileUtils  | 
11 |  | -from oracle.weblogic.deploy.util import VariableException  | 
12 | 14 | from oracle.weblogic.deploy.util import TranslateException  | 
 | 15 | +from oracle.weblogic.deploy.util import VariableException  | 
13 | 16 | from oracle.weblogic.deploy.validate import ValidateException  | 
 | 17 | + | 
 | 18 | +import oracle.weblogic.deploy.util.PyOrderedDict as OrderedDict  | 
14 | 19 | from wlsdeploy.exception import exception_helper  | 
15 | 20 | from wlsdeploy.logging.platform_logger import PlatformLogger  | 
16 | 21 | from wlsdeploy.tool.util import filter_helper  | 
17 | 22 | from wlsdeploy.tool.util.archive_helper import ArchiveHelper  | 
18 | 23 | from wlsdeploy.tool.validate.validator import Validator  | 
19 | 24 | from wlsdeploy.util import cla_utils  | 
 | 25 | +from wlsdeploy.util import getcreds  | 
20 | 26 | from wlsdeploy.util import model_helper  | 
 | 27 | +from wlsdeploy.util import model_translator, path_utils  | 
21 | 28 | from wlsdeploy.util import tool_exit  | 
22 |  | -from wlsdeploy.util import getcreds  | 
23 | 29 | from wlsdeploy.util import variables  | 
24 | 30 | from wlsdeploy.util.cla_utils import CommandLineArgUtil  | 
25 | 31 | from wlsdeploy.util.model_translator import FileToPython  | 
26 | 32 | 
 
  | 
27 |  | -import oracle.weblogic.deploy.util.PyOrderedDict as OrderedDict  | 
28 |  | - | 
29 | 33 | __logger = PlatformLogger('wlsdeploy.util')  | 
30 | 34 | _class_name = 'cla_helper'  | 
31 | 35 | 
 
  | 
 | 36 | +_store_environment_variable = '__WLSDEPLOY_STORE_MODEL__'  | 
 | 37 | + | 
32 | 38 | __tmp_model_dir = None  | 
33 | 39 | 
 
  | 
34 | 40 | 
 
  | 
@@ -240,6 +246,8 @@ def load_model(program_name, model_context, aliases, filter_type, wlst_mode):  | 
240 | 246 |         clean_up_temp_files()  | 
241 | 247 |         tool_exit.end(model_context, CommandLineArgUtil.PROG_ERROR_EXIT_CODE)  | 
242 | 248 | 
 
  | 
 | 249 | +    persist_model(model_context, model_dictionary)  | 
 | 250 | + | 
243 | 251 |     validate_model(program_name, model_dictionary, model_context, aliases, wlst_mode)  | 
244 | 252 | 
 
  | 
245 | 253 |     if filter_helper.apply_filters(model_dictionary, filter_type):  | 
@@ -352,3 +360,41 @@ def _get_merge_match_key(key, variable_map):  | 
352 | 360 |     if model_helper.is_delete_name(match_key):  | 
353 | 361 |         match_key = model_helper.get_delete_item_name(match_key)  | 
354 | 362 |     return match_key  | 
 | 363 | + | 
 | 364 | + | 
 | 365 | +def persist_model(model_context, model_dictionary):  | 
 | 366 | +    """  | 
 | 367 | +    If environment variable __WLSDEPLOY_STORE_MODEL__ is set, save the specified model.  | 
 | 368 | +    If the variable's value starts with a slash, save to that file, otherwise use a default location.  | 
 | 369 | +    :param model_context: the model context  | 
 | 370 | +    :param model_dictionary: the model to be saved  | 
 | 371 | +    """  | 
 | 372 | +    _method_name = 'persist_model'  | 
 | 373 | + | 
 | 374 | +    if check_persist_model():  | 
 | 375 | +        store_value = os.environ.get(_store_environment_variable)  | 
 | 376 | + | 
 | 377 | +        if store_value.startswith('/') or store_value.startswith('\\'):  | 
 | 378 | +            file_path = store_value  | 
 | 379 | +        elif model_context.get_domain_home() is not None:  | 
 | 380 | +            file_path = model_context.get_domain_home() + os.sep + 'wlsdeploy' + os.sep + 'domain_model.json'  | 
 | 381 | +        else:  | 
 | 382 | +            file_dir = FileUtils.createTempDirectory('wlsdeploy')  | 
 | 383 | +            file_path = File(file_dir, 'domain_model.json').getAbsolutePath()  | 
 | 384 | + | 
 | 385 | +        __logger.info('WLSDPLY-01650', file_path, class_name=_class_name, method_name=_method_name)  | 
 | 386 | + | 
 | 387 | +        persist_dir = path_utils.get_parent_directory(file_path)  | 
 | 388 | +        if not os.path.exists(persist_dir):  | 
 | 389 | +            os.makedirs(persist_dir)  | 
 | 390 | + | 
 | 391 | +        model_file = FileUtils.getCanonicalFile(File(file_path))  | 
 | 392 | +        model_translator.PythonToFile(model_dictionary).write_to_file(model_file.getAbsolutePath())  | 
 | 393 | + | 
 | 394 | + | 
 | 395 | +def check_persist_model():  | 
 | 396 | +    """  | 
 | 397 | +    Determine if the model should be persisted, based on the environment variable __WLSDEPLOY_STORE_MODEL__  | 
 | 398 | +    :return: True if the model should be persisted  | 
 | 399 | +    """  | 
 | 400 | +    return os.environ.has_key(_store_environment_variable)  | 
0 commit comments