<s> from items_selector import ItemsSelector <EOL> from utils import compact_dict , get_client , chunk_index_list <EOL> from easydict import EasyDict <EOL> from collections import defaultdict <EOL> import curator . api as api <EOL> import logging <EOL> import sys <EOL> logger = logging . getLogger ( __name__ ) <EOL> class CuratorInvoke ( object ) : <EOL> SUPPORTS = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> } <EOL> def __init__ ( self , ** opts ) : <EOL> self . opts = EasyDict ( opts ) <EOL> self . _client = None <EOL> self . _iselector = None <EOL> @ property <EOL> def client ( self ) : <EOL> if not self . _client : <EOL> o = self . opts <EOL> self . _client = get_client ( ** ( { <EOL> '<STR_LIT:host>' : o . host , '<STR_LIT:port>' : o . port , '<STR_LIT>' : o . url_prefix , <EOL> '<STR_LIT>' : o . http_auth , '<STR_LIT>' : o . use_ssl , <EOL> '<STR_LIT>' : o . master_only , '<STR_LIT>' : o . timeout <EOL> } ) ) <EOL> return self . _client <EOL> @ property <EOL> def iselector ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _iselector : <EOL> self . _iselector = ItemsSelector ( self . client , ** self . opts ) <EOL> return self . _iselector <EOL> def _enhanced_working_list ( self , command , act_on ) : <EOL> """<STR_LIT>""" <EOL> working_list = self . iselector . fetch ( act_on = act_on ) <EOL> if command == '<STR_LIT>' : <EOL> logger . info ( "<STR_LIT>" ) <EOL> working_list = api . utils . prune_kibana ( working_list ) <EOL> if working_list and command == '<STR_LIT>' : <EOL> if self . opts . disk_space : <EOL> working_list = api . filter . filter_by_space ( self . client , working_list , <EOL> disk_space = float ( self . opts . disk_space ) , <EOL> reverse = ( self . opts . reverse or True ) ) <EOL> if not working_list : <EOL> logger . error ( '<STR_LIT>' , act_on , self . opts ) <EOL> print "<STR_LIT>" . format ( act_on ) <EOL> sys . exit ( <NUM_LIT> ) <EOL> return working_list <EOL> def fetch ( self , act_on , on_nofilters_showall = False ) : <EOL> """<STR_LIT>""" <EOL> return self . iselector . fetch ( act_on = act_on , on_nofilters_showall = on_nofilters_showall ) <EOL> def command_kwargs ( self , command ) : <EOL> """<STR_LIT>""" <EOL> opts = defaultdict ( lambda : None , self . opts ) <EOL> kwargs = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : opts [ '<STR_LIT:name>' ] , '<STR_LIT>' : opts [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : opts [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : opts [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : opts [ '<STR_LIT:count>' ] } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : opts [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : opts [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : opts [ '<STR_LIT>' ] <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : opts [ '<STR_LIT:name>' ] , '<STR_LIT>' : opts [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : opts [ '<STR_LIT>' ] , '<STR_LIT>' : opts [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : opts [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : opts [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : opts [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : opts [ '<STR_LIT>' ] <EOL> } <EOL> } . get ( command , { } ) <EOL> return compact_dict ( kwargs ) <EOL> def _call_api ( self , method , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> api_method = api . __dict__ . get ( method ) <EOL> logger . debug ( "<STR_LIT>" , <EOL> method , args , kwargs ) <EOL> return api_method ( self . client , * args , ** kwargs ) <EOL> def command_on_indices ( self , command , working_list ) : <EOL> """<STR_LIT>""" <EOL> kwargs = self . command_kwargs ( command ) <EOL> method = '<STR_LIT>' if command == '<STR_LIT>' else command <EOL> if len ( api . utils . to_csv ( working_list ) ) > <NUM_LIT> : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> success = True <EOL> for indices in chunk_index_list ( working_list ) : <EOL> if not self . _call_api ( method , indices , ** kwargs ) : <EOL> success = False <EOL> return success <EOL> else : <EOL> return self . _call_api ( method , working_list , ** kwargs ) <EOL> def command_on_snapshots ( self , command , working_list ) : <EOL> """<STR_LIT>""" <EOL> if command == '<STR_LIT>' : <EOL> method = '<STR_LIT>' <EOL> kwargs = self . command_kwargs ( command ) <EOL> kwargs [ '<STR_LIT>' ] = working_list <EOL> return self . _call_api ( method , ** kwargs ) <EOL> elif command == '<STR_LIT>' : <EOL> method = '<STR_LIT>' <EOL> success = True <EOL> for s in working_list : <EOL> if not self . _call_api ( method , repository = self . opts . repository , <EOL> snapshot = s ) : <EOL> success = False <EOL> return success <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" . format ( '<STR_LIT>' , command ) ) <EOL> def invoke ( self , command = None , act_on = None ) : <EOL> """<STR_LIT>""" <EOL> if command not in self . SUPPORTS [ act_on ] : <EOL> raise ValueError ( "<STR_LIT>" . format ( command , act_on ) ) <EOL> if act_on is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> working_list = self . _enhanced_working_list ( command , act_on ) <EOL> if act_on == '<STR_LIT>' and command != '<STR_LIT>' : <EOL> return self . command_on_indices ( command , working_list ) <EOL> else : <EOL> return self . command_on_snapshots ( command , working_list ) </s>
