<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import copy <EOL> import logging <EOL> import time <EOL> import traceback <EOL> import salt . crypt <EOL> import salt . payload <EOL> import salt . utils <EOL> import salt . utils . network <EOL> import salt . utils . event <EOL> from salt . exceptions import SaltClientError <EOL> import salt . ext . six as six <EOL> MINE_INTERNAL_KEYWORDS = frozenset ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> __proxyenabled__ = [ '<STR_LIT:*>' ] <EOL> log = logging . getLogger ( __name__ ) <EOL> def _auth ( ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' not in __context__ : <EOL> try : <EOL> __context__ [ '<STR_LIT>' ] = salt . crypt . SAuth ( __opts__ ) <EOL> except SaltClientError : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return __context__ [ '<STR_LIT>' ] <EOL> def _mine_function_available ( func ) : <EOL> if func not in __salt__ : <EOL> log . error ( '<STR_LIT>' <EOL> . format ( func ) ) <EOL> return False <EOL> return True <EOL> def _mine_send ( load , opts ) : <EOL> eventer = salt . utils . event . MinionEvent ( opts , listen = False ) <EOL> event_ret = eventer . fire_event ( load , '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> return event_ret <EOL> def _mine_get ( load , opts ) : <EOL> if opts . get ( '<STR_LIT>' , '<STR_LIT>' ) in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> load [ '<STR_LIT>' ] = _auth ( ) . gen_token ( '<STR_LIT>' ) <EOL> except AttributeError : <EOL> log . error ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> return False <EOL> channel = salt . transport . Channel . factory ( opts ) <EOL> ret = channel . send ( load ) <EOL> return ret <EOL> def update ( clear = False ) : <EOL> '''<STR_LIT>''' <EOL> m_data = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' , { } ) <EOL> if not m_data : <EOL> return <EOL> data = { } <EOL> for func in m_data : <EOL> try : <EOL> if m_data [ func ] and isinstance ( m_data [ func ] , dict ) : <EOL> mine_func = m_data [ func ] . pop ( '<STR_LIT>' , func ) <EOL> if not _mine_function_available ( mine_func ) : <EOL> continue <EOL> data [ func ] = __salt__ [ mine_func ] ( ** m_data [ func ] ) <EOL> elif m_data [ func ] and isinstance ( m_data [ func ] , list ) : <EOL> mine_func = func <EOL> if isinstance ( m_data [ func ] [ <NUM_LIT:0> ] , dict ) and '<STR_LIT>' in m_data [ func ] [ <NUM_LIT:0> ] : <EOL> mine_func = m_data [ func ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> m_data [ func ] . pop ( <NUM_LIT:0> ) <EOL> if not _mine_function_available ( mine_func ) : <EOL> continue <EOL> data [ func ] = __salt__ [ mine_func ] ( * m_data [ func ] ) <EOL> else : <EOL> if not _mine_function_available ( func ) : <EOL> continue <EOL> data [ func ] = __salt__ [ func ] ( ) <EOL> except Exception : <EOL> trace = traceback . format_exc ( ) <EOL> log . error ( '<STR_LIT>' <EOL> . format ( func ) ) <EOL> log . debug ( '<STR_LIT>' . format ( trace ) ) <EOL> continue <EOL> if __opts__ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if not clear : <EOL> old = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> if isinstance ( old , dict ) : <EOL> old . update ( data ) <EOL> data = old <EOL> return __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' , data ) <EOL> load = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:data>' : data , <EOL> '<STR_LIT:id>' : __opts__ [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : clear , <EOL> } <EOL> return _mine_send ( load , __opts__ ) <EOL> def send ( func , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> kwargs = salt . utils . clean_kwargs ( ** kwargs ) <EOL> mine_func = kwargs . pop ( '<STR_LIT>' , func ) <EOL> if mine_func not in __salt__ : <EOL> return False <EOL> data = { } <EOL> arg_data = salt . utils . arg_lookup ( __salt__ [ mine_func ] ) <EOL> func_data = copy . deepcopy ( kwargs ) <EOL> for ind , _ in enumerate ( arg_data . get ( '<STR_LIT:args>' , [ ] ) ) : <EOL> try : <EOL> func_data [ arg_data [ '<STR_LIT:args>' ] [ ind ] ] = args [ ind ] <EOL> except IndexError : <EOL> pass <EOL> f_call = salt . utils . format_call ( __salt__ [ mine_func ] , <EOL> func_data , <EOL> expected_extra_kws = MINE_INTERNAL_KEYWORDS ) <EOL> for arg in args : <EOL> if arg not in f_call [ '<STR_LIT:args>' ] : <EOL> f_call [ '<STR_LIT:args>' ] . append ( arg ) <EOL> try : <EOL> if '<STR_LIT>' in f_call : <EOL> data [ func ] = __salt__ [ mine_func ] ( * f_call [ '<STR_LIT:args>' ] , ** f_call [ '<STR_LIT>' ] ) <EOL> else : <EOL> data [ func ] = __salt__ [ mine_func ] ( * f_call [ '<STR_LIT:args>' ] ) <EOL> except Exception as exc : <EOL> log . error ( '<STR_LIT>' <EOL> . format ( mine_func , exc ) ) <EOL> return False <EOL> if __opts__ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> old = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> if isinstance ( old , dict ) : <EOL> old . update ( data ) <EOL> data = old <EOL> return __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' , data ) <EOL> load = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:data>' : data , <EOL> '<STR_LIT:id>' : __opts__ [ '<STR_LIT:id>' ] , <EOL> } <EOL> return _mine_send ( load , __opts__ ) <EOL> def get ( tgt , fun , expr_form = '<STR_LIT>' , exclude_minion = False ) : <EOL> '''<STR_LIT>''' <EOL> if __opts__ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> ret = { } <EOL> is_target = { '<STR_LIT>' : __salt__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : __salt__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT:list>' : __salt__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : __salt__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : __salt__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : __salt__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : __salt__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : __salt__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : __salt__ [ '<STR_LIT>' ] , <EOL> } [ expr_form ] ( tgt ) <EOL> if is_target : <EOL> data = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> if isinstance ( data , dict ) and fun in data : <EOL> ret [ __opts__ [ '<STR_LIT:id>' ] ] = data [ fun ] <EOL> return ret <EOL> load = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : __opts__ [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : tgt , <EOL> '<STR_LIT>' : fun , <EOL> '<STR_LIT>' : expr_form , <EOL> } <EOL> ret = _mine_get ( load , __opts__ ) <EOL> if exclude_minion : <EOL> if __opts__ [ '<STR_LIT:id>' ] in ret : <EOL> del ret [ __opts__ [ '<STR_LIT:id>' ] ] <EOL> return ret <EOL> def delete ( fun ) : <EOL> '''<STR_LIT>''' <EOL> if __opts__ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> data = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' ) <EOL> if isinstance ( data , dict ) and fun in data : <EOL> del data [ fun ] <EOL> return __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' , data ) <EOL> load = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : __opts__ [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : fun , <EOL> } <EOL> return _mine_send ( load , __opts__ ) <EOL> def flush ( ) : <EOL> '''<STR_LIT>''' <EOL> if __opts__ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' , { } ) <EOL> load = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : __opts__ [ '<STR_LIT:id>' ] , <EOL> } <EOL> return _mine_send ( load , __opts__ ) <EOL> def get_docker ( interfaces = None , cidrs = None , with_container_id = False ) : <EOL> '''<STR_LIT>''' <EOL> if interfaces : <EOL> interface_ = [ ] <EOL> interface_ . extend ( interfaces if isinstance ( interfaces , list ) else [ interfaces ] ) <EOL> interfaces = interface_ <EOL> if cidrs : <EOL> cidr_ = [ ] <EOL> cidr_ . extend ( cidrs if isinstance ( cidrs , list ) else [ cidrs ] ) <EOL> cidrs = cidr_ <EOL> cmd = '<STR_LIT>' <EOL> docker_hosts = get ( '<STR_LIT:*>' , cmd ) <EOL> proxy_lists = { } <EOL> for containers in six . itervalues ( docker_hosts ) : <EOL> host = containers . pop ( '<STR_LIT:host>' ) <EOL> host_ips = [ ] <EOL> if not interfaces : <EOL> for info in six . itervalues ( host [ '<STR_LIT>' ] ) : <EOL> if '<STR_LIT>' in info : <EOL> for ip_ in info [ '<STR_LIT>' ] : <EOL> host_ips . append ( ip_ [ '<STR_LIT:address>' ] ) <EOL> else : <EOL> for interface in interfaces : <EOL> if interface in host [ '<STR_LIT>' ] : <EOL> if '<STR_LIT>' in host [ '<STR_LIT>' ] [ interface ] : <EOL> for item in host [ '<STR_LIT>' ] [ interface ] [ '<STR_LIT>' ] : <EOL> host_ips . append ( item [ '<STR_LIT:address>' ] ) <EOL> host_ips = list ( set ( host_ips ) ) <EOL> if cidrs : <EOL> good_ips = [ ] <EOL> for cidr in cidrs : <EOL> for ip_ in host_ips : <EOL> if salt . utils . network . in_subnet ( cidr , [ ip_ ] ) : <EOL> good_ips . append ( ip_ ) <EOL> host_ips = list ( set ( good_ips ) ) <EOL> for container in six . itervalues ( containers ) : <EOL> container_id = container [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if container [ '<STR_LIT>' ] not in proxy_lists : <EOL> proxy_lists [ container [ '<STR_LIT>' ] ] = { } <EOL> for dock_port in container [ '<STR_LIT>' ] : <EOL> ip_address = dock_port . get ( '<STR_LIT>' ) <EOL> if ip_address == '<STR_LIT>' : <EOL> for ip_ in host_ips : <EOL> containers = proxy_lists [ container [ '<STR_LIT>' ] ] . setdefault ( '<STR_LIT>' , { } ) . setdefault ( dock_port [ '<STR_LIT>' ] , [ ] ) <EOL> container_network_footprint = '<STR_LIT>' . format ( ip_ , dock_port [ '<STR_LIT>' ] ) <EOL> if with_container_id : <EOL> value = ( container_network_footprint , container_id ) <EOL> else : <EOL> value = container_network_footprint <EOL> if value not in containers : <EOL> containers . append ( value ) <EOL> elif ip_address : <EOL> containers = proxy_lists [ container [ '<STR_LIT>' ] ] . setdefault ( '<STR_LIT>' , { } ) . setdefault ( dock_port [ '<STR_LIT>' ] , [ ] ) <EOL> container_network_footprint = '<STR_LIT>' . format ( dock_port [ '<STR_LIT>' ] , dock_port [ '<STR_LIT>' ] ) <EOL> if with_container_id : <EOL> value = ( container_network_footprint , container_id ) <EOL> else : <EOL> value = container_network_footprint <EOL> if value not in containers : <EOL> containers . append ( value ) <EOL> return proxy_lists </s>
