From fc7e57b778b48eb28662c0fe85e883034d67042b Mon Sep 17 00:00:00 2001
From: Marco Capitani <m.capitani@nextworks.it>
Date: Wed, 4 Apr 2018 11:00:38 +0200
Subject: qemu: fix failure when there is no id_rsa.pub

Added parameter for passing key path (in case it is not standard)
and exception handling, printing only a warning.

Fixes #48
---
 rumba/testbeds/qemu.py | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/rumba/testbeds/qemu.py b/rumba/testbeds/qemu.py
index 218f3e1..fab1726 100644
--- a/rumba/testbeds/qemu.py
+++ b/rumba/testbeds/qemu.py
@@ -35,6 +35,7 @@ import rumba.log as log
 import rumba.multiprocess as m_processing
 
 from rumba.executors.ssh import SSHExecutor
+from rumba.ssh_support import SSHException
 
 if sys.version_info[0] >= 3:
     from urllib.request import urlretrieve
@@ -44,6 +45,8 @@ else:
 
 logger = log.get_logger(__name__)
 
+USER_HOME = os.path.expanduser("~")
+
 
 class Testbed(mod.Testbed):
     """
@@ -51,6 +54,7 @@ class Testbed(mod.Testbed):
     """
     def __init__(self, exp_name, bzimage_path=None, initramfs_path=None,
                  proj_name="ARCFIRE", password="root", username="root",
+                 public_key_path=os.path.join(USER_HOME, '.ssh', 'id_rsa.pub'),
                  use_vhost=True):
         """
         Initializes the testbed class.
@@ -61,6 +65,10 @@ class Testbed(mod.Testbed):
         :param proj_name: Project name of the experiment.
         :param password: Password of the user.
         :param username: User of the VM.
+        :param public_key_path: path to the public key used to connect via
+                                ssh to the virtual machines.
+                                If `None`, password auth (root, root)
+                                will be used.
         :param use_vhost: Use virtual hosting or not?
 
         .. note:: In case no bzimage or initramfs is provided, Rumba
@@ -77,6 +85,7 @@ class Testbed(mod.Testbed):
         self.bzimage_path = bzimage_path
         self.initramfs_path = initramfs_path
         self.multiproc_manager = None
+        self.key_path = public_key_path
 
         self.executor = SSHExecutor(self)
 
@@ -355,15 +364,20 @@ class Testbed(mod.Testbed):
 
         self._recover_if_names(experiment)
 
-        for node in experiment.nodes:
-            pub_key = os.path.join(os.path.expanduser("~"),
-                                   '.ssh',
-                                   'id_rsa.pub')
-            node.copy_file(pub_key, '/root/.ssh')
-            node.execute_command('cd /root/.ssh; '
-                                 'mv authorized_keys old.authorized_keys; '
-                                 'cat old.authorized_keys id_rsa.pub '
-                                 '> authorized_keys')
+        if self.key_path is not None:
+            for node in experiment.nodes:
+                try:
+                    node.copy_file(self.key_path, '/root/.ssh')
+                    node.execute_command(
+                        'cd /root/.ssh; '
+                        'mv authorized_keys old.authorized_keys; '
+                        'cat old.authorized_keys id_rsa.pub '
+                        '> authorized_keys'
+                    )
+                except SSHException as e:
+                    logger.warning("Could not install ssh key into node %s. "
+                                   "%s.", node.name, str(e))
+                    logger.debug("Exception details:", exc_info=e)
 
         logger.info('Experiment has been successfully swapped in.')
 
-- 
cgit v1.2.3