From 1426061815d5d9d660697d8c115cd143dcfacd71 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Mon, 22 Nov 2021 12:02:16 -0800 Subject: [PATCH] mgr/rgw: use tool_exec instead of directly spawning commands Signed-off-by: Yehuda Sadeh --- src/pybind/mgr/rgw/module.py | 24 +++++++------------ src/python-common/ceph/rgw/rgwam_core.py | 29 ++++++++--------------- src/python-common/ceph/rgw/types.py | 4 ++++ src/rgw/rgwam.py | 30 +++++++++++++++++++----- 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/pybind/mgr/rgw/module.py b/src/pybind/mgr/rgw/module.py index 325629c691e..c8b7a59bbf4 100644 --- a/src/pybind/mgr/rgw/module.py +++ b/src/pybind/mgr/rgw/module.py @@ -19,6 +19,11 @@ class RGWAMOrchMgr(RGWAMEnvMgr): def __init__(self, mgr): self.mgr = mgr + def tool_exec(self, prog, args): + cmd = [ prog ] + args + rc, stdout, stderr = self.mgr.tool_exec(args = cmd) + return cmd, rc, stdout, stderr + def apply_rgw(self, svc_id, realm_name, zone_name, port = None): spec = RGWSpec(service_id = svc_id, rgw_realm = realm_name, @@ -52,10 +57,7 @@ class Module(orchestrator.OrchestratorClientMixin, MgrModule): with self.lock: self.inited = True - self.env = EnvArgs(RGWAMOrchMgr(self), - str(self.get_ceph_conf_path()), - f'mgr.{self.get_mgr_id()}', - str(self.get_ceph_option('keyring'))) + self.env = EnvArgs(RGWAMOrchMgr(self)) # set up some members to enable the serve() method and shutdown() self.run = True @@ -87,21 +89,13 @@ class Module(orchestrator.OrchestratorClientMixin, MgrModule): @CLICommand('rgw admin', perm='rw') def _cmd_rgw_admin(self, params: Sequence[str]): """rgw admin""" - run_cmd = [ './bin/radosgw-admin', - '-c', str(self.get_ceph_conf_path()), - '-k', str(self.get_ceph_option('keyring')), - '-n', f'mgr.{self.get_mgr_id()}' ] + (params or []) - - result = subprocess.run(run_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + cmd, returncode, out, err = self.env.mgr.tool_exec('radosgw-admin', params or []) - # TODO Extend export creation for rgw. - out = result.stdout.decode('utf-8') - err = result.stderr.decode('utf-8') - self.log.error('retcode=%d' % result.returncode) + self.log.error('retcode=%d' % returncode) self.log.error('out=%s' % out) self.log.error('err=%s' % err) - return HandleCommandResult(retval=result.returncode, stdout=out, stderr=err) + return HandleCommandResult(retval=returncode, stdout=out, stderr=err) @CLICommand('rgw realm bootstrap', perm='rw') def _cmd_rgw_realm_bootstrap(self, diff --git a/src/python-common/ceph/rgw/rgwam_core.py b/src/python-common/ceph/rgw/rgwam_core.py index 3a9df6f0b8c..99ff100a35f 100644 --- a/src/python-common/ceph/rgw/rgwam_core.py +++ b/src/python-common/ceph/rgw/rgwam_core.py @@ -60,11 +60,8 @@ def get_endpoints(endpoints, period = None): class EnvArgs: - def __init__(self, mgr, ceph_conf, ceph_name, ceph_keyring): + def __init__(self, mgr): self.mgr = mgr - self.ceph_conf = ceph_conf - self.ceph_name = ceph_name - self.ceph_keyring = ceph_keyring class EntityKey: def __init__(self, name = None, id = None): @@ -131,12 +128,10 @@ def opt_arg_bool(params, flag, arg): class RGWCmdBase: def __init__(self, prog, zone_env : ZoneEnv): - env = zone_env.env - self.cmd_prefix = [ prog ] + self.env = zone_env.env + self.mgr = self.env.mgr + self.prog = prog self.cmd_suffix = [ ] - opt_arg(self.cmd_prefix, '-c', env.ceph_conf ) - opt_arg(self.cmd_prefix, '-n', env.ceph_name ) - opt_arg(self.cmd_prefix, '-k', env.ceph_keyring ) if zone_env.realm: opt_arg(self.cmd_suffix, '--rgw-realm', zone_env.realm.name ) opt_arg(self.cmd_suffix, '--realm-id', zone_env.realm.id ) @@ -148,20 +143,16 @@ class RGWCmdBase: opt_arg(self.cmd_suffix, '--zone-id', zone_env.zone.id ) def run(self, cmd): - run_cmd = self.cmd_prefix + cmd + self.cmd_suffix - result = subprocess.run(run_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - - stdout = result.stdout.decode('utf-8') - stderr = result.stderr.decode('utf-8') + args = cmd + self.cmd_suffix + cmd, returncode, stdout, stderr = self.mgr.tool_exec(self.prog, args) log.debug('cmd=%s' % str(cmd)) - log.debug('stdout=%s' % stdout) - if result.returncode != 0: - cmd_str = ' '.join(run_cmd) - log.error('ERROR: command exited with error status (%d): %s\nstdout=%s\nstderr=%s' % (result.returncode, cmd_str, stdout, stderr)) - raise RGWAMCmdRunException(cmd_str, -result.returncode, stdout, stderr) + if returncode != 0: + cmd_str = ' '.join(cmd) + log.error('ERROR: command exited with error status (%d): %s\nstdout=%s\nstderr=%s' % (returncode, cmd_str, stdout, stderr)) + raise RGWAMCmdRunException(cmd_str, -returncode, stdout, stderr) return (stdout, stderr) diff --git a/src/python-common/ceph/rgw/types.py b/src/python-common/ceph/rgw/types.py index 688905cc5d1..f2c962e1092 100644 --- a/src/python-common/ceph/rgw/types.py +++ b/src/python-common/ceph/rgw/types.py @@ -23,6 +23,10 @@ class RGWAMCmdRunException(RGWAMException): self.stderr = stderr class RGWAMEnvMgr: + @abstractmethod + def tool_exec(self, prog, args): + pass + @abstractmethod def apply_rgw(self, svc_id, realm_name, zone_name, port = None): pass diff --git a/src/rgw/rgwam.py b/src/rgw/rgwam.py index c0017dc21e7..f07d2b423e7 100755 --- a/src/rgw/rgwam.py +++ b/src/rgw/rgwam.py @@ -22,8 +22,29 @@ from ceph.rgw.rgwam_core import RGWAM, EnvArgs from ceph.rgw.types import RGWAMEnvMgr, RGWAMException class RGWAMCLIMgr(RGWAMEnvMgr): - def __init__(self): - pass + def __init__(self, common_args): + args = [] + + if common_args.conf_path: + args += [ '-c', common_args.conf_path ] + + if common_args.ceph_name: + args += [ '-n', common_args.ceph_name ] + + if common_args.ceph_keyring: + args += [ '-k', common_args.ceph_keyring ] + + self.args_prefix = args + + def tool_exec(self, prog, args): + run_cmd = [ prog ] + self.args_prefix + args + + result = subprocess.run(run_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + stdout = result.stdout.decode('utf-8') + stderr = result.stderr.decode('utf-8') + + return run_cmd, result.returncode, stdout, stderr def apply_rgw(self, svc_id, realm_name, zone_name, port = None): return None @@ -201,10 +222,7 @@ def main(): (cmd, common_args, args)= TopLevelCommand()._parse() - env = EnvArgs(RGWAMCLIMgr(), - common_args.conf_path, - common_args.ceph_name, - common_args.ceph_keyring) + env = EnvArgs(RGWAMCLIMgr(common_args)) try: retval, out, err = cmd(env, args) -- 2.39.5