<s> """<STR_LIT>""" <EOL> from docopt import docopt <EOL> import time <EOL> import salt . client <EOL> import socket <EOL> def check_if_first_highstate_ever ( minion_id ) : <EOL> '''<STR_LIT>''' <EOL> client = salt . client . LocalClient ( ) <EOL> mypath = [ "<STR_LIT>" ] <EOL> results_by_minion = client . cmd ( tgt = [ minion_id ] , fun = '<STR_LIT>' , arg = mypath , expr_form = '<STR_LIT:list>' ) <EOL> if results_by_minion . get ( minion_id , False ) == False or len ( results_by_minion ) < <NUM_LIT:1> : <EOL> return False <EOL> else : <EOL> return results_by_minion [ minion_id ] <EOL> def check_if_highstate_running ( minion_id ) : <EOL> '''<STR_LIT>''' <EOL> client = salt . client . LocalClient ( ) <EOL> running_function = [ "<STR_LIT>" ] <EOL> results_by_minion = client . cmd ( tgt = minion_id , fun = '<STR_LIT>' , arg = running_function ) <EOL> minion_results = results_by_minion . get ( minion_id , [ ] ) <EOL> if not minion_results : <EOL> return False <EOL> else : <EOL> return True <EOL> def write_highstate_runner_claim ( minion_id ) : <EOL> '''<STR_LIT>''' <EOL> client = salt . client . LocalClient ( ) <EOL> path = "<STR_LIT>" <EOL> hostname = socket . gethostname ( ) <EOL> results = client . cmd ( tgt = minion_id , fun = '<STR_LIT>' , arg = [ path ] ) <EOL> results_by_minion = client . cmd ( tgt = minion_id , fun = '<STR_LIT>' , arg = [ path , hostname ] ) <EOL> return <EOL> def should_run_highstate ( minion_id ) : <EOL> '''<STR_LIT>''' <EOL> client = salt . client . LocalClient ( ) <EOL> path = "<STR_LIT>" <EOL> hostname = socket . gethostname ( ) <EOL> results_by_minion = client . cmd ( tgt = minion_id , fun = '<STR_LIT>' , arg = [ "<STR_LIT>" ] ) <EOL> contents = results_by_minion . get ( minion_id , '<STR_LIT>' ) <EOL> lines = contents . split ( "<STR_LIT:\n>" ) <EOL> first_line = lines [ <NUM_LIT:0> ] . strip ( ) <EOL> if first_line == hostname : <EOL> return True <EOL> else : <EOL> return False <EOL> def call_highstate ( minion_id ) : <EOL> '''<STR_LIT>''' <EOL> client = salt . client . LocalClient ( ) <EOL> minions = client . cmd_async ( tgt = minion_id , fun = '<STR_LIT>' , arg = [ ] , timeout = <NUM_LIT:1> , expr_form = '<STR_LIT>' ) <EOL> return <EOL> def write_highstate_ran ( minion_id ) : <EOL> '''<STR_LIT>''' <EOL> client = salt . client . LocalClient ( ) <EOL> path = "<STR_LIT>" <EOL> seconds = time . time ( ) <EOL> results = client . cmd ( tgt = minion_id , fun = '<STR_LIT>' , arg = [ path ] ) <EOL> results_by_minion = client . cmd ( tgt = minion_id , fun = '<STR_LIT>' , arg = [ path , seconds ] ) <EOL> return <EOL> def main ( minion_id ) : <EOL> '''<STR_LIT>''' <EOL> highstate_already_run = check_if_first_highstate_ever ( minion_id ) <EOL> if highstate_already_run : <EOL> print "<STR_LIT>" <EOL> return <EOL> highstate_running_now = check_if_highstate_running ( minion_id ) <EOL> if highstate_running_now : <EOL> print "<STR_LIT>" <EOL> return <EOL> print "<STR_LIT>" <EOL> write_highstate_runner_claim ( minion_id ) <EOL> winner = should_run_highstate ( minion_id ) <EOL> if winner : <EOL> print "<STR_LIT>" <EOL> call_highstate ( minion_id ) <EOL> write_highstate_ran ( minion_id ) <EOL> return True <EOL> else : <EOL> print "<STR_LIT>" <EOL> return False <EOL> def manage ( miniondata = "<STR_LIT>" ) : <EOL> result = False <EOL> minion_id = miniondata [ '<STR_LIT:id>' ] <EOL> act = miniondata [ '<STR_LIT>' ] <EOL> if act == '<STR_LIT>' : <EOL> result = main ( minion_id ) <EOL> return result <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> arguments = docopt ( __doc__ ) <EOL> main ( arguments [ '<STR_LIT>' ] ) </s>
