]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/rgw: use tool_exec instead of directly spawning commands
authorYehuda Sadeh <yehuda@redhat.com>
Mon, 22 Nov 2021 20:02:16 +0000 (12:02 -0800)
committerYehuda Sadeh <yehuda@redhat.com>
Wed, 24 Nov 2021 20:54:30 +0000 (12:54 -0800)
Signed-off-by: Yehuda Sadeh <yehuda@redhat.com>
src/pybind/mgr/rgw/module.py
src/python-common/ceph/rgw/rgwam_core.py
src/python-common/ceph/rgw/types.py
src/rgw/rgwam.py

index 325629c691e027ee3f29ac8bee033e068b906d2a..c8b7a59bbf4a306e674843a0ca50371354270853 100644 (file)
@@ -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,
index 3a9df6f0b8c24d4ebb8a75584bda896f24e27f29..99ff100a35f3fc4b95b857d473697976f9edcc0a 100644 (file)
@@ -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)
 
index 688905cc5d1b61408d2f2ebf4f6d075b6db4df3e..f2c962e1092826cf45b41c4b7ed7ada5156035de 100644 (file)
@@ -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
index c0017dc21e7efcfac933021f764f2d18b08e357b..f07d2b423e73a708cc311a7013531f2cab6f0e0e 100755 (executable)
@@ -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)