]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
orchestra: add remote.sh commands analogous to misc.sh 1233/head
authorKyr Shatskyy <kyrylo.shatskyy@gmail.com>
Tue, 30 Oct 2018 13:17:05 +0000 (14:17 +0100)
committerKyr Shatskyy <kyrylo.shatskyy@gmail.com>
Tue, 30 Oct 2018 13:41:58 +0000 (14:41 +0100)
Adds a remote.sh similar to misc.sh, in fact a shortcut for remote.run,
but return output instead of proc
Example:
    my_name = Remote('127.0.0.1').sh('whoami')

Adds a remote.sh_file run a script as file on a remote with or without sudo
Example 1: Run python script
    Remote('127.0.0.1').sh_file("#!/usr/bin/env python3\n"
                                "import sys\n"
                                "print(sys.version_info)")
Example 2: Run script as root
    Remote('user@host.domain').sh_file("whoami", sudo=True,
                                       label="who-am-i-for-the-real")
Example 3: Run script as other user
    Remote('user@host.domain').sh_file("whoami", sudo='nobody', )

Signed-off-by: Kyr Shatskyy <kyrylo.shatskyy@suse.de>
teuthology/orchestra/remote.py

index a93a9a208bd2fba005824ae7255986de187dd391..4ed73e6d0959f0444036f465779e75213205a407 100644 (file)
@@ -218,6 +218,47 @@ class Remote(object):
         data = proc.stdout.getvalue()
         return data
 
+    def sh(self, script, **kwargs):
+        """
+        Shortcut for run method.
+
+        Usage:
+            my_name = remote.sh('whoami')
+            remote_date = remote.sh('date')
+        """
+        if 'stdout' not in kwargs:
+            kwargs['stdout'] = StringIO()
+        if 'args' not in kwargs:
+            kwargs['args'] = script
+        proc=self.run(**kwargs)
+        return proc.stdout.getvalue()
+
+
+    def sh_file(self, script, label="script", sudo=False, **kwargs):
+        """
+        Run shell script after copying its contents to a remote file
+
+        :param script:  string with script text, or file object
+        :param sudo:    run command with sudo if True,
+                        run as user name if string value (defaults to False)
+        :param label:   string value which will be part of file name
+        Returns: stdout
+        """
+        ftempl = '/tmp/teuthology-remote-$(date +%Y%m%d%H%M%S)-{}-XXXX'\
+                 .format(label)
+        script_file = self.sh("mktemp %s" % ftempl, stdout=StringIO()).strip()
+        self.sh("cat - | tee {script} ; chmod a+rx {script}"\
+            .format(script=script_file), stdin=script)
+        if sudo:
+            if isinstance(sudo, str):
+                command="sudo -u %s %s" % (sudo, script_file)
+            else:
+                command="sudo %s" % script_file
+        else:
+            command="%s" % script_file
+
+        return self.sh(command, **kwargs)
+
     def chmod(self, file_path, permissions):
         """
         As super-user, set permissions on the remote file specified.