<s> import logging <EOL> from util import full_stack <EOL> from util import get_credentials_for <EOL> from workflow . steps . util . base import BaseStep <EOL> from workflow . exceptions . error_codes import DBAAS_0020 <EOL> from dbaas_credentials . models import CredentialType <EOL> from dbaas_cloudstack . models import DatabaseInfraAttr <EOL> from dbaas_aclapi . models import DatabaseInfraInstanceBind <EOL> from dbaas_aclapi . acl_base_client import AclClient <EOL> from dbaas_aclapi import helpers <EOL> from dbaas_aclapi . models import ERROR <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class BindNewInstances ( BaseStep ) : <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def do ( self , workflow_dict ) : <EOL> try : <EOL> database = workflow_dict [ '<STR_LIT>' ] <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] <EOL> acl_credential = get_credentials_for ( environment = database . environment , <EOL> credential_type = CredentialType . ACLAPI ) <EOL> acl_client = AclClient ( acl_credential . endpoint , <EOL> acl_credential . user , <EOL> acl_credential . password , <EOL> database . environment ) <EOL> instances = databaseinfra . instances . filter ( <EOL> future_instance__isnull = True ) <EOL> databaseinfraattr_instances = DatabaseInfraAttr . objects . filter ( databaseinfra = databaseinfra , <EOL> equivalent_dbinfraattr__isnull = True ) <EOL> instance_address_list = [ ] <EOL> for instance in instances : <EOL> instance_address_list . append ( instance . address ) <EOL> for instance in databaseinfraattr_instances : <EOL> instance_address_list . append ( instance . ip ) <EOL> for database_bind in database . acl_binds . all ( ) : <EOL> if helpers . bind_address ( <EOL> database_bind , acl_client , instances = instances , <EOL> infra_attr_instances = databaseinfraattr_instances ) : <EOL> continue <EOL> else : <EOL> LOG . error ( "<STR_LIT>" ) <EOL> database_bind . bind_status = ERROR <EOL> database_bind . save ( ) <EOL> DatabaseInfraInstanceBind . objects . filter ( databaseinfra = databaseinfra , <EOL> bind_address = database_bind . bind_address , <EOL> instance__in = instance_address_list <EOL> ) . update ( bind_status = ERROR ) <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> database = workflow_dict [ '<STR_LIT>' ] <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] <EOL> acl_credential = get_credentials_for ( environment = database . environment , <EOL> credential_type = CredentialType . ACLAPI ) <EOL> acl_client = AclClient ( acl_credential . endpoint , <EOL> acl_credential . user , <EOL> acl_credential . password , <EOL> database . environment ) <EOL> instances = databaseinfra . instances . filter ( <EOL> future_instance__isnull = True ) <EOL> databaseinfraattr_instances = DatabaseInfraAttr . objects . filter ( databaseinfra = databaseinfra , <EOL> equivalent_dbinfraattr__isnull = True ) <EOL> instance_address_list = [ ] <EOL> for instance in instances : <EOL> instance_address_list . append ( instance . address ) <EOL> for instance in databaseinfraattr_instances : <EOL> instance_address_list . append ( instance . ip ) <EOL> for database_bind in database . acl_binds . all ( ) : <EOL> infra_instances_binds = DatabaseInfraInstanceBind . objects . filter ( <EOL> databaseinfra = databaseinfra , <EOL> instance__in = instance_address_list , <EOL> bind_address = database_bind . bind_address ) <EOL> if helpers . unbind_address ( <EOL> database_bind , acl_client , infra_instances_binds , False <EOL> ) : <EOL> continue <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> class UnbindOldInstances ( BaseStep ) : <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def do ( self , workflow_dict ) : <EOL> try : <EOL> database = workflow_dict [ '<STR_LIT>' ] <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] <EOL> acl_credential = get_credentials_for ( environment = database . environment , <EOL> credential_type = CredentialType . ACLAPI ) <EOL> acl_client = AclClient ( acl_credential . endpoint , <EOL> acl_credential . user , <EOL> acl_credential . password , <EOL> database . environment ) <EOL> instances = databaseinfra . instances . filter ( <EOL> future_instance__isnull = False ) <EOL> databaseinfraattr_instances = DatabaseInfraAttr . objects . filter ( databaseinfra = databaseinfra , <EOL> equivalent_dbinfraattr__isnull = False ) <EOL> instance_address_list = [ ] <EOL> for instance in instances : <EOL> instance_address_list . append ( instance . address ) <EOL> for instance in databaseinfraattr_instances : <EOL> instance_address_list . append ( instance . ip ) <EOL> for database_bind in database . acl_binds . all ( ) : <EOL> infra_instances_binds = DatabaseInfraInstanceBind . objects . filter ( <EOL> databaseinfra = databaseinfra , <EOL> instance__in = instance_address_list , <EOL> bind_address = database_bind . bind_address ) <EOL> if helpers . unbind_address ( <EOL> database_bind , acl_client , infra_instances_binds , False <EOL> ) : <EOL> continue <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> database = workflow_dict [ '<STR_LIT>' ] <EOL> databaseinfra = workflow_dict [ '<STR_LIT>' ] <EOL> acl_credential = get_credentials_for ( environment = database . environment , <EOL> credential_type = CredentialType . ACLAPI ) <EOL> acl_client = AclClient ( acl_credential . endpoint , <EOL> acl_credential . user , <EOL> acl_credential . password , <EOL> database . environment ) <EOL> instances = databaseinfra . instances . filter ( <EOL> future_instance__isnull = False ) <EOL> databaseinfraattr_instances = DatabaseInfraAttr . objects . filter ( databaseinfra = databaseinfra , <EOL> equivalent_dbinfraattr__isnull = False ) <EOL> instance_address_list = [ ] <EOL> for instance in instances : <EOL> instance_address_list . append ( instance . address ) <EOL> for instance in databaseinfraattr_instances : <EOL> instance_address_list . append ( instance . ip ) <EOL> for database_bind in database . acl_binds . all ( ) : <EOL> if helpers . bind_address ( <EOL> database_bind , acl_client , instances = instances , <EOL> infra_attr_instances = databaseinfraattr_instances ) : <EOL> continue <EOL> else : <EOL> LOG . error ( "<STR_LIT>" ) <EOL> database_bind . bind_status = ERROR <EOL> database_bind . save ( ) <EOL> DatabaseInfraInstanceBind . objects . filter ( databaseinfra = databaseinfra , <EOL> bind_address = database_bind . bind_address , <EOL> instance__in = instance_address_list <EOL> ) . update ( bind_status = ERROR ) <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>
