<s> import collections <EOL> from fabric import api as fabric_api <EOL> from fabric import network <EOL> from neutronclient . common import exceptions <EOL> import yaml <EOL> import cloudferry_devlab . tests . config as config <EOL> def convert ( data ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( data , basestring ) : <EOL> return str ( data ) <EOL> elif isinstance ( data , collections . Mapping ) : <EOL> return dict ( convert ( item ) for item in data . iteritems ( ) ) <EOL> elif isinstance ( data , collections . Iterable ) : <EOL> return type ( data ) ( convert ( item ) for item in data ) <EOL> else : <EOL> return data <EOL> class Utils ( object ) : <EOL> @ staticmethod <EOL> def load_file ( file_path ) : <EOL> with open ( file_path , "<STR_LIT:r>" ) as f : <EOL> filter_dict = yaml . load ( f ) <EOL> if filter_dict is None : <EOL> filter_dict = { } <EOL> return [ filter_dict , file_path ] <EOL> class FilteringUtils ( Utils ) : <EOL> def __init__ ( self , path_to_filter ) : <EOL> super ( FilteringUtils , self ) . __init__ ( ) <EOL> self . filter_file_path = path_to_filter <EOL> self . filters_file_naming_template = config . filters_file_naming_template <EOL> def build_filter_files_list ( self ) : <EOL> return [ self . filters_file_naming_template . format ( <EOL> tenant_name = tenant [ '<STR_LIT:name>' ] ) <EOL> for tenant in config . tenants <EOL> if '<STR_LIT>' not in tenant and not tenant [ '<STR_LIT>' ] ] <EOL> def filter_vms_with_filter_config_file ( self , src_data_list ) : <EOL> loaded_data = self . load_file ( self . filter_file_path ) <EOL> filter_dict = loaded_data [ <NUM_LIT:0> ] <EOL> current_data_list = list ( src_data_list ) <EOL> popped_vm_list = [ ] <EOL> if '<STR_LIT>' in filter_dict : <EOL> filtered_vms_by_tenant = [ ] <EOL> for vm in current_data_list : <EOL> if vm . tenant_id in filter_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> filtered_vms_by_tenant . append ( vm ) <EOL> else : <EOL> popped_vm_list . append ( vm ) <EOL> current_data_list = filtered_vms_by_tenant <EOL> if '<STR_LIT>' in filter_dict : <EOL> filtered_vms_by_id = [ ] <EOL> for vm in current_data_list : <EOL> if vm . id in filter_dict [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] : <EOL> filtered_vms_by_id . append ( vm ) <EOL> else : <EOL> popped_vm_list . append ( vm ) <EOL> current_data_list = filtered_vms_by_id <EOL> return [ current_data_list , popped_vm_list ] <EOL> def filter_images ( self , src_data_list ) : <EOL> loaded_data = self . load_file ( self . filter_file_path ) <EOL> filter_dict = loaded_data [ <NUM_LIT:0> ] <EOL> popped_img_list = [ ] <EOL> default_img = '<STR_LIT>' <EOL> if '<STR_LIT>' not in filter_dict : <EOL> return [ src_data_list , [ ] ] <EOL> for img in src_data_list [ : ] : <EOL> if img . id not in filter_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> if img . name != default_img : <EOL> popped_img_list . append ( img ) <EOL> index = src_data_list . index ( img ) <EOL> src_data_list . pop ( index ) <EOL> return [ src_data_list , popped_img_list ] <EOL> def filter_volumes ( self , src_data_list ) : <EOL> loaded_data = self . load_file ( self . filter_file_path ) <EOL> filter_dict = loaded_data [ <NUM_LIT:0> ] <EOL> popped_vol_list = [ ] <EOL> if '<STR_LIT>' not in filter_dict : <EOL> return [ src_data_list , [ ] ] <EOL> for vol in src_data_list [ : ] : <EOL> if vol . id not in filter_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> popped_vol_list . append ( vol ) <EOL> index = src_data_list . index ( vol ) <EOL> src_data_list . pop ( index ) <EOL> return [ src_data_list , popped_vol_list ] <EOL> def filter_tenants ( self , src_data_list ) : <EOL> loaded_data = self . load_file ( self . filter_file_path ) <EOL> filter_dict = loaded_data [ <NUM_LIT:0> ] <EOL> popped_tenant_list = [ ] <EOL> if '<STR_LIT>' not in filter_dict : <EOL> return [ src_data_list , [ ] ] <EOL> for tenant in src_data_list : <EOL> if tenant . id not in filter_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> popped_tenant_list . append ( tenant ) <EOL> index = src_data_list . index ( tenant ) <EOL> src_data_list . pop ( index ) <EOL> return [ src_data_list , popped_tenant_list ] <EOL> class MigrationUtils ( object ) : <EOL> def __init__ ( self , conf ) : <EOL> self . config = conf <EOL> def execute_command_on_vm ( self , ip_addr , cmd , username = None , <EOL> warn_only = False , password = None , key = None , <EOL> use_sudo = True ) : <EOL> if username is None : <EOL> username = self . config . username_for_ssh <EOL> if password is None and key is None : <EOL> password = self . config . password_for_ssh <EOL> with fabric_api . settings ( fabric_api . hide ( '<STR_LIT>' ) , <EOL> host_string = ip_addr , user = username , <EOL> password = password , key = key , <EOL> abort_on_prompts = True , connection_attempts = <NUM_LIT:3> , <EOL> disable_known_hosts = True , no_agent = True , <EOL> warn_only = warn_only ) : <EOL> try : <EOL> if use_sudo : <EOL> return fabric_api . sudo ( cmd , shell = False ) <EOL> else : <EOL> return fabric_api . run ( cmd , shell = False ) <EOL> except network . NetworkError : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> % ip_addr ) <EOL> def get_all_vms_from_config ( self ) : <EOL> vms = self . config . vms <EOL> for tenant in self . config . tenants : <EOL> if not tenant . get ( '<STR_LIT>' ) or tenant . get ( '<STR_LIT>' ) : <EOL> continue <EOL> for vm in tenant [ '<STR_LIT>' ] : <EOL> vms . append ( vm ) <EOL> vms . extend ( self . config . vms_from_volumes ) <EOL> return vms <EOL> def get_all_images_from_config ( self ) : <EOL> images = self . config . images <EOL> for tenant in self . config . tenants : <EOL> if not tenant . get ( '<STR_LIT>' ) : <EOL> continue <EOL> for image in tenant [ '<STR_LIT>' ] : <EOL> images . append ( image ) <EOL> return images <EOL> def wait_until_vm_accessible_via_ssh ( self , ip_addr , cmd ) : <EOL> try : <EOL> self . execute_command_on_vm ( ip_addr , cmd ) <EOL> except RuntimeError : <EOL> return False <EOL> return True <EOL> @ staticmethod <EOL> def open_ssh_port_secgroup ( client , tenant_id ) : <EOL> sec_grps = client . get_sec_group_id_by_tenant_id ( tenant_id ) <EOL> for sec_gr in sec_grps : <EOL> try : <EOL> client . create_security_group_rule ( <EOL> sec_gr , tenant_id , protocol = '<STR_LIT>' , port_range_max = <NUM_LIT> , <EOL> port_range_min = <NUM_LIT> , direction = '<STR_LIT>' ) <EOL> except exceptions . NeutronClientException as e : <EOL> if e . status_code != <NUM_LIT> : <EOL> raise e <EOL> @ staticmethod <EOL> def get_vm_fip ( vm ) : <EOL> for net in vm . addresses : <EOL> for addr in vm . addresses [ net ] : <EOL> if addr [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return addr [ '<STR_LIT>' ] <EOL> raise RuntimeError ( '<STR_LIT>' . format ( <EOL> vm . name , vm . id ) ) <EOL> class AddAdminUserRoleToNonAdminTenant ( object ) : <EOL> def __init__ ( self , ks_client , admin_user , tenant ) : <EOL> self . keystone = ks_client <EOL> self . tenant = self . keystone . tenants . find ( name = tenant ) <EOL> self . user = self . keystone . users . find ( name = admin_user ) <EOL> self . role = self . keystone . roles . find ( name = '<STR_LIT>' ) <EOL> self . user_has_role = False <EOL> roles = self . keystone . users . list_roles ( self . user . id , self . tenant . id ) <EOL> for role in roles : <EOL> if role . id == self . role . id : <EOL> self . user_has_role = True <EOL> def __enter__ ( self ) : <EOL> if not self . user_has_role : <EOL> self . keystone . roles . add_user_role ( user = self . user , <EOL> role = self . role , <EOL> tenant = self . tenant ) <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> if not self . user_has_role : <EOL> self . keystone . roles . remove_user_role ( user = self . user , <EOL> role = self . role , <EOL> tenant = self . tenant ) </s>
