<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import time <EOL> import logging <EOL> import re <EOL> import traceback <EOL> from salt . utils import dictdiffer <EOL> from salt . exceptions import CommandExecutionError <EOL> import salt . ext . six as six <EOL> log = logging . getLogger ( __name__ ) <EOL> def datasource_exists ( name , jboss_config , datasource_properties , recreate = False , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( "<STR_LIT>" , name ) <EOL> ret = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:result>' : True , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> has_changed = False <EOL> ds_current_properties = { } <EOL> ds_result = __salt__ [ '<STR_LIT>' ] ( jboss_config = jboss_config , name = name , profile = profile ) <EOL> if ds_result [ '<STR_LIT:success>' ] : <EOL> ds_current_properties = ds_result [ '<STR_LIT:result>' ] <EOL> if recreate : <EOL> remove_result = __salt__ [ '<STR_LIT>' ] ( jboss_config = jboss_config , name = name , profile = profile ) <EOL> if remove_result [ '<STR_LIT:success>' ] : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = name <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' + remove_result [ '<STR_LIT>' ] <EOL> return ret <EOL> has_changed = True <EOL> create_result = __salt__ [ '<STR_LIT>' ] ( jboss_config = jboss_config , name = name , datasource_properties = datasource_properties , profile = profile ) <EOL> if create_result [ '<STR_LIT:success>' ] : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = name <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' + create_result [ '<STR_LIT>' ] <EOL> return ret <EOL> read_result = __salt__ [ '<STR_LIT>' ] ( jboss_config = jboss_config , name = name , profile = profile ) <EOL> if read_result [ '<STR_LIT:success>' ] : <EOL> ds_new_properties = read_result [ '<STR_LIT:result>' ] <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' + read_result [ '<STR_LIT>' ] <EOL> return ret <EOL> else : <EOL> update_result = __salt__ [ '<STR_LIT>' ] ( jboss_config = jboss_config , name = name , new_properties = datasource_properties , profile = profile ) <EOL> if not update_result [ '<STR_LIT:success>' ] : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' + update_result [ '<STR_LIT>' ] <EOL> else : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> read_result = __salt__ [ '<STR_LIT>' ] ( jboss_config = jboss_config , name = name , profile = profile ) <EOL> ds_new_properties = read_result [ '<STR_LIT:result>' ] <EOL> else : <EOL> if ds_result [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> create_result = __salt__ [ '<STR_LIT>' ] ( jboss_config = jboss_config , name = name , datasource_properties = datasource_properties , profile = profile ) <EOL> if create_result [ '<STR_LIT:success>' ] : <EOL> read_result = __salt__ [ '<STR_LIT>' ] ( jboss_config = jboss_config , name = name , profile = profile ) <EOL> ds_new_properties = read_result [ '<STR_LIT:result>' ] <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' + create_result [ '<STR_LIT>' ] <EOL> else : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( ds_result [ '<STR_LIT>' ] ) ) <EOL> if ret [ '<STR_LIT:result>' ] : <EOL> log . debug ( "<STR_LIT>" , str ( ds_new_properties ) ) <EOL> log . debug ( "<STR_LIT>" , str ( ds_current_properties ) ) <EOL> diff = dictdiffer . diff ( ds_new_properties , ds_current_properties ) <EOL> added = diff . added ( ) <EOL> if len ( added ) > <NUM_LIT:0> : <EOL> has_changed = True <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = __format_ds_changes ( added , ds_current_properties , ds_new_properties ) <EOL> removed = diff . removed ( ) <EOL> if len ( removed ) > <NUM_LIT:0> : <EOL> has_changed = True <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = __format_ds_changes ( removed , ds_current_properties , ds_new_properties ) <EOL> changed = diff . changed ( ) <EOL> if len ( changed ) > <NUM_LIT:0> : <EOL> has_changed = True <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = __format_ds_changes ( changed , ds_current_properties , ds_new_properties ) <EOL> if not has_changed : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def __format_ds_changes ( keys , old_dict , new_dict ) : <EOL> log . debug ( "<STR_LIT>" , str ( keys ) , str ( old_dict ) , str ( new_dict ) ) <EOL> changes = '<STR_LIT>' <EOL> for key in keys : <EOL> log . debug ( "<STR_LIT>" , str ( key ) ) <EOL> if key in old_dict and key in new_dict : <EOL> changes += key + '<STR_LIT::>' + __get_ds_value ( old_dict , key ) + '<STR_LIT>' + __get_ds_value ( new_dict , key ) + '<STR_LIT:\n>' <EOL> elif key in old_dict : <EOL> changes += key + '<STR_LIT:\n>' <EOL> elif key in new_dict : <EOL> changes += key + '<STR_LIT::>' + __get_ds_value ( new_dict , key ) + '<STR_LIT:\n>' <EOL> return changes <EOL> def __get_ds_value ( dct , key ) : <EOL> log . debug ( "<STR_LIT>" , key ) <EOL> if key == "<STR_LIT:password>" : <EOL> return "<STR_LIT>" <EOL> elif dct [ key ] is None : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return str ( dct [ key ] ) <EOL> def bindings_exist ( name , jboss_config , bindings , profile = None ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( "<STR_LIT>" , name , profile ) <EOL> log . debug ( '<STR_LIT>' + str ( bindings ) ) <EOL> ret = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:result>' : True , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> has_changed = False <EOL> for key in bindings : <EOL> value = str ( bindings [ key ] ) <EOL> query_result = __salt__ [ '<STR_LIT>' ] ( binding_name = key , jboss_config = jboss_config , profile = profile ) <EOL> if query_result [ '<STR_LIT:success>' ] : <EOL> current_value = query_result [ '<STR_LIT:result>' ] [ '<STR_LIT:value>' ] <EOL> if current_value != value : <EOL> update_result = __salt__ [ '<STR_LIT>' ] ( binding_name = key , value = value , jboss_config = jboss_config , profile = profile ) <EOL> if update_result [ '<STR_LIT:success>' ] : <EOL> has_changed = True <EOL> __log_binding_change ( ret [ '<STR_LIT>' ] , '<STR_LIT>' , key , value , current_value ) <EOL> else : <EOL> raise CommandExecutionError ( update_result [ '<STR_LIT>' ] ) <EOL> else : <EOL> if query_result [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> create_result = __salt__ [ '<STR_LIT>' ] ( binding_name = key , value = value , jboss_config = jboss_config , profile = profile ) <EOL> if create_result [ '<STR_LIT:success>' ] : <EOL> has_changed = True <EOL> __log_binding_change ( ret [ '<STR_LIT>' ] , '<STR_LIT>' , key , value ) <EOL> else : <EOL> raise CommandExecutionError ( create_result [ '<STR_LIT>' ] ) <EOL> else : <EOL> raise CommandExecutionError ( query_result [ '<STR_LIT>' ] ) <EOL> if has_changed : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def __log_binding_change ( changes , type_ , key , new , old = None ) : <EOL> if type_ not in changes : <EOL> changes [ type_ ] = '<STR_LIT>' <EOL> if old is None : <EOL> changes [ type_ ] += key + '<STR_LIT::>' + new + '<STR_LIT:\n>' <EOL> else : <EOL> changes [ type_ ] += key + '<STR_LIT::>' + old + '<STR_LIT>' + new + '<STR_LIT:\n>' <EOL> def deployed ( name , jboss_config , salt_source = None ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( "<STR_LIT>" , name ) <EOL> ret = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:result>' : True , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> comment = '<STR_LIT>' <EOL> validate_success , validate_comment = __validate_arguments ( jboss_config , salt_source ) <EOL> if not validate_success : <EOL> return _error ( ret , validate_comment ) <EOL> resolved_source , get_artifact_comment = __get_artifact ( salt_source ) <EOL> log . debug ( '<STR_LIT>' , resolved_source ) <EOL> log . debug ( '<STR_LIT>' , get_artifact_comment ) <EOL> comment = __append_comment ( new_comment = get_artifact_comment , current_comment = comment ) <EOL> if resolved_source is None : <EOL> return _error ( ret , get_artifact_comment ) <EOL> find_success , deployment , find_comment = __find_deployment ( jboss_config , salt_source ) <EOL> if not find_success : <EOL> return _error ( ret , find_comment ) <EOL> log . debug ( '<STR_LIT>' , deployment ) <EOL> if deployment is not None : <EOL> __salt__ [ '<STR_LIT>' ] ( jboss_config , deployment ) <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = deployment <EOL> deploy_result = __salt__ [ '<STR_LIT>' ] ( jboss_config = jboss_config , source_file = resolved_source ) <EOL> log . debug ( '<STR_LIT>' , str ( deploy_result ) ) <EOL> if deploy_result [ '<STR_LIT:success>' ] : <EOL> comment = __append_comment ( new_comment = '<STR_LIT>' , current_comment = comment ) <EOL> ret [ '<STR_LIT>' ] = comment <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = resolved_source <EOL> else : <EOL> comment = __append_comment ( new_comment = '''<STR_LIT>''' . format ( ** deploy_result ) , current_comment = comment ) <EOL> return _error ( ret , comment ) <EOL> return ret <EOL> def __validate_arguments ( jboss_config , salt_source ) : <EOL> result , comment = __check_dict_contains ( jboss_config , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if salt_source is None : <EOL> result = False <EOL> comment = __append_comment ( '<STR_LIT>' , comment ) <EOL> result , comment = __check_dict_contains ( salt_source , '<STR_LIT>' , [ '<STR_LIT>' ] , comment , result ) <EOL> return result , comment <EOL> def __find_deployment ( jboss_config , salt_source = None ) : <EOL> result = None <EOL> success = True <EOL> comment = '<STR_LIT>' <EOL> deployments = __salt__ [ '<STR_LIT>' ] ( jboss_config ) <EOL> if salt_source is not None and '<STR_LIT>' in salt_source and salt_source [ '<STR_LIT>' ] : <EOL> deployment_re = re . compile ( salt_source [ '<STR_LIT>' ] ) <EOL> for deployment in deployments : <EOL> if deployment_re . match ( deployment ) : <EOL> if result is not None : <EOL> success = False <EOL> comment = "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" . format ( salt_source [ '<STR_LIT>' ] , "<STR_LIT:U+002C>" . join ( deployments ) ) <EOL> else : <EOL> result = deployment <EOL> return success , result , comment <EOL> def __get_artifact ( salt_source ) : <EOL> resolved_source = None <EOL> comment = None <EOL> if salt_source is None : <EOL> log . debug ( '<STR_LIT>' ) <EOL> comment = '<STR_LIT>' <EOL> elif isinstance ( salt_source , dict ) : <EOL> log . debug ( '<STR_LIT>' ) <EOL> if '<STR_LIT:source>' in salt_source : <EOL> try : <EOL> sfn , source_sum , comment_ = __salt__ [ '<STR_LIT>' ] ( <EOL> name = salt_source [ '<STR_LIT>' ] , <EOL> template = None , <EOL> source = salt_source [ '<STR_LIT:source>' ] , <EOL> source_hash = None , <EOL> user = None , <EOL> group = None , <EOL> mode = None , <EOL> saltenv = __env__ , <EOL> context = None , <EOL> defaults = None , <EOL> kwargs = None ) <EOL> manage_result = __salt__ [ '<STR_LIT>' ] ( <EOL> name = salt_source [ '<STR_LIT>' ] , <EOL> sfn = sfn , <EOL> ret = None , <EOL> source = salt_source [ '<STR_LIT:source>' ] , <EOL> source_sum = source_sum , <EOL> user = None , <EOL> group = None , <EOL> mode = None , <EOL> saltenv = __env__ , <EOL> backup = None , <EOL> makedirs = False , <EOL> template = None , <EOL> show_diff = True , <EOL> contents = None , <EOL> dir_mode = None ) <EOL> if manage_result [ '<STR_LIT:result>' ] : <EOL> resolved_source = salt_source [ '<STR_LIT>' ] <EOL> else : <EOL> comment = manage_result [ '<STR_LIT>' ] <EOL> except Exception as e : <EOL> log . debug ( traceback . format_exc ( ) ) <EOL> comment = '<STR_LIT>' . format ( e ) <EOL> else : <EOL> resolved_source = salt_source [ '<STR_LIT>' ] <EOL> comment = '<STR_LIT>' <EOL> return resolved_source , comment <EOL> def reloaded ( name , jboss_config , timeout = <NUM_LIT> , interval = <NUM_LIT:5> ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( "<STR_LIT>" , name ) <EOL> ret = { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:result>' : True , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> status = __salt__ [ '<STR_LIT>' ] ( jboss_config ) <EOL> if not status [ '<STR_LIT:success>' ] or status [ '<STR_LIT:result>' ] not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> return ret <EOL> result = __salt__ [ '<STR_LIT>' ] ( jboss_config ) <EOL> if result [ '<STR_LIT:success>' ] or '<STR_LIT>' in result [ '<STR_LIT>' ] or '<STR_LIT>' in result [ '<STR_LIT>' ] : <EOL> wait_time = <NUM_LIT:0> <EOL> status = None <EOL> while ( status is None or not status [ '<STR_LIT:success>' ] or status [ '<STR_LIT:result>' ] != '<STR_LIT>' ) and wait_time < timeout : <EOL> time . sleep ( interval ) <EOL> wait_time += interval <EOL> status = __salt__ [ '<STR_LIT>' ] ( jboss_config ) <EOL> if status [ '<STR_LIT:success>' ] and status [ '<STR_LIT:result>' ] == '<STR_LIT>' : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( timeout ) <EOL> if not status [ '<STR_LIT:success>' ] : <EOL> ret [ '<STR_LIT>' ] = __append_comment ( '<STR_LIT>' , ret [ '<STR_LIT>' ] ) <EOL> else : <EOL> ret [ '<STR_LIT>' ] = __append_comment ( ( '<STR_LIT>' . format ( status [ '<STR_LIT:result>' ] ) ) , ret [ '<STR_LIT>' ] ) <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' + result [ '<STR_LIT>' ] <EOL> return ret <EOL> def __check_dict_contains ( dct , dict_name , keys , comment = '<STR_LIT>' , result = True ) : <EOL> for key in keys : <EOL> if key not in six . iterkeys ( dct ) : <EOL> result = False <EOL> comment = __append_comment ( "<STR_LIT>" . format ( key , dict_name ) , comment ) <EOL> return result , comment <EOL> def __append_comment ( new_comment , current_comment = '<STR_LIT>' ) : <EOL> return current_comment + '<STR_LIT:\n>' + new_comment <EOL> def _error ( ret , err_msg ) : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = err_msg <EOL> return ret </s>
