<s> import os <EOL> import subprocess <EOL> import time <EOL> import uuid <EOL> import netaddr <EOL> from rally . deployment . serverprovider import provider <EOL> @ provider . configure ( name = "<STR_LIT>" ) <EOL> class VirshProvider ( provider . ProviderFactory ) : <EOL> """<STR_LIT>""" <EOL> CONFIG_SCHEMA = { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } <EOL> } , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> } <EOL> def create_servers ( self , image_uuid = None , type_id = None , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return [ self . create_vm ( str ( uuid . uuid4 ( ) ) ) for i in range ( amount ) ] <EOL> def create_vm ( self , vm_name ) : <EOL> """<STR_LIT>""" <EOL> virt_url = self . _get_virt_connection_url ( self . config [ "<STR_LIT>" ] ) <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" % virt_url , <EOL> "<STR_LIT>" , self . config [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" , vm_name , "<STR_LIT>" ] <EOL> subprocess . check_call ( cmd ) <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" % virt_url , "<STR_LIT:start>" , vm_name ] <EOL> subprocess . check_call ( cmd ) <EOL> self . resources . create ( { "<STR_LIT:name>" : vm_name } ) <EOL> return provider . Server ( <EOL> self . _determine_vm_ip ( vm_name ) , <EOL> self . config [ "<STR_LIT>" ] , <EOL> password = self . config . get ( "<STR_LIT>" ) , <EOL> ) <EOL> def destroy_servers ( self ) : <EOL> """<STR_LIT>""" <EOL> for resource in self . resources . get_all ( ) : <EOL> self . destroy_vm ( resource [ "<STR_LIT:info>" ] [ "<STR_LIT:name>" ] ) <EOL> self . resources . delete ( resource ) <EOL> def destroy_vm ( self , vm_name ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" % vm_name ) <EOL> vconnection = self . _get_virt_connection_url ( self . config [ "<STR_LIT>" ] ) <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" % vconnection , "<STR_LIT>" , vm_name ] <EOL> subprocess . check_call ( cmd ) <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" % vconnection , "<STR_LIT>" , vm_name , <EOL> "<STR_LIT>" ] <EOL> subprocess . check_call ( cmd ) <EOL> return True <EOL> @ staticmethod <EOL> def _get_virt_connection_url ( connection ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % connection <EOL> def _determine_vm_ip ( self , vm_name ) : <EOL> ssh_opt = "<STR_LIT>" <EOL> script_path = os . path . dirname ( __file__ ) + "<STR_LIT>" <EOL> cmd = [ "<STR_LIT>" , ssh_opt , script_path , <EOL> "<STR_LIT>" % self . config [ "<STR_LIT>" ] ] <EOL> subprocess . check_call ( cmd ) <EOL> tries = <NUM_LIT:0> <EOL> ip = None <EOL> while tries < <NUM_LIT:3> and not ip : <EOL> cmd = [ "<STR_LIT>" , ssh_opt , self . config [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" , vm_name ] <EOL> out = subprocess . check_output ( cmd ) <EOL> try : <EOL> ip = netaddr . IPAddress ( out ) <EOL> except netaddr . core . AddrFormatError : <EOL> ip = None <EOL> tries += <NUM_LIT:1> <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> return str ( ip ) </s>
