<s> """<STR_LIT>""" <EOL> import logging <EOL> from perfkitbenchmarker import flags <EOL> from perfkitbenchmarker import linux_virtual_machine as linux_vm <EOL> from perfkitbenchmarker import virtual_machine <EOL> from perfkitbenchmarker import vm_util <EOL> from perfkitbenchmarker . providers . cloudstack import cloudstack_disk <EOL> from perfkitbenchmarker . providers . cloudstack import cloudstack_network <EOL> from perfkitbenchmarker . providers . cloudstack import util <EOL> from perfkitbenchmarker import providers <EOL> UBUNTU_IMAGE = '<STR_LIT>' <EOL> RHEL_IMAGE = '<STR_LIT>' <EOL> FLAGS = flags . FLAGS <EOL> class CloudStackVirtualMachine ( virtual_machine . BaseVirtualMachine ) : <EOL> """<STR_LIT>""" <EOL> CLOUD = providers . CLOUDSTACK <EOL> DEFAULT_ZONE = '<STR_LIT>' <EOL> DEFAULT_MACHINE_TYPE = '<STR_LIT>' <EOL> DEFAULT_IMAGE = '<STR_LIT>' <EOL> DEFAULT_USER_NAME = '<STR_LIT>' <EOL> DEFAULT_PROJECT = '<STR_LIT>' <EOL> def __init__ ( self , vm_spec ) : <EOL> """<STR_LIT>""" <EOL> super ( CloudStackVirtualMachine , self ) . __init__ ( vm_spec ) <EOL> self . network = cloudstack_network . CloudStackNetwork . GetNetwork ( self ) <EOL> self . cs = util . CsClient ( FLAGS . CS_API_URL , <EOL> FLAGS . CS_API_KEY , <EOL> FLAGS . CS_API_SECRET ) <EOL> self . project_id = None <EOL> if FLAGS . project : <EOL> project = self . cs . get_project ( FLAGS . project ) <EOL> assert project , "<STR_LIT>" <EOL> self . project_id = project [ '<STR_LIT:id>' ] <EOL> zone = self . cs . get_zone ( self . zone ) <EOL> assert zone , "<STR_LIT>" <EOL> self . zone_id = zone [ '<STR_LIT:id>' ] <EOL> self . user_name = self . DEFAULT_USER_NAME <EOL> self . image = self . image or self . DEFAULT_IMAGE <EOL> self . disk_counter = <NUM_LIT:0> <EOL> @ vm_util . Retry ( max_retries = <NUM_LIT:3> ) <EOL> def _CreateDependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> with open ( self . ssh_public_key ) as keyfd : <EOL> self . ssh_keypair_name = '<STR_LIT>' % FLAGS . run_uri <EOL> pub_key = keyfd . read ( ) <EOL> if not self . cs . get_ssh_keypair ( self . ssh_keypair_name , self . project_id ) : <EOL> res = self . cs . register_ssh_keypair ( self . ssh_keypair_name , <EOL> pub_key , <EOL> self . project_id ) <EOL> assert res , "<STR_LIT>" <EOL> network_id = self . network . id <EOL> if self . network . is_vpc : <EOL> network_id = self . network . vpc_id <EOL> public_ip = self . cs . alloc_public_ip ( network_id , self . network . is_vpc ) <EOL> if public_ip : <EOL> self . ip_address = public_ip [ '<STR_LIT>' ] <EOL> self . ip_address_id = public_ip [ '<STR_LIT:id>' ] <EOL> else : <EOL> logging . warn ( "<STR_LIT>" ) <EOL> def _DeleteDependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . cs . get_ssh_keypair ( self . ssh_keypair_name , self . project_id ) : <EOL> self . cs . unregister_ssh_keypair ( self . ssh_keypair_name , self . project_id ) <EOL> if self . ip_address_id : <EOL> self . cs . release_public_ip ( self . ip_address_id ) <EOL> @ vm_util . Retry ( max_retries = <NUM_LIT:3> ) <EOL> def _Create ( self ) : <EOL> """<STR_LIT>""" <EOL> service_offering = self . cs . get_serviceoffering ( self . machine_type ) <EOL> assert service_offering , "<STR_LIT>" <EOL> template = self . cs . get_template ( self . image , self . project_id ) <EOL> assert template , "<STR_LIT>" <EOL> network_id = self . network . id <EOL> vm = None <EOL> vm = self . cs . create_vm ( self . name , <EOL> self . zone_id , <EOL> service_offering [ '<STR_LIT:id>' ] , <EOL> template [ '<STR_LIT:id>' ] , <EOL> [ network_id ] , <EOL> self . ssh_keypair_name , <EOL> self . project_id ) <EOL> assert vm , "<STR_LIT>" <EOL> self . _vm = vm <EOL> self . id = vm [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] <EOL> @ vm_util . Retry ( max_retries = <NUM_LIT:3> ) <EOL> def _PostCreate ( self ) : <EOL> """<STR_LIT>""" <EOL> network_interface = self . _vm [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . internal_ip = network_interface [ '<STR_LIT>' ] <EOL> if not self . cs . snat_rule_exists ( self . ip_address_id , self . id ) : <EOL> snat_rule = self . cs . enable_static_nat ( self . ip_address_id , <EOL> self . id , <EOL> self . network . id ) <EOL> assert snat_rule , "<STR_LIT>" <EOL> def _Delete ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cs . delete_vm ( self . id ) <EOL> def _Exists ( self ) : <EOL> """<STR_LIT>""" <EOL> vm = self . cs . get_virtual_machine ( self . name , self . project_id ) <EOL> if vm and '<STR_LIT:id>' in vm : <EOL> return True <EOL> return False <EOL> def CreateScratchDisk ( self , disk_spec ) : <EOL> """<STR_LIT>""" <EOL> self . disks = [ ] <EOL> for i in xrange ( disk_spec . num_striped_disks ) : <EOL> name = '<STR_LIT>' % ( self . name , i + <NUM_LIT:1> , self . disk_counter ) <EOL> scratch_disk = cloudstack_disk . CloudStackDisk ( disk_spec , <EOL> name , <EOL> self . zone_id , <EOL> self . project_id ) <EOL> self . disks . append ( scratch_disk ) <EOL> self . disk_counter += <NUM_LIT:1> <EOL> self . _CreateScratchDiskFromDisks ( disk_spec , self . disks ) <EOL> class DebianBasedCloudStackVirtualMachine ( CloudStackVirtualMachine , <EOL> linux_vm . DebianMixin ) : <EOL> DEFAULT_IMAGE = UBUNTU_IMAGE <EOL> class RhelBasedCloudStackVirtualMachine ( CloudStackVirtualMachine , <EOL> linux_vm . RhelMixin ) : <EOL> DEFAULT_IMAGE = RHEL_IMAGE </s>
