<s> import logging <EOL> import threading <EOL> import time <EOL> import timeit <EOL> import random <EOL> import re <EOL> import string <EOL> import smtplib <EOL> from email . mime . multipart import MIMEMultipart <EOL> from email . mime . text import MIMEText <EOL> import json <EOL> import os <EOL> import inspect <EOL> from multiprocessing import Lock <EOL> from fabric . api import run , settings , local , env , sudo <EOL> from fabric . context_managers import hide <EOL> import yaml <EOL> LOG = logging . getLogger ( __name__ ) <EOL> ISCSI = "<STR_LIT>" <EOL> CEPH = "<STR_LIT>" <EOL> BOOT_FROM_VOLUME = "<STR_LIT>" <EOL> BOOT_FROM_IMAGE = "<STR_LIT>" <EOL> ANY = "<STR_LIT>" <EOL> NO = "<STR_LIT>" <EOL> EPHEMERAL = "<STR_LIT>" <EOL> REMOTE_FILE = "<STR_LIT>" <EOL> QCOW2 = "<STR_LIT>" <EOL> RAW = "<STR_LIT>" <EOL> YES = "<STR_LIT:yes>" <EOL> PATH_TO_SNAPSHOTS = '<STR_LIT>' <EOL> AVAILABLE = '<STR_LIT>' <EOL> IN_USE = '<STR_LIT>' <EOL> STATUS = '<STR_LIT:status>' <EOL> DISK = "<STR_LIT>" <EOL> DISK_EPHEM = "<STR_LIT>" <EOL> DISK_CONFIG = "<STR_LIT>" <EOL> LEN_UUID_INSTANCE = <NUM_LIT> <EOL> HOST_SRC = '<STR_LIT>' <EOL> HOST_DST = '<STR_LIT>' <EOL> PATH_SRC = '<STR_LIT>' <EOL> PATH_DST = '<STR_LIT>' <EOL> STORAGE_RESOURCE = '<STR_LIT>' <EOL> VOLUMES_TYPE = '<STR_LIT>' <EOL> VOLUME_BODY = '<STR_LIT>' <EOL> VOLUMES_DB = '<STR_LIT>' <EOL> SNAPSHOTS = '<STR_LIT>' <EOL> OBJSTORAGE_RESOURCE = '<STR_LIT>' <EOL> CONTAINERS = '<STR_LIT>' <EOL> CONTAINER_BODY = '<STR_LIT>' <EOL> COMPUTE_RESOURCE = '<STR_LIT>' <EOL> INSTANCES_TYPE = '<STR_LIT>' <EOL> INSTANCE_BODY = '<STR_LIT>' <EOL> NETWORK_RESOURCE = '<STR_LIT>' <EOL> NETWORKS_TYPE = '<STR_LIT>' <EOL> NETWORK_BODY = '<STR_LIT>' <EOL> DIFF_BODY = '<STR_LIT>' <EOL> EPHEMERAL_BODY = '<STR_LIT>' <EOL> INTERFACES = '<STR_LIT>' <EOL> IMAGE_RESOURCE = '<STR_LIT:image>' <EOL> IMAGES_TYPE = '<STR_LIT>' <EOL> IMAGE_BODY = '<STR_LIT:image>' <EOL> IDENTITY_RESOURCE = '<STR_LIT>' <EOL> TENANTS_TYPE = '<STR_LIT>' <EOL> USERS_TYPE = '<STR_LIT>' <EOL> IGNORE = '<STR_LIT:ignore>' <EOL> RESOURCE_TYPES = { <EOL> STORAGE_RESOURCE : VOLUMES_TYPE , <EOL> COMPUTE_RESOURCE : INSTANCES_TYPE , <EOL> NETWORK_RESOURCE : NETWORKS_TYPE , <EOL> IMAGE_RESOURCE : IMAGES_TYPE , <EOL> OBJSTORAGE_RESOURCE : CONTAINERS , <EOL> IDENTITY_RESOURCE : TENANTS_TYPE , <EOL> } <EOL> META_INFO = '<STR_LIT>' <EOL> OLD_ID = '<STR_LIT>' <EOL> FILTER_PATH = '<STR_LIT>' <EOL> up_ssh_tunnel = None <EOL> SSH_CMD = "<STR_LIT>" <EOL> SSH_KEY_LIST_RE = re . compile ( r'<STR_LIT>' ) <EOL> class ext_dict ( dict ) : <EOL> def __getattr__ ( self , name ) : <EOL> if name in self : <EOL> return self [ name ] <EOL> raise AttributeError ( "<STR_LIT>" % name ) <EOL> def get_snapshots_list_repository ( path = PATH_TO_SNAPSHOTS ) : <EOL> path_source = path + '<STR_LIT>' <EOL> path_dest = path + '<STR_LIT>' <EOL> s = os . listdir ( path_source ) <EOL> s . sort ( ) <EOL> source = [ { '<STR_LIT:path>' : '<STR_LIT>' % ( path_source , f ) , <EOL> '<STR_LIT>' : f . replace ( "<STR_LIT>" , "<STR_LIT>" ) } for f in s ] <EOL> d = os . listdir ( path_dest ) <EOL> d . sort ( ) <EOL> dest = [ { '<STR_LIT:path>' : '<STR_LIT>' % ( path_dest , f ) , <EOL> '<STR_LIT>' : f . replace ( "<STR_LIT>" , "<STR_LIT>" ) } for f in d ] <EOL> return { <EOL> '<STR_LIT:source>' : source , <EOL> '<STR_LIT>' : dest <EOL> } <EOL> def dump_to_file ( path , snapshot ) : <EOL> with open ( path , "<STR_LIT>" ) as f : <EOL> json . dump ( convert_to_dict ( snapshot ) , f ) <EOL> def load_json_from_file ( file_path ) : <EOL> f = open ( file_path , '<STR_LIT:r>' ) <EOL> return json . load ( f ) <EOL> primitive = [ int , long , bool , float , type ( None ) , str , unicode ] <EOL> def convert_to_dict ( obj , ident = <NUM_LIT:0> , limit_ident = <NUM_LIT:6> ) : <EOL> ident += <NUM_LIT:1> <EOL> if type ( obj ) in primitive : <EOL> return obj <EOL> if isinstance ( obj , inspect . types . InstanceType ) or ( type ( obj ) not in ( list , tuple , dict ) ) : <EOL> if ident <= limit_ident : <EOL> try : <EOL> obj = obj . convert_to_dict ( ) <EOL> except AttributeError : <EOL> try : <EOL> t = obj . __dict__ <EOL> t [ '<STR_LIT>' ] = str ( obj . __class__ ) <EOL> obj = t <EOL> except AttributeError : <EOL> return str ( obj . __class__ if hasattr ( obj , '<STR_LIT>' ) <EOL> else type ( obj ) ) <EOL> else : <EOL> return str ( obj . __class__ if hasattr ( obj , '<STR_LIT>' ) <EOL> else type ( obj ) ) <EOL> if type ( obj ) is dict : <EOL> res = { } <EOL> for item in obj : <EOL> if ident <= limit_ident : <EOL> res [ item ] = convert_to_dict ( obj [ item ] , ident ) <EOL> else : <EOL> res [ item ] = str ( obj [ item ] ) <EOL> return res <EOL> if type ( obj ) in ( list , tuple ) : <EOL> res = [ ] <EOL> for item in obj : <EOL> if ident <= limit_ident : <EOL> res . append ( convert_to_dict ( item , ident ) ) <EOL> else : <EOL> res . append ( str ( item ) ) <EOL> return res if type ( obj ) is list else tuple ( res ) <EOL> def convert_to_obj ( obj , restore_object , namespace ) : <EOL> if type ( obj ) in primitive : <EOL> return obj <EOL> if type ( obj ) is dict : <EOL> for item in obj : <EOL> obj [ item ] = convert_to_obj ( obj [ item ] , restore_object , namespace ) <EOL> obj = restore_object . restore ( obj , namespace ) <EOL> if type ( obj ) in ( list , tuple ) : <EOL> res = [ ] <EOL> for item in obj : <EOL> res . append ( convert_to_obj ( item , restore_object , namespace ) ) <EOL> obj = res if type ( obj ) is list else tuple ( res ) <EOL> return obj <EOL> class GeneratorPassword ( object ) : <EOL> def __init__ ( self , length = <NUM_LIT:7> ) : <EOL> self . length = length <EOL> self . chars = string . ascii_letters + string . digits + '<STR_LIT>' <EOL> def get_random_password ( self ) : <EOL> return self . __generate_password ( ) <EOL> def __generate_password ( self ) : <EOL> return '<STR_LIT>' . join ( random . choice ( self . chars ) for i in range ( self . length ) ) <EOL> class Postman ( object ) : <EOL> def __init__ ( self , username , password , from_addr , mail_server ) : <EOL> self . username = username <EOL> self . password = password <EOL> self . from_addr = from_addr <EOL> self . mail_server = mail_server <EOL> self . server = None <EOL> def __enter__ ( self ) : <EOL> self . server = smtplib . SMTP ( self . mail_server ) <EOL> self . server . ehlo ( ) <EOL> self . server . starttls ( ) <EOL> self . server . login ( self . username , self . password ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> self . server . quit ( ) <EOL> def send ( self , to , subject , msg ) : <EOL> msg_mime = MIMEMultipart ( '<STR_LIT>' ) <EOL> msg_mime . attach ( MIMEText ( msg , '<STR_LIT:html>' ) ) <EOL> msg_mime [ '<STR_LIT>' ] = subject <EOL> msg_mime [ '<STR_LIT>' ] = self . from_addr <EOL> msg_mime [ '<STR_LIT>' ] = to <EOL> self . server . sendmail ( self . from_addr , to , msg_mime . as_string ( ) ) <EOL> def close ( self ) : <EOL> self . server . quit ( ) <EOL> class Templater ( object ) : <EOL> def render ( self , name_file , args ) : <EOL> temp_file = open ( name_file , '<STR_LIT:r>' ) <EOL> temp_render = temp_file . read ( ) <EOL> for arg in args : <EOL> temp_render = temp_render . replace ( "<STR_LIT>" % arg , args [ arg ] ) <EOL> temp_file . close ( ) <EOL> return temp_render <EOL> class forward_agent ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key_files ) : <EOL> self . key_files = key_files <EOL> def __enter__ ( self ) : <EOL> ensure_ssh_key_added ( self . key_files ) <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> pass <EOL> def ensure_ssh_key_added ( key_files ) : <EOL> need_adding = set ( os . path . abspath ( os . path . expanduser ( p ) ) <EOL> for p in key_files ) <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> warn_only = True ) : <EOL> res = local ( "<STR_LIT>" , capture = True ) <EOL> if res . succeeded : <EOL> for line in res . splitlines ( ) : <EOL> m = SSH_KEY_LIST_RE . match ( line ) <EOL> if not m : <EOL> continue <EOL> path = os . path . abspath ( os . path . expanduser ( m . group ( '<STR_LIT>' ) ) ) <EOL> need_adding . discard ( path ) <EOL> with settings ( hide ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> if need_adding : <EOL> key_string = '<STR_LIT:U+0020>' . join ( need_adding ) <EOL> start_ssh_agent = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % key_string <EOL> info_agent = local ( start_ssh_agent , capture = True ) . splitlines ( ) <EOL> os . environ [ "<STR_LIT>" ] = info_agent [ <NUM_LIT:0> ] . split ( ) [ - <NUM_LIT:1> ] <EOL> os . environ [ "<STR_LIT>" ] = info_agent [ <NUM_LIT:1> ] <EOL> return False <EOL> else : <EOL> return True <EOL> class wrapper_singletone_ssh_tunnel ( object ) : <EOL> def __init__ ( self , interval_ssh = "<STR_LIT>" , locker = Lock ( ) ) : <EOL> self . interval_ssh = [ int ( interval_ssh . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] ) , <EOL> int ( interval_ssh . split ( '<STR_LIT:->' ) [ <NUM_LIT:1> ] ) ] <EOL> self . busy_port = [ ] <EOL> self . locker = locker <EOL> def get_free_port ( self ) : <EOL> with self . locker : <EOL> beg = self . interval_ssh [ <NUM_LIT:0> ] <EOL> end = self . interval_ssh [ <NUM_LIT:1> ] <EOL> while beg <= end : <EOL> if beg not in self . busy_port : <EOL> self . busy_port . append ( beg ) <EOL> return beg <EOL> beg += <NUM_LIT:1> <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def free_port ( self , port ) : <EOL> with self . locker : <EOL> if port in self . busy_port : <EOL> self . busy_port . remove ( port ) <EOL> def __call__ ( self , address_dest_compute , address_dest_controller , host , <EOL> ** kwargs ) : <EOL> return UpSshTunnelClass ( address_dest_compute , <EOL> address_dest_controller , <EOL> host , <EOL> self . get_free_port , <EOL> self . free_port ) <EOL> class UpSshTunnelClass ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , address_dest_compute , address_dest_controller , host , <EOL> callback_get , callback_free ) : <EOL> self . address_dest_compute = address_dest_compute <EOL> self . address_dest_controller = address_dest_controller <EOL> self . get_free_port = callback_get <EOL> self . remove_port = callback_free <EOL> self . host = host <EOL> self . cmd = SSH_CMD <EOL> self . port = None <EOL> def __enter__ ( self ) : <EOL> self . port = self . get_free_port ( ) <EOL> with settings ( host_string = self . host , <EOL> connection_attempts = env . connection_attempts ) : <EOL> run ( self . cmd % ( self . port , <EOL> self . address_dest_compute , <EOL> self . port , <EOL> self . port , <EOL> self . address_dest_controller ) + "<STR_LIT>" ) <EOL> return self . port <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> with settings ( host_string = self . host , <EOL> connection_attempts = env . connection_attempts ) : <EOL> run ( ( "<STR_LIT>" + self . cmd + "<STR_LIT:'>" ) % <EOL> ( self . port , <EOL> self . address_dest_compute , <EOL> self . port , <EOL> self . port , <EOL> self . address_dest_controller ) ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> self . remove_port ( self . port ) <EOL> def libvirt_instance_exists ( libvirt_name , init_host , compute_host , ssh_user , <EOL> ssh_sudo_password ) : <EOL> with settings ( host_string = compute_host , <EOL> user = ssh_user , <EOL> password = ssh_sudo_password , <EOL> gateway = init_host , <EOL> connection_attempts = env . connection_attempts , <EOL> warn_only = True , <EOL> quiet = True ) : <EOL> command = '<STR_LIT>' % libvirt_name <EOL> LOG . debug ( '<STR_LIT>' , compute_host , command ) <EOL> out = sudo ( command ) <EOL> LOG . debug ( '<STR_LIT>' , compute_host , command , out ) <EOL> return out . succeeded <EOL> def get_libvirt_block_info ( libvirt_name , init_host , compute_host , ssh_user , <EOL> ssh_sudo_password ) : <EOL> with settings ( host_string = compute_host , <EOL> user = ssh_user , <EOL> password = ssh_sudo_password , <EOL> gateway = init_host , <EOL> connection_attempts = env . connection_attempts ) : <EOL> command = "<STR_LIT>" % libvirt_name <EOL> LOG . debug ( '<STR_LIT>' , compute_host , command ) <EOL> out = sudo ( command ) <EOL> LOG . debug ( '<STR_LIT>' , compute_host , command , out ) <EOL> libvirt_output = out . split ( ) <EOL> return libvirt_output <EOL> def find_element_by_in ( list_values , word ) : <EOL> for i in list_values : <EOL> if word in i : <EOL> return i <EOL> def init_singletones ( cfg ) : <EOL> globals ( ) [ '<STR_LIT>' ] = wrapper_singletone_ssh_tunnel ( <EOL> cfg . migrate . ssh_transfer_port ) <EOL> def get_disk_path ( instance , blk_list , is_ceph_ephemeral = False , disk = DISK ) : <EOL> disk_path = None <EOL> if not is_ceph_ephemeral : <EOL> disk = "<STR_LIT:/>" + disk <EOL> for i in blk_list : <EOL> if instance . id + disk == i [ - ( LEN_UUID_INSTANCE + len ( disk ) ) : ] : <EOL> disk_path = i <EOL> if instance . name + disk == i [ - ( len ( instance . name ) + len ( disk ) ) : ] : <EOL> disk_path = i <EOL> else : <EOL> disk = "<STR_LIT:_>" + disk <EOL> for i in blk_list : <EOL> if ( "<STR_LIT>" % ( instance . id , disk ) ) == i : <EOL> disk_path = i <EOL> return disk_path <EOL> def check_file ( file_path ) : <EOL> return file_path is not None and os . path . isfile ( file_path ) <EOL> def read_yaml_file ( yaml_file_path ) : <EOL> if not check_file ( yaml_file_path ) : <EOL> return None <EOL> with open ( yaml_file_path ) as yfile : <EOL> return yaml . load ( yfile ) <EOL> def write_yaml_file ( file_name , content ) : <EOL> with open ( file_name , '<STR_LIT:w>' ) as yfile : <EOL> yaml . safe_dump ( content , yfile ) <EOL> def timer ( func , * args , ** kwargs ) : <EOL> t = timeit . Timer ( lambda : func ( * args , ** kwargs ) ) <EOL> elapsed = t . timeit ( number = <NUM_LIT:1> ) <EOL> return elapsed <EOL> def import_class_by_string ( name ) : <EOL> """<STR_LIT>""" <EOL> module , class_name = name . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] , name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> mod = __import__ ( "<STR_LIT:.>" . join ( module ) ) <EOL> for comp in module [ <NUM_LIT:1> : ] : <EOL> mod = getattr ( mod , comp ) <EOL> return getattr ( mod , class_name ) <EOL> def qualname ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . __module__ + '<STR_LIT:.>' + cls . __name__ <EOL> class ThreadLocalStorage ( object ) : <EOL> def __init__ ( self , ** defaults ) : <EOL> self . _tls = threading . local ( ) <EOL> self . _defaults = defaults <EOL> def __getattr__ ( self , item ) : <EOL> return getattr ( self . _tls , item , self . _defaults . get ( item ) ) <EOL> def __setattr__ ( self , key , value ) : <EOL> if key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> super ( ThreadLocalStorage , self ) . __setattr__ ( key , value ) <EOL> return setattr ( self . _tls , key , value ) </s>
