# under the License.
#
+import os
+import re
+import time
+
import compute
-from glanceclient.v2 import client as glanceclient
from log import LOG
+
+from glanceclient.v2 import client as glanceclient
from neutronclient.neutron import client as neutronclient
from novaclient.client import Client
-import os
-import time
class StageClientException(Exception):
def __init__(self, config, cred):
self.comp = None
self.image_instance = None
+ self.image_name = None
self.config = config
self.cred = cred
self.nets = []
self.host_ips = None
def _ensure_vms_active(self):
- for _ in range(self.config.generic_retry_count):
+ retry_count = (self.config.check_traffic_time_sec +
+ self.config.generic_poll_sec - 1) / self.config.generic_poll_sec
+ for _ in range(retry_count):
for i, instance in enumerate(self.vms):
if instance.status == 'ACTIVE':
continue
LOG.info('Created instance: %s', instance.name)
self.vms[i] = instance
setattr(self.vms[i], 'is_reuse', is_reuse)
- if all(map(lambda instance: instance.status == 'ACTIVE', self.vms)):
+
+ if all([(vm.status == 'ACTIVE') for vm in self.vms]):
return
time.sleep(self.config.generic_poll_sec)
raise StageClientException('Timed out waiting for VMs to spawn')
networks = self.neutron.list_networks(name=network_name)
return networks['networks'][0] if networks['networks'] else None
- def _create_net(self, name, subnet, cidr, network_type=None, segmentation_id=None, physical_network=None):
+ def _create_net(self, name, subnet, cidr, network_type=None,
+ segmentation_id=None, physical_network=None):
network = self._lookup_network(name)
if network:
# a network of same name already exists, we need to verify it has the same
# add subnet id to the network dict since it has just been added
network['subnets'] = [subnet['id']]
network['is_reuse'] = False
- LOG.info('Created network: %s.' % name)
+ LOG.info('Created network: %s.', name)
return network
def _create_port(self, net):
except Exception:
retry += 1
time.sleep(self.config.generic_poll_sec)
- LOG.error('Unable to delete port: %s' % (port['id']))
+ LOG.error('Unable to delete port: %s', port['id'])
def __delete_net(self, network):
retry = 0
except Exception:
retry += 1
time.sleep(self.config.generic_poll_sec)
- LOG.error('Unable to delete network: %s' % (network['name']))
+ LOG.error('Unable to delete network: %s', network['name'])
def __get_server_az(self, server):
availability_zone = getattr(server, 'OS-EXT-AZ:availability_zone', None)
def _lookup_servers(self, name=None, nets=None, az=None, flavor_id=None):
error_msg = 'VM with the same name, but non-matching {} found. Aborting.'
- networks = set(map(lambda net: net['name'], nets)) if nets else None
+ networks = set([net['name'] for net in nets]) if nets else None
server_list = self.comp.get_server_list()
matching_servers = []
if name and server.name != name:
continue
- if az and self.__get_server_az(server) != az:
- raise StageClientException(error_msg.format('availability zones'))
-
if flavor_id and server.flavor['id'] != flavor_id:
raise StageClientException(error_msg.format('flavors'))
return matching_servers
def _create_server(self, name, ports, az, nfvbenchvm_config):
- port_ids = map(lambda port: {'port-id': port['id']}, ports)
+ port_ids = [{'port-id': port['id']} for port in ports]
nfvbenchvm_config_location = os.path.join('/etc/', self.nfvbenchvm_config_name)
server = self.comp.create_server(name,
self.image_instance,
files={nfvbenchvm_config_location: nfvbenchvm_config})
if server:
setattr(server, 'is_reuse', False)
- LOG.info('Creating instance: %s on %s' % (name, az))
+ LOG.info('Creating instance: %s on %s', name, az)
else:
raise StageClientException('Unable to create instance: %s.' % (name))
return server
def _setup_resources(self):
- if not self.image_instance:
- self.image_instance = self.comp.find_image(self.config.image_name)
- if self.image_instance is None:
+ # To avoid reuploading image in server mode, check whether image_name is set or not
+ if self.image_name:
+ self.image_instance = self.comp.find_image(self.image_name)
+ if self.image_instance:
+ LOG.info("Reusing image %s", self.image_name)
+ else:
+ image_name_search_pattern = r'(nfvbenchvm-\d+(\.\d+)*).qcow2'
if self.config.vm_image_file:
- LOG.info('%s: image for VM not found, trying to upload it ...'
- % self.config.image_name)
- res = self.comp.upload_image_via_url(self.config.image_name,
+ match = re.search(image_name_search_pattern, self.config.vm_image_file)
+ if match:
+ self.image_name = match.group(1)
+ LOG.info('Using provided VM image file %s', self.config.vm_image_file)
+ else:
+ raise StageClientException('Provided VM image file name %s must start with '
+ '"nfvbenchvm-<version>"' % self.config.vm_image_file)
+ else:
+ pkg_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+ for f in os.listdir(pkg_root):
+ if re.search(image_name_search_pattern, f):
+ self.config.vm_image_file = pkg_root + '/' + f
+ self.image_name = f.replace('.qcow2', '')
+ LOG.info('Found built-in VM image file %s', f)
+ break
+ else:
+ raise StageClientException('Cannot find any built-in VM image file.')
+ if self.image_name:
+ self.image_instance = self.comp.find_image(self.image_name)
+ if not self.image_instance:
+ LOG.info('Uploading %s', self.image_name)
+ res = self.comp.upload_image_via_url(self.image_name,
self.config.vm_image_file)
if not res:
raise StageClientException('Error uploading image %s from %s. ABORTING.'
- % (self.config.image_name,
+ % (self.image_name,
self.config.vm_image_file))
- self.image_instance = self.comp.find_image(self.config.image_name)
- else:
- raise StageClientException('%s: image to launch VM not found. ABORTING.'
- % self.config.image_name)
-
- LOG.info('Found image %s to launch VM' % self.config.image_name)
+ LOG.info('Image %s successfully uploaded.', self.image_name)
+ self.image_instance = self.comp.find_image(self.image_name)
self.__setup_flavor()
override=True,
**flavor_dict)
- LOG.info("Flavor '%s' was created." % self.config.flavor_type)
+ LOG.info("Flavor '%s' was created.", self.config.flavor_type)
if extra_specs:
self.flavor_type['flavor'].set_keys(extra_specs)
def __delete_flavor(self, flavor):
if self.comp.delete_flavor(flavor=flavor):
- LOG.info("Flavor '%s' deleted" % self.config.flavor_type)
+ LOG.info("Flavor '%s' deleted", self.config.flavor_type)
self.flavor_type = {'is_reuse': False, 'flavor': None}
else:
- LOG.error('Unable to delete flavor: %s' % self.config.flavor_type)
+ LOG.error('Unable to delete flavor: %s', self.config.flavor_type)
def get_config_file(self, chain_index, src_mac, dst_mac):
boot_script_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
with open(boot_script_file, 'r') as boot_script:
content = boot_script.read()
- g1cidr = self.config.generator_config.src_device.gateway_ip_list[chain_index] + '/8'
- g2cidr = self.config.generator_config.dst_device.gateway_ip_list[chain_index] + '/8'
+ g1cidr = self.config.generator_config.src_device.get_gw_ip(chain_index) + '/8'
+ g2cidr = self.config.generator_config.dst_device.get_gw_ip(chain_index) + '/8'
vm_config = {
'forwarder': self.config.vm_forwarder,
"""
Disable security at port level.
"""
- vm_ids = map(lambda vm: vm.id, self.vms)
+ vm_ids = [vm.id for vm in self.vms]
for net in self.nets:
for port in self.ports[net['id']]:
if port['device_id'] in vm_ids:
'port_security_enabled': False,
}
})
- LOG.info('Security disabled on port {}'.format(port['id']))
+ LOG.info('Security disabled on port %s', port['id'])
def get_loop_vm_hostnames(self):
return [getattr(vm, 'OS-EXT-SRV-ATTR:hypervisor_hostname') for vm in self.vms]
if not self.host_ips:
# get the hypervisor object from the host name
self.host_ips = [self.comp.get_hypervisor(
- getattr(vm, 'OS-EXT-SRV-ATTR:hypervisor_hostname')).host_ip
- for vm in self.vms]
+ getattr(vm, 'OS-EXT-SRV-ATTR:hypervisor_hostname')).host_ip for vm in self.vms]
return self.host_ips
def get_loop_vm_compute_nodes(self):
flavor_id=self.flavor_type['flavor'].id)
if servers:
server = servers[0]
- LOG.info('Reusing existing server: ' + name)
+ LOG.info('Reusing existing server: %s', name)
setattr(server, 'is_reuse', True)
return server
- else:
- return None
+ return None
def get_networks_uuids(self):
"""
"""
vlans = []
for net in self.nets:
- assert(net['provider:network_type'] == 'vlan')
+ assert net['provider:network_type'] == 'vlan'
vlans.append(net['provider:segmentation_id'])
return vlans
if not getattr(vm, 'is_reuse', True):
self.comp.delete_server(vm)
else:
- LOG.info('Server %s not removed since it is reused' % vm.name)
+ LOG.info('Server %s not removed since it is reused', vm.name)
for port in self.created_ports:
self.__delete_port(port)
if 'is_reuse' in net and not net['is_reuse']:
self.__delete_net(net)
else:
- LOG.info('Network %s not removed since it is reused' % (net['name']))
+ LOG.info('Network %s not removed since it is reused', net['name'])
if not self.flavor_type['is_reuse']:
self.__delete_flavor(self.flavor_type['flavor'])
class EXTStageClient(BasicStageClient):
-
- def __init__(self, config, cred):
- super(EXTStageClient, self).__init__(config, cred)
-
def setup(self):
super(EXTStageClient, self).setup()
class PVPStageClient(BasicStageClient):
-
- def __init__(self, config, cred):
- super(PVPStageClient, self).__init__(config, cred)
-
def get_end_port_macs(self):
- vm_ids = map(lambda vm: vm.id, self.vms)
+ vm_ids = [vm.id for vm in self.vms]
port_macs = []
- for index, net in enumerate(self.nets):
+ for _index, net in enumerate(self.nets):
vm_mac_map = {port['device_id']: port['mac_address'] for port in self.ports[net['id']]}
port_macs.append([vm_mac_map[vm_id] for vm_id in vm_ids])
return port_macs
class PVVPStageClient(BasicStageClient):
-
- def __init__(self, config, cred):
- super(PVVPStageClient, self).__init__(config, cred)
-
def get_end_port_macs(self):
port_macs = []
for index, net in enumerate(self.nets[:2]):
- vm_ids = map(lambda vm: vm.id, self.vms[index::2])
+ vm_ids = [vm.id for vm in self.vms[index::2]]
vm_mac_map = {port['device_id']: port['mac_address'] for port in self.ports[net['id']]}
port_macs.append([vm_mac_map[vm_id] for vm_id in vm_ids])
return port_macs