<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> from salt . ext . six . moves . urllib . parse import urlencode as _urlencode <EOL> from salt . ext . six . moves . urllib . request import ( <EOL> HTTPBasicAuthHandler as _HTTPBasicAuthHandler , <EOL> HTTPDigestAuthHandler as _HTTPDigestAuthHandler , <EOL> urlopen as _urlopen , <EOL> build_opener as _build_opener , <EOL> install_opener as _install_opener <EOL> ) <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> return True <EOL> def _auth ( url , user , passwd , realm ) : <EOL> '''<STR_LIT>''' <EOL> basic = _HTTPBasicAuthHandler ( ) <EOL> basic . add_password ( realm = realm , uri = url , user = user , passwd = passwd ) <EOL> digest = _HTTPDigestAuthHandler ( ) <EOL> digest . add_password ( realm = realm , uri = url , user = user , passwd = passwd ) <EOL> return _build_opener ( basic , digest ) <EOL> def _do_http ( opts , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> url = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' . format ( profile ) , '<STR_LIT>' ) <EOL> user = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' . format ( profile ) , '<STR_LIT>' ) <EOL> passwd = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' . format ( profile ) , '<STR_LIT>' ) <EOL> realm = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' . format ( profile ) , '<STR_LIT>' ) <EOL> timeout = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' . format ( profile ) , '<STR_LIT>' ) <EOL> if not url : <EOL> raise Exception ( '<STR_LIT>' . format ( profile ) ) <EOL> if user and passwd : <EOL> auth = _auth ( url , realm , user , passwd ) <EOL> _install_opener ( auth ) <EOL> url += '<STR_LIT>' . format ( _urlencode ( opts ) ) <EOL> for line in _urlopen ( url , timeout = timeout ) . read ( ) . splitlines ( ) : <EOL> splt = line . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if splt [ <NUM_LIT:0> ] in ret : <EOL> ret [ splt [ <NUM_LIT:0> ] ] += '<STR_LIT>' . format ( splt [ <NUM_LIT:1> ] ) <EOL> else : <EOL> ret [ splt [ <NUM_LIT:0> ] ] = splt [ <NUM_LIT:1> ] <EOL> return ret <EOL> def _worker_ctl ( worker , lbn , vwa , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> cmd = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:w>' : lbn , <EOL> '<STR_LIT>' : worker , <EOL> '<STR_LIT>' : vwa , <EOL> } <EOL> return _do_http ( cmd , profile ) [ '<STR_LIT>' ] == '<STR_LIT:OK>' <EOL> def version ( profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> cmd = { <EOL> '<STR_LIT>' : '<STR_LIT:version>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> return _do_http ( cmd , profile ) [ '<STR_LIT>' ] . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> def get_running ( profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> cmd = { <EOL> '<STR_LIT>' : '<STR_LIT:list>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> return _do_http ( cmd , profile ) <EOL> def dump_config ( profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> cmd = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> return _do_http ( cmd , profile ) <EOL> def list_configured_members ( lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> config = dump_config ( profile ) <EOL> try : <EOL> ret = config [ '<STR_LIT>' . format ( lbn ) ] <EOL> except KeyError : <EOL> return [ ] <EOL> return [ _f for _f in ret . strip ( ) . split ( '<STR_LIT:U+002C>' ) if _f ] <EOL> def workers ( profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> config = get_running ( profile ) <EOL> lbn = config [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> worker_list = [ ] <EOL> ret = { } <EOL> for lb in lbn : <EOL> try : <EOL> worker_list . extend ( <EOL> config [ '<STR_LIT>' . format ( lb ) ] . split ( '<STR_LIT:U+002C>' ) <EOL> ) <EOL> except KeyError : <EOL> pass <EOL> worker_list = list ( set ( worker_list ) ) <EOL> for worker in worker_list : <EOL> ret [ worker ] = { <EOL> '<STR_LIT>' : config [ '<STR_LIT>' . format ( worker ) ] , <EOL> '<STR_LIT:state>' : config [ '<STR_LIT>' . format ( worker ) ] , <EOL> } <EOL> return ret <EOL> def recover_all ( lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> config = get_running ( profile ) <EOL> try : <EOL> workers_ = config [ '<STR_LIT>' . format ( lbn ) ] . split ( '<STR_LIT:U+002C>' ) <EOL> except KeyError : <EOL> return ret <EOL> for worker in workers_ : <EOL> curr_state = worker_status ( worker , profile ) <EOL> if curr_state [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> worker_activate ( worker , lbn , profile ) <EOL> if not curr_state [ '<STR_LIT:state>' ] . startswith ( '<STR_LIT:OK>' ) : <EOL> worker_recover ( worker , lbn , profile ) <EOL> ret [ worker ] = worker_status ( worker , profile ) <EOL> return ret <EOL> def reset_stats ( lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> cmd = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:w>' : lbn , <EOL> } <EOL> return _do_http ( cmd , profile ) [ '<STR_LIT>' ] == '<STR_LIT:OK>' <EOL> def lb_edit ( lbn , settings , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> settings [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> settings [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> settings [ '<STR_LIT:w>' ] = lbn <EOL> return _do_http ( settings , profile ) [ '<STR_LIT>' ] == '<STR_LIT:OK>' <EOL> def bulk_stop ( workers , lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> if isinstance ( workers , str ) : <EOL> workers = workers . split ( '<STR_LIT:U+002C>' ) <EOL> for worker in workers : <EOL> try : <EOL> ret [ worker ] = worker_stop ( worker , lbn , profile ) <EOL> except Exception : <EOL> ret [ worker ] = False <EOL> return ret <EOL> def bulk_activate ( workers , lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> if isinstance ( workers , str ) : <EOL> workers = workers . split ( '<STR_LIT:U+002C>' ) <EOL> for worker in workers : <EOL> try : <EOL> ret [ worker ] = worker_activate ( worker , lbn , profile ) <EOL> except Exception : <EOL> ret [ worker ] = False <EOL> return ret <EOL> def bulk_disable ( workers , lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> if isinstance ( workers , str ) : <EOL> workers = workers . split ( '<STR_LIT:U+002C>' ) <EOL> for worker in workers : <EOL> try : <EOL> ret [ worker ] = worker_disable ( worker , lbn , profile ) <EOL> except Exception : <EOL> ret [ worker ] = False <EOL> return ret <EOL> def bulk_recover ( workers , lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> if isinstance ( workers , str ) : <EOL> workers = workers . split ( '<STR_LIT:U+002C>' ) <EOL> for worker in workers : <EOL> try : <EOL> ret [ worker ] = worker_recover ( worker , lbn , profile ) <EOL> except Exception : <EOL> ret [ worker ] = False <EOL> return ret <EOL> def worker_status ( worker , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> config = get_running ( profile ) <EOL> try : <EOL> return { <EOL> '<STR_LIT>' : config [ '<STR_LIT>' . format ( worker ) ] , <EOL> '<STR_LIT:state>' : config [ '<STR_LIT>' . format ( worker ) ] , <EOL> } <EOL> except KeyError : <EOL> return False <EOL> def worker_recover ( worker , lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> cmd = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:w>' : lbn , <EOL> '<STR_LIT>' : worker , <EOL> } <EOL> return _do_http ( cmd , profile ) <EOL> def worker_disable ( worker , lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> return _worker_ctl ( worker , lbn , '<STR_LIT:d>' , profile ) <EOL> def worker_activate ( worker , lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> return _worker_ctl ( worker , lbn , '<STR_LIT:a>' , profile ) <EOL> def worker_stop ( worker , lbn , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> return _worker_ctl ( worker , lbn , '<STR_LIT:s>' , profile ) <EOL> def worker_edit ( worker , lbn , settings , profile = '<STR_LIT:default>' ) : <EOL> '''<STR_LIT>''' <EOL> settings [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> settings [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> settings [ '<STR_LIT:w>' ] = lbn <EOL> settings [ '<STR_LIT>' ] = worker <EOL> return _do_http ( settings , profile ) [ '<STR_LIT>' ] == '<STR_LIT:OK>' </s>
