From 412b596b5965a1caf897242b338e18be6916c356 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Wed, 18 Dec 2019 10:02:23 -0600 Subject: [PATCH] mgr/cephadm: try various pythons for root mode _run_cephadm Some distros don't have the /usr/bin/python link. Signed-off-by: Sage Weil --- src/pybind/mgr/cephadm/module.py | 40 +++++++++++++++++++++---------- src/pybind/mgr/cephadm/remotes.py | 18 ++++++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index 0649736cbe12c..a1e9cea1bc05b 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -582,9 +582,9 @@ class CephadmOrchestrator(MgrModule, orchestrator.Orchestrator): logger=self.log.getChild(n), ssh_options=self._ssh_options) - conn.import_module(remotes) + r = conn.import_module(remotes) - return conn + return conn, r def _executable_path(self, conn, executable): """ @@ -610,7 +610,7 @@ class CephadmOrchestrator(MgrModule, orchestrator.Orchestrator): """ Run cephadm on the remote host with the given command + args """ - conn = self._get_connection(host) + conn, connr = self._get_connection(host) try: if not image: @@ -632,22 +632,36 @@ class CephadmOrchestrator(MgrModule, orchestrator.Orchestrator): final_args += args if self.mode == 'root': - self.log.debug('args: %s' % final_args) + self.log.debug('args: %s' % (' '.join(final_args))) self.log.debug('stdin: %s' % stdin) script = 'injected_argv = ' + json.dumps(final_args) + '\n' if stdin: script += 'injected_stdin = ' + json.dumps(stdin) + '\n' script += self._cephadm - out, err, code = remoto.process.check( - conn, - ['/usr/bin/python', '-u'], - stdin=script.encode('utf-8')) + python = connr.choose_python() + if not python: + raise RuntimeError( + 'unable to find python on %s (tried %s in %s)' % ( + host, remotes.PYTHONS, remotes.PATH)) + try: + out, err, code = remoto.process.check( + conn, + [python, '-u'], + stdin=script.encode('utf-8')) + except RuntimeError as e: + if error_ok: + return '', str(e), 1 + raise elif self.mode == 'cephadm-package': - out, err, code = remoto.process.check( - conn, - ['sudo', '/usr/bin/cephadm'] + final_args, - stdin=stdin) - self.log.debug('exit code %s out %s err %s' % (code, out, err)) + try: + out, err, code = remoto.process.check( + conn, + ['sudo', '/usr/bin/cephadm'] + final_args, + stdin=stdin) + except RuntimeError as e: + if error_ok: + return '', str(e), 1 + raise if code and not error_ok: raise RuntimeError( 'cephadm exited with an error code: %d, stderr:%s' % ( diff --git a/src/pybind/mgr/cephadm/remotes.py b/src/pybind/mgr/cephadm/remotes.py index 94f978dd2c0f2..aad5481c62948 100644 --- a/src/pybind/mgr/cephadm/remotes.py +++ b/src/pybind/mgr/cephadm/remotes.py @@ -4,6 +4,24 @@ import errno import tempfile import shutil +PYTHONS = ['python3', 'python2', 'python'] +PATH = [ + '/usr/bin', + '/usr/local/bin', + '/bin', + '/usr/sbin', + '/usr/local/sbin', + '/sbin', +] + +def choose_python(): + for e in PYTHONS: + for b in PATH: + p = os.path.join(b, e) + if os.path.exists(p): + return p + return None + def safe_makedirs(path, uid=-1, gid=-1): """ create path recursively if it doesn't exist """ try: -- 2.39.5