#NEWLINE# @ app . task ( bind = True ) #NEWLINE# def volume_migration( self , database , user , task_history = None ) : #NEWLINE# #INDENT# from dbaas_nfsaas.models import HostAttr , PlanAttr #NEWLINE# from workflow.settings import VOLUME_MIGRATION #NEWLINE# from util import build_dict #NEWLINE# from workflow.workflow import start_workflow #NEWLINE# from time import sleep #NEWLINE# worker_name = get_worker_name ( ) #NEWLINE# task_history = TaskHistory . register ( request = self . request , task_history = hosts , user = user , worker_name = worker_name ) #NEWLINE# stop_now = False #NEWLINE# if ( ( database . status != Database . ALIVE ) or ( not database . database_status . is_alive ) ) : #NEWLINE# #INDENT# msg = 'Database is not alive!' #NEWLINE# stop_now = True #NEWLINE# #UNINDENT# if database . is_beeing_used_elsewhere ( task_id = self . request . id ) : #NEWLINE# #INDENT# msg = 'Database is in use by another task!' #NEWLINE# stop_now = True #NEWLINE# #UNINDENT# if database . has_migration_started ( ) : #NEWLINE# #INDENT# msg = 'Region migration for this database has already started!' #NEWLINE# stop_now = True #NEWLINE# #UNINDENT# if stop_now : #NEWLINE# #INDENT# task_history . update_status_for ( TaskHistory . STATUS_SUCCESS , details = msg ) #NEWLINE# LOG . info ( 'Migration finished' ) #NEWLINE# return #NEWLINE# #UNINDENT# default_plan_size = PlanAttr . objects . get ( dbaas_plan = database . plan ) . nfsaas_plan #NEWLINE# LOG . info ( 'Migrating {} volumes' . format ( database ) ) #NEWLINE# databaseinfra = database . databaseinfra #NEWLINE# driver = databaseinfra . get_driver ( ) #NEWLINE# environment = database . environment #NEWLINE# plan = database . plan #NEWLINE# instances = driver . get_slave_instances ( ) #NEWLINE# master_instance = driver . get_master_instance ( ) #NEWLINE# instances . append ( master_instance ) #NEWLINE# LOG . info ( 'Instances: {}' . format ( str ( instances ) ) ) #NEWLINE# hosts = [ instance . hostname for instance in instances ] #NEWLINE# volumes = HostAttr . objects . filter ( host__in = hosts , is_active = True , nfsaas_size_id = default_plan_size ) #NEWLINE# if ( len ( volumes ) == len ( hosts ) ) : #NEWLINE# #INDENT# task_history . update_status_for ( TaskHistory . STATUS_SUCCESS , details = 'Volumes already migrated!' ) #NEWLINE# LOG . info ( 'Migration finished' ) #NEWLINE# return #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# disable_zabbix_alarms ( database ) #NEWLINE# for ( index , instance ) in enumerate ( instances ) : #NEWLINE# #INDENT# if ( not driver . check_instance_is_eligible_for_backup ( instance = instance ) ) : #NEWLINE# #INDENT# LOG . info ( 'Instance is not eligible for backup {}' . format ( str ( instance ) ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# LOG . info ( 'Volume migration for instance {}' . format ( str ( instance ) ) ) #NEWLINE# host = instance . hostname #NEWLINE# old_volume = HostAttr . objects . get ( host = host , is_active = True ) #NEWLINE# if ( old_volume . nfsaas_size_id == default_plan_size ) : #NEWLINE# #INDENT# if databaseinfra . plan . is_ha : #NEWLINE# #INDENT# driver . check_replication_and_switch ( instance ) #NEWLINE# #UNINDENT# continue #NEWLINE# #UNINDENT# workflow_dict = build_dict ( databaseinfra = databaseinfra , database = database , environment = environment , plan = plan , host = host , instance = instance , old_volume = old_volume , steps = VOLUME_MIGRATION ) #NEWLINE# start_workflow ( workflow_dict = workflow_dict , task = task_history ) #NEWLINE# if workflow_dict [ 'exceptions' ] [ 'traceback' ] : #NEWLINE# #INDENT# error = '\n' . join ( ( ': ' . join ( err ) for err in workflow_dict [ 'exceptions' ] [ 'error_codes' ] ) ) #NEWLINE# traceback = '\nException Traceback\n' . join ( workflow_dict [ 'exceptions' ] [ 'traceback' ] ) #NEWLINE# error = '{}\n{}\n{}' . format ( error , traceback , error ) #NEWLINE# task_history . update_status_for ( TaskHistory . STATUS_ERROR , details = error ) #NEWLINE# LOG . info ( 'Migration finished with errors' ) #NEWLINE# return #NEWLINE# #UNINDENT# if databaseinfra . plan . is_ha : #NEWLINE# #INDENT# LOG . info ( 'Waiting 60s to check continue...' ) #NEWLINE# sleep ( 60 ) #NEWLINE# driver . check_replication_and_switch ( instance ) #NEWLINE# LOG . info ( 'Waiting 60s to check continue...' ) #NEWLINE# sleep ( 60 ) #NEWLINE# #UNINDENT# #UNINDENT# task_history . update_status_for ( TaskHistory . STATUS_SUCCESS , details = 'Volumes sucessfully migrated!' ) #NEWLINE# LOG . info ( 'Migration finished' ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# task_history . update_status_for ( TaskHistory . STATUS_ERROR , details = e ) #NEWLINE# LOG . warning ( 'Migration finished with errors' ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# enable_zabbix_alarms ( database )