<s> from . exceptions import * <EOL> from . utils import * <EOL> import logging <EOL> import time <EOL> from datetime import datetime <EOL> class Alias ( object ) : <EOL> def __init__ ( self , name = None , extra_settings = { } ) : <EOL> """<STR_LIT>""" <EOL> if not name : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> self . name = parse_date_pattern ( name ) <EOL> self . actions = [ ] <EOL> self . client = None <EOL> self . extra_settings = extra_settings <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> def add ( self , ilo ) : <EOL> """<STR_LIT>""" <EOL> verify_index_list ( ilo ) <EOL> if not self . client : <EOL> self . client = ilo . client <EOL> ilo . empty_list_check ( ) <EOL> for index in ilo . working_list ( ) : <EOL> self . loggit . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( index , self . name , self . extra_settings ) <EOL> ) <EOL> add_dict = { '<STR_LIT>' : { '<STR_LIT:index>' : index , '<STR_LIT>' : self . name } } <EOL> add_dict [ '<STR_LIT>' ] . update ( self . extra_settings ) <EOL> self . actions . append ( add_dict ) <EOL> def remove ( self , ilo ) : <EOL> """<STR_LIT>""" <EOL> verify_index_list ( ilo ) <EOL> if not self . client : <EOL> self . client = ilo . client <EOL> ilo . empty_list_check ( ) <EOL> for index in ilo . working_list ( ) : <EOL> self . loggit . debug ( <EOL> '<STR_LIT>' . format ( index , self . name ) ) <EOL> self . actions . append ( <EOL> { '<STR_LIT>' : { '<STR_LIT:index>' : index , '<STR_LIT>' : self . name } } ) <EOL> def body ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . actions : <EOL> raise ActionError ( '<STR_LIT>' ) <EOL> self . loggit . debug ( '<STR_LIT>' . format ( self . actions ) ) <EOL> return { '<STR_LIT>' : self . actions } <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . loggit . info ( '<STR_LIT>' ) <EOL> for item in self . body ( ) [ '<STR_LIT>' ] : <EOL> job = list ( item . keys ( ) ) [ <NUM_LIT:0> ] <EOL> index = item [ job ] [ '<STR_LIT:index>' ] <EOL> alias = item [ job ] [ '<STR_LIT>' ] <EOL> self . loggit . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> job . rstrip ( '<STR_LIT:e>' ) , <EOL> index , <EOL> '<STR_LIT:to>' if job is '<STR_LIT>' else '<STR_LIT>' , <EOL> alias <EOL> ) <EOL> ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> self . loggit . info ( '<STR_LIT>' ) <EOL> try : <EOL> self . client . indices . update_aliases ( body = self . body ( ) ) <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> class Allocation ( object ) : <EOL> def __init__ ( self , ilo , key = None , value = None , allocation_type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> verify_index_list ( ilo ) <EOL> if not key : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> if not value : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> if allocation_type not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( allocation_type ) <EOL> ) <EOL> self . index_list = ilo <EOL> self . client = ilo . client <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> self . body = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( allocation_type , key , value ) <EOL> ) <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> show_dry_run ( self . index_list , '<STR_LIT>' , body = self . body ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> self . loggit . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> self . index_list . filter_closed ( ) <EOL> self . index_list . empty_list_check ( ) <EOL> self . loggit . info ( '<STR_LIT>' . format ( self . body ) ) <EOL> try : <EOL> index_lists = chunk_index_list ( self . index_list . indices ) <EOL> for l in index_lists : <EOL> self . client . indices . put_settings ( <EOL> index = to_csv ( l ) , body = self . body <EOL> ) <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> class Close ( object ) : <EOL> def __init__ ( self , ilo ) : <EOL> """<STR_LIT>""" <EOL> verify_index_list ( ilo ) <EOL> self . index_list = ilo <EOL> self . client = ilo . client <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> show_dry_run ( self . index_list , '<STR_LIT>' ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> self . index_list . filter_closed ( ) <EOL> self . index_list . empty_list_check ( ) <EOL> self . loggit . info ( '<STR_LIT>' ) <EOL> try : <EOL> index_lists = chunk_index_list ( self . index_list . indices ) <EOL> for l in index_lists : <EOL> self . client . indices . flush ( <EOL> index = to_csv ( l ) , ignore_unavailable = True ) <EOL> self . client . indices . close ( <EOL> index = to_csv ( l ) , ignore_unavailable = True ) <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> class CreateIndex ( object ) : <EOL> def __init__ ( self , client , name , extra_settings = { } ) : <EOL> """<STR_LIT>""" <EOL> verify_client_object ( client ) <EOL> if not name : <EOL> raise ConfigurationError ( '<STR_LIT>' ) <EOL> self . name = parse_date_pattern ( name ) <EOL> self . body = extra_settings <EOL> self . client = client <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> logger . info ( '<STR_LIT>' ) <EOL> self . loggit . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . name , self . body ) <EOL> ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> self . loggit . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . name , self . body ) <EOL> ) <EOL> try : <EOL> self . client . indices . create ( index = self . name , body = self . body ) <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> class DeleteIndices ( object ) : <EOL> def __init__ ( self , ilo , master_timeout = <NUM_LIT:30> ) : <EOL> """<STR_LIT>""" <EOL> verify_index_list ( ilo ) <EOL> if not type ( master_timeout ) == type ( int ( ) ) : <EOL> raise TypeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( type ( master_timeout ) ) <EOL> ) <EOL> self . index_list = ilo <EOL> self . client = ilo . client <EOL> self . master_timeout = str ( master_timeout ) + '<STR_LIT:s>' <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> self . loggit . debug ( '<STR_LIT>' . format ( <EOL> self . master_timeout ) ) <EOL> def _verify_result ( self , result , count ) : <EOL> """<STR_LIT>""" <EOL> if len ( result ) > <NUM_LIT:0> : <EOL> self . loggit . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( count ) <EOL> ) <EOL> for idx in result : <EOL> self . loggit . error ( "<STR_LIT>" . format ( idx ) ) <EOL> return False <EOL> else : <EOL> self . loggit . debug ( <EOL> '<STR_LIT>' . format ( count ) <EOL> ) <EOL> return True <EOL> def __chunk_loop ( self , chunk_list ) : <EOL> """<STR_LIT>""" <EOL> working_list = chunk_list <EOL> for count in range ( <NUM_LIT:1> , <NUM_LIT:4> ) : <EOL> for i in working_list : <EOL> self . loggit . info ( "<STR_LIT>" . format ( i ) ) <EOL> self . client . indices . delete ( <EOL> index = to_csv ( working_list ) , master_timeout = self . master_timeout ) <EOL> result = [ i for i in working_list if i in get_indices ( self . client ) ] <EOL> if self . _verify_result ( result , count ) : <EOL> return <EOL> else : <EOL> working_list = result <EOL> self . loggit . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( result ) <EOL> ) <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> show_dry_run ( self . index_list , '<STR_LIT>' ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> self . index_list . empty_list_check ( ) <EOL> self . loggit . info ( '<STR_LIT>' ) <EOL> try : <EOL> index_lists = chunk_index_list ( self . index_list . indices ) <EOL> for l in index_lists : <EOL> self . __chunk_loop ( l ) <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> class ForceMerge ( object ) : <EOL> def __init__ ( self , ilo , max_num_segments = None , delay = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> verify_index_list ( ilo ) <EOL> if not max_num_segments : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> self . client = ilo . client <EOL> self . index_list = ilo <EOL> self . max_num_segments = max_num_segments <EOL> self . delay = delay <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> show_dry_run ( <EOL> self . index_list , '<STR_LIT>' , <EOL> max_num_segments = self . max_num_segments , <EOL> delay = self . delay , <EOL> ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> self . index_list . empty_list_check ( ) <EOL> self . index_list . filter_forceMerged ( <EOL> max_num_segments = self . max_num_segments ) <EOL> self . loggit . info ( '<STR_LIT>' ) <EOL> try : <EOL> for index_name in self . index_list . indices : <EOL> self . loggit . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( index_name , self . max_num_segments ) <EOL> ) <EOL> if get_version ( self . client ) < ( <NUM_LIT:5> , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> self . client . indices . optimize ( index = index_name , <EOL> max_num_segments = self . max_num_segments ) <EOL> else : <EOL> self . client . indices . forcemerge ( index = index_name , <EOL> max_num_segments = self . max_num_segments ) <EOL> if self . delay > <NUM_LIT:0> : <EOL> self . loggit . info ( <EOL> '<STR_LIT>' . format ( <EOL> self . delay ) <EOL> ) <EOL> time . sleep ( self . delay ) <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> class Open ( object ) : <EOL> def __init__ ( self , ilo ) : <EOL> """<STR_LIT>""" <EOL> verify_index_list ( ilo ) <EOL> self . client = ilo . client <EOL> self . index_list = ilo <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> show_dry_run ( self . index_list , '<STR_LIT>' ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> self . index_list . empty_list_check ( ) <EOL> self . loggit . info ( '<STR_LIT>' ) <EOL> try : <EOL> index_lists = chunk_index_list ( self . index_list . indices ) <EOL> for l in index_lists : <EOL> self . client . indices . open ( index = to_csv ( l ) ) <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> class Replicas ( object ) : <EOL> def __init__ ( self , ilo , count = None ) : <EOL> """<STR_LIT>""" <EOL> verify_index_list ( ilo ) <EOL> if count == <NUM_LIT:0> : <EOL> pass <EOL> elif not count : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> self . client = ilo . client <EOL> self . index_list = ilo <EOL> self . count = count <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> show_dry_run ( self . index_list , '<STR_LIT>' , count = self . count ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> self . index_list . empty_list_check ( ) <EOL> self . loggit . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> self . index_list . filter_closed ( ) <EOL> self . loggit . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . count ) <EOL> ) <EOL> try : <EOL> index_lists = chunk_index_list ( self . index_list . indices ) <EOL> for l in index_lists : <EOL> self . client . indices . put_settings ( index = to_csv ( l ) , <EOL> body = '<STR_LIT>' . format ( self . count ) ) <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> class DeleteSnapshots ( object ) : <EOL> def __init__ ( self , slo , retry_interval = <NUM_LIT> , retry_count = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> verify_snapshot_list ( slo ) <EOL> self . client = slo . client <EOL> self . retry_interval = retry_interval <EOL> self . retry_count = retry_count <EOL> self . snapshot_list = slo <EOL> self . repository = slo . repository <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> logger . info ( '<STR_LIT>' ) <EOL> mykwargs = { <EOL> '<STR_LIT>' : self . repository , <EOL> '<STR_LIT>' : self . retry_interval , <EOL> '<STR_LIT>' : self . retry_count , <EOL> } <EOL> for snap in self . snapshot_list . snapshots : <EOL> logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( snap , mykwargs ) ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> self . snapshot_list . empty_list_check ( ) <EOL> self . loggit . info ( '<STR_LIT>' ) <EOL> if not safe_to_snap ( <EOL> self . client , repository = self . repository , <EOL> retry_interval = self . retry_interval , retry_count = self . retry_count ) : <EOL> raise FailedExecution ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> for s in self . snapshot_list . snapshots : <EOL> self . loggit . info ( '<STR_LIT>' . format ( s ) ) <EOL> self . client . snapshot . delete ( <EOL> repository = self . repository , snapshot = s ) <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> class Snapshot ( object ) : <EOL> def __init__ ( self , ilo , repository = None , name = None , <EOL> ignore_unavailable = False , include_global_state = True , <EOL> partial = False , wait_for_completion = True , <EOL> skip_repo_fs_check = False ) : <EOL> """<STR_LIT>""" <EOL> verify_index_list ( ilo ) <EOL> if not repository_exists ( ilo . client , repository = repository ) : <EOL> raise ActionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( repository ) <EOL> ) <EOL> if not name : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> self . name = parse_date_pattern ( name ) <EOL> self . client = ilo . client <EOL> self . index_list = ilo <EOL> self . repository = repository <EOL> self . wait_for_completion = wait_for_completion <EOL> self . skip_repo_fs_check = skip_repo_fs_check <EOL> self . state = None <EOL> self . body = create_snapshot_body ( <EOL> ilo . indices , <EOL> ignore_unavailable = ignore_unavailable , <EOL> include_global_state = include_global_state , <EOL> partial = partial <EOL> ) <EOL> self . loggit = logging . getLogger ( '<STR_LIT>' ) <EOL> def get_state ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . state = self . client . snapshot . get ( <EOL> repository = self . repository , <EOL> snapshot = self . name ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:state>' ] <EOL> return self . state <EOL> except IndexError : <EOL> raise CuratorException ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . name , self . repository ) <EOL> ) <EOL> def report_state ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get_state ( ) <EOL> if self . state == '<STR_LIT>' : <EOL> self . loggit . info ( <EOL> '<STR_LIT>' . format ( self . name ) ) <EOL> else : <EOL> self . loggit . warn ( <EOL> '<STR_LIT>' . format ( self . state ) ) <EOL> def do_dry_run ( self ) : <EOL> """<STR_LIT>""" <EOL> show_dry_run ( self . index_list , '<STR_LIT>' , body = self . body ) <EOL> def do_action ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . skip_repo_fs_check : <EOL> test_repo_fs ( self . client , self . repository ) <EOL> if snapshot_running ( self . client ) : <EOL> raise SnapshotInProgress ( '<STR_LIT>' ) <EOL> try : <EOL> self . client . snapshot . create ( <EOL> repository = self . repository , snapshot = self . name , body = self . body , <EOL> wait_for_completion = self . wait_for_completion <EOL> ) <EOL> if self . wait_for_completion : <EOL> self . report_state ( ) <EOL> else : <EOL> self . loggit . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( self . wait_for_completion ) <EOL> ) <EOL> except Exception as e : <EOL> report_failure ( e ) </s>
