From 53c3d7336835af1cf71bab8841c366489e54d8ef Mon Sep 17 00:00:00 2001 From: Connor Fawcett Date: Tue, 26 Nov 2024 04:01:05 +0000 Subject: [PATCH] qa/vstart_runner: Add get_file and write_file equivalents to the LocalRemoteProcess class Signed-off-by: Connor Fawcett --- qa/tasks/vstart_runner.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/qa/tasks/vstart_runner.py b/qa/tasks/vstart_runner.py index 2ed2143133064..755ea642d95da 100644 --- a/qa/tasks/vstart_runner.py +++ b/qa/tasks/vstart_runner.py @@ -354,10 +354,20 @@ class LocalRemote(RemoteShell): self._hostname = 'localhost' return self._hostname - def get_file(self, path, sudo, dest_dir): - tmpfile = tempfile.NamedTemporaryFile(delete=False).name - shutil.copy(path, tmpfile) - return tmpfile + def get_file(self, path, sudo=False, dest_dir='/tmp'): + if dest_dir == '/tmp': + # If we're storing in /tmp, generate a unique filename + (_fd, local_path) = tempfile.mkstemp(dir=dest_dir) + else: + # If we are storing somewhere other than /tmp, use the original + # filename + local_path = os.path.join(dest_dir, path.split(os.path.sep)[-1]) + try: + shutil.copy(path, local_path) + except shutil.SameFileError: + log.info("path and local_path refer to the same file") + pass + return local_path # XXX: This method ignores the error raised when src and dst are # holding same path. For teuthology, same path still represents @@ -368,6 +378,26 @@ class LocalRemote(RemoteShell): except shutil.SameFileError: pass + def write_file(self, path, data, owner=None, + mode='0644', mkdir=False, append=False, sudo=False): + dd = 'sudo dd' if sudo else 'dd' + args = dd + ' of=' + path + if append: + args += ' conv=notrunc oflag=append' + if mkdir: + mkdirp = 'sudo mkdir -p' if sudo else 'mkdir -p' + dirpath = os.path.dirname(path) + if dirpath: + args = mkdirp + ' ' + dirpath + '\n' + args + if mode: + chmod = 'sudo chmod' if sudo else 'chmod' + args += '\n' + chmod + ' ' + mode + ' ' + path + if owner: + chown = 'sudo chown' if sudo else 'chown' + args += '\n' + chown + ' ' + owner + ' ' + path + args = 'set -ex' + '\n' + args + self.run(args=args, stdin=data, quiet=True) + def _expand_teuthology_tools(self, args): assert isinstance(args, list) for tool in self.rewrite_helper_tools: -- 2.39.5