#NEWLINE# def ext_pillar( minion_id , pillar , resource , sequence , subkey = False , subkey_only = False ) : #NEWLINE# #INDENT# '\n    Evaluate Pepa templates\n    ' #NEWLINE# roots = __opts__ [ 'pepa_roots' ] #NEWLINE# inp = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# inp [ 'default' ] = 'default' #NEWLINE# inp [ 'hostname' ] = minion_id #NEWLINE# if ( 'environment' in pillar ) : #NEWLINE# #INDENT# inp [ 'environment' ] = pillar [ 'environment' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( 'environment' in __grains__ ) : #NEWLINE# #INDENT# inp [ 'environment' ] = __grains__ [ 'environment' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# inp [ 'environment' ] = 'base' #NEWLINE# #UNINDENT# #UNINDENT# output = inp #NEWLINE# output [ 'pepa_templates' ] = [ ] #NEWLINE# immutable = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( categ , info ) in [ next ( six . iteritems ( s ) ) for s in sequence ] : #NEWLINE# #INDENT# if ( categ not in inp ) : #NEWLINE# #INDENT# log . warning ( 'Category is not defined: {0}' . format ( categ ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# alias = None #NEWLINE# if ( isinstance ( info , dict ) and ( 'name' in info ) ) : #NEWLINE# #INDENT# alias = info [ 'name' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# alias = categ #NEWLINE# #UNINDENT# templdir = None #NEWLINE# if ( info and ( 'base_only' in info ) and info [ 'base_only' ] ) : #NEWLINE# #INDENT# templdir = join ( roots [ 'base' ] , resource , alias ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# templdir = join ( roots [ inp [ 'environment' ] ] , resource , alias ) #NEWLINE# #UNINDENT# entries = [ ] #NEWLINE# if isinstance ( inp [ categ ] , list ) : #NEWLINE# #INDENT# entries = inp [ categ ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not inp [ categ ] ) : #NEWLINE# #INDENT# log . warning ( 'Category has no value set: {0}' . format ( categ ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# entries = [ inp [ categ ] ] #NEWLINE# #UNINDENT# #UNINDENT# for entry in entries : #NEWLINE# #INDENT# results_jinja = None #NEWLINE# results = None #NEWLINE# fn = join ( templdir , ( re . sub ( '\\W' , '_' , entry . lower ( ) ) + '.yaml' ) ) #NEWLINE# if isfile ( fn ) : #NEWLINE# #INDENT# log . info ( 'Loading template: {0}' . format ( fn ) ) #NEWLINE# with salt . utils . fopen ( fn ) as fhr : #NEWLINE# #INDENT# template = jinja2 . Template ( fhr . read ( ) ) #NEWLINE# #UNINDENT# output [ 'pepa_templates' ] . append ( fn ) #NEWLINE# try : #NEWLINE# #INDENT# data = key_value_to_tree ( output ) #NEWLINE# data [ 'grains' ] = __grains__ . copy ( ) #NEWLINE# data [ 'pillar' ] = pillar . copy ( ) #NEWLINE# results_jinja = template . render ( data ) #NEWLINE# results = yaml . load ( results_jinja ) #NEWLINE# #UNINDENT# except jinja2 . UndefinedError as err : #NEWLINE# #INDENT# log . error ( 'Failed to parse JINJA template: {0}\n{1}' . format ( fn , err ) ) #NEWLINE# #UNINDENT# except yaml . YAMLError as err : #NEWLINE# #INDENT# log . error ( 'Failed to parse YAML in template: {0}\n{1}' . format ( fn , err ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# log . info ( "Template doesn't exist: {0}" . format ( fn ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( results is not None ) : #NEWLINE# #INDENT# for key in results : #NEWLINE# #INDENT# skey = key . rsplit ( __opts__ [ 'pepa_delimiter' ] , 1 ) #NEWLINE# rkey = None #NEWLINE# operator = None #NEWLINE# if ( ( len ( skey ) > 1 ) and ( key . rfind ( '()' ) > 0 ) ) : #NEWLINE# #INDENT# rkey = skey [ 0 ] . rstrip ( __opts__ [ 'pepa_delimiter' ] ) #NEWLINE# operator = skey [ 1 ] #NEWLINE# #UNINDENT# if ( key in immutable ) : #NEWLINE# #INDENT# log . warning ( 'Key {0} is immutable, changes are not allowed' . format ( key ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( rkey in immutable ) : #NEWLINE# #INDENT# log . warning ( 'Key {0} is immutable, changes are not allowed' . format ( rkey ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( operator == 'merge()' ) or ( operator == 'imerge()' ) ) : #NEWLINE# #INDENT# if ( operator == 'merge()' ) : #NEWLINE# #INDENT# log . debug ( 'Merge key {0}: {1}' . format ( rkey , results [ key ] ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . debug ( 'Set immutable and merge key {0}: {1}' . format ( rkey , results [ key ] ) ) #NEWLINE# immutable [ rkey ] = True #NEWLINE# #UNINDENT# if ( rkey not in output ) : #NEWLINE# #INDENT# log . error ( "Cant't merge key {0} doesn't exist" . format ( rkey ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not isinstance ( results [ key ] , type ( output [ rkey ] ) ) ) : #NEWLINE# #INDENT# log . error ( "Can't merge different types for key {0}" . format ( rkey ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( results [ key ] , dict ) : #NEWLINE# #INDENT# output [ rkey ] . update ( results [ key ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( results [ key ] , list ) : #NEWLINE# #INDENT# output [ rkey ] . extend ( results [ key ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . error ( 'Unsupported type need to be list or dict for key {0}' . format ( rkey ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( operator == 'unset()' ) or ( err == 'iunset()' ) ) : #NEWLINE# #INDENT# if ( operator == 'unset()' ) : #NEWLINE# #INDENT# log . debug ( 'Unset key {0}' . format ( rkey ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . debug ( 'Set immutable and unset key {0}' . format ( rkey ) ) #NEWLINE# immutable [ rkey ] = True #NEWLINE# #UNINDENT# if ( rkey in output ) : #NEWLINE# #INDENT# del output [ rkey ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator == 'immutable()' ) : #NEWLINE# #INDENT# log . debug ( 'Set immutable and substitute key {0}: {1}' . format ( rkey , results [ key ] ) ) #NEWLINE# immutable [ rkey ] = True #NEWLINE# output [ rkey ] = results [ key ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( operator is not None ) : #NEWLINE# #INDENT# log . error ( 'Unsupported operator {0}, skipping key {1}' . format ( operator , rkey ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . debug ( 'Substitute key {0}: {1}' . format ( key , results [ key ] ) ) #NEWLINE# output [ key ] = results [ key ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# tree = key_value_to_tree ( output ) #NEWLINE# pillar_data = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if subkey_only : #NEWLINE# #INDENT# pillar_data [ resource ] = tree . copy ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if subkey : #NEWLINE# #INDENT# pillar_data = tree #NEWLINE# pillar_data [ resource ] = tree . copy ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# pillar_data = tree #NEWLINE# #UNINDENT# #UNINDENT# if __opts__ [ 'pepa_validate' ] : #NEWLINE# #INDENT# pillar_data [ 'pepa_keys' ] = output . copy ( ) #NEWLINE# #UNINDENT# return pillar_data