<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import re <EOL> import yaml <EOL> from salt . exceptions import CommandExecutionError <EOL> from salt . utils . dictupdate import update as dict_merge <EOL> try : <EOL> import consul <EOL> HAS_CONSUL = True <EOL> except ImportError : <EOL> HAS_CONSUL = False <EOL> __virtualname__ = '<STR_LIT>' <EOL> log = logging . getLogger ( __name__ ) <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> return __virtualname__ if HAS_CONSUL else False <EOL> def ext_pillar ( minion_id , <EOL> pillar , <EOL> conf ) : <EOL> '''<STR_LIT>''' <EOL> comps = conf . split ( ) <EOL> profile = None <EOL> if comps [ <NUM_LIT:0> ] : <EOL> profile = comps [ <NUM_LIT:0> ] <EOL> client = get_conn ( __opts__ , profile ) <EOL> path = '<STR_LIT>' <EOL> if len ( comps ) > <NUM_LIT:1> and comps [ <NUM_LIT:1> ] . startswith ( '<STR_LIT>' ) : <EOL> path = comps [ <NUM_LIT:1> ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> role = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> path %= { <EOL> '<STR_LIT>' : minion_id , <EOL> '<STR_LIT>' : role <EOL> } <EOL> try : <EOL> pillar = fetch_tree ( client , path ) <EOL> except KeyError : <EOL> log . error ( '<STR_LIT>' <EOL> . format ( profile , path ) ) <EOL> pillar = { } <EOL> return pillar <EOL> def consul_fetch ( client , path ) : <EOL> '''<STR_LIT>''' <EOL> return client . kv . get ( path , recurse = True ) <EOL> def fetch_tree ( client , path ) : <EOL> '''<STR_LIT>''' <EOL> index , items = consul_fetch ( client , path ) <EOL> ret = { } <EOL> has_children = re . compile ( r'<STR_LIT>' ) <EOL> log . debug ( '<STR_LIT>' , format ( items ) ) <EOL> if items is None : <EOL> return ret <EOL> for item in reversed ( items ) : <EOL> key = re . sub ( r'<STR_LIT>' + path + '<STR_LIT>' , '<STR_LIT>' , item [ '<STR_LIT>' ] ) <EOL> if key != "<STR_LIT>" : <EOL> log . debug ( '<STR_LIT>' . format ( path , key ) ) <EOL> log . debug ( '<STR_LIT>' , format ( has_children . search ( key ) ) ) <EOL> if has_children . search ( key ) is None : <EOL> ret = pillar_format ( ret , key . split ( '<STR_LIT:/>' ) , item [ '<STR_LIT>' ] ) <EOL> log . debug ( '<STR_LIT>' , format ( item ) ) <EOL> return ret <EOL> def pillar_format ( ret , keys , value ) : <EOL> '''<STR_LIT>''' <EOL> if value is None : <EOL> return ret <EOL> pillar_value = yaml . load ( value ) <EOL> keyvalue = keys . pop ( ) <EOL> pil = { keyvalue : pillar_value } <EOL> keys . reverse ( ) <EOL> for k in keys : <EOL> pil = { k : pil } <EOL> return dict_merge ( ret , pil ) <EOL> def get_conn ( opts , profile ) : <EOL> '''<STR_LIT>''' <EOL> opts_pillar = opts . get ( '<STR_LIT>' , { } ) <EOL> opts_master = opts_pillar . get ( '<STR_LIT>' , { } ) <EOL> opts_merged = { } <EOL> opts_merged . update ( opts_master ) <EOL> opts_merged . update ( opts_pillar ) <EOL> opts_merged . update ( opts ) <EOL> if profile : <EOL> conf = opts_merged . get ( profile , { } ) <EOL> else : <EOL> conf = opts_merged <EOL> params = { } <EOL> for key in ( '<STR_LIT:host>' , '<STR_LIT:port>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> prefixed_key = '<STR_LIT>' . format ( key = key ) <EOL> if prefixed_key in conf : <EOL> params [ key ] = conf [ prefixed_key ] <EOL> if '<STR_LIT>' in params : <EOL> pillarenv = opts_merged . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> params [ '<STR_LIT>' ] = _resolve_datacenter ( params [ '<STR_LIT>' ] , pillarenv ) <EOL> if HAS_CONSUL : <EOL> return consul . Consul ( ** params ) <EOL> else : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> def _resolve_datacenter ( dc , pillarenv ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( "<STR_LIT>" . format ( dc = dc ) ) <EOL> try : <EOL> mappings = dc . items ( ) <EOL> except AttributeError : <EOL> log . debug ( '<STR_LIT>' . format ( dc = dc ) ) <EOL> return dc <EOL> log . debug ( <EOL> '<STR_LIT>' . format ( <EOL> num = len ( mappings ) <EOL> ) <EOL> ) <EOL> log . debug ( '<STR_LIT>' . format ( env = pillarenv ) ) <EOL> sorted_mappings = sorted ( mappings , key = lambda m : ( - len ( m [ <NUM_LIT:0> ] ) , m [ <NUM_LIT:0> ] ) ) <EOL> for pattern , target in sorted_mappings : <EOL> match = re . match ( pattern , pillarenv ) <EOL> if match : <EOL> log . debug ( '<STR_LIT>' . format ( pattern = pattern ) ) <EOL> result = target . format ( ** match . groupdict ( ) ) <EOL> log . debug ( '<STR_LIT>' . format ( result = result ) ) <EOL> return result <EOL> log . debug ( <EOL> '<STR_LIT>' . format ( <EOL> env = pillarenv , lst = '<STR_LIT:U+002CU+0020>' . join ( repr ( x ) for x in mappings ) <EOL> ) <EOL> ) </s>
