<s> import logging <EOL> import threading <EOL> import time <EOL> from perfkitbenchmarker import virtual_machine , linux_virtual_machine <EOL> from perfkitbenchmarker import errors <EOL> from perfkitbenchmarker import flags <EOL> from perfkitbenchmarker import vm_util <EOL> from perfkitbenchmarker import providers <EOL> from perfkitbenchmarker . providers . openstack import os_disk <EOL> from perfkitbenchmarker . providers . openstack import os_network <EOL> from perfkitbenchmarker . providers . openstack import utils as os_utils <EOL> RHEL_IMAGE = '<STR_LIT>' <EOL> UBUNTU_IMAGE = '<STR_LIT>' <EOL> NONE = '<STR_LIT:None>' <EOL> FLAGS = flags . FLAGS <EOL> class OpenStackVirtualMachine ( virtual_machine . BaseVirtualMachine ) : <EOL> """<STR_LIT>""" <EOL> CLOUD = providers . OPENSTACK <EOL> DEFAULT_IMAGE = None <EOL> _floating_ip_lock = threading . Lock ( ) <EOL> def __init__ ( self , vm_spec ) : <EOL> """<STR_LIT>""" <EOL> super ( OpenStackVirtualMachine , self ) . __init__ ( vm_spec ) <EOL> self . firewall = os_network . OpenStackFirewall . GetFirewall ( ) <EOL> self . firewall . AllowICMP ( self ) <EOL> self . firewall . AllowPort ( self , <NUM_LIT:1> , os_network . MAX_PORT ) <EOL> self . name = '<STR_LIT>' % ( self . instance_number , FLAGS . run_uri ) <EOL> self . key_name = '<STR_LIT>' % ( self . instance_number , <EOL> FLAGS . run_uri ) <EOL> self . client = os_utils . NovaClient ( ) <EOL> self . network_name = ( FLAGS . openstack_network or <EOL> FLAGS . openstack_private_network ) <EOL> self . floating_ip_pool_name = ( FLAGS . openstack_floating_ip_pool or <EOL> FLAGS . openstack_public_network ) <EOL> self . public_network = os_network . OpenStackPublicNetwork ( <EOL> FLAGS . openstack_floating_ip_pool ) <EOL> self . id = None <EOL> self . pk = None <EOL> self . user_name = FLAGS . openstack_image_username <EOL> self . boot_wait_time = None <EOL> self . image = self . image or self . DEFAULT_IMAGE <EOL> self . public_net = None <EOL> self . private_net = None <EOL> self . floating_ip = None <EOL> def _Create ( self ) : <EOL> image = self . client . images . findall ( name = self . image ) [ <NUM_LIT:0> ] <EOL> flavor = self . client . flavors . findall ( name = self . machine_type ) [ <NUM_LIT:0> ] <EOL> self . private_net = self . client . networks . find ( label = self . network_name ) <EOL> if self . floating_ip_pool_name : <EOL> self . public_net = self . client . networks . find ( <EOL> label = self . floating_ip_pool_name ) <EOL> if not self . private_net : <EOL> if self . public_net : <EOL> raise errors . Error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> raise errors . Error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> nics = [ { '<STR_LIT>' : self . private_net . id } ] <EOL> image_id = image . id <EOL> boot_from_vol = [ ] <EOL> scheduler_hints = self . _GetSchedulerHints ( ) <EOL> if FLAGS . openstack_boot_from_volume : <EOL> volume_size = FLAGS . openstack_volume_size or flavor . disk <EOL> image_id = None <EOL> boot_from_vol = [ { '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : image . id , <EOL> '<STR_LIT>' : volume_size , <EOL> '<STR_LIT>' : '<STR_LIT:image>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } ] <EOL> vm = self . client . servers . create ( <EOL> name = self . name , <EOL> image = image_id , <EOL> flavor = flavor . id , <EOL> key_name = self . key_name , <EOL> security_groups = [ '<STR_LIT>' ] , <EOL> nics = nics , <EOL> availability_zone = self . zone , <EOL> block_device_mapping_v2 = boot_from_vol , <EOL> scheduler_hints = scheduler_hints , <EOL> config_drive = FLAGS . openstack_config_drive ) <EOL> self . id = vm . id <EOL> def _GetSchedulerHints ( self ) : <EOL> scheduler_hints = None <EOL> if FLAGS . openstack_scheduler_policy != NONE : <EOL> group_name = '<STR_LIT>' % FLAGS . run_uri <EOL> try : <EOL> group = self . client . server_groups . findall ( name = group_name ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> group = self . client . server_groups . create ( <EOL> policies = [ FLAGS . openstack_scheduler_policy ] , <EOL> name = group_name ) <EOL> scheduler_hints = { '<STR_LIT>' : group . id } <EOL> return scheduler_hints <EOL> @ vm_util . Retry ( max_retries = <NUM_LIT:4> , poll_interval = <NUM_LIT:2> ) <EOL> def _PostCreate ( self ) : <EOL> status = '<STR_LIT>' <EOL> instance = None <EOL> while status == '<STR_LIT>' : <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> instance = self . client . servers . get ( self . id ) <EOL> status = instance . status <EOL> assert self . private_net is not None , '<STR_LIT>' <EOL> self . internal_ip = instance . networks [ self . network_name ] [ <NUM_LIT:0> ] <EOL> self . ip_address = self . internal_ip <EOL> if self . public_net : <EOL> self . floating_ip = self . _AllocateFloatingIP ( instance ) <EOL> self . ip_address = self . floating_ip . ip <EOL> def _AllocateFloatingIP ( self , instance ) : <EOL> with self . _floating_ip_lock : <EOL> floating_ip = self . public_network . get_or_create ( ) <EOL> instance . add_floating_ip ( floating_ip ) <EOL> logging . info ( <EOL> '<STR_LIT>' . format ( floating_ip . ip ) ) <EOL> while not self . public_network . is_attached ( floating_ip ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> return floating_ip <EOL> def _Delete ( self ) : <EOL> from novaclient . exceptions import NotFound <EOL> try : <EOL> self . client . servers . delete ( self . id ) <EOL> self . _WaitForDeleteCompletion ( ) <EOL> except NotFound : <EOL> logging . info ( '<STR_LIT>' ) <EOL> if self . floating_ip : <EOL> self . public_network . release ( self . floating_ip ) <EOL> def _Exists ( self ) : <EOL> from novaclient . exceptions import NotFound <EOL> try : <EOL> return self . client . servers . get ( self . id ) is not None <EOL> except NotFound : <EOL> return False <EOL> def WaitForBootCompletion ( self ) : <EOL> if self . boot_wait_time is None : <EOL> self . boot_wait_time = <NUM_LIT:15> <EOL> time . sleep ( self . boot_wait_time ) <EOL> self . boot_wait_time = <NUM_LIT:5> <EOL> resp , _ = self . RemoteCommand ( '<STR_LIT>' , retries = <NUM_LIT:1> ) <EOL> if self . bootable_time is None : <EOL> self . bootable_time = time . time ( ) <EOL> if self . hostname is None : <EOL> self . hostname = resp [ : - <NUM_LIT:1> ] <EOL> @ vm_util . Retry ( poll_interval = <NUM_LIT:5> , max_retries = - <NUM_LIT:1> , timeout = <NUM_LIT> , <EOL> log_errors = False , <EOL> retryable_exceptions = ( <EOL> errors . Resource . RetryableDeletionError , ) ) <EOL> def _WaitForDeleteCompletion ( self ) : <EOL> instance = self . client . servers . get ( self . id ) <EOL> if instance and instance . status == '<STR_LIT>' : <EOL> raise errors . Resource . RetryableDeletionError ( <EOL> '<STR_LIT>' <EOL> % self . name ) <EOL> def CreateScratchDisk ( self , disk_spec ) : <EOL> disks_names = ( '<STR_LIT>' <EOL> % ( self . name , len ( self . scratch_disks ) , i ) <EOL> for i in range ( disk_spec . num_striped_disks ) ) <EOL> disks = [ os_disk . OpenStackDisk ( disk_spec , name , self . zone ) <EOL> for name in disks_names ] <EOL> self . _CreateScratchDiskFromDisks ( disk_spec , disks ) <EOL> def _CreateDependencies ( self ) : <EOL> self . ImportKeyfile ( ) <EOL> self . AllowRemoteAccessPorts ( ) <EOL> def _DeleteDependencies ( self ) : <EOL> self . DeleteKeyfile ( ) <EOL> def ImportKeyfile ( self ) : <EOL> if not ( self . client . keypairs . findall ( name = self . key_name ) ) : <EOL> cat_cmd = [ '<STR_LIT>' , <EOL> vm_util . GetPublicKeyPath ( ) ] <EOL> key_file , _ = vm_util . IssueRetryableCommand ( cat_cmd ) <EOL> pk = self . client . keypairs . create ( self . key_name , <EOL> public_key = key_file ) <EOL> else : <EOL> pk = self . client . keypairs . findall ( name = self . key_name ) [ <NUM_LIT:0> ] <EOL> self . pk = pk <EOL> def DeleteKeyfile ( self ) : <EOL> from novaclient . exceptions import NotFound <EOL> try : <EOL> self . client . keypairs . delete ( self . pk ) <EOL> except NotFound : <EOL> logging . info ( "<STR_LIT>" ) <EOL> class DebianBasedOpenStackVirtualMachine ( OpenStackVirtualMachine , <EOL> linux_virtual_machine . DebianMixin ) : <EOL> DEFAULT_IMAGE = UBUNTU_IMAGE <EOL> class RhelBasedOpenStackVirtualMachine ( OpenStackVirtualMachine , <EOL> linux_virtual_machine . RhelMixin ) : <EOL> DEFAULT_IMAGE = RHEL_IMAGE </s>
