Skip to content

Commit 53c3d73

Browse files
author
Connor Fawcett
committed
qa/vstart_runner: Add get_file and write_file equivalents to the LocalRemoteProcess class
Signed-off-by: Connor Fawcett <[email protected]>
1 parent 202d87a commit 53c3d73

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

qa/tasks/vstart_runner.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,20 @@ def hostname(self):
354354
self._hostname = 'localhost'
355355
return self._hostname
356356

357-
def get_file(self, path, sudo, dest_dir):
358-
tmpfile = tempfile.NamedTemporaryFile(delete=False).name
359-
shutil.copy(path, tmpfile)
360-
return tmpfile
357+
def get_file(self, path, sudo=False, dest_dir='/tmp'):
358+
if dest_dir == '/tmp':
359+
# If we're storing in /tmp, generate a unique filename
360+
(_fd, local_path) = tempfile.mkstemp(dir=dest_dir)
361+
else:
362+
# If we are storing somewhere other than /tmp, use the original
363+
# filename
364+
local_path = os.path.join(dest_dir, path.split(os.path.sep)[-1])
365+
try:
366+
shutil.copy(path, local_path)
367+
except shutil.SameFileError:
368+
log.info("path and local_path refer to the same file")
369+
pass
370+
return local_path
361371

362372
# XXX: This method ignores the error raised when src and dst are
363373
# holding same path. For teuthology, same path still represents
@@ -368,6 +378,26 @@ def put_file(self, src, dst, sudo=False):
368378
except shutil.SameFileError:
369379
pass
370380

381+
def write_file(self, path, data, owner=None,
382+
mode='0644', mkdir=False, append=False, sudo=False):
383+
dd = 'sudo dd' if sudo else 'dd'
384+
args = dd + ' of=' + path
385+
if append:
386+
args += ' conv=notrunc oflag=append'
387+
if mkdir:
388+
mkdirp = 'sudo mkdir -p' if sudo else 'mkdir -p'
389+
dirpath = os.path.dirname(path)
390+
if dirpath:
391+
args = mkdirp + ' ' + dirpath + '\n' + args
392+
if mode:
393+
chmod = 'sudo chmod' if sudo else 'chmod'
394+
args += '\n' + chmod + ' ' + mode + ' ' + path
395+
if owner:
396+
chown = 'sudo chown' if sudo else 'chown'
397+
args += '\n' + chown + ' ' + owner + ' ' + path
398+
args = 'set -ex' + '\n' + args
399+
self.run(args=args, stdin=data, quiet=True)
400+
371401
def _expand_teuthology_tools(self, args):
372402
assert isinstance(args, list)
373403
for tool in self.rewrite_helper_tools:

0 commit comments

Comments
 (0)