<s> import re <EOL> import sys <EOL> import time <EOL> import os <EOL> import pycurl <EOL> import logging <EOL> import urllib2 <EOL> from psphere . client import Client <EOL> from psphere . errors import TemplateNotFoundError <EOL> from psphere . soap import VimFault <EOL> from time import sleep , time <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . INFO ) <EOL> class VSphereHelper : <EOL> def __init__ ( self , url , username , password ) : <EOL> self . log = logging . getLogger ( '<STR_LIT>' % ( __name__ , self . __class__ . __name__ ) ) <EOL> if ( url . startswith ( '<STR_LIT>' ) or url . startswith ( '<STR_LIT>' ) ) : <EOL> server = urllib2 . Request ( url ) . get_host ( ) <EOL> else : <EOL> server = url <EOL> self . client = Client ( server = server , username = username , password = password ) <EOL> def curl_progress ( self , download_t , download_d , upload_t , upload_d ) : <EOL> curtime = time ( ) <EOL> if ( curtime - self . time_at_last_poke ) >= <NUM_LIT:5> : <EOL> self . lease . HttpNfcLeaseProgress ( percent = int ( upload_d * <NUM_LIT:100> / upload_t ) ) <EOL> self . time_at_last_poke = time ( ) <EOL> def create_vm ( self , imagefilename , name , compute_resource , datastore , disksize , nics , <EOL> memory , num_cpus , guest_id , host = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . client <EOL> self . log . debug ( "<STR_LIT>" % name ) <EOL> if host is None : <EOL> target = client . find_entity_view ( "<STR_LIT>" , <EOL> filter = { "<STR_LIT:name>" : compute_resource } ) <EOL> resource_pool = target . resourcePool <EOL> else : <EOL> target = client . find_entity_view ( "<STR_LIT>" , filter = { "<STR_LIT:name>" : host } ) <EOL> resource_pool = target . parent . resourcePool <EOL> disksize_pattern = re . compile ( "<STR_LIT>" ) <EOL> if disksize_pattern . match ( disksize ) is None : <EOL> raise Exception ( "<STR_LIT>" % disksize ) <EOL> if disksize . endswith ( "<STR_LIT>" ) : <EOL> disksize_kb = int ( disksize [ : - <NUM_LIT:2> ] ) * <NUM_LIT> * <NUM_LIT> <EOL> elif disksize . endswith ( "<STR_LIT>" ) : <EOL> disksize_kb = int ( disksize [ : - <NUM_LIT:2> ] ) * <NUM_LIT> <EOL> elif disksize . endswith ( "<STR_LIT>" ) : <EOL> disksize_kb = int ( disksize [ : - <NUM_LIT:2> ] ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % disksize ) <EOL> memory_pattern = re . compile ( "<STR_LIT>" ) <EOL> if memory_pattern . match ( memory ) is None : <EOL> raise Exception ( "<STR_LIT>" % memory ) <EOL> if memory . endswith ( "<STR_LIT>" ) : <EOL> memory_mb = int ( memory [ : - <NUM_LIT:2> ] ) * <NUM_LIT> <EOL> elif memory . endswith ( "<STR_LIT>" ) : <EOL> memory_mb = int ( memory [ : - <NUM_LIT:2> ] ) <EOL> elif memory . endswith ( "<STR_LIT>" ) : <EOL> memory_mb = int ( memory [ : - <NUM_LIT:2> ] ) / <NUM_LIT> <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % memory ) <EOL> vm_devices = [ ] <EOL> controller = self . create_controller ( "<STR_LIT>" ) <EOL> vm_devices . append ( controller ) <EOL> ds_to_use = None <EOL> for ds in target . datastore : <EOL> if ds . name == datastore : <EOL> ds_to_use = ds <EOL> break <EOL> if ds_to_use is None : <EOL> raise Exception ( "<STR_LIT>" % <EOL> ( target . name , datastore ) ) <EOL> if ds_to_use . summary . accessible is not True : <EOL> raise Exception ( "<STR_LIT>" % <EOL> ds_to_use . summary . name ) <EOL> if ds_to_use . summary . freeSpace < disksize_kb * <NUM_LIT> : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" % ds_to_use . summary . name ) <EOL> disk = self . create_disk ( datastore = ds_to_use , disksize_kb = disksize_kb ) <EOL> vm_devices . append ( disk ) <EOL> cdrom = self . create_cdrom ( datastore = ds_to_use ) <EOL> vm_devices . append ( cdrom ) <EOL> for nic in nics : <EOL> nic_spec = self . create_nic ( target , nic ) <EOL> if nic_spec is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> vm_devices . append ( nic_spec ) <EOL> vmfi = client . create ( "<STR_LIT>" ) <EOL> vmfi . vmPathName = "<STR_LIT>" % ds_to_use . summary . name <EOL> vm_config_spec = client . create ( "<STR_LIT>" ) <EOL> vm_config_spec . name = name <EOL> vm_config_spec . memoryMB = memory_mb <EOL> vm_config_spec . files = vmfi <EOL> vm_config_spec . annotation = "<STR_LIT>" <EOL> vm_config_spec . numCPUs = num_cpus <EOL> vm_config_spec . guestId = guest_id <EOL> vm_config_spec . deviceChange = vm_devices <EOL> if target . __class__ . __name__ == "<STR_LIT>" : <EOL> datacenter = target . parent . parent . parent <EOL> else : <EOL> datacenter = target . parent . parent <EOL> importspec = client . create ( '<STR_LIT>' ) <EOL> importspec . configSpec = vm_config_spec <EOL> importspec . resPoolEntity = None <EOL> lease = resource_pool . ImportVApp ( spec = importspec , folder = datacenter . vmFolder ) <EOL> self . lease = lease <EOL> for i in range ( <NUM_LIT:1000> ) : <EOL> if lease . state == "<STR_LIT>" : <EOL> break <EOL> if lease . state == "<STR_LIT:error>" : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> sleep ( <NUM_LIT:5> ) <EOL> lease . update_view_data ( properties = [ "<STR_LIT:state>" ] ) <EOL> upload_url = None <EOL> for url_candidate in lease . info . deviceUrl : <EOL> if url_candidate [ '<STR_LIT>' ] : <EOL> upload_url = str ( url_candidate [ '<STR_LIT:url>' ] ) <EOL> if not upload_url : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . log . debug ( "<STR_LIT>" % ( upload_url ) ) <EOL> lease_timeout = lease . info . leaseTimeout <EOL> self . time_at_last_poke = time ( ) <EOL> image_file = open ( imagefilename ) <EOL> image_size = os . path . getsize ( imagefilename ) <EOL> curl = pycurl . Curl ( ) <EOL> curl . setopt ( pycurl . URL , upload_url ) <EOL> curl . setopt ( pycurl . SSL_VERIFYPEER , <NUM_LIT:0> ) <EOL> curl . setopt ( pycurl . POST , <NUM_LIT:1> ) <EOL> curl . setopt ( pycurl . POSTFIELDSIZE , image_size ) <EOL> curl . setopt ( pycurl . READFUNCTION , image_file . read ) <EOL> curl . setopt ( pycurl . HTTPHEADER , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> curl . setopt ( pycurl . NOPROGRESS , <NUM_LIT:0> ) <EOL> curl . setopt ( pycurl . PROGRESSFUNCTION , self . curl_progress ) <EOL> curl . perform ( ) <EOL> curl . close ( ) <EOL> image_file . close ( ) <EOL> lease . HttpNfcLeaseComplete ( ) <EOL> vm = lease . info . entity <EOL> vm . MarkAsTemplate ( ) <EOL> def create_nic ( self , target , nic ) : <EOL> client = self . client <EOL> """<STR_LIT>""" <EOL> for network in target . network : <EOL> if network . name == nic [ "<STR_LIT>" ] : <EOL> net = network <EOL> break <EOL> else : <EOL> return None <EOL> backing = client . create ( "<STR_LIT>" ) <EOL> backing . deviceName = nic [ "<STR_LIT>" ] <EOL> backing . network = net <EOL> connect_info = client . create ( "<STR_LIT>" ) <EOL> connect_info . allowGuestControl = True <EOL> connect_info . connected = False <EOL> connect_info . startConnected = True <EOL> new_nic = client . create ( nic [ "<STR_LIT:type>" ] ) <EOL> new_nic . backing = backing <EOL> new_nic . key = <NUM_LIT:2> <EOL> new_nic . unitNumber = <NUM_LIT:1> <EOL> new_nic . addressType = "<STR_LIT>" <EOL> new_nic . connectable = connect_info <EOL> nic_spec = client . create ( "<STR_LIT>" ) <EOL> nic_spec . device = new_nic <EOL> nic_spec . fileOperation = None <EOL> operation = client . create ( "<STR_LIT>" ) <EOL> nic_spec . operation = ( operation . add ) <EOL> return nic_spec <EOL> def create_controller ( self , controller_type ) : <EOL> client = self . client <EOL> controller = client . create ( controller_type ) <EOL> controller . key = <NUM_LIT:0> <EOL> controller . device = [ <NUM_LIT:0> ] <EOL> controller . busNumber = <NUM_LIT:0> , <EOL> controller . sharedBus = client . create ( "<STR_LIT>" ) . noSharing <EOL> spec = client . create ( "<STR_LIT>" ) <EOL> spec . device = controller <EOL> spec . fileOperation = None <EOL> spec . operation = client . create ( "<STR_LIT>" ) . add <EOL> return spec <EOL> def create_disk ( self , datastore , disksize_kb ) : <EOL> client = self . client <EOL> backing = client . create ( "<STR_LIT>" ) <EOL> backing . datastore = None <EOL> backing . diskMode = "<STR_LIT>" <EOL> backing . fileName = "<STR_LIT>" % datastore . summary . name <EOL> backing . thinProvisioned = True <EOL> disk = client . create ( "<STR_LIT>" ) <EOL> disk . backing = backing <EOL> disk . controllerKey = <NUM_LIT:0> <EOL> disk . key = <NUM_LIT:0> <EOL> disk . unitNumber = <NUM_LIT:0> <EOL> disk . capacityInKB = disksize_kb <EOL> disk_spec = client . create ( "<STR_LIT>" ) <EOL> disk_spec . device = disk <EOL> file_op = client . create ( "<STR_LIT>" ) <EOL> disk_spec . fileOperation = file_op . create <EOL> operation = client . create ( "<STR_LIT>" ) <EOL> disk_spec . operation = operation . add <EOL> return disk_spec <EOL> def create_cdrom ( self , datastore ) : <EOL> client = self . client <EOL> connectable = client . create ( '<STR_LIT>' ) <EOL> connectable . allowGuestControl = True <EOL> connectable . connected = True <EOL> connectable . startConnected = True <EOL> backing = client . create ( '<STR_LIT>' ) <EOL> backing . datastore = None <EOL> backing . fileName = '<STR_LIT>' % datastore . summary . name <EOL> cdrom = client . create ( '<STR_LIT>' ) <EOL> cdrom . connectable = connectable <EOL> cdrom . backing = backing <EOL> cdrom . controllerKey = <NUM_LIT> <EOL> cdrom . key = <NUM_LIT:10> <EOL> cdrom . unitNumber = <NUM_LIT:0> <EOL> cdrom_spec = client . create ( '<STR_LIT>' ) <EOL> cdrom_spec . fileOperation = None <EOL> cdrom_spec . device = cdrom <EOL> operation = client . create ( '<STR_LIT>' ) <EOL> cdrom_spec . operation = operation . add <EOL> return cdrom_spec <EOL> def delete_vm ( self , name ) : <EOL> vm = self . client . find_entity_view ( "<STR_LIT>" , filter = { "<STR_LIT:name>" : name } ) <EOL> if not vm : <EOL> raise Exception ( "<STR_LIT>" % ( name ) ) <EOL> vmdestroy = vm . Destroy_Task ( ) <EOL> for i in range ( <NUM_LIT> ) : <EOL> if not ( vmdestroy . info . state in [ "<STR_LIT>" , "<STR_LIT>" ] ) : <EOL> break <EOL> sleep ( <NUM_LIT:1> ) <EOL> vmdestroy . update_view_data ( properties = [ "<STR_LIT:info>" ] ) <EOL> if vmdestroy . info . state != "<STR_LIT:success>" : <EOL> raise Exception ( "<STR_LIT>" % ( name ) ) </s>
