CSIT-441 vhost - Parametrized qemu install
[csit.git] / resources / libraries / python / QemuUtils.py
index 675f074..244ece2 100644 (file)
@@ -26,10 +26,10 @@ from resources.libraries.python.topology import NodeType
 class QemuUtils(object):
     """QEMU utilities."""
 
-    __QEMU_BIN = '/usr/bin/qemu-system-x86_64'
-
     def __init__(self, qemu_id=1):
         self._qemu_id = qemu_id
+        # Path to QEMU binary
+        self._qemu_bin = '/usr/bin/qemu-system-x86_64'
         # QEMU Machine Protocol socket
         self._qmp_sock = '/tmp/qmp{0}.sock'.format(self._qemu_id)
         # QEMU Guest Agent socket
@@ -69,8 +69,16 @@ class QemuUtils(object):
         self._node = None
         self._socks = [self._qmp_sock, self._qga_sock]
 
+    def qemu_set_bin(self, path):
+        """Set binary path for QEMU.
+
+        :param path: Absolute path in filesystem.
+        :type path: str
+        """
+        self._qemu_bin = path
+
     def qemu_set_smp(self, cpus, cores, threads, sockets):
-        """Set SMP option for QEMU
+        """Set SMP option for QEMU.
 
         :param cpus: Number of CPUs.
         :param cores: Number of CPU cores on one socket.
@@ -520,7 +528,7 @@ class QemuUtils(object):
 
         # Run QEMU
         cmd = '{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}'.format(
-            self.__QEMU_BIN, self._qemu_opt.get('smp'), mem, ssh_fwd,
+            self._qemu_bin, self._qemu_opt.get('smp'), mem, ssh_fwd,
             self._qemu_opt.get('options'),
             drive, qmp, serial, qga, graphic)
         (ret_code, _, stderr) = self._ssh.exec_command_sudo(cmd, timeout=300)
@@ -620,19 +628,31 @@ class QemuUtils(object):
                 'error: {1}'.format(self._node['host'], json.dumps(err)))
 
     @staticmethod
-    def build_qemu(node):
+    def build_qemu(node, force_install=False, apply_patch=False):
         """Build QEMU from sources.
 
         :param node: Node to build QEMU on.
+        :param force_install: If True, then remove previous build.
+        :param apply_patch: If True, then apply patches from qemu_patches dir.
         :type node: dict
+        :type force_install: bool
+        :type apply_patch: bool
+        :raises: RuntimeError if building QEMU failed.
         """
         ssh = SSH()
         ssh.connect(node)
 
+        directory = ' --directory={0}'.format(Constants.QEMU_INSTALL_DIR)
+        version = ' --version={0}'.format(Constants.QEMU_INSTALL_VERSION)
+        force = ' --force' if force_install else ''
+        patch = ' --patch' if apply_patch else ''
+
         (ret_code, stdout, stderr) = \
-            ssh.exec_command('sudo -Sn bash {0}/{1}/qemu_build.sh'.format(
-                Constants.REMOTE_FW_DIR, Constants.RESOURCES_LIB_SH), 1000)
-        logger.trace(stdout)
+            ssh.exec_command(
+                "sudo -E sh -c '{0}/{1}/qemu_build.sh{2}{3}{4}{5}'"\
+                .format(Constants.REMOTE_FW_DIR, Constants.RESOURCES_LIB_SH,
+                        version, directory, force, patch), 1000)
+
         if int(ret_code) != 0:
-            logger.debug('QEMU build failed {0}'.format(stderr))
+            logger.debug('QEMU build failed {0}'.format(stdout + stderr))
             raise RuntimeError('QEMU build failed on {0}'.format(node['host']))