NFVBENCH-27 Search vm image under project folder
[nfvbench.git] / nfvbench / chain_clients.py
index bf51552..c6df08a 100644 (file)
@@ -20,6 +20,7 @@ from log import LOG
 from neutronclient.neutron import client as neutronclient
 from novaclient.client import Client
 import os
+import re
 import time
 
 
@@ -35,6 +36,7 @@ class BasicStageClient(object):
     def __init__(self, config, cred):
         self.comp = None
         self.image_instance = None
+        self.image_name = None
         self.config = config
         self.cred = cred
         self.nets = []
@@ -229,25 +231,45 @@ class BasicStageClient(object):
         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 = '(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()
 
@@ -381,7 +403,7 @@ class BasicStageClient(object):
         """
         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
@@ -419,7 +441,6 @@ class BasicStageClient(object):
 
 
 class EXTStageClient(BasicStageClient):
-
     def __init__(self, config, cred):
         super(EXTStageClient, self).__init__(config, cred)
 
@@ -436,7 +457,6 @@ class EXTStageClient(BasicStageClient):
 
 
 class PVPStageClient(BasicStageClient):
-
     def __init__(self, config, cred):
         super(PVPStageClient, self).__init__(config, cred)
 
@@ -480,7 +500,6 @@ class PVPStageClient(BasicStageClient):
 
 
 class PVVPStageClient(BasicStageClient):
-
     def __init__(self, config, cred):
         super(PVVPStageClient, self).__init__(config, cred)