<s> import logging <EOL> from util import full_stack <EOL> from workflow . steps . util . base import BaseStep <EOL> from workflow . exceptions . error_codes import DBAAS_0020 <EOL> from workflow . steps . redis . util import change_slave_priority_file <EOL> from workflow . steps . redis . util import change_slave_priority_instance <EOL> from workflow . steps . redis . util import failover_sentinel <EOL> from time import sleep <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class SwitchMaster ( BaseStep ) : <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def do ( self , workflow_dict ) : <EOL> try : <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] <EOL> driver = databaseinfra . get_driver ( ) <EOL> for source_instance in workflow_dict [ '<STR_LIT>' ] : <EOL> if source_instance . instance_type == source_instance . REDIS : <EOL> source_host = source_instance . hostname <EOL> change_slave_priority_file ( <EOL> host = source_host , original_value = <NUM_LIT:100> , final_value = <NUM_LIT:0> ) <EOL> change_slave_priority_instance ( <EOL> instance = source_instance , final_value = <NUM_LIT:0> ) <EOL> target_instance = source_instance . future_instance <EOL> target_host = target_instance . hostname <EOL> change_slave_priority_file ( <EOL> host = target_host , original_value = <NUM_LIT:0> , final_value = <NUM_LIT:100> ) <EOL> change_slave_priority_instance ( <EOL> instance = target_instance , final_value = <NUM_LIT:100> ) <EOL> attempts = <NUM_LIT:1> <EOL> max_attempts = <NUM_LIT:10> <EOL> while True : <EOL> sentinel_instance = driver . get_non_database_instances ( ) [ <NUM_LIT:0> ] <EOL> failover_sentinel ( host = sentinel_instance . hostname , <EOL> sentinel_host = sentinel_instance . address , <EOL> sentinel_port = sentinel_instance . port , <EOL> service_name = databaseinfra . name ) <EOL> sleep ( <NUM_LIT:30> ) <EOL> sentinel_client = driver . get_sentinel_client ( sentinel_instance ) <EOL> master_address = sentinel_client . discover_master ( databaseinfra . name ) [ <NUM_LIT:0> ] <EOL> target_is_master = False <EOL> for source_instance in workflow_dict [ '<STR_LIT>' ] : <EOL> target_instance = source_instance . future_instance <EOL> if target_instance . address == master_address : <EOL> target_is_master = True <EOL> LOG . info ( "<STR_LIT>" . format ( target_instance ) ) <EOL> break <EOL> if target_is_master : <EOL> break <EOL> if attempts >= max_attempts : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> attempts += <NUM_LIT:1> <EOL> LOG . info ( "<STR_LIT>" ) <EOL> LOG . info ( "<STR_LIT>" . format ( attempts , max_attempts ) ) <EOL> return True <EOL> except Exception : <EOL> traceback = full_stack ( ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( DBAAS_0020 ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( traceback ) <EOL> return False <EOL> def undo ( self , workflow_dict ) : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> try : <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] <EOL> driver = databaseinfra . get_driver ( ) <EOL> for source_instance in workflow_dict [ '<STR_LIT>' ] : <EOL> if source_instance . instance_type == source_instance . REDIS : <EOL> source_host = source_instance . hostname <EOL> change_slave_priority_file ( <EOL> host = source_host , original_value = <NUM_LIT:0> , final_value = <NUM_LIT:100> ) <EOL> change_slave_priority_instance ( <EOL> instance = source_instance , final_value = <NUM_LIT:100> ) <EOL> target_instance = source_instance . future_instance <EOL> target_host = target_instance . hostname <EOL> change_slave_priority_file ( <EOL> host = target_host , original_value = <NUM_LIT:100> , final_value = <NUM_LIT:0> ) <EOL> change_slave_priority_instance ( <EOL> instance = target_instance , final_value = <NUM_LIT:0> ) <EOL> attempts = <NUM_LIT:1> <EOL> max_attempts = <NUM_LIT:10> <EOL> while True : <EOL> sentinel_instance = driver . get_non_database_instances ( ) [ <NUM_LIT:0> ] <EOL> failover_sentinel ( host = sentinel_instance . hostname , <EOL> sentinel_host = sentinel_instance . address , <EOL> sentinel_port = sentinel_instance . port , <EOL> service_name = databaseinfra . name ) <EOL> sleep ( <NUM_LIT:30> ) <EOL> sentinel_client = driver . get_sentinel_client ( sentinel_instance ) <EOL> master_address = sentinel_client . discover_master ( databaseinfra . name ) [ <NUM_LIT:0> ] <EOL> source_is_master = False <EOL> for source_instance in workflow_dict [ '<STR_LIT>' ] : <EOL> if source_instance . address == master_address : <EOL> source_is_master = True <EOL> LOG . info ( "<STR_LIT>" . format ( source_instance ) ) <EOL> break <EOL> if source_is_master : <EOL> break <EOL> if attempts >= max_attempts : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> attempts += <NUM_LIT:1> <EOL> LOG . info ( "<STR_LIT>" ) <EOL> LOG . info ( "<STR_LIT>" . format ( attempts , max_attempts ) ) <EOL> return True <EOL> except Exception : <EOL> traceback = full_stack ( ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( DBAAS_0020 ) <EOL> workflow_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] . append ( traceback ) <EOL> return False </s>
