X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=nfvbench%2Fcompute.py;h=575744c7dab135dccc96b5fc9dcecd6100716d7c;hb=8da439b932537748d379c7bd3bdf560ef739b203;hp=681a852e6e6c5d685a3806e7ccc11e9bc424a8ab;hpb=1fe5e7adb3e1d90910c18386ed64d0a787a7bf94;p=nfvbench.git diff --git a/nfvbench/compute.py b/nfvbench/compute.py index 681a852..575744c 100644 --- a/nfvbench/compute.py +++ b/nfvbench/compute.py @@ -11,16 +11,16 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +"""Module for Openstack compute operations""" +import os +import time +import traceback -'''Module for Openstack compute operations''' -from glanceclient import exc as glance_exception import keystoneauth1 from log import LOG import novaclient -import os -import time -import traceback +from glanceclient import exc as glance_exception try: from glanceclient.openstack.common.apiclient.exceptions import NotFound as GlanceImageNotFound @@ -29,7 +29,6 @@ except ImportError: class Compute(object): - def __init__(self, nova_client, glance_client, neutron_client, config): self.novaclient = nova_client self.glance_client = glance_client @@ -51,9 +50,7 @@ class Compute(object): retry = 0 try: # check image is file/url based. - file_prefix = "file://" - image_location = image_file.split(file_prefix)[1] - with open(image_location) as f_image: + with open(image_file) as f_image: img = self.glance_client.images.create(name=str(final_image_name), disk_format="qcow2", container_format="bare", @@ -82,8 +79,7 @@ class Compute(object): return False except Exception: LOG.error(traceback.format_exc()) - LOG.error("Failed while uploading the image, please make sure the " - "cloud under test has the access to file: %s.", image_file) + LOG.error("Failed to upload image %s.", image_file) return False return True @@ -140,10 +136,9 @@ class Compute(object): ''' if ssh_access.public_key_file: return self.add_public_key(kp_name, ssh_access.public_key_file) - else: - keypair = self.create_keypair(kp_name, None) - ssh_access.private_key = keypair.private_key - return keypair + keypair = self.create_keypair(kp_name, None) + ssh_access.private_key = keypair.private_key + return keypair def find_network(self, label): net = self.novaclient.networks.find(label=label) @@ -293,7 +288,7 @@ class Compute(object): if hyp.zone == zone: # matches return az_host - # else continue - another zone with same host name? + # else continue - another zone with same host name? # no match LOG.error('No match for availability zone and host ' + az_host) return None @@ -378,16 +373,15 @@ class Compute(object): LOG.warning('Operation Forbidden: could not retrieve list of hypervisors' ' (likely no permission)') - hypervisor_list = filter(lambda h: h.status == 'enabled' and h.state == 'up', - hypervisor_list) + hypervisor_list = [h for h in hypervisor_list if h.status == 'enabled' and h.state == 'up'] if self.config.availability_zone: - host_list = filter(lambda h: h.zone == self.config.availability_zone, host_list) + host_list = [h for h in host_list if h.zone == self.config.availability_zone] if self.config.compute_nodes: - host_list = filter(lambda h: h.host in self.config.compute_nodes, host_list) + host_list = [h for h in host_list if h.host in self.config.compute_nodes] hosts = [h.hypervisor_hostname for h in hypervisor_list] - host_list = filter(lambda h: h.host in hosts, host_list) + host_list = [h for h in host_list if h.host in hosts] avail_list = [] for host in host_list: @@ -411,10 +405,7 @@ class Compute(object): try: server_instance_1 = self.novaclient.servers.get(vm_instance1) server_instance_2 = self.novaclient.servers.get(vm_instance2) - if server_instance_1.hostId == server_instance_2.hostId: - return True - else: - return False + return bool(server_instance_1.hostId == server_instance_2.hostId) except novaclient.exceptions: LOG.warning("Exception in retrieving the hostId of servers") @@ -423,7 +414,7 @@ class Compute(object): # check first the security group exists sec_groups = self.neutronclient.list_security_groups()['security_groups'] group = [x for x in sec_groups if x['name'] == self.config.security_group_name] - if len(group) > 0: + if group: return group[0] body = {