<s> from datetime import timedelta , datetime , date <EOL> import elasticsearch <EOL> import copy <EOL> import time <EOL> import re <EOL> import sys <EOL> import logging <EOL> import yaml <EOL> from . exceptions import * <EOL> from . settings import DATE_REGEX , VERSION_MAX , VERSION_MIN , ACTION_DEFAULTS <EOL> from . _version import __version__ <EOL> logger = logging . getLogger ( __name__ ) <EOL> def read_file ( myfile ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with open ( myfile , '<STR_LIT:r>' ) as f : <EOL> data = f . read ( ) <EOL> return data <EOL> except IOError as e : <EOL> raise FailedExecution ( <EOL> '<STR_LIT>' . format ( myfile ) <EOL> ) <EOL> def get_yaml ( path ) : <EOL> """<STR_LIT>""" <EOL> raw = read_file ( path ) <EOL> try : <EOL> cfg = yaml . load ( raw ) <EOL> except yaml . scanner . ScannerError as e : <EOL> raise ConfigurationError ( <EOL> '<STR_LIT>' . format ( e ) ) <EOL> return cfg <EOL> def test_client_options ( config ) : <EOL> """<STR_LIT>""" <EOL> if config [ '<STR_LIT>' ] : <EOL> if '<STR_LIT>' in config and config [ '<STR_LIT>' ] : <EOL> read_file ( config [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in config and config [ '<STR_LIT>' ] : <EOL> read_file ( config [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in config and config [ '<STR_LIT>' ] : <EOL> read_file ( config [ '<STR_LIT>' ] ) <EOL> def verify_client_object ( test ) : <EOL> """<STR_LIT>""" <EOL> if str ( type ( test ) ) == "<STR_LIT>" : <EOL> pass <EOL> elif not type ( test ) == type ( elasticsearch . Elasticsearch ( ) ) : <EOL> raise TypeError ( <EOL> '<STR_LIT>' . format ( type ( test ) ) <EOL> ) <EOL> def verify_index_list ( test ) : <EOL> """<STR_LIT>""" <EOL> if not str ( type ( test ) ) == "<STR_LIT>" : <EOL> raise TypeError ( <EOL> '<STR_LIT>' . format ( type ( test ) ) <EOL> ) <EOL> def verify_snapshot_list ( test ) : <EOL> """<STR_LIT>""" <EOL> if not str ( type ( test ) ) == "<STR_LIT>" : <EOL> raise TypeError ( <EOL> '<STR_LIT>' . format ( type ( test ) ) <EOL> ) <EOL> def report_failure ( exception ) : <EOL> """<STR_LIT>""" <EOL> raise FailedExecution ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( exception ) <EOL> ) <EOL> def get_date_regex ( timestring ) : <EOL> """<STR_LIT>""" <EOL> prev = '<STR_LIT>' ; curr = '<STR_LIT>' ; regex = '<STR_LIT>' <EOL> for s in range ( <NUM_LIT:0> , len ( timestring ) ) : <EOL> curr = timestring [ s ] <EOL> if curr == '<STR_LIT:%>' : <EOL> pass <EOL> elif curr in DATE_REGEX and prev == '<STR_LIT:%>' : <EOL> regex += '<STR_LIT>' + DATE_REGEX [ curr ] + '<STR_LIT:}>' <EOL> elif curr in [ '<STR_LIT:.>' , '<STR_LIT:->' ] : <EOL> regex += "<STR_LIT:\\>" + curr <EOL> else : <EOL> regex += curr <EOL> prev = curr <EOL> logger . debug ( "<STR_LIT>" . format ( regex ) ) <EOL> return regex <EOL> def get_datetime ( index_timestamp , timestring ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in timestring : <EOL> timestring += '<STR_LIT>' <EOL> index_timestamp += '<STR_LIT:1>' <EOL> elif '<STR_LIT>' in timestring : <EOL> timestring += '<STR_LIT>' <EOL> index_timestamp += '<STR_LIT:1>' <EOL> elif '<STR_LIT>' in timestring : <EOL> if not '<STR_LIT>' in timestring : <EOL> timestring += '<STR_LIT>' <EOL> index_timestamp += '<STR_LIT:1>' <EOL> return datetime . strptime ( index_timestamp , timestring ) <EOL> def fix_epoch ( epoch ) : <EOL> """<STR_LIT>""" <EOL> epoch = int ( epoch ) <EOL> if len ( str ( epoch ) ) <= <NUM_LIT:10> : <EOL> return epoch <EOL> elif len ( str ( epoch ) ) == <NUM_LIT> : <EOL> return int ( epoch / <NUM_LIT:1000> ) <EOL> elif len ( str ( epoch ) ) > <NUM_LIT:10> and len ( str ( epoch ) ) < <NUM_LIT> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> else : <EOL> orders_of_magnitude = len ( str ( epoch ) ) - <NUM_LIT:10> <EOL> powers_of_ten = <NUM_LIT:10> ** orders_of_magnitude <EOL> epoch = int ( epoch / powers_of_ten ) <EOL> return epoch <EOL> class TimestringSearch ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , timestring ) : <EOL> regex = r'<STR_LIT>' . format ( get_date_regex ( timestring ) ) <EOL> self . pattern = re . compile ( regex ) <EOL> self . timestring = timestring <EOL> def get_epoch ( self , searchme ) : <EOL> """<STR_LIT>""" <EOL> match = self . pattern . search ( searchme ) <EOL> if match : <EOL> if match . group ( "<STR_LIT:date>" ) : <EOL> timestamp = match . group ( "<STR_LIT:date>" ) <EOL> return ( <EOL> ( get_datetime ( timestamp , self . timestring ) - <EOL> datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) . total_seconds ( ) <EOL> ) <EOL> def get_point_of_reference ( unit , count , epoch = None ) : <EOL> """<STR_LIT>""" <EOL> if unit == '<STR_LIT>' : <EOL> multiplier = <NUM_LIT:1> <EOL> elif unit == '<STR_LIT>' : <EOL> multiplier = <NUM_LIT> <EOL> elif unit == '<STR_LIT>' : <EOL> multiplier = <NUM_LIT> <EOL> elif unit == '<STR_LIT>' : <EOL> multiplier = <NUM_LIT> * <NUM_LIT> <EOL> elif unit == '<STR_LIT>' : <EOL> multiplier = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> <EOL> elif unit == '<STR_LIT>' : <EOL> multiplier = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:30> <EOL> elif unit == '<STR_LIT>' : <EOL> multiplier = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' . format ( unit ) ) <EOL> if not epoch : <EOL> epoch = time . time ( ) <EOL> epoch = fix_epoch ( epoch ) <EOL> return epoch - multiplier * count <EOL> def byte_size ( num , suffix = '<STR_LIT:B>' ) : <EOL> """<STR_LIT>""" <EOL> for unit in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:M>' , '<STR_LIT>' , '<STR_LIT:T>' , '<STR_LIT:P>' , '<STR_LIT:E>' , '<STR_LIT>' ] : <EOL> if abs ( num ) < <NUM_LIT> : <EOL> return "<STR_LIT>" % ( num , unit , suffix ) <EOL> num /= <NUM_LIT> <EOL> return "<STR_LIT>" % ( num , '<STR_LIT:Y>' , suffix ) <EOL> def ensure_list ( indices ) : <EOL> """<STR_LIT>""" <EOL> if type ( indices ) is not type ( list ( ) ) : <EOL> indices = [ indices ] <EOL> return indices <EOL> def to_csv ( indices ) : <EOL> """<STR_LIT>""" <EOL> indices = ensure_list ( indices ) <EOL> if indices : <EOL> return '<STR_LIT:U+002C>' . join ( sorted ( indices ) ) <EOL> else : <EOL> return None <EOL> def check_csv ( value ) : <EOL> """<STR_LIT>""" <EOL> if type ( value ) is type ( list ( ) ) : <EOL> return True <EOL> string = False <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> if type ( value ) is type ( unicode ( ) ) : <EOL> value = str ( value ) <EOL> if type ( value ) is type ( str ( ) ) : <EOL> if len ( value . split ( '<STR_LIT:U+002C>' ) ) > <NUM_LIT:1> : <EOL> return True <EOL> else : <EOL> return False <EOL> else : <EOL> raise TypeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( value , type ( value ) ) <EOL> ) <EOL> def chunk_index_list ( indices ) : <EOL> """<STR_LIT>""" <EOL> chunks = [ ] <EOL> chunk = "<STR_LIT>" <EOL> for index in indices : <EOL> if len ( chunk ) < <NUM_LIT> : <EOL> if not chunk : <EOL> chunk = index <EOL> else : <EOL> chunk += "<STR_LIT:U+002C>" + index <EOL> else : <EOL> chunks . append ( chunk . split ( '<STR_LIT:U+002C>' ) ) <EOL> chunk = index <EOL> chunks . append ( chunk . split ( '<STR_LIT:U+002C>' ) ) <EOL> return chunks <EOL> def get_indices ( client ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> indices = list ( <EOL> client . indices . get_settings ( <EOL> index = '<STR_LIT>' , params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> ) <EOL> logger . debug ( "<STR_LIT>" . format ( indices ) ) <EOL> return indices <EOL> except Exception as e : <EOL> raise FailedExecution ( '<STR_LIT>' . format ( e ) ) <EOL> def get_version ( client ) : <EOL> """<STR_LIT>""" <EOL> version = client . info ( ) [ '<STR_LIT:version>' ] [ '<STR_LIT>' ] <EOL> version = version . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] <EOL> if len ( version . split ( '<STR_LIT:.>' ) ) > <NUM_LIT:3> : <EOL> version = version . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] <EOL> else : <EOL> version = version . split ( '<STR_LIT:.>' ) <EOL> return tuple ( map ( int , version ) ) <EOL> def is_master_node ( client ) : <EOL> """<STR_LIT>""" <EOL> my_node_id = list ( client . nodes . info ( '<STR_LIT>' ) [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> master_node_id = client . cluster . state ( metric = '<STR_LIT>' ) [ '<STR_LIT>' ] <EOL> return my_node_id == master_node_id <EOL> def check_version ( client ) : <EOL> """<STR_LIT>""" <EOL> version_number = get_version ( client ) <EOL> logger . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( "<STR_LIT:.>" . join ( map ( str , version_number ) ) ) <EOL> ) <EOL> if version_number >= VERSION_MAX or version_number < VERSION_MIN : <EOL> raise CuratorException ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( "<STR_LIT:.>" . join ( map ( str , version_number ) ) , __version__ ) <EOL> ) <EOL> def check_master ( client , master_only = False ) : <EOL> """<STR_LIT>""" <EOL> if master_only and not is_master_node ( client ) : <EOL> logger . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def get_client ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> if ( <EOL> type ( kwargs [ '<STR_LIT>' ] ) == type ( None ) or <EOL> kwargs [ '<STR_LIT>' ] == "<STR_LIT:None>" <EOL> ) : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:127.0.0.1>' if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = False if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = False if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = False if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = False if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = False if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = False if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = ensure_list ( kwargs [ '<STR_LIT>' ] ) <EOL> logger . debug ( "<STR_LIT>" . format ( kwargs ) ) <EOL> master_only = kwargs . pop ( '<STR_LIT>' ) <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> else : <EOL> logger . info ( '<STR_LIT>' ) <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> kwargs [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> try : <EOL> import certifi <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> kwargs [ '<STR_LIT>' ] = certifi . where ( ) <EOL> except ImportError : <EOL> logger . warn ( '<STR_LIT>' ) <EOL> try : <EOL> from requests_aws4auth import AWS4Auth <EOL> kwargs [ '<STR_LIT>' ] = False if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = False if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = False if not '<STR_LIT>' in kwargs else kwargs [ '<STR_LIT>' ] <EOL> if kwargs [ '<STR_LIT>' ] or kwargs [ '<STR_LIT>' ] or kwargs [ '<STR_LIT>' ] : <EOL> if not kwargs [ '<STR_LIT>' ] and kwargs [ '<STR_LIT>' ] and kwargs [ '<STR_LIT>' ] : <EOL> raise MissingArgument ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> kwargs [ '<STR_LIT>' ] = elasticsearch . RequestsHttpConnection <EOL> kwargs [ '<STR_LIT>' ] = ( <EOL> AWS4Auth ( <EOL> kwargs [ '<STR_LIT>' ] , kwargs [ '<STR_LIT>' ] , <EOL> kwargs [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> ) <EOL> else : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> except ImportError : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> if master_only : <EOL> if len ( kwargs [ '<STR_LIT>' ] ) > <NUM_LIT:1> : <EOL> raise ConfigurationError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( kwargs [ '<STR_LIT>' ] ) <EOL> ) <EOL> try : <EOL> client = elasticsearch . Elasticsearch ( ** kwargs ) <EOL> check_version ( client ) <EOL> check_master ( client , master_only = master_only ) <EOL> return client <EOL> except Exception as e : <EOL> raise elasticsearch . ElasticsearchException ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( e ) <EOL> ) <EOL> def override_timeout ( timeout , action ) : <EOL> """<STR_LIT>""" <EOL> retval = timeout <EOL> if action in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if timeout == <NUM_LIT:30> : <EOL> if action in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> retval = <NUM_LIT> <EOL> elif action == '<STR_LIT>' : <EOL> retval = <NUM_LIT> <EOL> logger . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( action . upper ( ) , timeout ) <EOL> ) <EOL> return retval <EOL> def show_dry_run ( ilo , action , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> logger . info ( '<STR_LIT>' ) <EOL> logger . info ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( action ) <EOL> ) <EOL> indices = sorted ( ilo . indices ) <EOL> for idx in indices : <EOL> index_closed = ilo . index_info [ idx ] [ '<STR_LIT:state>' ] == '<STR_LIT>' <EOL> logger . info ( <EOL> '<STR_LIT>' . format ( <EOL> action , idx , '<STR_LIT>' if index_closed else '<STR_LIT>' , kwargs <EOL> ) <EOL> ) <EOL> def get_repository ( client , repository = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return client . snapshot . get_repository ( repository = repository ) <EOL> except ( elasticsearch . TransportError , elasticsearch . NotFoundError ) : <EOL> logger . error ( "<STR_LIT>" . format ( repository ) ) <EOL> return False <EOL> def get_snapshot ( client , repository = None , snapshot = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not repository : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> snapname = '<STR_LIT>' if snapshot == '<STR_LIT>' else snapshot <EOL> try : <EOL> return client . snapshot . get ( repository = repository , snapshot = snapshot ) <EOL> except ( elasticsearch . TransportError , elasticsearch . NotFoundError ) as e : <EOL> raise FailedExecution ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( snapname , repository , e ) <EOL> ) <EOL> def get_snapshot_data ( client , repository = None ) : <EOL> """<STR_LIT>""" <EOL> if not repository : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> try : <EOL> return client . snapshot . get ( <EOL> repository = repository , snapshot = "<STR_LIT>" ) [ '<STR_LIT>' ] <EOL> except ( elasticsearch . TransportError , elasticsearch . NotFoundError ) as e : <EOL> raise FailedExecution ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( repository , e ) <EOL> ) <EOL> def snapshot_in_progress ( client , repository = None , snapshot = None ) : <EOL> """<STR_LIT>""" <EOL> allsnaps = get_snapshot_data ( client , repository = repository ) <EOL> inprogress = ( <EOL> [ snap [ '<STR_LIT>' ] for snap in allsnaps if '<STR_LIT:state>' in snap . keys ( ) and snap [ '<STR_LIT:state>' ] == '<STR_LIT>' ] <EOL> ) <EOL> if snapshot : <EOL> return snapshot if snapshot in inprogress else False <EOL> else : <EOL> if len ( inprogress ) == <NUM_LIT:0> : <EOL> return False <EOL> elif len ( inprogress ) == <NUM_LIT:1> : <EOL> return inprogress [ <NUM_LIT:0> ] <EOL> else : <EOL> raise CuratorException ( <EOL> '<STR_LIT>' . format ( inprogress ) <EOL> ) <EOL> def safe_to_snap ( client , repository = None , retry_interval = <NUM_LIT> , retry_count = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> if not repository : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> for count in range ( <NUM_LIT:1> , retry_count + <NUM_LIT:1> ) : <EOL> in_progress = snapshot_in_progress ( <EOL> client , repository = repository <EOL> ) <EOL> if in_progress : <EOL> logger . info ( <EOL> '<STR_LIT>' . format ( in_progress ) ) <EOL> logger . info ( <EOL> '<STR_LIT>' . format ( <EOL> retry_interval ) <EOL> ) <EOL> time . sleep ( retry_interval ) <EOL> logger . info ( '<STR_LIT>' . format ( count , retry_count ) ) <EOL> else : <EOL> return True <EOL> return False <EOL> def create_snapshot_body ( indices , ignore_unavailable = False , <EOL> include_global_state = True , partial = False ) : <EOL> """<STR_LIT>""" <EOL> if not indices : <EOL> logger . error ( '<STR_LIT>' ) <EOL> return False <EOL> body = { <EOL> "<STR_LIT>" : ignore_unavailable , <EOL> "<STR_LIT>" : include_global_state , <EOL> "<STR_LIT>" : partial , <EOL> } <EOL> if indices == '<STR_LIT>' : <EOL> body [ "<STR_LIT>" ] = indices <EOL> else : <EOL> body [ "<STR_LIT>" ] = to_csv ( indices ) <EOL> return body <EOL> def create_repo_body ( repo_type = None , <EOL> compress = True , chunk_size = None , <EOL> max_restore_bytes_per_sec = None , <EOL> max_snapshot_bytes_per_sec = None , <EOL> location = None , <EOL> bucket = None , region = None , base_path = None , access_key = None , <EOL> secret_key = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not repo_type : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> argdict = locals ( ) <EOL> body = { } <EOL> body [ '<STR_LIT:type>' ] = argdict [ '<STR_LIT>' ] <EOL> body [ '<STR_LIT>' ] = { } <EOL> settings = [ ] <EOL> maybes = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> s3 = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> settings += [ i for i in maybes if argdict [ i ] ] <EOL> if argdict [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> settings . append ( '<STR_LIT:location>' ) <EOL> if argdict [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> settings += [ i for i in s3 if argdict [ i ] ] <EOL> for k in settings : <EOL> body [ '<STR_LIT>' ] [ k ] = argdict [ k ] <EOL> return body <EOL> def create_repository ( client , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in kwargs : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> else : <EOL> repository = kwargs [ '<STR_LIT>' ] <EOL> try : <EOL> body = create_repo_body ( ** kwargs ) <EOL> logger . info ( <EOL> '<STR_LIT>' . format ( repository ) <EOL> ) <EOL> result = get_repository ( client , repository = repository ) <EOL> logger . debug ( "<STR_LIT>" . format ( result ) ) <EOL> if not result : <EOL> logger . info ( <EOL> '<STR_LIT>' . format ( <EOL> repository <EOL> ) <EOL> ) <EOL> client . snapshot . create_repository ( repository = repository , body = body ) <EOL> elif result is not None and repository not in result : <EOL> logger . info ( <EOL> '<STR_LIT>' . format ( <EOL> repository <EOL> ) <EOL> ) <EOL> client . snapshot . create_repository ( repository = repository , body = body ) <EOL> else : <EOL> raise FailedExecution ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( repository ) <EOL> ) <EOL> except elasticsearch . TransportError as e : <EOL> raise FailedExecution ( <EOL> """<STR_LIT>""" . format ( <EOL> repository , e . status_code , e . error <EOL> ) <EOL> ) <EOL> logger . info ( "<STR_LIT>" . format ( repository ) ) <EOL> return True <EOL> def repository_exists ( client , repository = None ) : <EOL> """<STR_LIT>""" <EOL> if not repository : <EOL> raise MissingArgument ( '<STR_LIT>' ) <EOL> test_result = get_repository ( client , repository ) <EOL> if repository in test_result : <EOL> logger . info ( "<STR_LIT>" . format ( repository ) ) <EOL> return True <EOL> else : <EOL> logger . info ( "<STR_LIT>" . format ( repository ) ) <EOL> return False <EOL> def test_repo_fs ( client , repository = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> nodes = client . snapshot . verify_repository ( <EOL> repository = repository ) [ '<STR_LIT>' ] <EOL> logger . info ( '<STR_LIT>' ) <EOL> logger . debug ( <EOL> '<STR_LIT>' . format ( nodes ) ) <EOL> except Exception as e : <EOL> try : <EOL> if e . status_code == <NUM_LIT> : <EOL> msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( repository , e . status_code , e . error ) <EOL> ) <EOL> else : <EOL> msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( e . status_code , e . error ) <EOL> ) <EOL> except AttributeError : <EOL> msg = ( '<STR_LIT>' . format ( e ) ) <EOL> raise ActionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( msg ) <EOL> ) <EOL> def snapshot_running ( client ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> status = client . snapshot . status ( ) [ '<STR_LIT>' ] <EOL> except Exception as e : <EOL> report_failure ( e ) <EOL> return False if status == [ ] else True <EOL> def parse_date_pattern ( name ) : <EOL> """<STR_LIT>""" <EOL> prev = '<STR_LIT>' ; curr = '<STR_LIT>' ; rendered = '<STR_LIT>' <EOL> for s in range ( <NUM_LIT:0> , len ( name ) ) : <EOL> curr = name [ s ] <EOL> if curr == '<STR_LIT:%>' : <EOL> pass <EOL> elif curr in DATE_REGEX and prev == '<STR_LIT:%>' : <EOL> rendered += str ( datetime . utcnow ( ) . strftime ( '<STR_LIT>' . format ( curr ) ) ) <EOL> else : <EOL> rendered += curr <EOL> logger . debug ( '<STR_LIT>' . format ( rendered ) ) <EOL> prev = curr <EOL> logger . debug ( '<STR_LIT>' . format ( rendered ) ) <EOL> return rendered <EOL> def prune_nones ( mydict ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> k : v for k , v in mydict . items ( ) if v != None and v != "<STR_LIT:None>" <EOL> } <EOL> def verify_args ( action , options ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( '<STR_LIT>' . format ( action , options ) ) <EOL> def matches_keys ( mydict ) : <EOL> logger . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( options . keys ( ) , mydict . keys ( ) ) <EOL> ) <EOL> return sorted ( list ( options . keys ( ) ) ) == sorted ( list ( mydict . keys ( ) ) ) <EOL> if not matches_keys ( copy . deepcopy ( ACTION_DEFAULTS [ action ] ) ) : <EOL> raise ConfigurationError ( <EOL> '<STR_LIT>' . format ( options ) ) </s>
