From: Melissa Date: Tue, 20 Jul 2021 21:22:00 +0000 (-0400) Subject: mgr/cephadm: create async function _write_remote_file to write files on remote host X-Git-Tag: v17.1.0~1051^2~11 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=be6673d1d7c7b1198f19d70fb83fe96fe4a22566;p=ceph.git mgr/cephadm: create async function _write_remote_file to write files on remote host _write_remote_file uses _check_execute_command in ssh.py which calls _execute_command which uses shlex quote. Thus, any commands with an int will need to be transformed into a str because shlex quote does not take int objects Fixes: https://tracker.ceph.com/issues/44676 Signed-off-by: Melissa Li --- diff --git a/src/pybind/mgr/cephadm/ssh.py b/src/pybind/mgr/cephadm/ssh.py index 9ccfb3064a5c..221d854c1dc8 100644 --- a/src/pybind/mgr/cephadm/ssh.py +++ b/src/pybind/mgr/cephadm/ssh.py @@ -1,4 +1,5 @@ import logging +import os from contextlib import contextmanager from io import StringIO from shlex import quote @@ -126,3 +127,29 @@ class SSHManager: raise OrchestratorError(msg) return out + async def _write_remote_file(self, + host: str, + path: str, + content: bytes, + mode: Optional[int] = None, + uid: Optional[int] = None, + gid: Optional[int] = None, + addr: Optional[str] = None, + **kwargs: Any, + ) -> None: + try: + dirname = os.path.dirname(path) + await self._check_execute_command(host, ['mkdir', '-p', dirname], addr=addr) + tmp_path = path + '.new' + await self._check_execute_command(host, ['touch', tmp_path], addr=addr) + if uid is not None and gid is not None and mode is not None: + # shlex quote takes str or byte object, not int + await self._check_execute_command(host, ['chown', '-R', str(uid) + ':' + str(gid), tmp_path], addr=addr) + await self._check_execute_command(host, ['chmod', oct(mode)[2:], tmp_path], addr=addr) + await self._check_execute_command(host, ['tee', '-', tmp_path], stdin=content, addr=addr) + await self._check_execute_command(host, ['mv', tmp_path, path], addr=addr) + except Exception as e: + msg = f"Unable to write {host}:{path}: {e}" + logger.exception(msg) + raise OrchestratorError(msg) +