From: Kyr Shatskyy Date: Tue, 30 Oct 2018 13:17:05 +0000 (+0100) Subject: orchestra: add remote.sh commands analogous to misc.sh X-Git-Tag: 1.1.0~292^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F1233%2Fhead;p=teuthology.git orchestra: add remote.sh commands analogous to misc.sh 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 --- diff --git a/teuthology/orchestra/remote.py b/teuthology/orchestra/remote.py index a93a9a20..4ed73e6d 100644 --- a/teuthology/orchestra/remote.py +++ b/teuthology/orchestra/remote.py @@ -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.