public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: Uladzimir Bely <ubely@ilbers.de>
To: isar-users@googlegroups.com
Subject: [PATCH 1/4] cibuilder.py: Refactor vm_start related functions
Date: Wed, 24 May 2023 04:45:11 +0200	[thread overview]
Message-ID: <20230524024514.17042-2-ubely@ilbers.de> (raw)
In-Reply-To: <20230524024514.17042-1-ubely@ilbers.de>

get_ssh_cmd_prefix: Add possibility to specify user and hostname
instead of default `ci@localhost`. This allows to run test on hardware
targets by their IP address or hostname.

prepare_priv_key: Copy private SSH key to build directory before use
since it's original location may be readonly.

remote_run: carve out ssh-related code from vm_start function.

Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
---
 testsuite/cibuilder.py | 67 ++++++++++++++++++++++++++----------------
 1 file changed, 41 insertions(+), 26 deletions(-)

diff --git a/testsuite/cibuilder.py b/testsuite/cibuilder.py
index 6aac2279..a4edb578 100755
--- a/testsuite/cibuilder.py
+++ b/testsuite/cibuilder.py
@@ -226,14 +226,12 @@ class CIBuilder(Test):
 
         return env['LAYERDIR_' + layer].strip('"')
 
-    def get_ssh_cmd_prefix(self, port, priv_key):
-        port_args = ''
-        if port:
-            port_args = ' -p ' + str(port)
+    def get_ssh_cmd_prefix(self, user, host, port, priv_key):
+        cmd_prefix = 'ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no '\
+                     '-p %s -o IdentityFile=%s %s@%s ' \
+                     % (port, priv_key, user, host)
 
-        cmd_prefix = 'ssh' + port_args + \
-                     ' -o ConnectTimeout=5 -o IdentityFile=' + priv_key + \
-                     ' -o StrictHostKeyChecking=no ci@localhost '
+        self.log.debug('Connect command:\n' + cmd_prefix)
 
         return cmd_prefix
 
@@ -258,6 +256,9 @@ class CIBuilder(Test):
 
 
     def wait_connection(self, proc, cmd_prefix, timeout):
+        if proc is None:
+            return 0
+
         self.log.debug('Waiting for SSH server ready...')
 
         rc = None
@@ -281,6 +282,35 @@ class CIBuilder(Test):
         return rc
 
 
+    def prepare_priv_key(self):
+        # copy private key to build directory (that is writable)
+        priv_key = '%s/ci_priv_key' % self.build_dir
+        if not os.path.exists(priv_key):
+            shutil.copy(os.path.dirname(__file__) + '/keys/ssh/id_rsa', priv_key)
+        os.chmod(priv_key, 0o400)
+
+        return priv_key
+
+
+    def remote_run(self, user, host, port, cmd, script, proc=None, timeout=0):
+        priv_key = self.prepare_priv_key()
+        cmd_prefix = self.get_ssh_cmd_prefix(user, host, port, priv_key)
+
+        rc = self.wait_connection(proc, cmd_prefix, timeout)
+
+        if rc == 0:
+            if cmd is not None:
+                rc = self.exec_cmd(cmd, cmd_prefix)
+                self.log.debug('`' + cmd + '` returned ' + str(rc))
+            elif script is not None:
+                rc = self.run_script(script, cmd_prefix)
+                self.log.debug('`' + script + '` returned ' + str(rc))
+            else:
+                return None
+
+        return rc
+
+
     def vm_start(self, arch='amd64', distro='buster',
                  enforce_pcbios=False, skip_modulecheck=False,
                  image='isar-image-base', cmd=None, script=None):
@@ -342,32 +372,17 @@ class CIBuilder(Test):
                               universal_newlines=True)
 
         if cmd is not None or script is not None:
-            rc = None
             try:
-                port = None
+                user='ci'
+                host='localhost'
+                port = 22
                 for arg in cmdline:
                     match = re.match(r".*hostfwd=tcp::(\d*).*", arg)
                     if match:
                         port = match.group(1)
                         break
 
-                # copy private key to build directory
-                priv_key = '%s/ci_priv_key' % self.build_dir
-                if not os.path.exists(priv_key):
-                    shutil.copy(os.path.dirname(__file__) + '/keys/ssh/id_rsa', priv_key)
-                    os.chmod(priv_key, 0o400)
-
-                cmd_prefix = self.get_ssh_cmd_prefix(port, priv_key)
-                self.log.debug('Connect command:\n' + cmd_prefix)
-                rc = self.wait_connection(p1, cmd_prefix, timeout)
-
-                if rc == 0:
-                    if cmd is not None:
-                        rc = self.exec_cmd(cmd, cmd_prefix)
-                        self.log.debug('`' + cmd + '` returned ' + str(rc))
-                    elif script is not None:
-                        rc = self.run_script(script, cmd_prefix)
-                        self.log.debug('`' + script + '` returned ' + str(rc))
+                rc = self.remote_run(user, host, port, cmd, script, p1, timeout)
 
             finally:
                 if p1.poll() is None:
-- 
2.20.1


  reply	other threads:[~2023-05-24  2:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-24  2:45 [PATCH 0/4] Basic hardware testing support Uladzimir Bely
2023-05-24  2:45 ` Uladzimir Bely [this message]
2023-05-24  2:45 ` [PATCH 2/4] testsuite: Add an interface to run commands over ssh Uladzimir Bely
2023-05-24  2:45 ` [PATCH 3/4] testsuite: Separate common part of kas-based test example Uladzimir Bely
2023-05-24  2:45 ` [PATCH 4/4] testsute: Provide an example of hardware test Uladzimir Bely
2023-05-24  3:06 ` [PATCH 0/4] Basic hardware testing support MOESSBAUER, Felix
2023-05-24  4:30   ` Florian Bezdeka
2023-05-24 11:37     ` Jan Kiszka
2023-05-31 21:01 ` Uladzimir Bely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230524024514.17042-2-ubely@ilbers.de \
    --to=ubely@ilbers.de \
    --cc=isar-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox