<s> from eucaops import Eucaops <EOL> import re <EOL> import time <EOL> import httplib <EOL> from xml . etree import ElementTree <EOL> import sys <EOL> from eutester . eutestcase import EutesterTestCase <EOL> from eutester . sshconnection import SshCbReturn <EOL> from eutester . machine import Machine <EOL> from conversiontask import ConversionTask <EOL> class ImageUtils ( EutesterTestCase ) : <EOL> gig = <NUM_LIT> <EOL> mb = <NUM_LIT> <EOL> kb = <NUM_LIT> <EOL> def __init__ ( self , <EOL> tester = None , <EOL> config_file = None , <EOL> password = "<STR_LIT>" , <EOL> credpath = None , <EOL> destpath = None , <EOL> time_per_gig = <NUM_LIT> , <EOL> eof = True , <EOL> worker_hostname = None , <EOL> worker_keypath = None , <EOL> worker_username = '<STR_LIT:root>' , <EOL> worker_password = None , <EOL> worker_machine = None ) : <EOL> self . setuptestcase ( ) <EOL> if not tester or not isinstance ( tester , Eucaops ) : <EOL> self . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( config_file , password , credpath ) ) <EOL> self . tester = Eucaops ( config_file = config_file , password = password , credpath = credpath ) <EOL> else : <EOL> self . tester = tester <EOL> self . tester . exit_on_fail = eof <EOL> self . debugmethod = self . tester . debug <EOL> self . worker_keypath = worker_keypath or self . tester . keypath <EOL> self . worker_username = worker_username <EOL> self . worker_password = worker_password or self . tester . password <EOL> self . worker_machine = self . _get_worker_machine ( worker_machine ) <EOL> if destpath is not None : <EOL> self . destpath = str ( destpath ) <EOL> else : <EOL> self . destpath = "<STR_LIT>" <EOL> self . time_per_gig = time_per_gig <EOL> self . credpath = credpath or self . tester . credpath <EOL> def _get_worker_machine ( self , worker ) : <EOL> '''<STR_LIT>''' <EOL> worker = worker or self . tester . clc <EOL> self . debug ( '<STR_LIT>' ) <EOL> if isinstance ( worker , Machine ) : <EOL> return worker <EOL> else : <EOL> self . debug ( '<STR_LIT>' + str ( worker ) + <EOL> "<STR_LIT>" ) <EOL> new_machine = Machine ( hostname = worker , <EOL> username = self . worker_username , <EOL> password = self . worker_password , <EOL> keypath = self . worker_keypath ) <EOL> return new_machine <EOL> def getHttpRemoteImageSize ( self , url , unit = None , maxredirect = <NUM_LIT:5> ) : <EOL> return ImageUtils . _getHttpRemoteImageSize ( url , unit = unit , maxredirect = maxredirect ) <EOL> @ staticmethod <EOL> def _getHttpRemoteImageSize ( url , unit = None , maxredirect = <NUM_LIT:5> , debug = None ) : <EOL> '''<STR_LIT>''' <EOL> unit = unit or <NUM_LIT> <EOL> if debug is None : <EOL> def printdebug ( msg ) : <EOL> print msg <EOL> debug = printdebug <EOL> def get_location ( url , depth , maxdepth ) : <EOL> if depth > maxdepth : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( depth , maxdepth ) ) <EOL> conn = None <EOL> try : <EOL> url = url . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> host = url . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] <EOL> path = url . replace ( host , '<STR_LIT>' ) <EOL> debug ( "<STR_LIT>" + host + "<STR_LIT>" + path + "<STR_LIT:)>" ) <EOL> conn = httplib . HTTPConnection ( host ) <EOL> conn . request ( "<STR_LIT>" , path ) <EOL> res = conn . getresponse ( ) <EOL> location = res . getheader ( '<STR_LIT:location>' ) <EOL> if location and location != url : <EOL> depth += <NUM_LIT:1> <EOL> debug ( '<STR_LIT>' . format ( depth , location ) ) <EOL> return get_location ( location , depth = depth , maxdepth = maxdepth ) <EOL> else : <EOL> content_length = res . getheader ( '<STR_LIT>' ) <EOL> if content_length is None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> . format ( url ) ) <EOL> fbytes = int ( content_length ) <EOL> return fbytes <EOL> except Exception as HE : <EOL> debug ( '<STR_LIT>' . format ( url ) ) <EOL> raise HE <EOL> finally : <EOL> if conn : <EOL> conn . close ( ) <EOL> try : <EOL> fbytes = get_location ( url , depth = <NUM_LIT:0> , maxdepth = maxredirect ) <EOL> debug ( "<STR_LIT>" + str ( fbytes ) ) <EOL> if fbytes == <NUM_LIT:0> : <EOL> rfsize = <NUM_LIT:0> <EOL> else : <EOL> rfsize = ( ( ( fbytes / unit ) + <NUM_LIT:1> ) or <NUM_LIT:1> ) <EOL> debug ( "<STR_LIT>" + str ( rfsize ) + "<STR_LIT:g>" ) <EOL> except Exception , e : <EOL> debug ( "<STR_LIT>" ) <EOL> raise e <EOL> return rfsize <EOL> def wget_image ( self , <EOL> url , <EOL> destpath = None , <EOL> dest_file_name = None , <EOL> machine = None , <EOL> user = None , <EOL> password = None , <EOL> retryconn = True , <EOL> time_per_gig = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> machine = machine or self . worker_machine <EOL> if destpath is None and self . destpath is not None : <EOL> destpath = self . destpath <EOL> size = self . getHttpRemoteImageSize ( url ) <EOL> if ( size < machine . get_available ( destpath , unit = self . __class__ . gig ) ) : <EOL> raise Exception ( "<STR_LIT>" + <EOL> str ( destpath ) + "<STR_LIT>" + str ( url ) ) <EOL> timeout = size * time_per_gig <EOL> self . debug ( '<STR_LIT>' + str ( url ) + '<STR_LIT>' + <EOL> str ( destpath ) + '<STR_LIT>' + str ( machine . hostname ) ) <EOL> saved_location = machine . wget_remote_image ( url , <EOL> path = destpath , <EOL> dest_file_name = dest_file_name , <EOL> user = user , <EOL> password = password , <EOL> retryconn = retryconn , <EOL> timeout = timeout ) <EOL> return ( size , saved_location ) <EOL> def get_manifest_obj ( self , path , machine = None , local = False , timeout = <NUM_LIT:30> ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' + str ( path ) <EOL> if not local : <EOL> machine = machine or self . worker_machine <EOL> out = machine . cmd ( cmd , timeout = timeout , verbose = False ) <EOL> if out [ '<STR_LIT:status>' ] != <NUM_LIT:0> : <EOL> raise Exception ( '<STR_LIT>' <EOL> + str ( out [ '<STR_LIT:status>' ] ) ) <EOL> output = out [ '<STR_LIT>' ] <EOL> else : <EOL> output = self . tester . sys ( cmd , timeout = timeout , <EOL> listformat = False , code = <NUM_LIT:0> ) <EOL> xml = ElementTree . fromstring ( output ) <EOL> return xml <EOL> def get_manifest_part_count ( self , <EOL> path , <EOL> machine = None , <EOL> local = False , <EOL> timeout = <NUM_LIT:30> ) : <EOL> '''<STR_LIT>''' <EOL> manifest_xml = self . get_manifest_obj ( path = path , <EOL> machine = machine , <EOL> local = local , <EOL> timeout = timeout ) <EOL> image = manifest_xml . find ( '<STR_LIT:image>' ) <EOL> parts = image . find ( '<STR_LIT>' ) <EOL> part_count = parts . get ( '<STR_LIT:count>' ) <EOL> self . debug ( '<STR_LIT>' + str ( path ) + <EOL> '<STR_LIT>' + str ( part_count ) ) <EOL> return int ( part_count ) <EOL> def get_manifest_image_name ( self , <EOL> path , <EOL> machine = None , <EOL> local = False , <EOL> timeout = <NUM_LIT:30> ) : <EOL> '''<STR_LIT>''' <EOL> manifest_xml = self . get_manifest_obj ( path = path , <EOL> machine = machine , <EOL> local = local , <EOL> timeout = timeout ) <EOL> image = manifest_xml . find ( '<STR_LIT:image>' ) <EOL> name_elem = image . find ( '<STR_LIT:name>' ) <EOL> return name_elem . text <EOL> def euca2ools_bundle_image ( self , <EOL> path , <EOL> machine = None , <EOL> machine_credpath = None , <EOL> prefix = None , <EOL> kernel = None , <EOL> ramdisk = None , <EOL> block_device_mapping = None , <EOL> destination = '<STR_LIT>' , <EOL> arch = '<STR_LIT>' , <EOL> debug = False , <EOL> interbundle_timeout = <NUM_LIT> , <EOL> time_per_gig = None ) : <EOL> '''<STR_LIT>''' <EOL> self . status ( '<STR_LIT>' . format ( path ) ) <EOL> time_per_gig = time_per_gig or self . time_per_gig <EOL> credpath = machine_credpath or self . credpath <EOL> machine = machine or self . worker_machine <EOL> image_size = machine . get_file_size ( path ) / self . gig or <NUM_LIT:1> <EOL> timeout = time_per_gig * image_size <EOL> cbargs = [ timeout , interbundle_timeout , time . time ( ) , <NUM_LIT:0> , True ] <EOL> if destination is None : <EOL> destination = machine . sys ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> freesize = machine . get_available ( str ( destination ) , ( self . gig / self . kb ) ) <EOL> if ( freesize < image_size ) : <EOL> raise Exception ( "<STR_LIT>" + str ( destination ) ) <EOL> cmdargs = "<STR_LIT>" <EOL> if prefix : <EOL> cmdargs = cmdargs + "<STR_LIT>" + str ( prefix ) <EOL> if kernel : <EOL> cmdargs = cmdargs + "<STR_LIT>" + str ( kernel ) <EOL> if ramdisk : <EOL> cmdargs = cmdargs + "<STR_LIT>" + str ( ramdisk ) <EOL> if block_device_mapping : <EOL> cmdargs = cmdargs + "<STR_LIT>" + str ( block_device_mapping ) <EOL> if destination : <EOL> cmdargs = cmdargs + "<STR_LIT>" + str ( destination ) <EOL> if arch : <EOL> cmdargs = cmdargs + "<STR_LIT>" + str ( arch ) <EOL> if debug : <EOL> cmdargs = cmdargs + "<STR_LIT>" <EOL> cmdargs = cmdargs + "<STR_LIT>" + str ( path ) <EOL> if credpath is not None : <EOL> cmd = '<STR_LIT>' + str ( credpath ) + '<STR_LIT>' + str ( cmdargs ) <EOL> else : <EOL> skey = self . tester . get_secret_key ( ) <EOL> akey = self . tester . get_access_key ( ) <EOL> cmd = '<STR_LIT>' + str ( akey ) + '<STR_LIT>' + str ( skey ) + str ( cmdargs ) <EOL> out = machine . cmd ( cmd , timeout = timeout , listformat = True , <EOL> cb = self . _bundle_status_cb , cbargs = cbargs ) <EOL> if out [ '<STR_LIT:status>' ] != <NUM_LIT:0> : <EOL> raise Exception ( '<STR_LIT>' + str ( path ) + <EOL> '<STR_LIT>' + str ( out [ '<STR_LIT:status>' ] ) ) <EOL> manifest = None <EOL> for line in out [ '<STR_LIT>' ] : <EOL> line = str ( line ) <EOL> if re . search ( "<STR_LIT>" , line ) : <EOL> manifest = line . split ( ) [ <NUM_LIT:2> ] <EOL> break <EOL> if manifest is None : <EOL> raise Exception ( '<STR_LIT>' + <EOL> str ( path ) ) <EOL> self . debug ( '<STR_LIT>' + str ( path ) + '<STR_LIT>' + str ( manifest ) ) <EOL> return manifest <EOL> def euca2ools_upload_bundle ( self , <EOL> manifest , <EOL> machine = None , <EOL> bucketname = None , <EOL> machine_credpath = None , <EOL> debug = False , <EOL> interbundle_timeout = <NUM_LIT> , <EOL> timeout = <NUM_LIT:0> , <EOL> image_check_timeout = <NUM_LIT> , <EOL> uniquebucket = True ) : <EOL> '''<STR_LIT>''' <EOL> self . status ( '<STR_LIT>' . format ( manifest ) ) <EOL> machine = machine or self . worker_machine <EOL> credpath = machine_credpath or self . credpath <EOL> cbargs = [ timeout , interbundle_timeout , time . time ( ) , <NUM_LIT:0> , True ] <EOL> bname = '<STR_LIT>' <EOL> cmdargs = "<STR_LIT>" <EOL> manifest = str ( manifest ) <EOL> upmanifest = None <EOL> part_count = - <NUM_LIT:1> <EOL> try : <EOL> part_count = self . get_manifest_part_count ( manifest , machine = machine ) <EOL> except : <EOL> pass <EOL> self . debug ( '<STR_LIT>' + str ( manifest ) + <EOL> "<STR_LIT>" + str ( bucketname ) + "<STR_LIT>" + <EOL> str ( part_count ) ) <EOL> if bucketname : <EOL> bname = bucketname <EOL> if uniquebucket : <EOL> bname = self . _get_unique_bucket_name ( bname ) <EOL> else : <EOL> bname = self . _generate_unique_bucket_name_from_manifest ( <EOL> manifest , unique = uniquebucket ) <EOL> self . debug ( '<STR_LIT>' + str ( bname ) ) <EOL> cmdargs = cmdargs + "<STR_LIT>" + str ( bname ) <EOL> if debug : <EOL> cmdargs = cmdargs + "<STR_LIT>" <EOL> cmdargs = cmdargs + "<STR_LIT>" + str ( bname ) + "<STR_LIT>" + str ( manifest ) <EOL> if credpath is not None : <EOL> cmd = '<STR_LIT>' + str ( credpath ) + '<STR_LIT>' + str ( cmdargs ) <EOL> else : <EOL> skey = self . tester . get_secret_key ( ) <EOL> akey = self . tester . get_access_key ( ) <EOL> cmd = '<STR_LIT>' + str ( akey ) + '<STR_LIT>' + str ( skey ) + str ( cmdargs ) <EOL> out = machine . cmd ( cmd , timeout = image_check_timeout , listformat = True , <EOL> cb = self . _bundle_status_cb , cbargs = cbargs ) <EOL> if out [ '<STR_LIT:status>' ] != <NUM_LIT:0> : <EOL> raise Exception ( '<STR_LIT>' + str ( manifest ) + <EOL> '<STR_LIT>' + str ( out [ '<STR_LIT:status>' ] ) ) <EOL> for line in out [ '<STR_LIT>' ] : <EOL> line = str ( line ) <EOL> if re . search ( '<STR_LIT>' , line ) and re . search ( '<STR_LIT>' , line ) : <EOL> upmanifest = line . split ( ) . pop ( ) <EOL> break <EOL> if upmanifest is None : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . debug ( '<STR_LIT>' + str ( manifest ) + '<STR_LIT>' + str ( upmanifest ) ) <EOL> return upmanifest <EOL> def euca2ools_bundle_and_upload ( self , <EOL> file , <EOL> arch , <EOL> bucket , <EOL> prefix = None , <EOL> directory = None , <EOL> kernel = None , <EOL> ramdisk = None , <EOL> block_device_map = [ ] , <EOL> product_codes = None , <EOL> acl = None , <EOL> location = None , <EOL> ) : <EOL> raise NotImplemented ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def euca2ools_register ( self , <EOL> manifest , <EOL> name , <EOL> description = None , <EOL> arch = None , <EOL> kernel = None , <EOL> ramdisk = None , <EOL> root_device_name = None , <EOL> snapshot_id = None , <EOL> block_dev_map = [ ] , <EOL> virtualization_type = None , <EOL> platform = None , <EOL> machine = None , <EOL> machine_credpath = None ) : <EOL> self . status ( '<STR_LIT>' <EOL> . format ( manifest , kernel , ramdisk ) ) <EOL> machine = machine or self . worker_machine <EOL> credpath = machine_credpath or self . credpath <EOL> cmdargs = str ( manifest ) + "<STR_LIT>" + str ( name ) <EOL> emi = None <EOL> if description : <EOL> cmdargs += '<STR_LIT>' + str ( description ) <EOL> if arch : <EOL> cmdargs += '<STR_LIT>' + str ( arch ) <EOL> if kernel : <EOL> cmdargs += '<STR_LIT>' + str ( kernel ) <EOL> if ramdisk : <EOL> cmdargs += '<STR_LIT>' + str ( ramdisk ) <EOL> if root_device_name : <EOL> cmdargs += '<STR_LIT>' + str ( root_device_name ) <EOL> if snapshot_id : <EOL> cmdargs += '<STR_LIT>' + str ( snapshot_id ) <EOL> for dev in block_dev_map : <EOL> cmdargs += '<STR_LIT>' + str ( dev ) <EOL> if virtualization_type : <EOL> cmdargs += '<STR_LIT>' + str ( virtualization_type ) <EOL> if platform : <EOL> cmdargs += '<STR_LIT>' + str ( platform ) <EOL> if credpath is not None : <EOL> cmd = '<STR_LIT>' + str ( credpath ) + '<STR_LIT>' + str ( cmdargs ) <EOL> else : <EOL> skey = self . tester . get_secret_key ( ) <EOL> akey = self . tester . get_access_key ( ) <EOL> cmd = ( '<STR_LIT>' + str ( akey ) + <EOL> '<STR_LIT>' + str ( skey ) + str ( cmdargs ) ) <EOL> out = machine . sys ( cmd = cmd , code = <NUM_LIT:0> ) <EOL> for line in out : <EOL> if re . search ( '<STR_LIT>' , line ) : <EOL> emi = line . split ( ) . pop ( ) . strip ( ) <EOL> assert emi , '<STR_LIT>' + str ( emi ) + '<STR_LIT:">' <EOL> self . tester . test_resources [ "<STR_LIT>" ] . append ( self . tester . ec2 . get_all_images ( [ emi ] ) [ <NUM_LIT:0> ] ) <EOL> return emi <EOL> def euca2ools_download_bundle ( self , <EOL> bucket , <EOL> manifest = None , <EOL> prefix = None , <EOL> directory = None , <EOL> image_name = None , <EOL> machine = None , <EOL> machine_credpath = None , <EOL> ) : <EOL> machine = machine or self . worker_machine <EOL> credpath = machine_credpath or self . credpath <EOL> cmdargs = "<STR_LIT>" + str ( bucket ) <EOL> if manifest : <EOL> cmdargs += "<STR_LIT>" + str ( manifest ) <EOL> if prefix : <EOL> cmdargs += "<STR_LIT>" + str ( prefix ) <EOL> if directory : <EOL> cmdargs += "<STR_LIT>" + str ( directory ) <EOL> if credpath is not None : <EOL> cmd = '<STR_LIT>' + str ( credpath ) + '<STR_LIT>' + str ( cmdargs ) <EOL> else : <EOL> skey = self . tester . get_secret_key ( ) <EOL> akey = self . tester . get_access_key ( ) <EOL> cmd = ( '<STR_LIT>' + str ( akey ) + <EOL> '<STR_LIT>' + str ( skey ) + str ( cmdargs ) ) <EOL> out = machine . sys ( cmd = cmd , code = <NUM_LIT:0> ) <EOL> raise NotImplemented ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def euca2ools_download_and_unbundle ( self , <EOL> bucket , <EOL> manifest = None , <EOL> prefix = None , <EOL> directory = None , <EOL> maxbytes = None , <EOL> ) : <EOL> raise NotImplemented ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def euca2ools_import_volume ( self , <EOL> import_file , <EOL> bucket , <EOL> zone , <EOL> format , <EOL> size = None , <EOL> presigned_manifest_url = None , <EOL> prefix = None , <EOL> days = None , <EOL> no_upload = None , <EOL> description = None , <EOL> s3_url = None , <EOL> ec2_url = None , <EOL> owner_sak = None , <EOL> owner_akid = None , <EOL> security_token = None , <EOL> machine = None , <EOL> machine_credpath = None , <EOL> misc = None ) : <EOL> '''<STR_LIT>''' <EOL> machine = machine or self . worker_machine <EOL> credpath = machine_credpath or self . credpath <EOL> cmdargs = str ( import_file ) + "<STR_LIT>" + str ( bucket ) + "<STR_LIT>" + str ( zone ) + "<STR_LIT>" + str ( format ) + "<STR_LIT>" <EOL> emi = None <EOL> if description : <EOL> cmdargs += '<STR_LIT>' + str ( description ) <EOL> if size : <EOL> cmdargs += '<STR_LIT>' + str ( size ) <EOL> if presigned_manifest_url : <EOL> cmdargs += '<STR_LIT>' + str ( presigned_manifest_url ) <EOL> if prefix : <EOL> cmdargs += "<STR_LIT>" + str ( prefix ) <EOL> if days : <EOL> cmdargs += "<STR_LIT>" + str ( days ) <EOL> if no_upload : <EOL> cmdargs += "<STR_LIT>" <EOL> if s3_url : <EOL> cmdargs += "<STR_LIT>" + str ( s3_url ) <EOL> if ec2_url : <EOL> cmdargs += "<STR_LIT>" + str ( ec2_url ) <EOL> if owner_sak : <EOL> cmdargs += "<STR_LIT>" + str ( owner_sak ) <EOL> if owner_akid : <EOL> cmdargs += "<STR_LIT>" + str ( owner_akid ) <EOL> if security_token : <EOL> cmdargs += "<STR_LIT>" + str ( security_token ) <EOL> if misc : <EOL> cmdargs += misc <EOL> if credpath is not None : <EOL> cmd = ( '<STR_LIT>' + str ( credpath ) + <EOL> '<STR_LIT>' + <EOL> str ( cmdargs ) ) <EOL> else : <EOL> cmd = '<STR_LIT>' <EOL> if not owner_sak : <EOL> owner_sak = self . tester . get_secret_key ( ) <EOL> cmdargs += "<STR_LIT>" + str ( owner_sak ) <EOL> if not owner_akid : <EOL> owner_akid = self . tester . get_access_key ( ) <EOL> cmdargs += "<STR_LIT>" + str ( owner_akid ) <EOL> cmd += str ( cmdargs ) <EOL> out = machine . sys ( cmd = cmd , code = <NUM_LIT:0> ) <EOL> for line in out : <EOL> lre = re . search ( '<STR_LIT>' , line ) <EOL> if lre : <EOL> taskid = lre . group ( ) <EOL> self . debug ( '<STR_LIT>' + str ( taskid ) ) <EOL> task = self . tester . get_conversion_task ( taskid = taskid ) <EOL> assert task , '<STR_LIT>' . format ( str ( taskid ) ) <EOL> self . tester . test_resources [ '<STR_LIT>' ] <EOL> return task <EOL> def euca2ools_import_instance ( self , <EOL> import_file , <EOL> bucket , <EOL> zone , <EOL> format , <EOL> instance_type , <EOL> arch , <EOL> platform , <EOL> size = None , <EOL> keypair = None , <EOL> presigned_manifest_url = None , <EOL> prefix = None , <EOL> days = None , <EOL> no_upload = None , <EOL> description = None , <EOL> group = None , <EOL> s3_url = None , <EOL> ec2_url = None , <EOL> image_size = None , <EOL> user_data = None , <EOL> user_data_file = None , <EOL> private_addr = None , <EOL> shutdown_behavior = None , <EOL> owner_sak = None , <EOL> owner_akid = None , <EOL> security_token = None , <EOL> machine = None , <EOL> machine_credpath = None , <EOL> misc = None , <EOL> time_per_gig = <NUM_LIT> ) : <EOL> machine = machine or self . worker_machine <EOL> try : <EOL> file_size = machine . get_file_size ( import_file ) <EOL> gb = file_size / self . gig or <NUM_LIT:1> <EOL> timeout = gb * time_per_gig <EOL> except : <EOL> timeout = <NUM_LIT> <EOL> credpath = machine_credpath or self . credpath <EOL> cmdargs = str ( import_file ) + "<STR_LIT>" + str ( bucket ) + "<STR_LIT>" + str ( zone ) + "<STR_LIT>" + str ( format ) + "<STR_LIT>" + str ( instance_type ) + "<STR_LIT>" + str ( arch ) + "<STR_LIT>" + str ( platform ) + "<STR_LIT>" <EOL> emi = None <EOL> if description : <EOL> cmdargs += '<STR_LIT>' + str ( description ) <EOL> if group : <EOL> cmdargs += '<STR_LIT>' + str ( group ) <EOL> if keypair : <EOL> cmdargs += '<STR_LIT>' + str ( keypair ) <EOL> if size : <EOL> cmdargs += '<STR_LIT>' + str ( size ) <EOL> if presigned_manifest_url : <EOL> cmdargs += '<STR_LIT>' + str ( presigned_manifest_url ) <EOL> if prefix : <EOL> cmdargs += "<STR_LIT>" + str ( prefix ) <EOL> if days : <EOL> cmdargs += "<STR_LIT>" + str ( days ) <EOL> if no_upload : <EOL> cmdargs += "<STR_LIT>" <EOL> if s3_url : <EOL> cmdargs += "<STR_LIT>" + str ( s3_url ) <EOL> if ec2_url : <EOL> cmdargs += "<STR_LIT>" + str ( ec2_url ) <EOL> if image_size : <EOL> cmdargs += "<STR_LIT>" + str ( image_size ) <EOL> if user_data : <EOL> cmdargs += '<STR_LIT>' + str ( user_data ) + '<STR_LIT:">' <EOL> if user_data_file : <EOL> cmdargs += "<STR_LIT>" + str ( user_data_file ) <EOL> if private_addr : <EOL> cmdargs += "<STR_LIT>" <EOL> if shutdown_behavior : <EOL> cmdargs += "<STR_LIT>" + str ( shutdown_behavior ) <EOL> if owner_sak : <EOL> cmdargs += "<STR_LIT>" + str ( owner_sak ) <EOL> if owner_akid : <EOL> cmdargs += "<STR_LIT>" + str ( owner_akid ) <EOL> if security_token : <EOL> cmdargs += "<STR_LIT>" + str ( security_token ) <EOL> if misc : <EOL> cmdargs += misc <EOL> if credpath is not None : <EOL> cmd = ( '<STR_LIT>' + str ( credpath ) + <EOL> '<STR_LIT>' + <EOL> str ( cmdargs ) ) <EOL> else : <EOL> cmd = '<STR_LIT>' <EOL> if not owner_sak : <EOL> owner_sak = self . tester . get_secret_key ( ) <EOL> cmdargs += "<STR_LIT>" + str ( owner_sak ) <EOL> if not owner_akid : <EOL> owner_akid = self . tester . get_access_key ( ) <EOL> cmdargs += "<STR_LIT>" + str ( owner_akid ) <EOL> cmd += str ( cmdargs ) <EOL> out = machine . sys ( cmd = cmd , timeout = timeout , code = <NUM_LIT:0> ) <EOL> taskid = None <EOL> for line in out : <EOL> lre = re . search ( '<STR_LIT>' , line ) <EOL> if lre : <EOL> taskid = lre . group ( ) <EOL> if not taskid : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> + str ( cmd ) ) <EOL> self . debug ( '<STR_LIT>' + str ( taskid ) ) <EOL> task = self . tester . get_conversion_task ( taskid = taskid ) <EOL> assert task , '<STR_LIT>' . format ( str ( taskid ) ) <EOL> return task <EOL> def _parse_euca2ools_conversion_task_output ( self , output ) : <EOL> splitlist = [ ] <EOL> retlist = [ ] <EOL> for line in output : <EOL> splitlist . extend ( line . split ( '<STR_LIT:\t>' ) ) <EOL> if len ( splitlist ) % <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" + str ( output ) + <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> task = ConversionTask ( ) <EOL> for i in xrange ( <NUM_LIT:0> , len ( splitlist ) ) : <EOL> if i % <NUM_LIT:2> : <EOL> task . endElement ( name = lastkey , value = splitlist [ i ] ) <EOL> else : <EOL> lastkey = str ( splitlist [ i ] ) . lower ( ) <EOL> if lastkey == '<STR_LIT>' : <EOL> if task : <EOL> retlist . append ( task ) <EOL> task = ConversionTask <EOL> return retlist <EOL> def _generate_unique_bucket_name_from_manifest ( self , manifest , unique = True ) : <EOL> mlist = str ( manifest . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) . split ( '<STR_LIT:/>' ) <EOL> basename = mlist [ len ( mlist ) - <NUM_LIT:1> ] . replace ( '<STR_LIT:_>' , '<STR_LIT>' ) . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) <EOL> if unique : <EOL> return self . _get_unique_bucket_name ( basename ) <EOL> return basename <EOL> def _get_unique_bucket_name ( self , basename , id = '<STR_LIT:test>' , start = <NUM_LIT:0> ) : <EOL> bx = start <EOL> bname = basename <EOL> while self . tester . get_bucket_by_name ( bname ) is not None : <EOL> bx += <NUM_LIT:1> <EOL> bname = basename + str ( id ) + str ( bx ) <EOL> return bname <EOL> def create_working_dir_on_worker_machine ( self , path , overwrite = False ) : <EOL> path = str ( path ) <EOL> if self . worker_machine . is_file_present ( path ) : <EOL> if not overwrite : <EOL> raise Exception ( '<STR_LIT>' + <EOL> str ( self . worker_machine . hostname ) + <EOL> '<STR_LIT>' + path + '<STR_LIT>' + <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> self . worker_machine . sys ( '<STR_LIT>' + path , code = <NUM_LIT:0> ) <EOL> def _bundle_status_cb ( self , buf , cmdtimeout , parttimeout , starttime , <EOL> lasttime , check_image_stage ) : <EOL> ret = SshCbReturn ( stop = False ) <EOL> if ( cmdtimeout != <NUM_LIT:0> ) and ( int ( time . time ( ) - starttime ) > cmdtimeout ) : <EOL> self . debug ( '<STR_LIT>' + <EOL> str ( cmdtimeout ) + '<STR_LIT>' ) <EOL> ret . statuscode = - <NUM_LIT:100> <EOL> ret . stop = True <EOL> return ret <EOL> if not check_image_stage : <EOL> ret . settimer = parttimeout <EOL> if ( parttimeout != <NUM_LIT:0> and lasttime != <NUM_LIT:0> ) and ( int ( time . time ( ) - lasttime ) > parttimeout ) : <EOL> self . debug ( '<STR_LIT>' + <EOL> str ( parttimeout ) + '<STR_LIT>' ) <EOL> ret . statuscode = - <NUM_LIT:100> <EOL> ret . stop = True <EOL> return ret <EOL> if re . search ( '<STR_LIT>' , buf ) : <EOL> sys . stdout . write ( "<STR_LIT>" + str ( buf ) . strip ( ) ) <EOL> sys . stdout . flush ( ) <EOL> check_image_stage = False <EOL> else : <EOL> ret . buf = buf <EOL> self . debug ( str ( buf ) ) <EOL> ret . stop = False <EOL> ret . nextargs = [ cmdtimeout , parttimeout , starttime , <EOL> time . time ( ) , check_image_stage ] <EOL> return ret <EOL> def create_emi ( self , <EOL> url , <EOL> machine = None , <EOL> bucketname = None , <EOL> machine_credpath = None , <EOL> debug = False , <EOL> prefix = None , <EOL> kernel = None , <EOL> ramdisk = None , <EOL> architecture = None , <EOL> block_device_mapping = [ ] , <EOL> destpath = None , <EOL> root_device_name = None , <EOL> description = None , <EOL> virtualization_type = None , <EOL> platform = None , <EOL> name = None , <EOL> interbundle_timeout = <NUM_LIT> , <EOL> upload_timeout = <NUM_LIT:0> , <EOL> uniquebucket = True , <EOL> wget_user = None , <EOL> wget_password = None , <EOL> wget_retryconn = True , <EOL> filepath = None , <EOL> bundle_manifest = None , <EOL> upload_manifest = None , <EOL> time_per_gig = <NUM_LIT> , <EOL> tagname = None , <EOL> overwrite = False <EOL> ) : <EOL> start = time . time ( ) <EOL> filesize = None <EOL> destpath = destpath or self . destpath <EOL> destpath = str ( destpath ) <EOL> if not destpath . endswith ( '<STR_LIT:/>' ) : <EOL> destpath += '<STR_LIT:/>' <EOL> if url : <EOL> filename = str ( url ) . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> destpath = destpath + str ( filename . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) ) <EOL> self . debug ( '<STR_LIT>' + str ( url ) + "<STR_LIT>" ) <EOL> if filepath is None and bundle_manifest is None and upload_manifest is None : <EOL> filepath = destpath + "<STR_LIT:/>" + str ( filename ) <EOL> self . create_working_dir_on_worker_machine ( path = destpath , <EOL> overwrite = overwrite ) <EOL> self . debug ( '<STR_LIT>' + str ( machine ) + '<STR_LIT::>' + <EOL> str ( filepath ) + '<STR_LIT>' + str ( url ) ) <EOL> filesize = self . wget_image ( url , <EOL> destpath = destpath , <EOL> machine = machine , <EOL> user = wget_user , <EOL> password = wget_password , <EOL> retryconn = wget_retryconn , <EOL> time_per_gig = time_per_gig ) <EOL> self . status ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if bundle_manifest is None and upload_manifest is None : <EOL> bundle_manifest = self . euca2ools_bundle_image ( <EOL> filepath , <EOL> machine = machine , <EOL> machine_credpath = machine_credpath , <EOL> prefix = prefix , <EOL> kernel = kernel , <EOL> ramdisk = ramdisk , <EOL> block_device_mapping = block_device_mapping , <EOL> destination = destpath , <EOL> debug = debug , <EOL> interbundle_timeout = interbundle_timeout , <EOL> time_per_gig = time_per_gig ) <EOL> self . status ( '<STR_LIT>' ) <EOL> if upload_manifest is None : <EOL> upload_manifest = self . euca2ools_upload_bundle ( <EOL> bundle_manifest , <EOL> machine = machine , <EOL> bucketname = bucketname , <EOL> machine_credpath = machine_credpath , <EOL> debug = debug , <EOL> interbundle_timeout = interbundle_timeout , <EOL> timeout = upload_timeout , <EOL> uniquebucket = uniquebucket ) <EOL> self . status ( '<STR_LIT>' ) <EOL> if name is None : <EOL> name = upload_manifest . split ( '<STR_LIT:/>' ) . pop ( ) . rstrip ( '<STR_LIT>' ) <EOL> name = "<STR_LIT>" . join ( re . findall ( r"<STR_LIT>" , name ) ) <EOL> name += '<STR_LIT:->' + str ( int ( time . time ( ) ) ) <EOL> try : <EOL> self . tester . get_emi ( name = '<STR_LIT:name>' ) <EOL> except : <EOL> name += '<STR_LIT:X>' <EOL> emi = self . euca2ools_register ( <EOL> manifest = upload_manifest , <EOL> name = name , <EOL> description = description , <EOL> arch = architecture , <EOL> kernel = kernel , <EOL> ramdisk = ramdisk , <EOL> root_device_name = root_device_name , <EOL> block_dev_map = block_device_mapping , <EOL> virtualization_type = virtualization_type , <EOL> platform = platform ) <EOL> self . debug ( '<STR_LIT>' + str ( emi ) + <EOL> '<STR_LIT>' ) <EOL> emi = self . tester . get_emi ( emi ) <EOL> try : <EOL> if filesize is not None : <EOL> emi . add_tag ( '<STR_LIT:size>' , value = str ( filesize ) ) <EOL> if url : <EOL> emi . add_tag ( '<STR_LIT:source>' , value = ( str ( url ) ) ) <EOL> emi . add_tag ( tagname or '<STR_LIT>' ) <EOL> except Exception , te : <EOL> self . debug ( '<STR_LIT>' + str ( emi . id ) + <EOL> "<STR_LIT>" + str ( te ) ) <EOL> elapsed = int ( time . time ( ) - start ) <EOL> self . status ( '<STR_LIT>' + <EOL> str ( emi . id ) + "<STR_LIT>" + str ( elapsed ) + "<STR_LIT>" ) <EOL> return emi </s>
